Laravel使用域名邮箱SMTP发送邮件认证失败求助
Laravel自定义域名邮箱SMTP认证失败问题排查
问题描述
在Laravel中使用自定义域名邮箱support@myDomain.io发送邮件时,出现SMTP认证失败错误,相同代码配置普通Gmail邮箱可正常发送。错误信息如下:
Swift_TransportException Failed to authenticate on SMTP server with username "support@xamer.io" 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 o28-20020aa7979c000000b005809a3c1b6asm11901153pfp.201 - 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 o28-20020aa7979c000000b005809a3c1b6asm11901153pfp.201 - 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 o28-20020aa7979c000000b005809a3c1b6asm11901153pfp.201 - gsmtp ".
相关配置
.env配置(自定义域名邮箱)
MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=support@myDomain.io MAIL_PASSWORD=****** MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=support@myDomain.io MAIL_FROM_NAME="${APP_NAME}"
控制器代码
Mail::to($data->email)->send(new EmailVerify($data));
app/Mail/EmailVerify.php代码
<?php namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; class EmailVerify extends Mailable { use Queueable, SerializesModels; /** * Create a new message instance. * * @return void */ public function __construct($data) { $this->data = $data; } /** * Build the message. * * @return $this */ public function build() { return $this->subject('Email from '.env('APP_NAME'))->view('mail.emailVerify')->with(['data' => $this->data]); } }
config/mail.php配置
<?php return [ /* |-------------------------------------------------------------------------- | Default Mailer |-------------------------------------------------------------------------- | | This option controls the default mailer that is used to send any email | messages sent by your application. Alternative mailers may be setup | and used as needed; however, this mailer will be used by default. | */ 'default' => env('MAIL_MAILER', 'smtp'), /* |-------------------------------------------------------------------------- | Mailer Configurations |-------------------------------------------------------------------------- | | Here you may configure all of the mailers used by your application plus | their respective settings. Several examples have been configured for | you and you are free to add your own as your application requires. | | Laravel supports a variety of mail "transport" drivers to be used while | sending an e-mail. You will specify which one you are using for your | mailers below. You are free to add additional mailers as required. | | Supported: "smtp", "sendmail", "mailgun", "ses", | "postmark", "log", "array" | */ 'mailers' => [ 'smtp' => [ 'transport' => 'smtp', 'host' => env('MAIL_HOST', 'smtp.mailgun.org'), 'port' => env('MAIL_PORT', 587), 'encryption' => env('MAIL_ENCRYPTION', 'tls'), 'username' => env('MAIL_USERNAME'), 'password' => env('MAIL_PASSWORD'), 'timeout' => null, 'auth_mode' => null, ], 'ses' => [ 'transport' => 'ses', ], 'mailgun' => [ 'transport' => 'mailgun', ], 'postmark' => [ 'transport' => 'postmark', ], 'sendmail' => [ 'transport' => 'sendmail', 'path' => '/usr/sbin/sendmail -bs', ], 'log' => [ 'transport' => 'log', 'channel' => env('MAIL_LOG_CHANNEL'), ], 'array' => [ 'transport' => 'array', ], ], /* |-------------------------------------------------------------------------- | Global "From" Address |-------------------------------------------------------------------------- | | You may wish for all e-mails sent by your application to be sent from | the same address. Here, you may specify a name and address that is | used globally for all e-mails that are sent by your application. | */ 'from' => [ 'address' => env('MAIL_FROM_ADDRESS', 'hello@example.com'), 'name' => env('MAIL_FROM_NAME', 'Example'), ], /* |-------------------------------------------------------------------------- | Markdown Mail Settings |-------------------------------------------------------------------------- | | If you are using Markdown based email rendering, you may configure your | theme and component paths here, allowing you to customize the design | of the emails. Or, you may simply stick with the Laravel defaults! | */ 'markdown' => [ 'theme' => 'default', 'paths' => [ resource_path('views/vendor/mail'), ], ], ];
正常工作的Gmail配置
MAIL_MAILER=smtp MAIL_HOST=smtp.gmail.com MAIL_PORT=587 MAIL_USERNAME=mymail.gmail.com MAIL_PASSWORD=******* MAIL_ENCRYPTION=tls MAIL_FROM_ADDRESS=mymail.gmail.com MAIL_FROM_NAME="${APP_NAME}"
解决方案
- 确认账号类型与权限
如果自定义域名邮箱属于Google Workspace(原G Suite)账号:- 不能直接使用邮箱登录密码,需先开启账号两步验证,再生成应用专用密码替换.env中的
MAIL_PASSWORD - 确保管理员未限制该账号的SMTP服务权限
- 不能直接使用邮箱登录密码,需先开启账号两步验证,再生成应用专用密码替换.env中的
- 检查账号安全状态
登录support@myDomain.io对应的Google账号,查看是否有异常登录提醒,解除账号安全限制 - 修正配置细节
确认.env中的MAIL_USERNAME是完整邮箱地址,域名部分无拼写错误 - 清除配置缓存
执行以下命令清除Laravel配置缓存,避免旧配置干扰:php artisan config:clear php artisan cache:clear
内容的提问来源于stack exchange,提问作者Neeraj Yadav
相关产品推荐
相关产品推荐

