Reputation: 3771
In our internal CRM we have a simple html input textarea where you can leave notes and messages. We later use this information to email this, only since that email is in HTML the formating is all wrong.
So if for example I have the following in my MYSQL table:
This is a test message!
Some line
Some more lines
If we later email this it comes out as:
This is a test message! Some line Some more lines
This is obviously not wanted but I don't want to add some complicated WYSIWYG editor to our CRM. Can I allow line-breaks? If so, how?
I don't want to use <pre></pre>
tags because I believe it is not supported in all email clients (I could be wrong).
Upvotes: 1
Views: 4664
Reputation: 111
Assuming PHP is in the mix, there's the nl2br() function. Otherwise, rolling your own won't be hard.
The root of this issue is that browsers (mail clients can either use embedded browsers for rendering - Outlook for example - or behave like browsers) will take any amount of whitespace/new line/carriage returns/etc outside of tags in HTML and render them as a single whitespace. This is so you can do things like indent your markup and still have it look sane in the browser.
You will have to insert markup in order to control the rendering as has been has suggested: convert newlines to <br>
or <p>
tags and so on, much like cms WYSIWYG editors do. Either that or chose a different format for your emails.
Upvotes: 0
Reputation: 399
You can send emails in two flavours: html and plain text. In Html, line-breaks are not processed (just like in your browser). Looks like this is what you are doing here.
Two solutions: either you send emails in plain text, or you change line-breaks to <br>.
Upvotes: 0
Reputation: 21
The problem is that html renders all whitespace as single spaces. If you look at the source of the email once it's received, I'll bet the newlines will be there (if they're not, then the problem is on the email generation side).
<pre></pre>
is the simplest thing you can do, I think.
Upvotes: 2
Reputation: 175017
You could use text/plain
header, if you don't intend on using any HTML tags in the message. (That would mean no colors, no links, and no text formatting).
You could also make a quick and dirty solution to replace all \n
s in your text to <br>\n
.
Upvotes: 2
Reputation: 944005
A basic solution would be to replace new lines with <br>
s.
A smarter one would give special consideration to multiple line breaks (e.g. treating /\n\s*\n/
as a point to end a paragraph and start a new one (</p><p>
)).
The specifics would depend on the language you are using to generate the email from the MySQL data. You might want to consider something like a Markdown parser.
Upvotes: 1