Open sdhull opened 8 months ago
The reason for that is that a Mail::Message
with one part is different to a Mail::Message
with multiple part. Having a html part and an attachment means it's multipart. You solution email_body = mail.body.to_s.presence || mail.html_part.to_s
is right, maybe just use decoded
and not to_s
, it's the same but more explicit.
What you can do is that you always add a text part to your mail, so it will always be multipart, no matter an attachment exists or not.
Apologies if a better venue for this question would be Stackoverflow or similar—feel free to send me away.
We're on mail 2.8.0.1 & Rails 6.0.6
We have a situation at work where a mailer (actionmailer) has multiple actions which all render the same template, eg:
We also have an
after_action
that parses the email and adjusts styling based on customer preferences, eg:However, when writing specs to troubleshoot a bug, I found that when the email had an ics attachment,
mail.decode_body
returnednil
, butmail.html_part.to_s
returned the email body. So I thought I'd switch to that to be safer, but in a different mailer action being tested (no attachment),mail.html_part.to_s
returned an empty string, whilemail.body.to_s
returned the html body!(
mail
in this context is theActionMailer::Base#mail
method which returns an instance ofMail::Message
.)So finally I ended up with
email_body = mail.body.to_s.presence || mail.html_part.to_s
as a best effort but I wanted to ask:Am I missing something? Is there a uniform interface to reliably get a string representation of the email body?