Chat Summary emails are being skipped for users with a valid email:
I’ve done some analysis and I believe I know why:
Here’s ultimately where the skipping happens:
This is because message.to is blank.
message.to is blank because the arguments sent to send_user_email do not include to_address and this is never enriched with the user’s primary email, e.g.:
This is called by Chat Mailer, and you will notice that to_address is missing:
and that is maybe fine, but by the time you get to the top of send_user_email, I believe to_address should be enriched with the user email so that when it is passed forward, the emailer is given access to this property and the emailing succeeds.
When I added this small monkey-patch to send_user_email, the problem goes away:
# CORE BUG: if we don't set to_address, ultimately the email won't send and will be skipped.
# This is a core bug and we will need to raise it on Meta.
if args[:to_address].blank? && user&.primary_email&.email
args[:to_address] = user&.primary_email&.email
end
I had a bit of a dig into this, and if we follow the trail there should be enrichment:
It starts from emailer here and then it calls the user notification extension which enriches with the user email here and then passes it to the build email helper here and finally sets the address in the message builder here before sending it.
Aside from that the chat summary email already works on other sites so there is likely something else that is causing an issue here.
I think an interim solution is to pass the user email to the job which should give us a bit more confidence/visibility that the correct data is there from the outset: