Message.body empty in ActionMailer Observer

本小妞迷上赌 提交于 2020-01-06 03:06:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!