WebDev Diary: PHP and Email Newsletters
If you ever plan to build a web app designed to send HTML-formatted email newsletters, I’m about to save you a bit of hassle. This article will be useful for those using SugarCRM and Drupal, too, as a lot of my Google research on the topic revealed similar problems for users of those two software packages.
What is the problem? Exclamation points. Random exclamation points inserted into outgoing emails.
At first glance, this problem is confounding because of its seemingly untraceable source. Is it my PHP script? Is it something in PHP’s mail function? Chances are, both of these are your problem.
I’m betting that somewhere in your script you’ve got a line that looks something like this:
It’s a safe bet, because you have to declare a content type at the beginning of your message, and 7-bit encoding is the typical encoding method for HTML-formatted emails.
And the culprit here is the Content-Transfer-Encoding. That 7-bit stuff means that the maximum length of each line of your outgoing email message can only be 76 characters. If you send a 7-bit-encoded message to sendmail without a \n newline character at least every 76 characters, sendmail goes a little wonky and starts inserting exclamation points here, there–everywhere, really. I’ve yet to find out why this is, but that’s not important, as the solution to the problem is relatively simple.
You need to insert a newline into your outgoing message every 76 characters before passing the variable to mail. This could be a chore if PHP didn’t already have a handy function called wordwrap. It works something like this:
Simple, right? You pass wordwrap your message variable (complete with its long lines of text), and wordwrap inserts a newline every 75 characters. Finish up by passing your newly formatted message variable to sendmail, and you’re all set. Here’s a shot of a sample script:
I hope this helps at least a few of you avoid a headache or two. A copy of the example script can be downloaded from here. Thanks to Sitepoint, TextMate, Skitch and the Corporate Ipsum widget.
About this entry
You’re currently reading “WebDev Diary: PHP and Email Newsletters,” an entry on Jeff Mueller
- Published:
- 08.30.07 / 10pm
- Category:
- Uncategorized, php, programming, tech writing, webdev











2 Comments
Jump to comment form | comments rss [?] | trackback uri [?]