简体   繁体   English

PHP mail()标头

[英]PHP mail() headers

I have the following headers that I want to pass to the mail() function: 我想将以下标头传递给mail()函数:

$headers = "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From:".$sender_email."\r\n";
$headers .= "Subject:".$subject."\r\n";
$headers .= "Reply-To: ".$sender_email."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";

$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."\r\n\r\n";

$headers .= "--".md5('boundary2')."\r\n";
$headers .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$headers .= $message."\r\n\r\n";

$headers .= "--".md5('boundary2')."--\r\n";
$headers .= "--".md5('boundary1')."\r\n";
$headers .= "Content-Type:  ".$file_type."; ";
$headers .= "name=\"".$file_name."\"\r\n";
$headers .= "Content-Transfer-Encoding:base64\r\n";
$headers .= "Content-Disposition:attachment; ";
$headers .= "filename=\"".$file_name."\"\r\n";
$headers .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$headers .= $encoded_content."\r\n";
$headers .= "--".md5('boundary1')."--"; 

$sentMail = @mail($recipient_email, $subject, $message, $headers);

Since I don't require the user to provide an e-mail address, can i exclude the "From" and Reply-To" fields and let the server auto-complete those values or would this action result in a malformed headers? 由于我不需要用户提供电子邮件地址,因此我可以排除“发件人”和“答复至”字段并让服务器自动完成这些值,还是会导致标头格式错误?

The server will automatically fill in the From line. 服务器将自动填写From行。 Depending on the OS, it either gets the default From header from php.ini or from the username that runs PHP (eg www-user@yourdomain.com ). 根据操作系统的不同,它可以从php.ini或运行PHP的用户名(例如www-user@yourdomain.com )中获取默认的From头。

By default there's no Reply-to , so replies are sent to the From address. 默认情况下,没有Reply-to ,因此答复将发送到From地址。 This header is only needed when you want replies to go to a different address, so in your case there's no need for it. 仅当您希望答复转至其他地址时才需要此标头,因此在您的情况下则不需要它。

If no replies are wanted you could let it default, but a common approach is to put an address like NoReply@yourdomain.com in the From line. 如果不需要答复,可以将其设为默认值,但是一种常见的方法是在“ From行中放入一个类似NoReply@yourdomain.com的地址。 This way, if they do reply, they'll get a bounce message saying that the account doesn't exist. 这样,如果他们确实回复了,他们会收到一条退回消息,说该帐户不存在。

You're also putting the message body into $headers , which isn't right. 您还将消息正文放入$headers ,这是不对的。 It's working because you also put a blank line before them -- that's treated as the separator between the headers and the message, so everything after that is actually sent in the body. 之所以起作用,是因为您还在它们之前放置了一个空行-这被视为标题和消息之间的分隔符,因此之后的所有内容实际上都在正文中发送。 But you shouldn't depend on this, so those lines should be sent as the message argument in mail() . 但是您不应该依赖于此,因此这些行应该作为mail()message参数mail()

$headers = "MIME-Version: 1.0\r\n";
$headers .= "X-Mailer: PHP/" . phpversion()."\r\n";
$headers .= "From: NoReply@yourdomain.com\r\n";
$headers .= "Subject:".$subject."\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."\r\n\r\n";

$body= "--".md5('boundary1')."\r\n";
$body .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."\r\n\r\n";

$body .= "--".md5('boundary2')."\r\n";
$body .= "Content-Type: text/plain; charset=ISO-8859-1\r\n\r\n";
$body .= $message."\r\n\r\n";

$body .= "--".md5('boundary2')."--\r\n";
$body .= "--".md5('boundary1')."\r\n";
$body .= "Content-Type:  ".$file_type."; ";
$body .= "name=\"".$file_name."\"\r\n";
$body .= "Content-Transfer-Encoding:base64\r\n";
$body .= "Content-Disposition:attachment; ";
$body .= "filename=\"".$file_name."\"\r\n";
$body .= "X-Attachment-Id:".rand(1000,9000)."\r\n\r\n";
$body .= $encoded_content."\r\n";
$body .= "--".md5('boundary1')."--"; 

$sentMail = @mail($recipient_email, $subject, $body, $headers);

There also seems to be no reason for multipart/alternative in the first body section, since you're not sending multiple versions of $message . 在第一正文部分,似乎也没有理由使用multipart/alternative ,因为您没有发送$message多个版本。

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

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