PHPMailer SMTP 连接错误 [英] PHPMailer SMTP Connect Error

查看:54
本文介绍了PHPMailer SMTP 连接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 AWS 中设置 PHPMailer.它返回错误,指出 SMTP connect() 失败.

I'm trying to setup PHPMailer in AWS. It returns error saying SMTP connect() failed.

代码

<?php
require 'vendor/autoload.php';
$mail = new PHPMailer;
$mail->isSMTP();
$mail->SMTPDebug =2;
$mail->Debugoutput = 'html';
$mail->Host = 'smtp.gmail.com';
$mail->Port = 587;
$mail->SMTPSecure = 'tls';
$mail->SMTPAuth = true;
$mail->AuthType = 'XOAUTH2';
$mail->oauthUserEmail = "xxx@gmail.com";
$mail->oauthClientId = "client-key.apps.googleusercontent.com";
$mail->oauthClientSecret = "secretkey";
$mail->oauthRefreshToken = "refresh_token";
$mail->setFrom('tittuonnet@gmail.com', 'ParkIt');
$mail->addAddress("tittuhpd@gmail.com", "Tittu Varghese");
$mail->Subject = 'ParkIt - Verification Code';
$mail->Body = 'Please verify your account';

$mail->send();
if (!$mail->send()) {
    echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";
}

?>

调试日志

SERVER -> CLIENT: 220 smtp.gmail.com ESMTP 22sm11056028qkv.52 - gsmtp
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection 22sm11056028qkv.52 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
SERVER -> CLIENT: 220 smtp.gmail.com ESMTP s191sm6833675qke.56 - gsmtp
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
CLIENT -> SERVER: STARTTLS
SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
CLIENT -> SERVER: EHLO server_domain_or_ip
SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.157.255.58]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
SMTP Error: Could not authenticate.
CLIENT -> SERVER: QUIT
SERVER -> CLIENT: 221 2.0.0 closing connection s191sm6833675qke.56 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting
Mailer Error: SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

AWS 安全规则

推荐答案

最后,我解决了我的问题.我在这个过程中发现了一些有用的资源(可能会在 SMTP 身份验证过程中导致类似问题.)

Finally, I resolved my issue. I have found some useful resources during the process (which may cause similar issues during SMTP authentication.)

解决方案即使您使用的是 Composer,您也必须在代码中包含 PHPMailerAutoload.php 文件.

Solution You have to include PHPMailerAutoload.php file to the code even if you are using composer.

require 'PHPMailer/PHPMailerAutoload.php';
require 'vendor/autoload.php';

加入群组允许风险访问未经审查的应用程序的权限,以便执行未经审查的/开发/单个 SMTP 身份验证的应用程序.您可以从此处阅读更多相关信息.

Join the group Allow Risky Access Permissions By Unreviewed Apps in order to execute unreviewed / development / single SMTP authenticated applications. You can read more about it from here.

这篇关于PHPMailer SMTP 连接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆