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

身份验证失败[SMTP: SMTP服务器不支持身份验证(code: 250, response: 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>");
 }
?>

当前回答

I have a solution for GSuite accounts that doesnt have the "@gmail.com" sufix. Also I think it will work for GSuite accounts with the @gmail.com but havent tried it. First you should have the privileges to change the option "allos¿w less secure app" for your GSuite account. If you have the privileges (you can check in account settings->security) then you have to deactivate "two step factor authentication" go to the end of the page and set to "yes" for allow less secure applications. That's all. If you dont have privileges to change those options the solution for this thread will not work. Check https://support.google.com/a/answer/6260879?hl=en to make changes to "allow less..." option.

其他回答

// 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>');
}

自2022年5月30日起,谷歌将不再支持使用允许您使用用户名和密码登录谷歌帐户的第三方应用程序和设备。

但是,谷歌提供了一个简单的解决方案。

请输入由谷歌生成的app密码,而不是密码。首先,进入设置并启用两步验证。

然后点击App密码。

您应该看到应用程序密码屏幕。应用程序密码可以让你从不支持两步验证的设备上的应用程序登录到你的谷歌帐户。选择“邮件”作为应用程序,然后选择设备。在我的例子中,我选择了Other,因为我想将我的应用程序部署到云中。

完成后,单击GENERATE按钮。您将看到您生成的应用程序密码。

只需复制密码,并替换之前的密码在您的电子邮件发送服务生成的一个。不过,您将无法再次看到密码。

就是这样!

要在Ubuntu中安装PEAR的Mail.php,运行以下命令:

    sudo apt-get install php-pear
    sudo pear install mail
    sudo pear install Net_SMTP
    sudo pear install Auth_SASL
    sudo pear install mail_mime

使用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);
?>

我也有这个问题。我设置了正确的设置,并启用了不太安全的应用程序,但它仍然不起作用。最后,我启用了这个https://accounts.google.com/UnlockCaptcha,它为我工作了。