Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
We currently don't log the sending of order receipts, I think it's a really nice to have feature.
The problem is that we can only react to an email being sent via a hook_mail_alter()
but unfortunately, that happens too early and the log module wouldn't be able to know whether the email was successfully sent.
So I think we need to introduce a new event that allows us to access the email params and the $message
array.
Comment | File | Size | Author |
---|---|---|---|
#7 | interdiff_4-7.txt | 8.47 KB | jsacksick |
#7 | 3136999-7.patch | 14.41 KB | jsacksick |
|
Comments
Comment #2
jsacksick CreditAttribution: jsacksick at Centarro commentedThe attached patch adds 2 log templates (one for when the email fails to be sent, and the other one on success).
I'm wondering whether we should create a log template per email sent? vs reusing a "generic" one, although we only send the order receipt from Commerce.
Also, the event subscriber that reacts to the email being sent just checks whether the mail id starts with "order_", so any other integration sending an email with an ID starting with "order_" is going to be logged.
The log templates could be improved, as well as the naming of the event introduced when an email is sent through the Commerce mail handler. (currently called
PostMailSend
), since it's fired AFTER the email is sent.These are 2 log templates that were created:
Comment #3
mglamanWe could provide specific logs for known email key IDs and fallback to this if none exists.
Maybe follow a pattern of `mail_${key}`? That way users can also provide log templates for custom emails they send through our mail handler.
So provide a default `mail_order_receipt`, then someone could provide `mail_order_shipped` if they send an `order_shipped` email through our service (I currently do this.)
So we can check if the definition exists and use that or the fallback for the SUCCESS, and the generic failure on any failure.
This only handles order mail, so maybe we should rename it as such?
I've stopped grabbing entity storage in constructors, as it adds a little bit of overhead fetching the storage when it may not be used.
When collecting event subscribers, this will always be constructed and always fetch and instantiate the log storage.
Comment #4
jsacksick CreditAttribution: jsacksick at Centarro commentedAttached patch is addressing the feedbacks from #3.
Comment #5
mglaman🥳 looks great to me, this will definitely help customer support users
Comment #6
rszrama CreditAttribution: rszrama at Centarro commentedI like the idea of logging failures, but it may be confusing what success or failure means in this context. A log message of:
Makes me wonder why the log says it was sent if the result was a failure; did it actually send? was the failure a lack of receipt by the customer? an error from an external email service? In reality, it just didn't send. I'm open to suggestions, but it seems better to have:
The problem is that seems to violate the expectation of log message templates. I can't tell if we'd have to register two templates per mail (successful send vs. failure) or perhaps just implement a single failure template for any email:
Using a specific template for failures may make it easier to add contextual links to log messages in the future (e.g. a link or button to attempt to redeliver that email in the stream), though I'm dependent on you to know what's really possible there.
Comment #7
jsacksick CreditAttribution: jsacksick at Centarro commentedMakes sense! Attached patch addresses the comment from #6 and also introduces a generic log template for "generic" order emails so that we still provide a message if no log template exists, both in case of failure and success.
Comment #8
rinasek CreditAttribution: rinasek at Centarro commentedLooking good to me.
Comment #9
rszrama CreditAttribution: rszrama at Centarro commented+1
Comment #11
mglaman🥳 Committed!