使用带有 Gmail 帐户的 PHPMailer 发送邮件

Sending a mail using PHPMailer with Gmail Account

我正在尝试使用 PHPMailer 库从我的网站发送电子邮件。我这样做的目的是:

  1. 我从 this link 下载了 PHPMailer 5.2-stable。

  2. 我在我的主机上上传了以下文件:class.phpmailer.php, class.smtp.phpPHPMailerAutoload.php

  3. 然后我创建了一个文件名contact.php并在里面写了如下代码:

    require 'phpmailer/PHPMailerAutoload.php';
    $mail = new PHPMailer();
    $mail->SMTPDebug = 1;  
    
    $mail->SMTPAuth = true; 
    $mail->IsSMTP();
    $mail->Host = "smtp.gmail.com";
    $mail->Username = "*****************"; //My Email Address
    $mail->Password = "*****************"; //My Email's Address
    $mail->SMTPSecure = "tls";   
    $mail->Port = 578; 
    
    $mail->AddAddress('*****************');
    $mail->From = "*****************";
    $mail->FromName = "Website Contact Form - " . "Website Name";
    $mail->Subject = "New Message from Contact Form";
    
    $mail->Body    = 'This is the HTML message body <b>in bold!</b>';    
    $mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
    $mail ->isHTML(true);
    $message = NULL;
    if(!$mail->Send()) {
       $message = "Mailer Error: " . $mail->ErrorInfo;
    }else{
       $message = "Message sent!";
    }
    

但是在 运行 这个文件之后,我得到以下错误:

2021-06-25 11:44:54 SMTP ERROR: Failed to connect to server: Network is unreachable (101) SMTP connect() failed.

我尝试了几封电子邮件,包括直接从我的托管服务获取的电子邮件地址、一个 Gmail 帐户和一个 Google Workspace 电子邮件(以前称为 G 套装)。所有失败都产生了提到的错误。

最后,我认为值得一提的是我的托管服务不支持 PHP 的 mail() 功能,这就是我需要使用 SMTP 的原因。

Update: After contacting the host and trying a lot, while I am sure that I enter the correct username and password, I get the following error:

2021-06-27 12:45:19 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP u21sm391388lfu.60 - gsmtp
2021-06-27 12:45:19 CLIENT -> SERVER: EHLO empiya.com.tr
2021-06-27 12:45:19 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [77.245.159.9]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-06-27 12:45:19 CLIENT -> SERVER: STARTTLS
2021-06-27 12:45:19 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2021-06-27 12:45:19 CLIENT -> SERVER: EHLO empiya.com.tr
2021-06-27 12:45:19 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [77.245.159.9]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2021-06-27 12:45:19 CLIENT -> SERVER: AUTH LOGIN
2021-06-27 12:45:19 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2021-06-27 12:45:19 CLIENT -> SERVER: [credentials hidden]
2021-06-27 12:45:19 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2021-06-27 12:45:19 CLIENT -> SERVER: [credentials hidden]
2021-06-27 12:45:20 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 vrVLkxbm7_GwFssilR38zKpR_BuczuSM3RIslh77zk6d-DzAo1btUW7FKO8f69tyzO7Fn534-5.7.14 6jydZV9BH6qfEQfpcOfh1z-_V5nn7beFbI0Ekuto0gyBede5j-6dniX9mG6jLWRp>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 u21sm391388lfu.60 - gsmtp
2021-06-27 12:45:20 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbu534-5.7.14 vrVLkxbm7_GwFssilR38zKpR_BuczuSM3RIslh77zk6d-DzAo1btUW7FKO8f69tyzO7Fn534-5.7.14 6jydZV9BH6qfEQfpcOfh1z-_V5nn7beFbI0Ekuto0gyBede5j-6dniX9mG6jLWRp>534-5.7.14 Please log in via your web browser and then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 u21sm391388lfu.60 - gsmtp
SMTP Error: Could not authenticate.
2021-06-27 12:45:20 CLIENT -> SERVER: QUIT
2021-06-27 12:45:20 SERVER -> CLIENT: 221 2.0.0 closing connection u21sm391388lfu.60 - gsmtp
SMTP Error: Could not authenticate.
Message could not be sent. Mailer Error: SMTP Error: Could not authenticate.
  1. 确保为您的帐户启用“不太安全的应用程序访问”设置。 https://support.google.com/accounts/answer/6010255?hl=en#zippy=%2Cif-less-secure-app-access-is-on-for-your-account

  2. 其次确保您的主机允许使用出站端口578,或者您甚至可以要求他们启用出站端口。

  3. 尝试使用 ssl 代替 tls,后者更安​​全可靠。

此错误通常在 SMTP 邮件服务器凭据正确但未提供应用程序特定密码时显示。 当我查看您的代码时,我发现您在代码中以错误的方式编写了端口:

$mail->Port = 578;

虽然它应该用于 tls :

$mail->Port = 587;

希望这个回答对您有所帮助,别忘了填写

$mail->AddAddress('*****************');
$mail->From = "*****************";

部分因为如果你不这样做可能会出错