Last active
January 26, 2018 09:58
-
-
Save expajp/a0064b4139727c032761ef2dc5e1c35b to your computer and use it in GitHub Desktop.
[ActionMailer]添付ファイルの有無にかかわらずメールの本文を取得する ref: https://qiita.com/expajp/items/207935caf3086ffc3307
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_mail_body(mail) | |
parts = mail.body.parts | |
# partsにさらにマルチパートメールが含まれる(=添付ファイルがある)場合はメール部分のみを取得 | |
if parts.map(&:multipart?).inject(:|) | |
multipart_mails = parts.find(&:multipart?).parts | |
else | |
multipart_mails = parts | |
end | |
# テキストメールの本文を取得して返す | |
multipart_mails.find{|part| part.content_type.match 'text/plain'}.body.raw_source | |
end | |
mail = HogeMailer.send_mail.deliver | |
body = get_mail_body(mail) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multipart_mails = (parts.map(&:multipart?).inject(:|) ? parts.find(&:multipart?).parts : parts) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment