Hi,
I am trying to set up a support form with input fields and a textarea where our customers can submit there problems.
After the customer submits the form, our support team gets an email with all the form fields. Easy so far.
Now I have the problem, that line breaks within the textarea are not interpreted as such in the email.
The text from the textarea is one big paragraph in the email.
Any ideas how the line breaks can be displayed?
Thanks for your help!
Florian, to be clear, there isn't a bug here, because the way newlines are entered into textareas is supposed to be different (by design) from the way you force newlines in HTML. Or maybe to put it another way, the text in a textbox isn't HTML, so you naturally have to change it if it's going in an HTML email.
Anyway, the solution is to just trap the text in the form's onSubmit and use one of the methods outlined here: javascript - How do I replace all line breaks in a string with <br /> tags? - Stack Overflow
Note you will be permanently altering the field this way. I expect that's not a problem for your case.
I tried to implement your suggested solution in my setup. And this becomes not that easy since we are using Marketo forms inside Marketo landing pages which is placed in a iFrame.
In this way I am not able to access the form by javascript because it is loaded differently.
Any ideas?
It'll work fine in an IFRAMEd LP. Just use the whenReady event in an HTML block in place of the onReady.
MktoForms2.whenReady(function(form){
form.onSubmit ...
I never used email scripting before, but this token really did the trick.
Since I want the line breaks to be displayed in an email, I used the following code as a email scripting token:
$lead.temporaryNotes.replaceAll("\n", "<br />")
Very simple. Never thought about it before.
For anyone looking at this post now the replaceAll method did not work for me. However, the replace method already replaces all matches
$lead.aIEmailBody.replace("\n", "<br /><br />")
Here is a useful piece of code that people can copy that sets a fallback if the field is empty or contains NA
#if (!$lead.aIEmailBody.isEmpty() && $lead.aIEmailBody != "NA")
#set( $b = $lead.aIEmailBody.replace("\n", "<br /><br />"))
#else
#set( $b = "The people have spoken: We’re named Leader in the most recent G2 report.")
#end
${b}
Here is some more context about my use case and why the code works for me. My field does not contain actual line breaks it contains "\n" characters (or whatever characters I want ChatGPT to use to denote a line break) and then my code replaces these characters. The field is only intended to be viewed from emails where the script runs to tidy it up. It is not intended to be viewed in it's raw form
https://www.loom.com/share/5d03c6218d184236a00014dc11a5bc37?sid=ecc6f4d5-7b37-477c-aef5-31bea5f569d9
OK, that’s a very different use case. And also explains why you didn’t use the regex \n, because you aren’t using U+000A line breaks at all.
When I do a test using Person Notes I get this
Which when run through this velocity code (And I've deliberately done this very stepwise for clarity)
#set ( $myNotesStep0 = $lead.MktoPersonNotes )
#set ($myNotesStep1 = $myNotesStep0.replaceAll("\n","ZZZZ") )
#set ($myNotesStep2 = $myNotesStep1.replaceAll(" ","AAAA") )
Notes are here: ${myNotesStep2}
Yields
in my email.
This (I think?) conclusively shows the newline is getting converted to a space by the Marketo email processer before Velocity gets its mitts on the string.
Cheers
Jo
This (I think?) conclusively shows the newline is getting converted to a space by the Marketo email processer before Velocity gets its mitts on the string.
Exactly @Jo_Pitts1! You can’t convert something that isn’t there.
I recommend separating with an RS (U+001E) character instead of, or in addition to, a newline. The U+001E isn’t removed by Marketo, so you can replace that character with a <br>. About to publish a blog post on this.
I went for broke on this and wrote some VTL to spit out each character in notes field. Here is the output
Here is the (ugly) code.
I wanted the output in decimal and hex, but I couldn't work out how to natively convert an integer to a hex string, so I wrote my own quick and dirty converter.
#set ( $hex = ["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"])
#set ( $myNotesStep0 = $lead.MktoPersonNotes )
#set ($myNotesStep1 = $myNotesStep0.replaceAll("\n","ZZZZ") )
#set ($myNotesStep2 = $myNotesStep1.replaceAll(" ","AAAA") )
Notes are here: ${myNotesStep2}<br><br>
#set ($myStr = $lead.MktoPersonNotes)
String Reader for ${myStr}<br>
#set($start = 0)
#set($end = $myStr.length() - 1)
#set($range = [$start..$end])
#foreach($i in $range)
#set( $aVal = $myStr.codePointAt($i) )
#set( $aChar = $myStr.substring($i,$math.add($i,1)) )
#set( $hexPart1 = $math.toInteger( $math.div($aVal,16) ) )
#set( $hexPart2 = $aVal % 16 )
#set( $hexCharPart1 = $hex[$hexPart1] )
#set( $hexCharPart2 = $hex[$hexPart2] )
Char ${aChar} - Val ${aVal} ( ${hexCharPart1}${hexCharPart2} ) <br>
#end
If you're old like me, you'll automatically know that decimal 32 (hex 20) is the Space character in ASCII.
Cheers
Jo
I wanted the output in decimal and hex, but I couldn't work out how to natively convert an integer to a hex string
#set( $myCharArray = $myString.toCharArray() )
#foreach( $char in $myCharArray )
#if( !$char.isLowSurrogate($char) )
#set( $cp = $char.codePointAt($myCharArray,$foreach.index) )
\u${cp.toHexString($cp).toUpperCase()} ##
#end
#end
Outputs the hex codepoints (don’t forget about HS/LS):
\u54 \u68 \u69 \u73 \u20 \u6F \u6E \u63 \u65 \u20 \u68 \u61 \u64 \u20 \u6E \u65 \u77 \u6C \u69 \u6E \u65 \u73 \u2E
Your instance actually exports the newlines into Velocity? In my tests, line breaks are transformed into spaces (U+0020) before the value is exported, so there’s nothing to replace.
Email scripting will work, too, but of course that doesn't fix up the display for a LP (i.e. including the field as a token).
Thank you for you help!
This seems to me to be the best way to do it.
Demo here BTW.
Thanks for that!
Do a search for that. I believe someone fixed this somewhere.
But it may not be fixable easily.
Also, i'd suggest using SFDC Cases or an actual support tracking tool instead.