1

I've been banging my head on this one for awhile and have been unable to find any helpful articles on my issues. I'm writing a PHP site using the built in mail function for some quick confirmation emails. I realize that there is a fair amount of prejudice against the built in mail function but it has been working well for me up to this point and I would like to be able to continue using it. When I send just a plain text email it all works great, as if I send just a HTML email. However if I try to do a multipart Text/HTML email both versions show up in my email client (tried both thunderbird and gmail). I'm hoping someone here can help me figure out what I'm doing wrong (besides using mail() instead of PHPMail). Here is my code snippet

    $uid = md5(uniqid(time()));

    $strSubject = "Confirmation for $strEventName on $strEventDate";
    $strHTMLMsg = "<h1><center>You are confirmed for the following event:</center></h1><br>\n$strEvenDetails";
    $strMsg  = strip_tags($strHTMLMsg);
    $toEmail = "\"$strName\" <$strEmail>";

    $header = "$FromEmail\n";
    $header .= "MIME-Version: 1.0\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\n\n";
    $header .= "Content-Transfer-Encoding: 7bit\n";
    $header .= "This is a MIME encoded message.\n\n";
    $header .= "--".$uid."\n";
    $header .= "Content-type:text/plain; charset=UTF-8\n";
    $header .= "Content-Transfer-Encoding: 7bit\n\n";
    $header .= $strMsg."\n\n";
    $header .= "--".$uid."\n";
    $header .= "Content-type:text/HTML; charset=UTF-8\n";
    $header .= "Content-Transfer-Encoding: 7bit\n\n";
    $header .= $strHTMLMsg."\n\n";
    $header .= "--".$uid."\n";
    $header .= "Content-Type: application/ics; name=\"".$strFileName."\";  method=PUBLISH; charset=UTF-8\n";
    $header .= "Content-Transfer-Encoding: 7bit\n";
    $header .= "Content-Disposition: attachment; filename=\"".$strFileName."\"\n\n";
    $header .= $strICSEvent."\n\n";
    $header .= "--".$uid."\n";

    $bSuccess = mail($toEmail,$strSubject,"",$header);
  • There are multiple issues: 1: The proper line ending would be `CRLF` (i. e., `\r\n`). 2: Header and body (including those of multipart parts) are separated by an empty line. 3: The final close-delimiter is `CRLF "--" boundary "--"`. – Gumbo Apr 05 '15 at 09:46
  • Thank you so much for your reply. However that did not seem to help. I changed the line ending from \n to \r\n. I'm not sure what you meant by #2 so I tried taking out the double line endings as well as few other things. Some of them made no difference some made it worse. Can you please clarify what you meant by that – Siggi Bjarnason Apr 06 '15 at 16:12
  • To be more specific: Referring to the `$header` lines: 3. line: `\n` too much; 4. line: `\n` missing; 19. line: `--` missing after boundary. Maybe you should use some abstraction and build the multipart message in a more structured way and not manually. – Gumbo Apr 06 '15 at 16:17
  • The only examples I've found on building the multi part was in this fashion. Any pointers to better examples would be greatly appreciated. I'm trying to learn how to properly build multipart messages. – Siggi Bjarnason Apr 06 '15 at 21:27

1 Answers1

1

Necrobump, but I arrived here after googling, so for others with the same search terms: multipart/mixed implies that you will view all parts. For both HTML and text parts where only one should be visible, use multipart/alternative instead. The last part gets the highest priority.

Also see Mail multipart/alternative vs multipart/mixed for more info, and info about "stacking" mime content.

mangrove
  • 31
  • 2