Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Making rails 2 use mail instead of tmail
module MailWithMail
# stolen + made static from Rails 3.0 action_mailer/old_api.rb
def self.set_content_type(m, user_content_type, class_default, params)
case
when user_content_type.present?
user_content_type
when m.has_attachments?
if m.attachments.detect { |a| a.inline? }
["multipart", "related", params]
else
["multipart", "mixed", params]
end
when m.multipart?
["multipart", "alternative", params]
else
m.content_type || class_default
end
end
# stolen from mail/fields/content_type_field.rb
def self.stringify(params)
params.map { |k,v| "#{k}=#{Mail::Encodings.param_encode(v)}" }.join("; ")
end
def self.stringify_content_type(content_type, parameters)
"#{content_type}; #{stringify(parameters)}"
end
end
ActionMailer::Base.class_eval do
# copy pasted from action_mailer 2.3.14 and modified to use Mail instead of TMail
def create_mail
m = Mail.new
m.charset = charset # has to be set first, other fields are set using this
m.subject = subject
m.to, m.from = recipients, from
m.bcc = bcc unless bcc.nil?
m.cc = cc unless cc.nil?
m.reply_to = reply_to unless reply_to.nil?
m.mime_version = mime_version unless mime_version.nil?
m.date = sent_on.to_time rescue sent_on if sent_on
headers.each { |k, v| m[k] = v.to_s }
real_content_type, ctype_attrs = parse_content_type
if @parts.empty?
m.content_type MailWithMail.set_content_type(m, real_content_type, nil, ctype_attrs)
m.body = normalize_new_lines(body)
else
if String === body
part = Mail::Part.new
part.body = normalize_new_lines(body)
part.content_type MailWithMail.set_content_type(part, real_content_type, nil, ctype_attrs)
part.content_disposition = "inline"
m.add_part part
end
@parts.each do |p|
part = (Mail::Part === p ? p : p.to_mail(self, @account))
m.add_part part
end
if real_content_type =~ /multipart/
ctype_attrs.delete "charset"
m.content_type MailWithMail.set_content_type(m, real_content_type, nil, ctype_attrs)
end
end
# fix missing boundary for multipart in old mailer api <-> https://github.com/rails/rails/pull/3090
if m.content_type =~ /^multipart/ && !m.content_type.include?("boundary=") && m.body.boundary.present?
real_content_type, ctype_attrs = parse_content_type(m.content_type)
main_type, sub_type = real_content_type.to_s.split("/")
ctype_attrs["boundary"] = m.body.boundary
m.content_type([main_type, sub_type, ctype_attrs])
end
@mail = m
end
# stolen + made static from Rails 3.0 action_mailer/old_api.rb
def parse_content_type_from(content_type)
old, @content_type = @content_type, content_type
parse_content_type
ensure
@content_type = old
end
end
ActionMailer::Part.class_eval do
# copy pasted from action_mailer 2.3.14 and modified to use Mail instead of TMail
def to_mail(defaults, account=nil)
part = Mail::Part.new
real_content_type, ctype_attrs = parse_content_type(defaults)
if @parts.empty?
# Always set the content_type after setting the body and or parts!
# Also don't set filename and name when there is none (like in
# non-attachment parts)
if content_disposition == "attachment"
part.body = body
ctype_attrs.delete "charset"
part.content_type = MailWithMail.set_content_type(part, real_content_type, nil,
squish("name" => filename).merge(ctype_attrs))
part.content_disposition = MailWithMail.stringify_content_type(content_disposition, squish("filename" => filename).merge(ctype_attrs))
else
part.content_transfer_encoding = transfer_encoding || "quoted-printable"
#HACK: quoted-printable somehow does not encode "=", so we do it <-> breaks application_mailer_test
part.body = if part.content_transfer_encoding.downcase == "quoted-printable" && body
body.gsub("=", "=3D")
else
body
end
part.body # HACK: breaks billing_mailer_test/japanese_mobile without this line
part.content_type = MailWithMail.set_content_type(part, real_content_type, nil, ctype_attrs)
part.charset = charset || "UTF-8"
part.content_disposition = content_disposition
end
else
if String === body
@parts.unshift ActionMailer::Part.new(:charset => charset, :body => @body, :content_type => 'text/plain')
@body = nil
end
@parts.each do |p|
prt = (Mail::Part === p ? p : p.to_mail(defaults, @account))
part.add_part prt
end
if real_content_type =~ /multipart/
ctype_attrs.delete 'charset'
part.content_type = MailWithMail.set_content_type(part, real_content_type, nil, ctype_attrs)
end
end
headers.each { |k,v| part[k] = v.to_s }
part
end
end
Mail::Part.class_eval do
alias_method :original_filename, :filename
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.