PHPmailer适用于localhost但不适用于heroku

时间:2018-01-30 00:14:15

标签: php email phpmailer

PHPMailer在localhost phpmyadmin上工作,但是我在heroku上部署后无法工作,似乎PHPMailer在heroku上验证了问题。谁知道原因?

$to = 'xxx@gmail.com';
$subject = 'Aliens Abducted Me - Abduction Report';
$msg = "$name was abducted $when_it_happened and was gone for $how_long.\n" .
"Number of aliens: $how_many\n" .
"Alien description: $alien_description\n" .
"What they did: $what_they_did\n" .
"Fang spotted: $fang_spotted\n" .
"Other comments: $other";

$mail = new PHPMailer();
$mail->  SMTPDebug=2;
$mail ->  isSMTP();
$mail ->  Host='smtp.gmail.com';
$mail ->  SMTPAuth =true;
$mail ->  Username='xxx@gmail.com';
$mail ->  Password= 'correct password';
$mail ->  SMTPSecure ='tls';
$mail ->  Port =587;

$mail ->  setFrom($to);
$mail ->  addAddress($to);

$mail ->  addReplyTo($to);
$mail ->  Subject=$subject;
$mail ->  Body=$msg;
$mail ->  send();

这就是我得到的错误

2018-01-29 23:50:50 SERVER -> CLIENT: 220 smtp.gmail.com ESMTP                      y29sm10762113qtk.47 - gsmtp
2018-01-29 23:50:50 CLIENT -> SERVER: EHLO immense-bayou-46149.herokuapp.com
2018-01-29 23:50:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.158.107.41]250-SIZE 35882577250-8BITMIME250-STARTTLS250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-01-29 23:50:50 CLIENT -> SERVER: STARTTLS
2018-01-29 23:50:50 SERVER -> CLIENT: 220 2.0.0 Ready to start TLS
2018-01-29 23:50:50 CLIENT -> SERVER: EHLO immense-bayou-46149.herokuapp.com
2018-01-29 23:50:50 SERVER -> CLIENT: 250-smtp.gmail.com at your service, [54.158.107.41]250-SIZE 35882577250-8BITMIME250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH250-ENHANCEDSTATUSCODES250-PIPELINING250-CHUNKING250 SMTPUTF8
2018-01-29 23:50:50 CLIENT -> SERVER: AUTH LOGIN
2018-01-29 23:50:50 SERVER -> CLIENT: 334 VXNlcm5hbWU6
2018-01-29 23:50:50 CLIENT -> SERVER: <credentials hidden>
2018-01-29 23:50:50 SERVER -> CLIENT: 334 UGFzc3dvcmQ6
2018-01-29 23:50:50 CLIENT -> SERVER: <credentials hidden>
2018-01-29 23:50:50 SERVER -> CLIENT: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbty534-5.7.14 OxYqa_vgUvCRSPkrtCYvPqBkjM-c0OPhKwgtdEA-4Du9zmkqJK4-wc6EnlEwORPnhd5VeD534-5.7.14 nGYK31F2CT1owGvIlWvSuyfXZ7YYqHnRK7y8HCRQR3OmDf9VC1YbXjy2lfanNuSZq1NLMB534-5.7.14 X1F27an8Z_tB7u9-an0ree4tmC6TUr6TuUnmxaybqouLZDiTGy1coPfbeD4JLr0CL59SSY534-5.7.14 U-Qigx5DTQIaZ8H0onzz3s9E-sLpg> Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 y29sm10762113qtk.47 - gsmtp
2018-01-29 23:50:50 SMTP ERROR: Password command failed: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbty534-5.7.14 OxYqa_vgUvCRSPkrtCYvPqBkjM-c0OPhKwgtdEA-4Du9zmkqJK4-wc6EnlEwORPnhd5VeD534-5.7.14 nGYK31F2CT1owGvIlWvSuyfXZ7YYqHnRK7y8HCRQR3OmDf9VC1YbXjy2lfanNuSZq1NLMB534-5.7.14 X1F27an8Z_tB7u9-an0ree4tmC6TUr6TuUnmxaybqouLZDiTGy1coPfbeD4JLr0CL59SSY534-5.7.14 U-Qigx5DTQIaZ8H0onzz3s9E-sLpg> Please log in via your web browser and534-5.7.14 then try again.534-5.7.14 Learn more at534 5.7.14 https://support.google.com/mail/answer/78754 y29sm10762113qtk.47 - gsmtp
SMTP Error: Could not authenticate.
2018-01-29 23:50:50 CLIENT -> SERVER: QUIT
2018-01-29 23:50:50 SERVER -> CLIENT: 221 2.0.0 closing connection y29sm10762113qtk.47 - gsmtp
SMTP connect() failed. https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting

1 个答案:

答案 0 :(得分:0)

我不确定Gmail为什么不起作用,但我强烈建议您使用更合适的服务。

Heroku使用support for sending mail很好variety of production-grade email services。我亲自使用SendGrid并对此非常满意。

您可以使用environment variables配置邮寄设置,例如将开发机器指向MailCatcher之类的工具,并将生产Heroku机器指向SendGrid。