php - 如何使用 pear mail mime

标签 php sendmail

如何将 pear mail mime 与 google 一起使用。我发现这个可以让你使用谷歌的 pear 邮件,但不能使用邮件 mime:http://globalconstant.scnay.com/2009/11/06/sending-email-through-gmail-using-php/

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

$from = "Sender <*******@googlemail.com>";
$to = "Receiver <*******@googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";

$headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject);


$host = "smtp.gmail.com";
$port = 465;
$username = "********@googlemail.com";
$password = "********";

$mime = new Mail_mime($crlf);
$mime->setHTMLBody($html);

$body = $mime->get();
$headers = $mime->headers($headers);

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

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

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";

我不断得到

Failed to add recipient: @localhost [SMTP: Invalid response code received from server (code: 555, response: 5.5.2 Syntax error. f52sm5542930wes.35)]

编辑:

现在邮件已经收到了,但是结果是这样的:

This is a message I sent from <a href=3D"http://www.php.net/">PHP</a> using=
 the PEAR Mail package and SMTP through Gmail. Enjoy!

最佳答案

您的电子邮件 header 似乎有问题。我根据 pear mail 文档 (http://pear.php.net/manual/en/package.mail.mail-mime.example.php) 更新了您的代码:

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

$from = "Sender <*******@googlemail.com>";
$to = "Receiver <*******@googlemail.com>";
$subject = "Welcome to SITENAME!";
$crlf = "\n";
$html = "<h1> This is HTML </h1>";

$headers = array('From' => $from,
                 'To' => $to,
                 'Subject' => $subject);


//$host = "smtp.gmail.com";
$host = "ssl://smtp.gmail.com"; // try this one to use ssl
$port = 465;
$username = "********@googlemail.com";
$password = "********";

//$mime = new Mail_mime($crlf);
$mime =  new Mail_mime(array('eol' => $crlf)); //based on pear doc     
$mime->setHTMLBody($html);

//$body = $mime->get();
$body = $mime->getMessageBody(); //based on pear doc above
$headers = $mime->headers($headers);

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

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

if (PEAR::isError($mail)) {
echo $mail->getMessage();
} else {
echo "Message sent successfully!";
}
echo "\n";

它对我有用,所以我希望它对你也有用! 干杯, 埃雷兹

关于php - 如何使用 pear mail mime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4535209/

相关文章:

php - 我的 CSS 有什么问题?

php - 在关联数组中查找和替换

php - AJAX 从 PHP 响应更改 div 内容不起作用

php - 如何读取我网站中的 wordpress cookie?

unix - 如何使用 sendmail 发送带有多个文本附件的 HTML 正文电子邮件

php - RecursiveIteratorIterator 和 RecursiveDirectoryIterator 到嵌套的 html 列表

php - 如何将 csv 文件附加到 php 电子邮件(从 magento root 运行的代码)

devise - Devise::Mailer的设置-散居者smtp_envelope_from和smtp_envelope_to sendmail

linux - 如何配置一个vps服务器在linux上发送邮件

linux - Content-Type 在线命令在 Centos 6.5 上不起作用