I need to save all sent emails to database. The usual way I would do it would be to add a custom delivery job to ApplicationMailer and handle it in that job (I got the idea from https://engineering.monstar-lab.com/en/post/2022/10/05/Handling-bounced-emails-by-SES-with-Rails/).
So my application_mailer.rb looks like this:
class ApplicationMailer < ActionMailer::Base
self.delivery_job = ::Jobs::EmailDeliveryJob
# ...
end
And the email_delivery_job.rb:
module Jobs
class EmailDeliveryJob < ActionMailer::MailDeliveryJob
queue_as :email_sending
def perform(mailer, mail_method, delivery_method, args:, kwargs: nil, params: nil)
response = super # Call the parent ActionMailer::MailDeliveryJob `perform` method to send the email
# Store the record inside SentEmails
user = params[:user]
return if user.blank?
SentEmail.create!(
user:,
email_type: "#{mailer}.#{mail_method}",
subject: response.subject,
sent_at: Time.zone.now,
content: response.body.to_s
)
end
end
end
It works fine when I call the mailer methods myself, but when an email is sent via Noticed (deliver_by :email, mailer: 'SomeMailer', method: :mail_type, format_data: :some_data, delay: 1.second
) it skips the MailDeliveryJob.
I need to get the subject and body data from the email that is being sent by Noticed (at least), but I don’t know how. Ive tried extending the deliver
method from the Noticed::Base (we’re using v1) but there is no useful data available at that point in the execution.
Any ideas?