Skip to content

Instantly share code, notes, and snippets.

@stack
Created April 7, 2011 16:40
Show Gist options
  • Save stack/908164 to your computer and use it in GitHub Desktop.
Save stack/908164 to your computer and use it in GitHub Desktop.
Multipart Alternative + Multipart Related Email with ActionMailer for Rails 2
class EmbeddedMailer < ActionMailer::Base
def embedded_mail(to, from, subject, html_message, text_message, files = {}, embeds = {})
# Hack TMail to allow embeds
TMail::HeaderField::FNAME_TO_CLASS.delete 'content-id'
recipients to
from from
subject subject
content_type 'multipart/mixed'
# The Message
part :content_type => 'multipart/alternative' do |m|
# Plain Text Message
m.part :content_type => 'text/plain' do |p|
p.transfer_encoding = 'base64'
p.body = text_message
end
# HTML Message
m.part :content_type => 'multipart/related' do |x|
x.part :content_type => 'text/html' do |p|
p.body = html_message
end
# Embedded
embeds.each do |key, embed|
x.part :content_type => embed[:content_type] do |p|
p.headers = { 'Content-ID' => "<#{key}>" }
p.transfer_encoding = 'base64'
p.body = File.read embed[:path]
end
end
end
end
# Attachments
files.each do |key, embed|
attachment :content_type => embed[:content_type] do |a|
a.filename = key
a.body = File.read embed[:path]
end
end
end
end
@stack
Copy link
Author

stack commented Jun 2, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment