### override mail method module ActionMailer class Base alias_method :origin_mail, :mail def mail(headers={}, &block) # order by latest uploaded, for get the latest updated 'subject' current_template = MailEngine::MailTemplate.where(:path => "#{controller_path}/#{action_name}", :locale => I18n.locale, :partial => false).order("updated_at desc").first # {'username' => @username, 'gender' => @gender} instance_variable_hash = {} self.instance_variables.each { |var| instance_variable_hash[var.sub(/^@/,'')] = self.instance_variable_get(var) } headers[:subject] = Liquid::Template.parse(current_template.subject).render(instance_variable_hash) if current_template.present? headers[:message_id] = "#{controller_path}/#{action_name}" unless block_given? puts "\e[1;31;40m[Mail Engine Warning]\e[0m Please pass a block to 'mail' method as below style, mail engine needs you to specify the format to send: \e[1;34;40m mail :to => 'xxx@xxx.com' do |format| format.text format.html end \e[0m" end # Add sendgrid header before sending mail. # Why here but not add to default_params of action_mailer? because the receiver email [:to] only can get here. if self.sendgrid_config self.sendgrid_config.set_send_to headers[:to] origin_mail(headers.merge(self.sendgrid_config.to_hash), &block) else origin_mail(headers, &block) end end protected ### # REASON TO OVERRIDE THIS METHOD: # need to set layout and pass partial path to each format of mail template. # # Completed copied from lib/action_mailer/base.rb#665 # but used revisioned ActionMailer::Collector def collect_responses_and_parts_order(headers) #:nodoc: responses, parts_order = [], nil if block_given? ### modified this like, it's very strange when use resolver.is_a?(MailEngine::MailTemplate::Resolver) it will give false sometime. if lookup_context.view_paths.detect {|resolver| resolver.class.to_s == "MailEngine::MailTemplate::Resolver" } collector = ActionMailer::Collector.new(lookup_context) { |mime| set_layout_and_partials(mime) } else collector = ActionMailer::Collector.new(lookup_context) { render(action_name) } end ###################################################################################### yield(collector) parts_order = collector.responses.map { |r| r[:content_type] } responses = collector.responses elsif headers[:body] responses << { :body => headers.delete(:body), :content_type => self.class.default[:content_type] || "text/plain" } else templates_path = headers.delete(:template_path) || self.class.mailer_name templates_name = headers.delete(:template_name) || action_name each_template(templates_path, templates_name) do |template| self.formats = template.formats responses << { :body => render(:template => template), :content_type => template.mime_type.to_s } end end [responses, parts_order] end # set layout and partials def set_layout_and_partials(format) # looking for system mail. template = MailEngine::MailTemplate.where(:path => "#{controller_path}/#{action_name}", :format => format, :locale => I18n.locale, :partial => false, :for_marketing => false).first # looking for marketing mail. template = MailEngine::MailTemplate.where(:path => action_name, :format => format, :locale => I18n.locale, :partial => false, :for_marketing => true).first if template.blank? # if found db template set the layout and partial for it. if template related_partial_paths = {} # set @footer or @header template.template_partials.each do |tmp| related_partial_paths["#{tmp.placeholder_name}_path".to_sym] = tmp.partial.path end # set layout render :template => "#{controller_path}/#{action_name}", :layout => "layouts/mail_engine/mail_template_layouts/#{template.layout}", :locals => related_partial_paths else # if not found db template should render file template render(action_name) end end end end ### # REASON TO OVERRIDE THIS METHOD: # pass mime format to renderrer # # One concern if user provided block, there will be problem. like: # format.text { xxxx; render 'xxx' } # module ActionMailer class Collector def custom(mime, options={}) options.reverse_merge!(:content_type => mime.to_s) @context.freeze_formats([mime.to_sym]) ### modified this line # change from: # options[:body] = block_given? ? yield : @default_render.call options[:body] = block_given? ? yield : @default_render.call(mime.to_sym) @responses << options end end end