简体   繁体   中英

Sending HTML Email in php?

Here is the code.

$to = 'youraddress@example.com';

$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";
?>

I don't follow it well. expect someone can do me a favor.

  1. Why should I generate a random hash?

  2. Why I must add boundary string and mime type specification to header?

  3. Why use ob_start();?

4.

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

What are those lines meaning? Could I delete them? Thank you.

  1. Generating a random hash is to avoid it colliding with your content.

  2. A "boundary string" tells the email clients where headers start and stop and where the actual email contents start. Since you want to send HTML email, you must specifically tell the email client it will be receiving HTML, not just any content.

  3. Otherwise the HTML and stuff will be sent directly to the browser, ie, the user viewing your site. Instead you want to store the HTML in a variable and use it instead.

  4. Content-Type tells the email client what kind of content you are sending and how it is encoded .

Of course you cannot delete them. It would be like sending you a PDF file without saying it is a PDF and without a proper extension - you won't know what to do with it.

Note

Emails, websites, anything which has a structure (including most files) usually are laid out in a structure of "header" and "body".

The header tells the file reader what to expect in the "body". The "body" is the actual content the reader should do something with.

I am not certain why the random hash is used here, but I think it is just additional safety to ensure a unique boundary string, preventing name collisions between parts.

As to the content-type: you need to specify that to tell the mail client that it should render HTML, and to indicate that your message is multipart. Multipart means there's more than one part, in your case a text-based part and a HTML part.

The boundary part is used to separate the contents of one part form the contents of another part, and from the header.

Using the PHP Output Buffer (ob_start and ob_end_clean) is not necessary at all, you can also just enter strings using quotes or using HEREDOC. An advantage of using the output buffer is that you can end the PHP (using ?> ) and have your IDE help you writing HTML. Make sure to add ob_end_clean(); though, it is not yet in your code.

  1. You don't have to. It's just that it makes things easier: the delimiter must be a string that's not part of the mail content.

  2. You need a boundary to split the message in parts. An e-mail message is nothing else that a stream of characters. You need a MIME type so the e-mail client can know what each part contains. Otherwise, it could not know whether it's HTML or not (or a JPEG picture, or a PowerPoint presentation...)

  3. Honestly, it looks like an overcomplicate replacement for regular string assignments. Rather than doing $message = 'Hello World!'; , it prints Hello World! to standard output and captures the standard output into a variable.

  4. These lines mean that you are finishing one part of the message and you are starting a new one that contains HTML. You can delete them if you don't want to add another message part that contains HTML but... isn't that what you want to do?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM