I want to make a method accessible from a mailer view that returns a boolean depending on the user that the email is being sent to.
For example, let's say the user has an attribute is_subscriber
which can be true or false.
The method would contain something like this:
def show_banner?
FeatureManager.is_feature_available?(:subscription_banners) && !user.is_subscriber
end
I want to access this method from within the mailer views.
The problem is, I have a lot of mailers in my app (UserMailer, SubscriptionMailer, FollowUpMailer) and relying on an instance variable @user for each mailer seems a bit dangerous.
Is there any way I can access the email address from the "to" field as in this example so I can find the user by his email address:
mail :to => @user.email, :subject => I18n.t('welcome_mail.subject')
Is it a good idea to do that or is there a more generic way of making such helper method?
You can use @_message in actionview which gives you access to the message object from which you can access fields like 'to', 'from'.
I had a case where I need a unsubscribe button on all mails, So placed it in my layout which was used across mailers. I used the above solution to find the mail receiver and implement unsubscribe for the respective mail.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With