Browse by Month RSS Feed
  • August 2010
  • July 2010
  • May 2010
  • April 2010
  • March 2010
  • February 2010

  • Filed under: Code, PHP @ 9:34 pm

    I got this sucker working for Form Tools last month after working my butt off making sense of the many idiosyncracies of the many servers out there. Well, turns out I missed a problem. After half a dozen people complained I bumped this up on my priorities and investigated it.

    If you need to ever send HTML+text emails in PHP, I'd thoroughly recommend the following article: http://www.zend.com/zend/trick/html-email.php

    But be careful not to deviate from the example code without fully understanding it! Every last aspect of the code is there for a purpose, including the occasional double endlines. Unfortunately, the writer doesn't delve into the details in any great depth so one is apt to assume they're not important. One thing that's not covered is determining an endline based on OS type. Here's my tweaked version:

    // determine the end of line character (based on OS)
    $eol = "\n";
    if (strtoupper(substr(PHP_OS, 0, 3) == 'WIN'))
    $eol = "\r\n";
    else if (strtoupper(substr(PHP_OS, 0, 3) == 'MAC'))
    $eol = "\r";

    // add From: header
    $headers = "From: webserver@localhost$eol";

    //specify MIME version 1.0
    $headers .= "MIME-Version: 1.0$eol";

    // unique boundary
    $boundary = uniqid("HTMLDEMO");

    // tell e-mail client this e-mail contains alternate versions
    $headers .= "Content-Type: multipart/alternative" .
    "; boundary = $boundary$eol$eol";

    // message to people with clients who don't understand MIME
    $headers .= "This is a MIME encoded message.$eol";

    // plain text version of message
    $headers .= "--$boundary$eol" .
    "Content-Type: text/plain; charset=ISO-8859-1$eol" .
    "Content-Transfer-Encoding: base64$eol$eol";
    $headers .= chunk_split(base64_encode("This is the plain text version!"));

    // HTML version of message
    $headers .= "--$boundary\r\n" .
    "Content-Type: text/html; charset=ISO-8859-1$eol" .
    "Content-Transfer-Encoding: base64$eol$eol";
    $headers .= chunk_split(base64_encode("This the <strong>HTML</strong> version!"));

    // send message
    mail("root@localhost", "An HTML Message", "", $headers);

    I can vouch for the code working on Windows and Unix servers. Good luck...!

    Comments (0)

    No Comments »

    No comments yet.

    RSS feed for comments on this post. TrackBack URI

    Leave a comment