使用php从Unix weberver网站使用GMail SMTP服务器发送电子邮件

时间:2014-06-27 13:16:11

标签: php email unix ssl smtp

我正在尝试使用以下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 webserver网页执行代码。我收到了以下错误。

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

但上面的代码在我的localhost中工作正常。

请帮我完成这个项目。

感谢。

1 个答案:

答案 0 :(得分:-1)

我遇到了同样的问题,您应该在G-Suite中启用SMTP,更多详情请点击此处: https://support.google.com/a/answer/2956491?hl=en