How to know if a string variable written by the user contains any line breaks?

0
Hi everyone, I am encountering a problem when including a string variable in an email HTML body. I usually construct the string in HTML format within a microflow to add the necessary line breaks. However, in this situation, I have a variable written by the user, which I am not constructing myself. Therefore, I am unable to determine where to add the line breaks that the user has included when writing this variable. As an example, If the user writes this: "line 1 line 2"   when I include this variable in the email body, the output shown is the following: "line 1 line 2"   Is there a solution in Mendix for line breaks to be displayed correctly with a string that 1) you do not know its content, and 2) it is not the developer who is creating it, but it is delivered to us by the user in a single variable?   Thank you in advance!
asked
3 answers
1

​​​​​​​when you're sending an email with a string that includes line breaks or newlines entered by a user, you might encounter an issue where the line breaks are lost when rendering the email body in HTML format. This is because HTML doesn't recognize plain newline characters (\n or \r\n) as line breaks by default.

To ensure that user-entered line breaks appear correctly in the email body, you can convert the newline characters to HTML-compatible <br> tags or wrap the text in a <pre> tag, which preserves formatting.

  • Replace newline characters with <br>: Use the String Replace action in Mendix to convert \n to <br>.
  • Use <pre> tag: Wrap the user input in a <pre> tag to preserve the formatting.
answered
1

Hi Clara, 

What if you let the user input be in Rich text instead of the text area widget. Then the output will also in HTML and thus be fine to be used in the Email body.

Be sure that you sanitize the user input as that is a security best practice. 

Good luck,

Jurre

answered
0

Thank you all for the suggestions!

All of the solutions are working for me, but from what I tried, I finally preferred to use the replaceAll string function because, from my point of view, it was the easiest way to keep everything else looking the same.

The rich text worked great but modified the look of my page a bit, and the <pre> tag for HTML was showing a different font than the standard one that HTML shows in a Mendix email. So, I found it faster to use the replaceAll function.

String Function Calls | Mendix Documentation

answered