-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Messenger][Mailer] Send mails after the main message succeeded #34291
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
I agree with this implementation. But Im not sure (or aware) if this is the correct solution to the problem. I think the main issue is that we dont have much control over how the mails are sent. (Ie a messages put on the bus). Could an alternative solution give the user control over this, so they can configure what stamp or what bus to use? |
Not sure it'll answer all your doubts, but at least since #34648, we can now configure the message bus to use to send mails. |
Is there any reason to not add the |
@weaverryan : Previous to #37976, we had to know whether or not we already are dispatching or not. It could now be simplified. About adding it directly from the Mailer component, I guess it needs the same considerations as the end of the PR description, aka, should it be enabled by default for all mails? |
With the current implementation of this PR, assuming you're using FrameworkBundle, then we will now ALWAYS add the stamp. So that seems barely different than adding it always inside of Is there any practical downside to adding the stamp always (either via a middleware like in this PR or in Mailer)? |
That's what I meant, the same considerations apply 😄
If for whatever reason the mail should still be sent whenever the handler failed. |
In that case, shouldn't this be something you can control on a case-by-case basis? And can't we "already" do that by putting your email logic at the end of the handler? I might be over-simplifying :) |
Nope, as something else after the handler can fail, e.g: a Doctrine transaction. |
Ok, fair enough.
But doesn't the point still stand that the user might want to control this behavior? |
We've just moved away from |
JFYI, I'm not reopening this one as it needs work according to the latest merges and discussions about it. Reopening it would only pollute. (if anyone is willing to work on it till there, though, feel free of course!) |
Description
When using the Mailer component with a message bus, given the following handler:
the mail is sent despite the handler failed (because of the exception). Same would go with an async email, as it'll still send the
SendEmailMessage
to the transport, independently from the main handler failure or success.In a real app, this means a mail can be sent for a treatment that has actually failed (for instance, because the Doctrine middleware failed to flush the changes made in the handler).
Possible solution
Rely on the
DispatchAfterCurrentBusMiddleware
and theDispatchAfterCurrentBusStamp
stamp.This stamps allows to only handle the stamped message if and only if the handler in which it was dispatched succeeded.
A new
SendMailAfterCurrentBusMiddleware
middleware can automatically add the stamp toSendEmailMessage
when necessary.But this middleware must be registered before
DispatchAfterCurrentBusMiddleware
to work (which make it difficult to register in userland, as you need to disable default middleware to prepend it before).Right now, I don't see much drawbacks.
default_middleware
value? (allowing an array, i.e: accept something like['allow_no_handlers', 'send_mail_after_current_bus']
.