Created
April 7, 2011 16:40
-
-
Save stack/908164 to your computer and use it in GitHub Desktop.
Multipart Alternative + Multipart Related Email with ActionMailer for Rails 2
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Blog Post: http://www.shortround.net/2010/11/24/multipart-alternative-multipart-related-email-with-actionmailer/