通过smtp向google gmail发送邮件报错问题处理

在Laravel里用smtp方式通过gmail发送邮件出现下面的错误:

Failed to authenticate on SMTP server with username "[email protected]" using 3 possible authenticators. Authenticator LOGIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials a25sm9780898pfo.27 - gsmtp
". Authenticator PLAIN returned Expected response code 235 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials a25sm9780898pfo.27 - gsmtp
". Authenticator XOAUTH2 returned Expected response code 250 but got code "535", with message "535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8 https://support.google.com/mail/?p=BadCredentials a25sm9780898pfo.27 - gsmtp
".

需要按下面的步骤去解决:

1、开启2步验证方式,通过这里:https://www.google.com/landing/2step/

2、为GMAIL设置一个单独的密码,通过这里:https://security.google.com/settings/security/apppasswords

3、把第二步里的密码设置到Laravel的.env文件里。

对于.env邮件的设置也要注意加密方式,下面的我的配置:

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=独立密码
MAIL_ENCRYPTION=tls
[email protected]
MAIL_FROM_NAME="${APP_NAME}"