回顾使用PHP原生发送电子邮件(二)

2022-10-11 20:47:04 171 0
魁首哥

That about covers the basics of the mail function; now let’s really get fancy and explore mail headers and what we can do with them!

上篇文章涵盖了邮件功能的基本知识;现在,让我们真正开始想象和探索邮件标题以及我们可以用它们做什么!

回顾使用PHP原生发送电子邮件(二)

HTML Email and Other Headers

HTML电子邮件和其他头信息

So now you can send email from your PHP scripts. How exciting! Although I’m sure you’re already drunk with power, would you like to know how to send HTML email too? Of course you would!

现在您可以从PHP脚本发送电子邮件了。多么令人兴奋!虽然我肯定你已经被权力迷住了,但你想知道如何发送HTML电子邮件吗?你当然会!

To understand HTML email, first you need to understand mail headers. Any given email message is made up of two parts: the headers and the message body. Here’s how a simple message looks when your email program fetches it from your ISP:

要理解HTML电子邮件,首先需要理解邮件头信息。任何给定的电子邮件都由两部分组成:头信息和邮件正文。下面是当您的电子邮件程序从ISP获取简单邮件时的展现:

 Return-Path: 
Delivered-To: you@some.net
Received: ...several lines like this...
From: Sender 
To: You 
Subject: A Simple Message
Date: Mon, 11 Feb 2002 16:08:19 -0500
Organization: Sender's Company
X-Mailer: Microsoft Outlook, Build 10.0.2616
Hi there!   Is this thing on?  

Everything up to the blank line makes up the headers for this message. In actual fact, most email messages will have many more header lines than this; however, to keep our focus I trimmed this example down to the bare essentials.

到空白行的所有内容构成了该消息的标头。实际上,大多数电子邮件的头信息都比这多很多;然而,为了保持我们的关注点,我把这个例子缩减到了最基本的部分。

As you can see, every line of the headers begins with the name of the header (From:, To:, Subject:, Date:, etc.), followed by some value. Most headers are standardized, and have a specific meaning, either to your mail program or to the mail servers that were responsible for getting the message to you. Non-standard headers exist as well, and they all begin with X- (e.g. X-Mailer:, a non-standard header, often appears to indicate the program that was used to send the message).

如您所见,标题的每一行都以标题的名称(From:、To:、Subject:、Date:,等等)开头,后跟一些值。大多数邮件头都是标准化的,对您的邮件程序或负责向您发送邮件的邮件服务器都有特定的含义。非标准标头也存在,它们都以X开头(例如,X-Mailer:,一个非标准标头,通常显示为指示用于发送消息的程序)。

NOTE: If a header’s value needs more than one line, additional lines should begin with a space. We’ll see an example of this in the next section.

如果标头的值需要多行,则其他行应以空格开头。我们将在下一节中看到一个例子。

As soon as your mail program gets to a blank line, it knows the headers are over and the rest of the email is the message body, which it should display. In this case, the message body is the last line above.

一旦你的邮件程序到达一个空行,它就知道邮件头已经结束,邮件的其余部分就是邮件正文,应该显示出来。在本例中,消息正文是上面的最后一行。

PHP’s mail function lets you specify your own headers, which it adds to those it generates automatically to get your message where it needs to go. For example, the following call to the mail function will add an X-Mailer: header to the outgoing message, identifying PHP 4.x as the sending program:

PHP的邮件功能允许您指定自己的邮件头,并将其添加到自动生成的邮件头中,以便将邮件发送到需要的地方。例如,以下对mail函数的调用将向传出消息添加一个X-Mailer:header,将PHP 4.X标识为发送程序:

   

This optional fourth parameter is most often used to specify a ‘from’ address other than the default specified in php.ini. Let’s add a From: header to do just that:

这个可选的第四个参数最常用于指定“from”地址,而不是php.ini中指定的默认地址。让我们添加一个From:header来实现这一点:

   

Note that since headers each appear on a single line, we must separate our two headers with a new line character (n), which means I need to use double quotes around the header string (single quoted strings don’t recognize special character codes like n).

请注意,由于每个标题都显示在一行上,因此我们必须用新行字符(n)分隔两个标题,这意味着我需要在标题字符串周围使用双引号(单引号字符串不能识别像n这样的特殊字符代码)。

Additional headers also let you assign names to email addresses by specifying them in the format name . Here’s our example again, but this time with names “The Sender” and “The Receiver” attached to the relevant addresses:

其他标题还允许您通过在格式名称中指定电子邮件地址来为其分配名称。下面是我们的示例,但这一次在相关地址上附加了名称“发件人”和“收件人”:

 n" .
"From: The Sender n" .
"X-Mailer: PHP 4.x");
?>  

Notice that to do this, we’ve had to specify the To: header manually, since the first parameter of PHP’s mail function doesn’t support this more advanced address format. We must still list all the recipients of the message as bare email addresses in the first parameter, however.

注意,要做到这一点,我们必须手动指定to:头,因为PHP邮件函数的第一个参数不支持这种更高级的地址格式。但是,我们仍然必须在第一个参数中将邮件的所有收件人列为裸电子邮件地址。

The cc: and Bcc: headers provide the ability to send carbon copies and blind carbon copies of a message as well:

cc:和Bcc:标头还提供发送邮件的复写副本和盲复写副本的功能:

 n" .
"From: The Sender n" .
"cc: Interested n" .
"Bcc: Me Too n" .
"X-Mailer: PHP 4.x");
?>  

See how the email addresses of all recipients, be they actual addressees (To), carbon copies (Cc), or blind carbon copies (Bcc) are listed in the first parameter? This isn’t documented anywhere, but in my testing I’ve found that it’s absolutely vital if you want the message to get to everyone it’s supposed to, especially on Windows servers where PHP’s mail function is especially sensitive.

查看如何在第一个参数中列出所有收件人的电子邮件地址,无论是实际收件人(收件人)、复写副本(抄送)还是盲复写副本(密件抄送)?这在任何地方都没有文档记录,但在我的测试中,我发现,如果希望消息传递给所有人,尤其是在PHP邮件功能特别敏感的Windows服务器上,这是绝对重要的。

What does all this have to do with HTML email, you ask? Well, a few special headers will cause the message body to be interpreted as HTML by email clients that support it:

你会问,这一切与HTML电子邮件有什么关系?好的,一些特殊的标题将导致支持邮件正文的电子邮件客户端将邮件正文解释为HTML:

 

Your message here.

', "To: The Receiver n" . "From: The Sender n" . "MIME-Version: 1.0n" . "Content-type: text/html; charset=iso-8859-1"); ?>

Note both the message in HTML format as well as the two new headers: Mime-Version: and Content-type:.

请注意HTML格式的消息以及两个新标头:Mime-Version:和Content-type:。

The MIME-Version: header indicates that the standard extended mail format will be used (MIME stands for Multipurpose Internet Mail Extensions), which allows for messages to be sent content types other than plain text. The Content-type: header then specifies that the message with contain HTML (and sets the character set to the standard for that format).

MIME-Version:标头表示将使用标准的扩展邮件格式(MIME代表多用途Internet邮件扩展),它允许发送除纯文本以外的内容类型的邮件。然后,Content-type:头指定消息包含HTML(并将字符集设置为该格式的标准)。

收藏
分享
海报
0 条评论
171
上一篇:毕业设计 | PHP版图书管理系统分享 下一篇:超实用简单轻量级 PHP 自建私有云网盘

本站已关闭游客评论,请登录或者注册后再评论吧~

忘记密码?

图形验证码