class ActionMailer::Base

Public Instance Methods

mail(headers={}, &block) click to toggle source
# File lib/mail_engine/action_mailer_patch.rb, line 5
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}"

  # 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
    # if add "intercept_email" option in config
    receiver = if (intercept_email = MailEngine::Base.current_config["intercept_email"]).present?
                 headers[:to] = intercept_email
               else
                 headers[:to]
               end
    self.sendgrid_config.set_send_to receiver
    origin_mail(headers.merge(self.sendgrid_config.to_hash), &block)
  else
    origin_mail(headers, &block)
  end
end
Also aliased as: origin_mail
origin_mail(headers={}, &block)
Alias for: mail

Protected Instance Methods

render_with_layout_and_partials(format) click to toggle source

render template with layout and partials

# File lib/mail_engine/action_mailer_patch.rb, line 75
def render_with_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