Skip to content

Instantly share code, notes, and snippets.

@nikosd
Created November 5, 2010 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikosd/663970 to your computer and use it in GitHub Desktop.
Save nikosd/663970 to your computer and use it in GitHub Desktop.
Monkey patching ActionMailer::Base#create! to support localized implicit multipart mails.
# This is a FUGLY hack to make multipart emails work with localized templates
# in the form of method_el_GR.text.plain.rhtml (or .erb).
#
# NOTE : This is tested against Rails 2.3.8 and is a very good candidate to break in 2.3.9 and up.
ActionMailer::Base.class_eval do
def create!(method_name, *parameters) #:nodoc:
initialize_defaults(method_name)
__send__(method_name, *parameters)
# If an explicit, textual body has not been set, we check assumptions.
unless String === @body
# First, we look to see if there are any likely templates that match,
# which include the content-type in their file name (i.e.,
# "the_template_file.text.html.erb", etc.). Only do this if parts
# have not already been specified manually.
if @parts.empty?
# XXX : Hacked part, no. 1
[Dir.glob("#{template_path}/#{@template}_#{GetText.locale}.*"),
Dir.glob("#{template_path}/#{@template}.*")].flatten.each do |path|
template = template_root["#{mailer_name}/#{File.basename(path)}"]
# XXX : Hacked part, no. 2
# Skip unless template has a multipart format and the current content type is not already included
content_type_not_already_included = @parts.select { |p| p.content_type == template.content_type }.blank?
next unless template && template.multipart? && content_type_not_already_included
@parts << ActionMailer::Part.new(
:content_type => template.content_type,
:disposition => "inline",
:charset => charset,
:body => render_message(template, @body)
)
end
unless @parts.empty?
@content_type = "multipart/alternative" if @content_type !~ /^multipart/
@parts = sort_parts(@parts, @implicit_parts_order)
end
end
# Then, if there were such templates, we check to see if we ought to
# also render a "normal" template (without the content type). If a
# normal template exists (or if there were no implicit parts) we render
# it.
template_exists = @parts.empty?
# XXX : Hacked part, no. 3
template_exists ||= template_root["#{mailer_name}/#{@template}_#{GetText.locale}"] || template_root["#{mailer_name}/#{@template}"]
@body = render_message(@template, @body) if template_exists
# Finally, if there are other message parts and a textual body exists,
# we shift it onto the front of the parts and set the body to nil (so
# that create_mail doesn't try to render it in addition to the parts).
if !@parts.empty? && String === @body
@parts.unshift ActionMailer::Part.new(:charset => charset, :body => @body)
@body = nil
end
end
# If this is a multipart e-mail add the mime_version if it is not
# already set.
@mime_version ||= "1.0" if !@parts.empty?
# build the mail object itself
@mail = create_mail
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment