Last active
August 29, 2015 14:01
-
-
Save mreinsch/6969111c708c1b90a933 to your computer and use it in GitHub Desktop.
Sample code for how to attach an email when generating an email in Rails 3/4
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
class ContactMailer < ActionMailer::Base | |
def receive(email) | |
ContactMailer.contact_admins(email).deliver | |
end | |
def contact_admins(orig_email) | |
m = mail(to: 'info@doorkeeper.jp', subject: 'Received email') do |format| | |
format.text { render } | |
format.html { render } | |
end | |
attach_original_email(m, orig_email) | |
end | |
def attach_original_email(m, orig_email) | |
if m.parts.empty? | |
m.convert_to_multipart | |
m.send(:add_boundary) | |
m.content_type = ["multipart", "mixed", m.content_type_parameters] | |
else | |
content_type_params = m.content_type_parameters | |
part = Mail::Part.new | |
part.content_type = ["multipart", "alternative", {}] | |
2.times { part.add_part(m.parts.delete_at(0)) } | |
m.content_type = ["multipart", "mixed", content_type_params] | |
m.add_part(part) | |
end | |
m.attachments["original_email.eml"] = {:mime_type => 'message/rfc822', :content => orig_email.raw_source } | |
m | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment