【问题标题】:mail header in php not workingphp中的邮件标头不起作用
【发布时间】:2011-05-12 20:43:06
【问题描述】:

请帮助我,我在使用 php mail() 发送 html 格式的邮件时遇到问题 我认为问题在于标题。我包含了两个仅在单引号或双引号中略有不同的标题:

标题 1:

$headers = 'From: webmaster@example.com\r\n Reply-To: webmaster@example.com';
$headers .= '\r\nContent-Type: multipart/alternative; boundary="'.$random_hash.'"'; 

当我像上面那样使用单引号时,我所有的 html 代码都作为简单的文本打印在邮件中,没有正确的 html 格式。在\r\n 丢失之后,我的标题也被显示出来了。

标题 2:

$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"".$random_hash."\"";

使用这个,我得到了一个完美的标题,但现在我的邮件是空的,附件是空的。我不知道这是从哪里来的,因为我没有在邮件中附加任何内容。

请提出建议

【问题讨论】:

    标签: php sendmail email-headers


    【解决方案1】:

    如果您在 PHP 字符串上使用单引号,则转义字符(如 \r\n)将停止工作。

    如果没有更多上下文,我不确定如何帮助您处理附件。

    【讨论】:

      【解决方案2】:

      我不喜欢 php 邮件。我建议使用 XpertMailer:http://www.xpertmailer.com/ 做得很好。

      【讨论】:

      • +1 不错的库,还没见过。看起来不错!
      【解决方案3】:
      • 使用library
      • 如果轮不到轮子,就不要重新发明轮子。
      • 使用library!

      【讨论】:

      • 我无法忍受不要重新发明轮子这句话,因为它需要假设轮子本质上是完美的(从什么时候开始有任何框架/图书馆完美)......但我认为你是扩展正确的点...... +1
      【解决方案4】:

      这是我在切换到 phpmailer 之前使用的。如前所述,使用库。

      // Make email headers
      $separator = '--==Multipart_Boundary_'.md5(time());
      $eol = PHP_EOL;
      
      $filepath = "filename.pdf";
      // open pdf file
      $handle = fopen($filepath, "r");
      // read pdf file
      $f_contents=fread($handle,filesize($filepath));
      // encode read pdf file
      $attachment = chunk_split(base64_encode($f_contents));
      // close pdf file
      fclose($handle);
      $message = "Text goes here";
      
      // main header (multipart mandatory)
      $headers = "MIME-Version: 1.0".$eol;
      $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\"".$eol;
      $headers .= "Content-Transfer-Encoding: 7bit".$eol;
      $headers .= "X-Priority: 1 (Highest)".$eol;
      $headers .= "X-MSMail-Priority: High".$eol;
      $headers .= "Importance: High".$eol;
      
      // message
      $headers .= "--".$separator.$eol;
      $headers .= "Content-Type: text/plain; charset=utf-8".$eol;
      $headers .= "Content-Transfer-Encoding: 8bit".$eol.$eol;
      $headers .= $message.$eol;
      
      // attachment
      $headers .= "--".$separator.$eol;
      $headers .= "Content-Type: application/pdf; name=".$filename.$eol;
      $headers .= "Content-Transfer-Encoding: base64".$eol;
      $headers .= "Content-Disposition: attachment; filename=".$filename.$eol.$eol;
      $headers .= $attachment.$eol.$eol;
      $headers .= "--".$separator."--";
      

      【讨论】:

        【解决方案5】:

        如果您实际上没有定义具有多部分边界的两个版本(纯文本/HTML),那么您应该将 Content-type: multipart/alternative 更改为适合您邮件正文的内容类型。

        此外,PHPMailer 等库通常比 PHP 的原生 mail() 函数更受欢迎,因为它们提供了更大的灵活性,同时不需要您手动构建复杂的标头。

        【讨论】:

          猜你喜欢
          • 2017-04-30
          • 2011-08-20
          • 1970-01-01
          • 2017-12-10
          • 2013-01-02
          • 1970-01-01
          • 1970-01-01
          • 2017-09-18
          • 1970-01-01
          相关资源
          最近更新 更多