I am trying to develop a multipart/alternative email to send both HTML and Plain Text versions.
From everything i've read in this forum and other resources, I have developed the following code:

$bodyt = "Plain Text Content";
$bodyh = "<p><b>HTML Content</b></p>";
			$semi_rand = md5(time());
			$mime_boundary = "==MULTIPART_BOUNDARY_$semi_rand";

		$to = "$to_mail";
		$subject = "Email Subject";

		$body = "
--$mime_boundary
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
$bodyt

--$mime_boundary
Content-Type: text/html; charset=iso-8859-1
Content-Transfer-Encoding: 7bit
$bodyh

--$mime_boundary--";

		$headers = "From: info@website.com\r\n";  
		$headers .= "MIME-Version: 1.0\r\n";
		$headers .= "Content-Type: multipart/alternative;\r\n" . 
	    "     boundary=" . $mime_boundary;

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

The email is sent, but I receive a blank email with no content. Any suggestions? Help is always appreciated.

    what happerns if you remove all the content in the variable body and replace with a simple string like "test"?

      Being lazy (not necessarily a bad attribute for a programmer 😉 ), I just use PHPMailer and utilize its methods for creating multi-part emails.

        Ive been resisting PHPMailer, although i think that was mostly laziness too. I'll give it a try tonight. Thanks.

          Ok. PHPMailer makes this easy. Up and running in 5 minutes and working much better than my own code for both html and plain text.

          Here is my code using PHPMailer and would recommend it to anyone who has been hand coding email...

          <?php
          $to = "email@website.com";
          $from = "email@thesite.com";
          $subject = "The Subject";
          $htmlbody = "<p><b>Message Body</b></p>";
          $textbody = "Message Body";
          
          if(isset($_POST['do'])) {
          	require_once('./class.phpmailer.php');
          	$mail = new PHPMailer();
          	$mail->From = $from;
          	$mail->AddAddress($to);
          
          $mail->Subject = $subject;
          $mail->Body = $htmlbody;
          $mail->AltBody= $textbody;
          $mail->WordWrap = 50;
          
          if(!$mail->Send()) {
          	echo 'Message was not sent.';
          	echo 'Mailer error: ' . $mail->ErrorInfo;
          	} else {
          	echo 'Message has been sent.';
          }
          }
          ?>
            2 months later

            Thank you, this is exactly what I've been looking for myself,I been using php's mail function for a long time to code both html and plain text messages.

            and was looking for a way to do both. easily

              3 months later

              Problem I have with php mailer is that Outlook 2003 is by default converting HTML to plain text, whether or not a multipart message is being sent. Outlook sees other HTML messages (including multipart) as HTML - so, although I have had a similar result from my own coding, I think there is a problem using phpmailer form my host's mail server.

              I have been sent a class by a friend who sends me multipart successfully - so I shall play with that. If I spot the significant difference I will post here.

                Write a Reply...