Sunday, November 8, 2009

PHP mail() bug? or feature?

PHP mail bug

I use the php mail() function to create lengthy html emails. The $message is a long string, up to 5000 characters, or more, long. Well, it turns that either the mail() function or the mail server at the other end has a character limit of 990 per string. If you exceed that, something automatically inserts 0D 0A 20 into the string (CR LF Space) every 990 characters, so that now the email conforms to the "rule." No crash, but every 990 characters you see a space inserted in the middle of words, or if the space lands in a tag, you have screwed up your message format.

The fix: create a function that takes your $message string, and inserts 0D 0A 20 more frequently than every 990 characters where there normally is just a space (20). (20 is hex ascii for space - used a hex editor to debug this thing). If your message is html, html ignores CR LF in strings, so inserting this causes no problems. Then, after applying that function, use

mail( $to, $subject, $message, $headers );

on the modified $message, and it goes through without the problem!

Mac Shortcut of the Day
I really like the Safari "Bookmark Bar" across the top of the toolbar for quickly clicking to favorite sites. As if that wasn't good enough already, try holding the command key down while clicking a book mark. This gives you a new tab in your Safari window AND jumps you to your favorite site in one click.

No comments:

Post a Comment