在Unix web服务器上使用php通过GMail SMTP服务器发送电子邮件


Send email using the GMail SMTP server from a Unix weberver website using php

我正在尝试使用下面的php代码发送SMTP电子邮件。

    <?php
require_once "Mail.php";

$from = "info@XXXXX.com";
$to = "info@XXXXXX.com";
$subject = "Hi";
$body = "Hi,'n'nHow are you?'nDoing anything this Friday evening?'n";
 $host = "ssl://smtp.gmail.com";
 $port = "465";
$username = "xxxxx";
$password = "xxxxx";
$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>" );
}
 ?>

而我正在执行从unix web服务器网页的代码。我得到以下错误。

authentication failure [SMTP: Invalid response code received from server (code: 535, response: Incorrect authentication data)]

但是上面的代码在我的本地主机上运行良好。

请帮助我完成这个项目。

谢谢。

我有同样的问题,你应该在G-Suite中启用SMTP,更多细节在这里:https://support.google.com/a/answer/2956491?hl=en