问题
I am using an Observer to log outgoing emails; it fires correctly but when I attempt to extract the body of the email I get an empty string. The emails are definitely not blank and the logging record is definitely created. Breakpointing and inspecting message.body
confirms that it is an empty string.
class MailObserver
def self.delivered_email(message)
for address in message.to
user = User.find_by_email(address)
if user
UserMailerLogging.create!(user_id: user.id, email_type: message.subject,
contents: message.body, sent_at: Time.now)
end
end
end
end
ActionMailer::Base.register_observer(MailObserver)
回答1:
In Rails 3, emails are built and sent using the Mail gem. According to their docs
message.text_part # plaintext version
message.html_part # html version
You can also go a bit further depending on whether your emails are multipart or not.
message.text_part.body.decoded
This question here on SO may also be hefpful.
来源:https://stackoverflow.com/questions/15256472/message-body-empty-in-actionmailer-observer