我试图通过Gmail的SMTP服务器从一个PHP页面发送电子邮件,但是我得到了这个错误:
身份验证失败[SMTP:SMTP服务器不支持身份验证(代码:250,响应:mx.google.com at your service,[98.117.99.235]SIZE 35651584 8Bitmime STARTTLS ENHANCEDSTATUSCODES PIPELINING)]
有人能帮忙吗? 下面是我的代码:
<?php
require_once "Mail.php";
$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <ramona@microsoft.com>";
$subject = "Hi!";
$body = "Hi,\n\nHow are you?";
$host = "smtp.gmail.com";
$port = "587";
$username = "testtest@gmail.com";
$password = "testtest";
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject);
$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("<p>" . $mail->getMessage() . "</p>");
} else {
echo("<p>Message successfully sent!</p>");
}
?>
// Pear Mail Library
require_once "Mail.php";
$from = '<fromaddress@gmail.com>';
$to = '<toaddress@yahoo.com>';
$subject = 'Hi!';
$body = "Hi,\n\nHow are you?";
$headers = array(
'From' => $from,
'To' => $to,
'Subject' => $subject
);
$smtp = Mail::factory('smtp', array(
'host' => 'ssl://smtp.gmail.com',
'port' => '465',
'auth' => true,
'username' => 'johndoe@gmail.com',
'password' => 'passwordxxx'
));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo('<p>' . $mail->getMessage() . '</p>');
} else {
echo('<p>Message successfully sent!</p>');
}
使用Swift mailer,通过Gmail凭据发送邮件相当容易:
<?php
require_once 'swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('smtp.gmail.com', 465, "ssl")
->setUsername('GMAIL_USERNAME')
->setPassword('GMAIL_PASSWORD');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Test Subject')
->setFrom(array('abc@example.com' => 'ABC'))
->setTo(array('xyz@test.com'))
->setBody('This is a test mail.');
$result = $mailer->send($message);
?>
您的代码似乎没有使用TLS(SSL),这是向Google传递邮件所必需的(并且使用端口465或587)。
您可以通过设置
$host=“ssl://smtp.gmail.com”;
您的代码看起来很像这个例子,在主机名方案中引用了ssl://。