Class: ActionMailer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_engine/action_mailer_patch.rb

Direct Known Subclasses

MailEngine::MailDispatcher

Instance Method Summary collapse

Instance Method Details

#mail(headers = {}, &block) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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

#origin_mailObject



4
# File 'lib/mail_engine/action_mailer_patch.rb', line 4

alias_method :origin_mail, :mail