我正在尝试在 PHP 中使用 SMTP 身份验证向 Gmail 发送 HTML 消息。这是我正在使用的脚本:
require_once "Mail.php";
require_once 'Mail/mime.php';
$from = "Some Name <[email protected]>";
$to = "Other Name <[email protected]>";
$subject = "This is a test";
$crlf = "\n";
$host = "ssl://smtp.gmail.com";
$port = "465";
$username = "[email protected]";
$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>");
}
Note: the $body
是一个包含图像和其他信息的 HTML 表格。
当我执行脚本时,它失败并出现以下错误:
无法设置发件人:某个名称 [SMTP:响应代码无效
从服务器收到(代码:555,响应:5.5.2 语法错误。
c6sm20541406obd.22)]
这是我尝试查看出了什么问题的方法:
1. 使用相同的脚本,使用“Mail”而不是“smtp”,即
$smtp = Mail::factory('Mail');
这很好用。
2. 使用相同的脚本,但不使用mime.php,这也可以,但不允许发送 HTML 电子邮件。
有谁知道如何将两者结合起来,以便我仍然使用 SMTP 身份验证and发送 HTML 消息?
EDIT:这是转储$mime->headers()
:
[MIME-Version] => 1.0
[From] => Some Name
[Return-Path] => Some Name
[Subject] => This is a test
[Content-Type] => multipart/alternative;
boundary="=_8662996a1f586248545d9f01f48e916d"