Direct text / HTML email not working in Mac mail client

Below is the code I am using. It works fine in thunderbird, but not in Mac mail client (and I assume anything done by microsoft. I currently don't have access to this to test it). As far as I know about the specifics of various email clients, I am confused by this! This is justifiably explanatory, but I am trying to send text and html emails to increase readership. Any help would be much appreciated.

EDIT

I had to clarify that the content is sent independently, but in thunderbird it displays the message correctly, but in the Mac mail client you get the whole thing from the first PHP-alt to the last PHP

<?php
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!! 
This is simple text email message. 

--PHP-alt-<?php echo $random_hash; ?> 
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>

--PHP-alt-<?php echo $random_hash; ?>--
<?
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>

      

0


source to share


2 answers


Instead of trying to flip your own email program, try for example. PHPMailer . It has very good support for multiple / alternate. It's a lot easier to integrate than to flip your own solution. I was there - after endless work around weird MIME problems, I dumped my mailing list, switched to it, and focused on other things in the time I had spared.



In other words, don't reinvent the wheel. While doing it yourself can be a good challenge and you will learn a lot during the process, if you just want it to work, these guys have handled the complexity for you.

+3


source


You are not using correct output buffering - see the ob_end_clean man page to see that it does not return captured output, you need ob_get_contents to do this:



$message =ob_get_contents();
ob_end_clean();

      

0


source







All Articles