Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eric-Guo/3d531fc4d881340c86e99d039d7f9710 to your computer and use it in GitHub Desktop.
Save Eric-Guo/3d531fc4d881340c86e99d039d7f9710 to your computer and use it in GitHub Desktop.
Override the version from the actionmailer gem in order to not log attachments.
ActionMailer::Base.class_eval do
class << self
protected
# Override the version from the actionmailer gem in order to not log attachments.
#
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
mail.without_attachments!
divider_line = '-' * 80 + "\n"
payload[:mail] = mail.header.encoded + "\n\n"
# 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 do |part|
next if part.parts.any?
divider_line +
part.inspect + "\n" +
part.decoded.force_encoding('utf-8')
end.join("\n") +
'-' * 80 + "\n\n" rescue $!.inspect
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment