Since upgrading to Laravel 5.4 I have been making use of notifications. I send some emails via mailgun and previously added a custom header so I could receive webhooks from mailgun and track mail delivery etc.
So.. how can I add the custom header I need to send in the email? I don't want to send the header in all emails, just some specific emails from my app.
When sending notifications a Illuminate\Mail\Events\MessageSending event is fired. If I create a listener for this event I can get access to the Swift_Message object. I can then append a header to the object like this:
public function handle(MessageSending $event)
{
// Get Swift_Message obj
$message = $event->message;
// Get Message Headers
$headers = $message->getHeaders();
// Append custom header
$headers->addTextHeader('Custom-Header-Name', 'some-value');
}
Problem now.. is that the header value I want to pass will be dynamic. I have no way of passing it to the listener and everything is getting muddier...
Moving from 5.2 to 5.4 threw me as there are quite a few changes making me think I needed to use notifications. Instead of using notifications I have used Mail class and been able to add the custom headers no problem. I can still access all the notification markdown email boilerplate so this is suitable for me to make all emails consistent.
Hello @squibby I have the same problem, I want to send notifications and attach a custom header with model id, how do you solve this? I have my code for mail notification but I cant find a way to attach the header.
public function toMail($notifiable)
{
return (new MailMessage)
->greeting('Hello ' . $this->appointment->customer)
->line('The introduction to the notification.')
->action('Notification Action', url('/'))
->line('Thank you for using our application!');
}