PHP Multipart Alternative with Attachment with Apple Mail

Discussion in 'Mac Programming' started by Darkroom, Aug 29, 2008.

  1. Darkroom Guest


    Dec 15, 2006
    Montréal, Canada
    this code is samples of edited AquaticPrime.PHP and Config.PHP. for those not familiar with Aquatic Prime, the AquaticPrime.php requires Config.php.

    essentially i'm trying to create a multipart/alternative email with
    the multipart/mixed attachment... it works fine except Apple's Mail
    app doesn't recognize the multipart/alternative portion of the script
    and only displays the HTML version (without giving me the option of
    viewing the next alternative). oddly, if i list the Content-Type:
    text/plain after Content-Type: text/html, it will only show a text
    email in Mail, again without seeing the next alternative. of course
    if i view the raw source of the email, everything shows up as it

    i know Apple's Mail program is kinda sketchy in Leopard, and i know
    i'm not a wizard at PHP, but according to other online samples i've
    viewed this should work. anyone see anything in this code that will
    make Mail only able to understand the HTML portion of the
    email, or maybe it's all as it should be and that Mail is the problem.


    // PORTION OF AquaticPrime.php
    function sendMail($to, $from, $subject, $message, $license, $name, $bcc='')
    	// Create a random boundary
    	$boundary = "PHP-MIX--".md5(date('r', time()));
    	$headers  = "From: $from\n";
    	if ($bcc != "")
    		$headers .= "Bcc: $bcc\n";
    	$headers .= "X-Mailer: PHP/".phpversion()."\n";
    	$headers .= "MIME-Version: 1.0\n";
    	$headers .= "Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
    	$headers .= "Content-Transfer-Encoding: 7bit\n";
    	$headers .= "This is a MIME encoded message.\n";
    	$headers .= "--$boundary\n";
    	$headers .= "$message";
    	$headers .= "--$boundary\n";
    	$headers .= "Content-Type: application/octet-stream; name=\"$name\"\n";
    	$headers .= "Content-Transfer-Encoding: base64\n";
    	$headers .= "Content-Disposition: attachment\n\n";
        $headers .= chunk_split(base64_encode($license))."\n";
        $headers .= "--$boundary--";
    	mail($to, $subject, "", utf8_encode($headers));
    // PORTION OF Config.php
    $product = "The Product";
    // These fields below should be customized for your application.  You can use ##NAME## in place of the customer's name and ##EMAIL## in place of his/her email
    $from = "Me <>";
    $subject = "$product License For ##NAME##";
    $mime_boundary = "PHP-ALT--".md5(time());
    $message = "Content-Type: multipart/alternative; boundary=\"$mime_boundary\"\n";
    $message .= "--$mime_boundary\n";
    $message .= "Content-Type: text/plain; charset=\"UTF-8\"\n";
    $message .= "Content-Transfer-Encoding: 7bit\n\n";
    $message .= "Plain Text Message\n\n";
    $message .= "--$mime_boundary\n";
    $message .= "Content-Type: text/html; charset=\"UTF-8\"\n";
    $message .= "Content-Transfer-Encoding: 7bit\n\n";
    $message .= "<html>\n";
    $message .= "<body>\n"; 
    $message .= "<h2>HTML Message</h2>\n";
    $message .= "</body>\n"; 
    $message .= "</html>\n\n";
    $message .= "--$mime_boundary--\n\n";
  2. Sayer macrumors 6502a


    Jan 4, 2002
    Austin, TX
    Try viewing the test emails in another Mac email client like Thunderbird to verify the format is correctly recognized at all in *any* email client.

    After you verify that, you can figure out how the minimalist Apple can be handled in your generated emails. Most likely you must accept the lowest common denominator for email to be HTML or even plain text. And in clients that support the extra content, they can show it if they are so configured.

    Welcome to the wonderful world of computer programming for more than just yourself.
  3. Darkroom thread starter Guest


    Dec 15, 2006
    Montréal, Canada
    yeah it wasn't happening... whatever... i'll just make it HTML only... i know there are some people out there who loath HTML email, but probably for reasons that are more suitable for 1997 than today... so HTML it is...
  4. ChadJK macrumors newbie

    Mar 20, 2008
    Gretna, Nebraska USA

    Doesn't section 2.1 of RFC 2822 state that lines need to be delimited with carriage return and line feed (CRLF)? So, all the lines should end with \r\n instead of just \n. :confused:

Share This Page