[Rails 3.2.13] I have a mailer that sends out implicit multipart emails based on .text.erb
and .html.haml
templates. No problems there.
I'm trying to add an after_filter to conditionally prevent sending to invalid emails. Based on this answer and all documentation I've read, this seems pretty straightforward.
However, including this after_filter changes content type from multipart/alternative
to text/plain
, even if the email is valid. In the docs: 'Implicit template rendering is not performed if any attachments or parts have been added to the email'. Does that apply here? Is this expected behavior, or am I doing something wrong?
Banging my head against the wall, because this seems so straightforward.
class Notify < ActionMailer::Base
include AbstractController::Callbacks
after_filter :ensure_valid
def some_email(user_id)
@user = User.find(user_id)
mail(to: @user.email)
end
...
private
def ensure_valid
if User.where(email: mail.to, invalid_email: true).any?
mail.perform_deliveries = false
end
true
end
end
Edit: after more tinkering, it seems calling the mail
object at all in the after_filter turns off multipart, even with something like:
def ensure_valid
puts "mail: #{mail}"
true
end
What's going on here?
Tried adding content_type + params back manually in the after_filter, e.g.
mail.content_type = 'multipart/alternative'
mail.header['Content-Type'].parameters[:boundary] = mail.body.boundary
mail.header['Content-Type'].parameters[:charset] = 'UTF-8'
But for whatever reason this doesn't seem to be working. It DOES set content_type if I only include
mail.content_type = 'multipart/alternative'
.. but then I'm missing boundary and charset parameters.
Other things I've tried that don't work:
- setting default content_type in environment.rb
- setting default content_type in the mailer