php发送邮件标题乱码_PHP电子邮件标题

php发送邮件标题乱码

Previously I've discussed sending a simple eMail with PHP. At its heart, the PHP mail() function requires just three arguments:

以前,我已经讨论过使用PHP发送简单的电子邮件 。 从本质上讲,PHP mail()函数仅需要三个参数:

<?php mail(to,subject,message); ?>

You'll note that the basic function is extremely sparse: there's not even information as to whom the eMail is from! (In fact the mail function will use the server administrator's eMail address as the default "Sent from" address for all eMail; as you will see, we can set the "from" address to any address we like, which is another reason to never trust an eMail based solely on who it appears to come from).

您会注意到基本功能极为稀疏:甚至没有关于电子邮件来自何人的信息! (实际上, mail功能将使用服务器管理员的电子邮件地址作为所有电子邮件的默认“发件人”地址;如您所见,我们可以将“发件人”地址设置为我们喜欢的任何地址,这是从不使用的另一个原因仅根据电子邮件来自谁来信任它)。

To add a sender, or to set the message body to HTML, as well as many other eMail options, we must use the fourth optional argument, header.

要添加发件人或将邮件正文设置为HTML以及许多其他电子邮件选项,我们必须使用第四个可选参数header

header is, in many ways, the trickiest part of sending an eMail via PHP: it must be formatted in exactly the right manner in order to achieve what we want. Because of the possible length and complication of the header section, we will often use the special concatenation method I mentioned in the earlier article to build up the header details: pay close attention to the use of periods, particularly before equals signs, in the code that follows.

在许多方面, header是通过PHP发送电子邮件的最棘手的部分:必须以完全正确的方式格式化header ,以实现我们想要的功能。 由于header部分的长度和复杂性,我们经常使用我在前一篇文章中提到特殊串联方法来构建header细节:在代码中请特别注意句点的使用,尤其是等号之前的使用随后。

As before, I'll form some variables before adding them into the mail function; this time, I'll add a $header variable and a matching argument to modify the sender information for the eMail. (And as before, you should check that your server has the mail function enabled, or if its use has any special tricks or conditions, before trying this code).

和以前一样,在将变量添加到mail函数之前,我将形成一些变量。 这次,我将添加$header变量和匹配的参数来修改电子邮件的发件人信息。 (和以前一样 ,在尝试此代码之前 ,您应该检查服务器是否启用了mail功能,或者是否使用了任何特殊的技巧或条件)。

<?php
$mailto = "youremailaddress@domain.com";
$mailsubject = "New eMail";
$mailmessage = "This is a new mail message.";
$mailheader = "From: sender@otherdomain.com\r\n";
$mailheader .= "Reply-To: sender@otherdomain.com\r\n";
mail($mailto,$mailsubject,$mailmessage,$mailheader); ?>

\r\n are escape characters, used to create a return and force a new line, respectively. You will need them after every new section added to the header. You can also use these characters as part of the $message variable to create new lines in the body of the eMail. (Alternatively you could format the message as HTML by further manipulation of the header section, using standard markup in the message body such as <p> to gain new lines (see below).)

\r\n是转义字符,分别用于创建返回值和强制换行。 在每个新部分添加到header后,您将需要它们。 您还可以将这些字符用作$message变量的一部分,以在电子邮件正文中创建新行。 (或者,您可以通过进一步操作header部分,使用消息主体中的标准标记(例如<p>来获得新行,从而将消息格式设置为HTML(请参见下文)。)

Note that the "From" and "Reply-To" eMail addresses may be different: the latter, if specified, is the address a reply eMail will be sent to.

请注意,“发件人”和“答复至”电子邮件地址可能有所不同:如果指定了后者,则是将电子邮件发送至的地址。

If you wanted to carbon-copy or blind carbon copy the eMail to a third party, add the following:

如果您想抄袭或盲目抄袭将电子邮件复制给第三方,请添加以下内容:

<?php
$mailheader = "From: sender@otherdomain.com\r\n";
$mailheader .= "Reply-To: sender@otherdomain.com\r\n";
$mailheader .= "CC: sombodyelse@domain.com\r\n";
$mailheader .= "BCC: hidden@domain.com\r\n"; ?>

To format the eMail as HTML:

要将电子邮件格式化为HTML:

<?php
$mailheader = "From: sender@otherdomain.com\r\n";
$mailheader .= "Reply-To: sender@otherdomain.com\r\n";
$mailheader .= "MIME-Version: 1.0\r\n";
$mailheader .= "Content-Type: text/html; charset=utf-8\r\n"; ?>

You can then format the eMail message in HTML:

然后,您可以用HTML格式化电子邮件消息:

<?php
$mailmessage = '<html><body>';
$mailmessage .= '<img src="http://thenewcode.com/assets/svg/favicon.svg" alt="The New Code" />';
$mailmessage .= '<h1>A Message from The New Code</h1>';
$mailmessage .= '<p>Message body text</p>';
$mailmessage .= '</body></html>'; ?>

(Note the use of an absolute path for the image src value, necessary due to the fact that the eMail may be viewed from anywhere.)

(请注意,对于图像src值,请使用绝对路径,这是必要的,因为可以从任何地方查看电子邮件。)

翻译自: https://thenewcode.com/338/PHP-eMail-Headers

php发送邮件标题乱码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值