【问题标题】:PHP MIME headerPHP MIME 标头
【发布时间】:2014-03-15 11:08:19
【问题描述】:

我的 php 邮件程序有一些问题,我只收到未解释的源代码。

有人可以检查我的标题吗?

$recipient = str_replace(Array("\r","\n"),"",$this->to);
$headers = 'From: "xxx.ch" <contact@xxx.ch> '."\r\n";
$headers .= 'Return-Path: <postmaster@xxx.ch>' . "\r\n";
if ( isset($this->replyTo) ){
    $headers .= 'Reply-To: contact@xxx.ch' . "\r\n";
}
$random_hash = md5(date('r', time()));
$headers .= "MIME-Version: 1.0 \r\n Content-Type: multipart/alternative; boundary=\"".$random_hash."\""; 
$body = '--'.$random_hash."\r".' 
         Content-Type: text/plain; charset="UTF-8"'."\r".'
         Content-Transfer-Encoding: 8bit'."\r".'

         Merci d\'utiliser un client mail supportant le format HTML'."\r".'

        --'.$random_hash."\r".'

        Content-Type: text/html; charset="UTF-8"'."\r".'
        Content-Transfer-Encoding: 8bit'."\r";
$body .= $this->HTMLBody ."\r".'--'.$random_hash.'--';

谢谢

【问题讨论】:

  • 您的服务器设置为发送电子邮件吗?正在运行什么 SMTP 服务?是否正确配置 PHP 以发送电子邮件?
  • 是的服务器配置没问题,但是对于一些旧的邮件客户端不支持 html,所以我必须添加对它们的支持,现在使用 MIME 我有很多错误:-(
  • 您是否考虑过使用 PEAR 的 MIME 邮件包而不是手动操作?
  • 感谢您的帮助,但我无法在我的服务器上安装新软件包...
  • 那么,不要使用 PEAR。使用 PHPMailer 或类似的。无论哪种方式,手动编写 MIME 标头,尤其是以一种骇人听闻的方式,都不值得您花时间。

标签: php email mime


【解决方案1】:

虽然我同意其他评论者的观点,即您应该研究 3rd-party 库而不是手动执行此操作,但您当前的问题可能与 MIME 非常挑剔的行尾和空格有关。

你目前有很多这样的代码:

$body = '--'.$random_hash."\r".' 
         Content-Type: text/plain; charset="UTF-8"'."\r".'
         Content-Transfer-Encoding: 8bit'."\r"; // (and so on)

您正在小心地插入回车符 ("\r"),但随后也在下一个单引号字符串中嵌入了换行符和大量空格。

相反,您应该包含回车符,并确保所有其他空格都在单引号之外(您希望 PHP 可读,但不影响输出):

$body = '--' . $random_hash . "\r"
         . 'Content-Type: text/plain; charset="UTF-8"'."\r"
         . 'Content-Transfer-Encoding: 8bit'."\r"; // (and so on)

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 2013-10-06
    • 1970-01-01
    • 2010-10-30
    • 2012-05-02
    • 2012-09-12
    • 2015-10-27
    • 2011-12-20
    • 2015-10-16
    相关资源
    最近更新 更多