回顾使用PHP原生发送电子邮件(三)混合信息

Mixed Messages

混合信息

In life, it’s rarely good to send mixed messages. At least, that’s what my last girlfriend told me! When it comes to email, however, mixed messages offer a whole lot of power. A single email message can contain both text and HTML versions of your message. That makes it viewable in most any email program, and you don’t sacrifice the power of HTML formatting for readers that are appropriately equipped.

在生活中,发送混合信息很少有好处。至少,这是我上一个女朋友告诉我的!然而,当谈到电子邮件时,混合信息提供了很大的力量。一封电子邮件可以包含邮件的文本和HTML版本。这使得它在大多数电子邮件程序中都是可见的,并且您不会为适当配备的读者牺牲HTML格式的功能。

Be aware that mixed messages have their weaknesses. First of all, since you’re sending two versions of your message, the message will typically be a lot larger than it would be if you sent just one format or the other. Also note that old email programs that don’t recognize the mixed message format may display both versions of the message as file attachments (one text, the other HTML).

请注意,混合消息有其弱点。首先,由于您要发送两个版本的消息,因此消息通常比仅发送一种或另一种格式的消息要大得多。还请注意,不识别混合邮件格式的旧电子邮件程序可能会将邮件的两个版本都显示为文件附件(一个文本,另一个HTML)。

Let’s look at a simple example of a mixed email message, and then look at the PHP code to send it:

让我们看一个简单的混合电子邮件示例,然后看发送它的PHP代码:

Date: Mon, 11 Feb 2002 16:08:19 -0500
To: The Receiver <recipient@some.net>
From: The Sender <sender@some.net>
Subject: A simple mixed message
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="==Multipart_Boundary_xc75j85x"
This is a multi-part message in MIME format.

--==Multipart_Boundary_xc75j85x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

This is the text portion of the mixed message.

--==Multipart_Boundary_xc75j85x
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<html>
<body>
<p>This is the <b>HTML portion</b> of the mixed message.</p>
</body>
</html>

--==Multipart_Boundary_xc75j85x--

After the initial, standard headers at the top of the message, we have the MIME-Version: 1.0 header that enables the advanced email features we need. The Content-Type: header is where things start to get funky:

在邮件顶部的初始标准邮件头之后,我们有了MIME-Version:1.0邮件头,它支持我们需要的高级电子邮件功能。Content-Type:header是事情开始变得古怪的地方:

Content-Type: multipart/alternative;

boundary="==Multipart_Boundary_xxc75885"

We specify a content type of multipart/alternative, which is the special type that allows us to send a message with two or more alternative versions of the message (from which the recipient’s email program will pick the most suitable for display). In addition, we use the Content-Type header to set a boundary string.

我们指定一种内容类型multipart/alternative,这是一种特殊类型,允许我们发送包含两个或多个备选版本的消息(收件人的电子邮件程序将从中选择最适合显示的版本)。此外,我们使用内容类型头来设置边界字符串。

To keep the header lines short, this part of the header appears on a second line (as mentioned in a note above, the second line must begin with one or more spaces to indicate that it’s a continuation of the previous line).

为了保持标题行短,标题的这一部分显示在第二行上(如上面的注释所述,第二行必须以一个或多个空格开头,以指示它是前一行的延续)。

In this case, I chose “==Multipart_Boundary_xc75j85x” as the boundary string. There is no special significance to this string, other than it is unlikely to appear as part of the message itself. I used characters like equals signs and underscores, and semi-random strings of letters and numbers to help ensure this. We then use this string to divide up our message into parts.

在本例中,我选择“==Multipart_Boundary_xc75j85x”作为边界字符串。此字符串没有特殊意义,只是它不太可能作为消息本身的一部分出现。我使用了等号和下划线等字符,以及字母和数字的半随机字符串来帮助确保这一点。然后,我们使用这个字符串将消息分成几个部分。

The text “This is a multi-part message in MIME format.” is included for the benefit of older mail programs, so that the user has some idea of why the email may not appear quite as expected. With that disclaimer out of the way, we use our boundary to mark the start of the first part of our message:

文本“This is a multi-part message in MIME format.”是为了旧邮件程序的利益而包含的,这样用户就可以了解为什么电子邮件可能不像预期的那样出现。有了这个免责声明,我们用我们的边界来标记我们信息第一部分的开始:.

--==Multipart_Boundary_xc75j85x
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
This is the text portion of the mixed message.

Notice that we add two dashes (--) to the beginning of the boundary string when we actually use it.

请注意,在实际使用边界字符串时,我们在它的开头添加了两个破折号(-)。

After the first boundary, we begin the text version of the message. Each part of the message begins with a couple of headers to indicate its content type and encoding. In the case of the text part, the content type is text/plain (with the standard character set, iso-8859-1), and the encoding is 7bit (plain ASCII text). A blank line marks the end of the headers, which are followed by the message body.

在第一个边界之后,我们开始消息的文本版本。消息的每个部分都以两个标题开头,以指示其内容类型和编码。对于文本部分,内容类型为text/plain(使用标准字符集iso-8859-1),编码为7bit(纯ASCII文本)。一个空行标记标题的结尾,后面是消息正文。

The HTML version then follows:

HTML版本如下所示:

--==Multipart_Boundary_xc75j85x
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<html>
<body>
<p>This is the <b>HTML portion</b> of the mixed message.</p>
</body>
</html>
The headers are almost identical to the text part, but this time we specify text/html as the content type. After the body of the HTML document, all that remains is the closing boundary, to which we add an extra two dashes on the end to mark the end of the message:

--==Multipart_Boundary_xc75j85x--

As you can see, mixed messages may look complicated, but they’re actually pretty simple when you take a closer look. The only tricky part from the standpoint of a PHP developer who wants to send a message like this is the task of generating a boundary string.

正如您所看到的,混合消息可能看起来很复杂,但仔细看,它们实际上非常简单。对于希望发送这样的消息的PHP开发人员来说,唯一棘手的部分是生成边界字符串。

Here’s how I like to do it:

我喜欢这样做:

<?php
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
?>

We take the current Unix timestamp (the number of seconds since January 1, 1970), as given by time(), and use the MD5 algorithm to convert it to a semi-random string. This string is then used to make up part of the boundary string. Feel free to use whatever method you like to generate your boundary strings.

我们使用time()给出的当前Unix时间戳(自1970年1月1日以来的秒数),并使用MD5算法将其转换为半随机字符串。然后,该字符串用于构成边界字符串的一部分。可以随意使用任何方法来生成边界字符串。

With all this in mind, you should be well-equipped to generate mixed messages in PHP. Since mixed messages are relatively uncommon, I’ll leave the actual code for you to write as an exercise. This business of splitting up messages into parts with a boundary string is important, however, when it comes to file attachments — the subject of our final section.

考虑到所有这些,您应该能够在PHP中生成混合消息。由于混合消息比较少见,我将把实际代码留给您作为练习来编写。然而,当涉及到文件附件(我们最后一节的主题)时,使用边界字符串将消息拆分为多个部分是很重要的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值