Retain text formatting, without using rich text editor for input?

TheBigK

Well-known member
#1
My 'Contact Us' addon uses a 'textarea' input field where the user can type his/her message. The addon's working fine, but I'd like the email to retain the formatting of the text entered by the user. I think one of the options is to use the rich text editor; but wondering if I can have the basic formatting (line breaks, for example) retained?

I use HTML format for all my outgoing emails.
 

Jake B.

Well-known member
#2
My 'Contact Us' addon uses a 'textarea' input field where the user can type his/her message. The addon's working fine, but I'd like the email to retain the formatting of the text entered by the user. I think one of the options is to use the rich text editor; but wondering if I can have the basic formatting (line breaks, for example) retained?

I use HTML format for all my outgoing emails.
You can keep the line breaks by using nl2br. Spaces/tabs you can convert to non-breaking spaces using str_replace. That should cover just about everything :)
 

TheBigK

Well-known member
#3
Hi Jake,

I changed my code to this:
PHP:
$senderMessage = nl2br($this->_input->filterSingle('sender_message', XenForo_Input::STRING));
Email Received:-

This is line 1<br /> <br /> This is line 2<br /> <br /> This is line 3

The email is in HTML format, but it's not converting <br/> tags to actual new lines in the body of email.

Screen Shot 2015-09-05 at 9.57.29 am.png
 

Jake B.

Well-known member
#4
Hi Jake,

I changed my code to this:
PHP:
$senderMessage = nl2br($this->_input->filterSingle('sender_message', XenForo_Input::STRING));
Email Received:-

This is line 1<br /> <br /> This is line 2<br /> <br /> This is line 3

The email is in HTML format, but it's not converting <br/> tags to actual new lines in the body of email.

View attachment 116128
You'll need to use {xen:raw to render it in the template. However, since you are allowing user input you should probably sanitize it as well first.
 

TheBigK

Well-known member
#5
You'll need to use {xen:raw to render it in the template. However, since you are allowing user input you should probably sanitize it as well first.
Thanks! I added {xen:raw $message} to the email template and it now retains breaks :D !
 
Top