dsiv4041 2015-08-25 00:55
浏览 53
已采纳

PHP mail()标头

I have the following headers that I want to pass to the mail() function:

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

";

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

";

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

";
$headers .= $message."

";

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

";
$headers .= $encoded_content."
";
$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?

  • 写回答

1条回答 默认 最新

  • dongnaoxia0927 2015-08-25 01:00
    关注

    The server will automatically fill in the From line. Depending on the OS, it either gets the default From header from php.ini or from the username that runs PHP (e.g. www-user@yourdomain.com).

    By default there's no Reply-to, so replies are sent to the From address. 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. 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. 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().

    $headers = "MIME-Version: 1.0
    ";
    $headers .= "X-Mailer: PHP/" . phpversion()."
    ";
    $headers .= "From: NoReply@yourdomain.com
    ";
    $headers .= "Subject:".$subject."
    ";
    $headers .= "Content-Type: multipart/mixed; boundary=".md5('boundary1')."
    
    ";
    
    $body= "--".md5('boundary1')."
    ";
    $body .= "Content-Type: multipart/alternative;  boundary=".md5('boundary2')."
    
    ";
    
    $body .= "--".md5('boundary2')."
    ";
    $body .= "Content-Type: text/plain; charset=ISO-8859-1
    
    ";
    $body .= $message."
    
    ";
    
    $body .= "--".md5('boundary2')."--
    ";
    $body .= "--".md5('boundary1')."
    ";
    $body .= "Content-Type:  ".$file_type."; ";
    $body .= "name=\"".$file_name."\"
    ";
    $body .= "Content-Transfer-Encoding:base64
    ";
    $body .= "Content-Disposition:attachment; ";
    $body .= "filename=\"".$file_name."\"
    ";
    $body .= "X-Attachment-Id:".rand(1000,9000)."
    
    ";
    $body .= $encoded_content."
    ";
    $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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#sql#的问题:#情况描述 在用vs对项目进行调试时,出现找不到网络路径,然后查看SQL配置工具,发现SQL服务显示远程调用过程失败(相关搜索:防火墙)
  • ¥15 主要进行描述泥浆在管路不同区段泥浆的密度不相同,泥浆的密度有高有低,此时管路的摩阻分布需要怎么计算,(标签-matlab)
  • ¥40 通过编制程序计算圆管内层流充分发展对流换热,参数如图5-4,以及公式5-16所示,要求用表5-6对程序计算准确性进行验证
  • ¥20 该学习哪个编程语言? AI会取代程序员吗?
  • ¥15 ensp如何拼通IP地址
  • ¥15 saber软件导入Ibis模型报错
  • ¥15 商城生产日期批次库存问题
  • ¥15 esp8266控制共阳极wrgb灯板无法关闭所有led灯
  • ¥100 python读取速度问题
  • ¥15 stm32f407使用DMA问题