简体   繁体   English

在php中发送HTML电子邮件?

[英]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? 为什么必须在标头中添加边界字符串和mime类型规范?

  3. Why use ob_start();? 为什么要使用ob_start();?

4. 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. 由于要发送HTML电子邮件,因此必须明确告知电子邮件客户端它将接收HTML,而不仅仅是任何内容。

  3. Otherwise the HTML and stuff will be sent directly to the browser, ie, the user viewing your site. 否则,HTML和内容将直接发送到浏览器,即用户正在查看您的网站。 Instead you want to store the HTML in a variable and use it instead. 相反,您想将HTML存储在变量中,并使用它。

  4. Content-Type tells the email client what kind of content you are sending and how it is encoded . Content-Type告诉电子邮件客户端您要发送哪种内容以及如何对其进行编码

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. 这就像向您发送PDF文件而没有说它是PDF且没有适当的扩展名-您将不知道如何处理。

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. 关于内容类型:您需要指定该类型以告知邮件客户端它应该呈现HTML,并指示您的邮件是多部分的。 Multipart means there's more than one part, in your case a text-based part and a HTML part. 多部分表示有多个部分,在您的情况下,这是一个基于文本的部分和一个HTML部分。

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. 根本不需要使用PHP输出缓冲区(ob_start和ob_end_clean),也可以只使用引号或HEREDOC输入字符串。 An advantage of using the output buffer is that you can end the PHP (using ?> ) and have your IDE help you writing HTML. 使用输出缓冲区的一个优点是可以结束PHP(使用?> ),并让IDE帮助您编写HTML。 Make sure to add ob_end_clean(); 确保添加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. 您需要一个MIME类型,以便电子邮件客户端可以知道每个部分包含的内容。 Otherwise, it could not know whether it's HTML or not (or a JPEG picture, or a PowerPoint presentation...) 否则,它不知道它是否为HTML(或JPEG图片或PowerPoint演示文稿...)。

  3. Honestly, it looks like an overcomplicate replacement for regular string assignments. 老实说,它看起来像是对常规字符串分配的过度复杂替换。 Rather than doing $message = 'Hello World!'; 而不是做$message = 'Hello World!'; , it prints Hello World! ,它会打印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. 这些行表示您正在完成消息的一部分,并且正在开始一个包含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? 如果您不想添加包含HTML但又不是...的消息部分,则可以删除它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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