【问题标题】:PHP Mail Headers does not work?PHP 邮件标头不起作用?
【发布时间】:2017-04-30 05:01:55
【问题描述】:

每当我将标题添加到mail() 时,收件人都不会收到任何电子邮件。

这工作正常:

// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send
mail('contact@xxx.net', 'My Subject 2', $message);

这样不行:

// The message
$message = "Line 1\r\nLine 2\r\nLine 3";

// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\r\n");

// Send with headers
mail('contact@xxx.net', 'My Subject', $message, 'From: Test <test123@yahoo.co.uk>'); 

有什么想法吗?

编辑:

这似乎是由@yahoo.co.uk 电子邮件地址引起的。 @gmail.com 没问题!

为什么!???这与我的生产服务器有关吗??

编辑 2:

即使我使用PHPMailer,也会发生同样的事情:

// Include Composer autoloader.
require_once __DIR__ . '/vendor/autoload.php';

$mail = new PHPMailer;

$mail->setFrom('test123@yahoo.co.uk', 'Mailer');
$mail->addAddress('contact@xxx.net', 'Joe User');     // Add a recipient
$mail->addAddress('ellen@example.com');               // Name is optional
$mail->addReplyTo('info@example.com', 'Information');
$mail->addCC('cc@example.com');
$mail->addBCC('bcc@example.com');

$mail->Subject = 'Here is the subject';
$mail->Body    = 'This is the HTML message body <b>in bold!</b>';
$mail->AltBody = 'This is the body in plain text for non-HTML mail clients';

if(!$mail->send()) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
} else {
    echo 'Message has been sent';
}

但它适用于@gmail.com

$mail->setFrom('test123@gmail.com', 'Mailer');

【问题讨论】:

  • 我知道这不是您确切问题的答案,但是您是否考虑过使用现有的库(例如 PHPMailer)来为您解决这个问题?
  • 您是否从 PHP 或您的操作系统日志(如 syslog)中收到错误?这可能会告诉你原因。
  • 我没有从 PHP 中得到任何错误。我在我的服务器上看不到任何错误日志 - 它是共享主机。
  • PHPMailer 默认使用mail 函数。测试是设置 yahoo smtp (github.com/PHPMailer/PHPMailer) 以检查您的主机服务器(默认 PHP 邮件配置)是否被 yahoo 阻止。

标签: php email email-headers


【解决方案1】:

更具体地说,要回答您的问题,请参阅 http://php.net/manual/en/function.mail.php Example 2,标题为“Example #2 Sending mail with extra headers”。

您需要为有效标题添加返回/新行。

【讨论】:

  • "多个额外的标题应该用 CRLF 分隔"。您不需要使用单个标题行。
  • 我已尝试使用该示例 #2。它仍然是一样的。我发现了一个导致问题的奇怪原因 - 请参阅上面的编辑。
猜你喜欢
  • 2011-05-12
  • 2011-08-20
  • 1970-01-01
  • 2013-01-02
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多