Skip to content

Instantly share code, notes, and snippets.

@softcraft-development
Created February 12, 2015 19:49
Show Gist options
  • Save softcraft-development/2ed70a2a4d6e2c829fac to your computer and use it in GitHub Desktop.
Save softcraft-development/2ed70a2a4d6e2c829fac to your computer and use it in GitHub Desktop.
Inline Mail Part URLs
module InlineMailPartUrls
def url
# I'm toggling this behaviour based on an environment variable.
# As far as I can see, there is no (better) way to decide whether or not
# the url is being called from a Rails Mailer Preview vs the actual Mailer call.
if ENV["INLINE_MAIL_PART_URLS"] == "true"
# Encode the part body in a data URI
# http://en.wikipedia.org/wiki/Data_URI_scheme
"data:#{mime_type};base64,#{Base64.encode64(body.decoded)}"
else
# If turned off, just defer to the superclass, which generates a
# "cid:" URI.
super
end
end
end
class Mail::Part
# use InlineMailPartUrls for all attachments
prepend InlineMailPartUrls
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment