使用GMail SMTP服务器从PHP页面发送电子邮件

我试图通过一个PHP页面通过GMail的SMTP服务器发送一封电子邮件,但是我得到这个错误:

身份验证失败[SMTP:SMTP服务器不支持身份验证(代码:250,响应:mx.google.com为您服务,[98.117.99.235] SIZE 35651584 8BITMIME STARTTLS ENHANCEDSTATUSCODES流水线]]

谁能帮忙? 这是我的代码:

<?php
require_once "Mail.php";

$from = "Sandra Sender <sender@example.com>";
$to = "Ramona Recipient <ramona@microsoft.com>";
$subject = "Hi!";
$body = "Hi,nnHow 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,nnHow 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邮件程序 ,通过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://。

链接地址: http://www.djcxy.com/p/50531.html

上一篇: Send email using the GMail SMTP server from a PHP page

下一篇: How can I send an email by Java application using GMail, Yahoo, or Hotmail?