使用SMTP验证时通过PEAR发送HTML邮件会返回错误 [英] Sending HTML message through PEAR while using SMTP authentication returns an error
问题描述
我试图在PHP中使用SMTP身份验证发送HTML邮件给Gmail。这里是我正在使用的脚本:
require_onceMail.php;
require_once'Mail / mime.php';
$ from =某个名称< myemail@gmail.com>;
$ to =其他名称< otheremail@gmail.com>;
$ subject =这是一个测试;
$ crlf =\\\
;
$ 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->头文件($头文件);
$ mail = $ smtp->发送($ to,$ headers,$ body); (PEAR :: isError($ mail)){
echo(< p>。$ mail-> getMessage()。< / p>)的
) ;
} else {
echo(< p>邮件成功发送!< / p>);
注意: $ body
是一个包含图片和其他信息的HTML表。
当我执行脚本时,它会失败,并显示以下错误:
lockquote
无法设置发件人:某些名称[SMTP:从服务器收到的无效响应代码
(代码:555,响应:5.5.2)语法错误。
c6sm20541406obd.22)]
以下是我尝试查看错误的原因:
1.使用'Mail'代替'smtp'的相同脚本,即
$ smtp = Mail :: factory('邮件');
这工作得很好。
2.使用与 mime.php 相同的脚本,这也适用,但不允许发送HTML电子邮件。
有人知道如何将两者结合起来,以便我仍然使用SMTP认证和发送HTML邮件吗?
编辑:
这是 $ mime-> headers()
的转储:
[MIME-Version] => 1.0
[From] =>一些名称
[Return-Path] =>一些名称
[Subject] =>这是一个测试
[Content-Type] =>多部分/替代;
boundary == _ 8662996a1f586248545d9f01f48e916d
它终于出来了。
感谢 Abid Hussain 提供的链接这里的例子: http://tonyvirelli.com/slider/php-html -email-using-smtp /
解决方案:
- 删除
'Return-Path'=> $ from
来自标题数组。 - 添加
To => $ to
添加到标题数组(没有这个,收到的电子邮件的 to 标题是未公开的收件人)
I'm trying to send an HTML message while using SMTP authentication to Gmail in PHP. Here is the script that I am using:
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>");
}
Note: the $body
is an HTML table with images and other info.
When I execute the script it fails with the following error:
Failed to set sender: Some name [SMTP: Invalid response code received from server (code: 555, response: 5.5.2 Syntax error. c6sm20541406obd.22)]
Here is what I've tried to see what is going wrong: 1. Using the same script using 'Mail' instead of 'smtp' i.e.
$smtp = Mail::factory('Mail');
This works just fine. 2. Using the same script w/o the mime.php, this also works but doesn't allow one to send an HTML email.
Does anybody know how I can combine the two so that I'm still using SMTP authentication and send an HTML message?
EDIT:
Here is the dump of $mime->headers()
:
[MIME-Version] => 1.0
[From] => Some Name
[Return-Path] => Some Name
[Subject] => This is a test
[Content-Type] => multipart/alternative;
boundary="=_8662996a1f586248545d9f01f48e916d"
Figured it out myself finally.
Thanks to Abid Hussain for providing the link to the example here: http://tonyvirelli.com/slider/php-html-email-using-smtp/
Solution:
- Remove the
'Return-Path' => $from
from the headers array. - Add
To => $to
to the headers array(without this, the to header of the received email is Undisclosed recipients)
这篇关于使用SMTP验证时通过PEAR发送HTML邮件会返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!