
Laravel邮件通知(Notification)与原生Mail类是两套独立机制,不可混用;Notification必须继承Notification类、实现toMail()方法,并由Notifiable模型触发,SMTP配置统一通过.env和config/mail.php管理,配置错误或缓存未清除将导致静默失败。
直接说结论:Laravel 的邮件通知(Notification)和原生邮件类(Mail)是两套机制,不能混用;发 Notification 必须用 Notifiable trait 和 toMail() 方法,而 SMTP 配置统一走 .env 和 config/mail.php,改错位置就发不出邮件。
toMail() 方法Notification 不是直接调用 Mail::to(),而是通过用户模型触发。常见错误是写了 Notification::send() 却没在 Notification 类里定义 toMail(),结果静默失败、无报错、无日志。
Notification,且至少实现 toMail($notifiable) 方法$notifiable 必须实现了 Notifiable trait,并有 routeNotificationForMail() 或 email 属性(默认读取模型的 email 字段)AnonymousNotifiable 实例php // app/Notifications/OrderShipped.php use Illuminate\Notifications\Notification; use Illuminate\Notifications\Messages\MailMessage;class OrderShipped extends Notification { public function toMail($notifiable) { return (new MailMessage) ->subject('订单已发货') ->line('您的订单 #'.$this->order->id.' 已发出。') ->action('查看订单', url('/orders/'.$this->order->id)); } }
.env 里的 MAIL_MAILER 和对应字段Laravel 9+ 默认用 MAIL_MAILER=smtp,但很多人改了 MAIL_HOST 却忘了同步改 MAIL_PORT 或漏设 MAIL_ENCRYPTION,导致连接被拒绝或认证失败。
MAIL_PORT=587 + MAIL_ENCRYPTION=tls;163 邮箱同理;Gmail 要开“应用专用密码”,端口也是 587MAIL_MAILER=smtp,MAIL_USERNAME 必须是完整邮箱地址(如 user@qq.com),不是用户名MAIL_MAILER=log 看日志是否生成,排除代码逻辑问题# .env
MAIL_MAILER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=587
MAIL_USERNAME=your@qq.com
MAIL_PASSWORD=your_app_password # 不是登录密码
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS=your@qq.com
MAIL_FROM_NAME="${APP_NAME}"有人试图在 toMail() 里写 Mail::to(...)->send(...),这是双重发送,不仅多余,还可能因重复认证失败。
Notification 就老实用 $user->notify(new OrderShipped($order)) 或 Notification::send($users, new OrderShipped($order))
Mail::to('x@example.com')->send(new WelcomeEmail()),这时需要自己写 Mailable 类(继承 Mailable,含 build())MailMessage 是链式构建,Mailable 才用 resources/views/emails/welcome.blade.php
queue:work 并检查 QUEUE_CONNECTION
Notification 默认不进队列,要发队列必须显式加 implements ShouldQueue,否则即使配置了 Redis/Database 队列,也还是同步发——本地开发容易误以为“没反应”其实是卡在 SMTP 连接超时。
.env 中 QUEUE_CONNECTION=redis(或 database)且服务
正常php artisan queue:work --tries=3,否则失败任务不会重试,也不会进 failed_jobs 表retry_after)必须大于这个值,否则任务被重复投递最常被忽略的是:Notification 类里没返回 MailMessage 实例(比如忘了 return),或者 .env 没 php artisan config:clear 导致缓存旧配置。这两点一错,连日志都看不到。