Last active
May 13, 2020 12:21
-
-
Save TylerRick/ec2d033766fd14f2afd6 to your computer and use it in GitHub Desktop.
Override the version from the actionmailer gem in order to not log attachments.
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
ActionMailer::Base.class_eval do | |
class << self | |
protected | |
# Override the version from the actionmailer gem in order to not log attachments. | |
# | |
# Note: This depends on the patch from https://github.com/mikel/mail/pull/858, which adds | |
# parts.inspect_structure and fixes an issue with mail.without_attachments! | |
# | |
# Until that's merged in, you can use: | |
# For Rails 4: gem 'mail', github: 'TylerRick/mail', branch: 'without_attachments.2' | |
# For Rails 3: gem 'mail', github: 'TylerRick/mail', branch: 'without_attachments_rails_3.2' | |
# | |
def set_payload_for_mail(payload, mail) #:nodoc: | |
payload[:mailer] = name | |
payload[:message_id] = mail.message_id | |
payload[:subject] = mail.subject | |
payload[:to] = mail.to | |
payload[:from] = mail.from | |
payload[:bcc] = mail.bcc if mail.bcc.present? | |
payload[:cc] = mail.cc if mail.cc.present? | |
payload[:date] = mail.date | |
# Don't include attachments in what is logged because it causes the log file to grow too | |
# quickly and is unreadable by a human reading the log file. Really the main things that | |
# would be useful to a developer looking back at the logs to debug a problem or to confirm | |
# that things went out as expected are: 1. The headers. 2. The message body (text and HTML). | |
# 3. Which files were attached (file name and type should be sufficient). | |
# If you want to see the attachments themselves, just add a mail interceptor that bcc's | |
# outgoing mail to your inbox. | |
# Copy the mail by re-parsing; self.dup does not create copies of the parts, so deleting attachments would also delete them from the original mail object. | |
mail = Mail.new(mail.encoded) | |
#mail = mail.dup | |
attachments = mail.attachments | |
original_structure = mail.inspect_structure | |
mail.without_attachments! | |
divider_line = '-'*80 + "\n" | |
payload[:mail] = mail.header.encoded + "\n\n" | |
# Log the structure of the email (which parts are within which other parts), including | |
# attachments | |
payload[:mail] += | |
divider_line + | |
"Structure:\n" + | |
original_structure + "\n\n" | |
payload[:mail] += | |
divider_line + | |
"Structure without attachments:\n" + | |
mail.inspect_structure + "\n\n" if attachments.any? | |
# Add a searchable, human-readable version of the body parts. Since the encoded version uses | |
# fixed line length, it may break a line at any point, even the middle of a word. So if you | |
# later go and grep for the word, you may not find it if all you have is the encoded version, | |
# even if the word is present! Calling mail.decoded gives an error, so we have to loop | |
# through the parts individually. | |
payload[:mail] += | |
divider_line + | |
"Decoded parts:\n" + | |
mail.parts.to_enum(:recursive_each).map {|part| | |
next if part.parts.any? | |
divider_line + | |
part.inspect + "\n"+ | |
part.decoded.force_encoding('utf-8') | |
}.join("\n") + | |
'-'*80 + "\n\n" rescue $!.inspect | |
end | |
end | |
end |
See problem as discussed here: http://stackoverflow.com/questions/28776785/how-do-i-get-actionmailer-to-log-sent-messages-but-not-include-the-attachments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note: This depends on the patch from mikel/mail#858, which adds parts.inspect_structure and fixes an issue with mail.without_attachments!