Sending multiple / alternative using PHP mail ()

So, I am trying to send a fairly simple HTML email address using PHP. I spent the last three days trying to find a good solution and I think I found it, but when I test it, it does not go out properly. I borrowed this code from one of the manuals that I referenced. The verification code is as follows:

<?php
//define the receiver of the email
$to = 'myemail@gmail.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=\"".$random_hash."\""; 
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--<?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 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 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";
?>

The problem is that although it sends the email just fine, it either sends it as plain text or an empty message in Gmail. Any ideas why?

+5
source share
1 answer

, , , , , , , , :)

, . , CRLF (\r\n), ob_start(), , LF (\n) .

, GMail . ;)

+7

All Articles