【问题标题】:Php Mail BCC not workingPhp邮件密件抄送不起作用
【发布时间】:2014-02-08 11:56:16
【问题描述】:

我正在尝试编辑此脚本以向自己发送密件抄送副本:

$to = $your_email;
$from = "Server Xt<dml_submitbot@noemail.com>";
$subject = "User Sent Msg :: $msg";
$HTMLmessage = $message;

emailHTML($to, $from, $subject, $HTMLmessage);

function emailHTML($to, $from, $subject, $HTMLmessage){

   $semi_rand = md5(time());  
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

   $headers = "From: ".$from;      
   $headers .=
   "\nMIME-Version: 1.0\n" .  
   "Content-Type: multipart/mixed;\n" .  
   " boundary=\"{$mime_boundary}\"";  

   $content .=
   "This is a multi-part message in MIME format.\n\n" .  
   "--{$mime_boundary}\n" .  
   "Content-Type:text/html; charset=\"iso-8859-1\"\n" .  
   "Content-Transfer-Encoding: 7bit\n\n" .  
   $HTMLmessage . "\n\n";  

   $ok = @mail($to, $subject, $content, $headers);  

   if(!$ok) {    
   die("Error sending email");  
   }  
}

我已尝试添加此 $headers .= "Bcc:email@example.com"\n";,但它不发送电子邮件...如何修改此脚本以使其正常工作?

【问题讨论】:

  • 最简单的方法:放弃它并使用 PHPMailer 或 Swiftmailer。两者都可以将密件抄送添加到单行代码中,并且如果出现问题,还可以提供更好的诊断。同样,您永远不应该使用@ 抑制运算符。这相当于把你的手指塞进耳朵里然后说“lalalalala听不到你的声音”
  • 您应该使用更结构化的方式来构建您的标题字段,例如$headers = array('…', '…'); $headers = implode("\r\n", $headers);。这样做可以消除在Content-TypeBCC 之间缺少换行符的情况。
  • 您的多部分消息也无效,因为它缺少final boundary delimiter

标签: php email bcc


【解决方案1】:

\r\n 分隔标题。

function emailHTML($to, $from, $subject, $HTMLmessage) {

   $semi_rand = md5(time());  
   $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";  

   $headers = "From: ".$from . "\r\n";   
   $headers .= "Bcc: email@example.com\r\n";   
   $headers .=
   "MIME-Version: 1.0\r\n" .  
   "Content-Type: multipart/mixed;\r\n" .  
   " boundary=\"{$mime_boundary}\"";  

   $content .=
   "This is a multi-part message in MIME format.\r\n\r\n" .  
   "--{$mime_boundary}\r\n" .  
   "Content-Type:text/html; charset=\"iso-8859-1\"\r\n" .  
   "Content-Transfer-Encoding: 7bit\r\n\r\n" .  
   $HTMLmessage . "\r\n\r\n";  

   $ok = @mail($to, $subject, $content, $headers);  

   if(!$ok) {    
   die("Error sending email");  
   }  
}

【讨论】:

    【解决方案2】:

    看来标题的顺序很重要!!!

    $from = "Sender Name<sender@stackoverflow.com>";
    $to="receiver@stackoverflow.com";
    $headers = "From: $from\r\n";
    $headers .= "To: $to\r\n";
    $headers .= "Return-Path: <".$to.">\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Bcc:email@gmail.com\r\n";
    $headers .= "Content-Type: text/HTML; charset=ISO-8859-1\r\n";
    

    【讨论】:

    • 我认为这里的顺序不相关?你确定这很重要吗?
    【解决方案3】:

    $headers .= "Bcc:email@example.com"\n" 是您使用的确切语法吗?

    如果这不是有效的 PHP 语法,您应该会收到错误消息。

    尝试更改为 $headers .= 'Bcc:email@example.com' . "\r\n"; 之类的内容

    【讨论】:

      猜你喜欢
      • 2011-04-02
      • 2018-08-24
      • 1970-01-01
      • 2017-08-28
      • 1970-01-01
      • 2015-12-19
      • 2012-03-20
      • 2014-07-22
      相关资源
      最近更新 更多