Formatting text for email in PHP

Published January 4th, 2007

When you’re using a script to send an email (for example, from a contact form) it’s a good idea to break up long lines, because some email software won’t word wrap automatically — so continuous text is rendered on a single line that disappears off the edge of the screen.

I’ve had to do this several times in the past, but each time I forget how I did it and end up searching through the PHP string functions list. So this is a note to myself: use wordwrap().

As you’ll probably have guessed, this function breaks text into chunks of a specific length without splitting individual words. The standard length for lines in an email appears to be 75 characters (the default for this function). For safety, I replace the default line ending of \n with \r\n — I haven’t researched this but I wouldn’t be surprised if some windows clients get confused otherwise.

Get a Trackback link

No Comments Yet

Be the first to comment!

Leave a comment

Comment Policy: First time comments are moderated. Please be patient.