Laravel如何发送邮件Notification_Laravel邮件类创建与SMTP服务器配置【方法】

11次阅读

laravel邮件通知(Notification)与原生Mail类是两套独立机制,不可混用;Notification必须继承Notification类、实现tomail()方法,并由Notifiable模型触发,SMTP配置统一通过.env和config/mail.php管理,配置错误或缓存未清除将导致静默失败。

Laravel如何发送邮件Notification_Laravel邮件类创建与SMTP服务器配置【方法】

直接说结论:Laravel 的邮件通知(Notification)和原生邮件类(Mail)是两套机制,不能混用;发 Notification 必须用 Notifiable trait 和 toMail() 方法,而 SMTP 配置统一走 .envconfig/mail.php,改错位置就发不出邮件。

Notification 类必须实现 toMail() 方法

Notification 不是直接调用 Mail::to(),而是通过用户模型触发。常见错误是写了 Notification::send() 却没在 Notification 类里定义 toMail(),结果静默失败、无报错、无日志。

  • Notification 类必须继承 Notification,且至少实现 toMail($notifiable) 方法
  • $notifiable 必须实现了 Notifiable trait,并有 routeNotificationForMail()email 属性(默认读取模型的 email 字段)
  • 如果想发给非模型对象(比如临时邮箱),得手动传入 AnonymousNotifiable 实例
php // app/Notifications/OrderShipped.php use IlluminateNotificationsNotification; use IlluminateNotificationsMessagesMailMessage; 

class OrderShipped extends Notification { public function toMail($notifiable) { return (new MailMessage) ->subject('订单已发货') ->line('您的订单 #'.$this->order->id.' 已发出。') ->action('查看订单', url('/orders/'.$this->order->id)); } }

SMTP 配置只认 .env 里的 MAIL_MAILER 和对应字段

Laravel 9+ 默认用 MAIL_MAILER=smtp,但很多人改了 MAIL_HOST 却忘了同步改 MAIL_PORT 或漏设 MAIL_ENCRYPTION,导致连接被拒绝或认证失败。

  • qq 邮箱MAIL_PORT=587 + MAIL_ENCRYPTION=tls;163 邮箱同理;Gmail 要开“应用专用密码”,端口也是 587
  • 若用 MAIL_MAILER=smtpMAIL_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}"

发 Notification 和发普通邮件别搞混

有人试图在 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()
  • 两者模板路径不同:Notification 的 MailMessage 是链式构建,Mailable 才用 resources/views/emails/welcome.blade.php

队列发邮件时记得运行 queue:work 并检查 QUEUE_CONNECTION

Notification 默认不进队列,要发队列必须显式加 implements ShouldQueue,否则即使配置了 redis/database 队列,也还是同步发——本地开发容易误以为“没反应”其实是卡在 SMTP 连接超时。

  • 加队列后,确保 .envQUEUE_CONNECTION=redis(或 database)且服务正常
  • 执行 php artisan queue:work --tries=3,否则失败任务不会重试,也不会进 failed_jobs
  • SMTP 超时默认 30 秒,队列任务超时(retry_after)必须大于这个值,否则任务被重复投递

最常被忽略的是:Notification 类里没返回 MailMessage 实例(比如忘了 return),或者 .envphp artisan config:clear 导致缓存旧配置。这两点一错,连日志都看不到。

text=ZqhQzanResources