Skip to content

Instantly share code, notes, and snippets.

@mreinsch
Last active August 29, 2015 14:01
Show Gist options
  • Save mreinsch/6969111c708c1b90a933 to your computer and use it in GitHub Desktop.
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
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