使用SMTP身份验证时通过PEAR发送HTML邮件会返回错误

Ant*_*doi 5 php gmail pear smtp

我正在尝试在PHP中使用SMTP身份验证向Gmail发送HTML消息.这是我正在使用的脚本:

require_once "Mail.php";
require_once 'Mail/mime.php';

$from = "Some Name <myemail@gmail.com>";
$to = "Other Name <otheremail@gmail.com>";
$subject = "This is a test";
$crlf = "\n";

$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "myemail@gmail.com";
$password = "mypass";

$headers = array ('From' => $from,
                  'Return-Path' => $from,
                  'Subject' => $subject);

$smtp = Mail::factory('smtp',
  array ('host' => $host,
    'port' => $port,
    'auth' => true,
    'username' => $username,
    'password' => $password));

$mime = new Mail_mime($crlf);
$mime->setTXTBody("This is a test email message");
$mime->setHTMLBody($body);
$body = $mime->get();
$headers = $mime->headers($headers);

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
  echo("<p>" . $mail->getMessage() . "</p>");
} else {
  echo("<p>Message successfully sent!</p>");
}
Run Code Online (Sandbox Code Playgroud)

注意:$body是一个包含图像和其他信息的HTML表.

当我执行脚本时,它失败并出现以下错误:

无法设置发件人:某些名称[SMTP:从服务器收到无效的响应代码(代码:555,响应:5.5.2语法错误.c6sm20541406obd.22)]

这是我试图看到出现问题的原因:1.使用相同的脚本使用'Mail'代替'smtp'即

$smtp = Mail::factory('Mail');
Run Code Online (Sandbox Code Playgroud)

这很好用.2.使用与mime.php相同的脚本,这也有效但不允许发送HTML电子邮件.

有谁知道如何将两者结合起来,以便我仍在使用SMTP身份验证发送HTML消息?

编辑: 这是转储$mime->headers():

[MIME-Version] => 1.0
[From] => Some Name
[Return-Path] => Some Name
[Subject] => This is a test
[Content-Type] => multipart/alternative;
boundary="=_8662996a1f586248545d9f01f48e916d"
Run Code Online (Sandbox Code Playgroud)

Ant*_*doi 1

终于我自己弄清楚了。

感谢Abid Hussain提供示例链接:http://tonyvirelli.com/slider/php-html-email-using-smtp/

解决方案:

  1. 'Return-Path' => $from从 headers 数组中删除。
  2. 添加To => $to到 headers 数组(如果没有这个,收到的电子邮件的to标头是Undisknownrecipients