Reputation: 168101
I want a mailer class (let's say UserMailer
) to call a method on another mailer class (let's say LoggerMailer
) when UserMailer
's method is called. My code is like this:
class LoggerMailer < ActionMailer::Base
def log_mail(...)
...
mail(layout: ..., template: ..., to: ..., from: ..., subject: ..., ...)
end
end
class UserMailer < ActionMailer::Base
def some_mail_method(...)
...
LoggerMailer.log_mail(...)
mail(layout: ..., to: ..., from: ..., subject: ..., ...)
end
end
UserMailer#some_mail_method
is the main mail method, and when this is called, I want it to call LoggerMailer#log_mail
in addition to its own mail
method call.
However, when I call UserMailer#some_mail_method
, it does not seem to call LoggerMailer#log_mail
. I feel so because when I put a line binding.pry
within the method body of LoggerMailer#log_mail
, it does not seem to stop at that point.
LoggerMailer.log_mail
actually called?Upvotes: 1
Views: 651
Reputation: 434665
Your problem is that mailer methods don't send anything on their own, they (by calling mail
) just return ActionMailer::MessageDelivery
instances and then someone calls deliver_now
or deliver_later
on that ActionMailer::MessageDelivery
to actually arrange delivery.
When you do this:
LoggerMailer.log_mail(...)
you're calling the mailer but throwing away the ActionMailer::MessageDelivery
that it returns so nothing useful happens.
You need to deliver the log mail:
LoggerMailer.log_mail(...).deliver_now
You could also put the LoggerMailer.log_mail
and UserMailer. some_mail_method
messages in a wrapper that would forward the deliver_now
or deliver_later
methods when the caller gets around to trying to deliver the some_mail_method
.
Upvotes: 2