I'm a bit surprised that this simple solution worked. Now, I'll ask if you've successfully sent a multi-part message (plain text + HTML) to a gmail address.
I don't need to send attachments, but do need to send plain vs. HTML versions of the mail.
Here is the code I've been using. It works with all recipients except Gmail. When sent to GMail users, the message is received, but the contents are not displayed.
<?php
function send_mail($emailaddress, $body, $emailsubject, $fromaddress, $fromname, $fileattach = false)
{
//define the receiver of the email
$to = "$emailaddress";
//define the subject of the email
$subject = "$emailsubject";
//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: $fromname<$fromaddress>\r\nReply-To: $fromname<$fromaddress>";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
$stripped = strip_tags(str_replace("<br>", "\r\n", $body));
//$stripped = "This is stripped";
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo "$random_hash\n"; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo $stripped; ?>
--PHP-alt-<?php echo "$random_hash\n"; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?php echo $body; ?>
--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 );
return $mail_sent;
}
$resultset = TraverseData(); // This creates the HTML body of the message
send_mail("recipient@gmail.com", $resultset, "Results of reminder check", "sentby@domainregisteredwith50websnet.com", "Sent by string", false)
?>