Laravel 10邮件通知出现意外默认签名,如何删除?
Laravel 10邮件通知中默认签名的来源及移除方法
我在Laravel 10项目中通过如下Notification发送邮件:
class ContactUsSentNotification extends Notification implements ShouldQueue { use Queueable; protected ContactUs $contactUs; protected User $manager; public function __construct(ContactUs $contactUs, User $manager) { $this->contactUs = $contactUs; $this->manager = $manager; } public function via(object $notifiable): array { return [ConfigValueEnum::get(ConfigValueEnum::NOTIFICATION_TARGET, 'mail')]; } public function toMail(object $notifiable): MailMessage { return (new MailMessage) ->line('Contact Us message sent') ->line('Dear ' . $this->manager->name . ', ') ->action('Notification Action', url(route('admin.contactUsFeedback.index'))) ->line($this->contactUs->author_name . ' (' . $this->contactUs->author_email . ') sent "' . $this->contactUs->subject . '" message:') ->line($this->contactUs->message ) ->line('As manager of the "' . (ConfigValueEnum::get(ConfigValueEnum::SITE_NAME)) . '" site you need to accept it.') ->line(PHP_EOL . ConfigValueEnum::get(ConfigValueEnum::SUPPORT_SIGNATURE)); // 我添加了从数据库读取的自定义支持签名 }
我已经自定义了从数据库读取的支持签名,但在Mailtrap中查看邮件时,发现末尾出现了使用.env文件中APP_NAME变量的意外默认签名,请问该默认签名来自哪里?如何找到并删除它?
默认签名的来源
这个默认签名是Laravel框架MailMessage类的内置行为:当你使用MailMessage构建邮件内容时,它会自动在邮件末尾追加一行默认问候语,内容为Regards,加上config('app.name')的值(也就是你.env文件里配置的APP_NAME)。
移除默认签名的方法
有两种简单方式可以取消这个默认签名:
- 调用
salutation方法设置空值
默认签名本质是通过MailMessage的salutation方法实现的,只需在你的toMail方法中给实例添加->salutation(''),就能覆盖默认的签名内容。修改后的代码如下:
public function toMail(object $notifiable): MailMessage { return (new MailMessage) ->line('Contact Us message sent') ->line('Dear ' . $this->manager->name . ', ') ->action('Notification Action', url(route('admin.contactUsFeedback.index'))) ->line($this->contactUs->author_name . ' (' . $this->contactUs->author_email . ') sent "' . $this->contactUs->subject . '" message:') ->line($this->contactUs->message ) ->line('As manager of the "' . (ConfigValueEnum::get(ConfigValueEnum::SITE_NAME)) . '" site you need to accept it.') ->line(PHP_EOL . ConfigValueEnum::get(ConfigValueEnum::SUPPORT_SIGNATURE)) ->salutation(''); // 取消默认签名 }
- 使用自定义邮件模板
如果需要完全掌控邮件内容结构,可以创建自定义的邮件视图模板,在toMail方法中使用->view()指定该模板,这样就不会触发MailMessage的默认签名逻辑。
内容的提问来源于stack exchange,提问作者mstdmstd
相关产品推荐
相关产品推荐

