Skip to content

[5.4] Dispatch Illuminate\Mail\Events\MessageSent event - #18744

Merged
taylorotwell merged 3 commits into
laravel:5.4from
CyrilMazur:event-message-sent
Apr 10, 2017
Merged

[5.4] Dispatch Illuminate\Mail\Events\MessageSent event#18744
taylorotwell merged 3 commits into
laravel:5.4from
CyrilMazur:event-message-sent

Conversation

@CyrilMazur

Copy link
Copy Markdown
Contributor

Related to proposal laravel/ideas#505. This PR dispatches a new Illuminate\Mail\Events\MessageSent event AFTER the message is actually sent (to the contrary of MessageSending which is dispatched BEFORE the message is sent).

Pros:

  • MessageSent is dispatched only if the message is successfully sent. It is not dispatched if the message isn't successfully sent (ie the Mailer throws an exception).
  • The $message variable contains the message id (SES) / transmission id (SparkPost) that's set into the swift message's headers after the message is sent.

I think the use case in the example from the official doc is a bit clumsy (https://laravel.com/docs/5.4/mail#events), because the listener for logging the message is executed whether the email is actually sent or not. There could be an error while actually sending the email (ex: network issue, temporary API error from Mandrill / SparkPost etc...) and MessageSending would still be triggered. MessageSent would be a better fit for this use case, because when it's triggered it's guaranteed the message was actually sent.

@tillkruss

tillkruss commented Apr 9, 2017

Copy link
Copy Markdown
Contributor

Would you mind adding a test?

@tillkruss tillkruss changed the title Dispatch Illuminate\Mail\Events\MessageSent event [5.4] Dispatch Illuminate\Mail\Events\MessageSent event Apr 9, 2017
@CyrilMazur

Copy link
Copy Markdown
Contributor Author

Done. The test checks that both MessageSending and MessageSent events are fired. I also handled the case when $this->events is null.

@taylorotwell
taylorotwell merged commit 7cf5d6d into laravel:5.4 Apr 10, 2017
@CyrilMazur

Copy link
Copy Markdown
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants