In Rails, the Mailer class, which inherits ActionMailer :: Base, uses a method called mail
to create a mail object.
Calling the method deliver
on the return value mail object will send the mail.
To get the body of this HTML email as text, call the method as follows:
mail.html_part.body.to_s
This html_part
is defined in the Gem mail used by Rails.
2020-11-Code as of 14
# Accessor for html_part
def html_part(&block)
if block_given?
self.html_part = Mail::Part.new(:content_type => 'text/html', &block)
else
@html_part || find_first_mime_type('text/html')
end
end
You can use text_part
to get it in Plain Text.
mail.text_part.body.to_s
Up to .body
will return an instance of Body class.
This article describes how to get the body from the return value of ActionMailer's mail method. If you want to get the text after sending it, the following article will be helpful.
Recommended Posts