Colibri
Colibri

Reputation: 1195

How to set a default header?

I have a lot of mailings (*Mailer). Everything works on SMTP.

I understand how to set a header for a particular *Mailer method. But how to set the header globally? That is, I need all the letters that my application sends to have my header. And so that this does not conflict with the individual mailing settings.

I tried to find in the documentation (and in google) but did not find anything.

Upvotes: 2

Views: 1489

Answers (3)

David Hempy
David Hempy

Reputation: 6237

For Rails 5:

class ApplicationMailer < ActionMailer::Base
  layout 'mailer'

  def mail(args)
    headers('X-MyCorp-customer' => @customer&.name)
    headers('X-MyCorp-env' => Rails.env)
    headers('X-MyCorp-app' => 'XRay Insights')
    super(args)
  end
end

Upvotes: 1

bright
bright

Reputation: 191

You could do something like this.

class ApplicationMailer < ActionMailer::Base
 default from: "[email protected]", "HEADER_KEY" => "VALUE"
end

Upvotes: 4

Les Nightingill
Les Nightingill

Reputation: 6156

All your mailers should inherit from ApplicationMailer, which itself inherits from ActionMailer::Base.

In ApplicationMailer you can define default smtp headers, default layout etc. Here's my application_mailer.rb, to give you some ideas what you can include:

#application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: "Site Admin<#{NO_REPLY_EMAIL}>"
  layout 'mailer'

  def mail
    super(options)
  end

  private
  def options
    {:'List-Unsubscribe-Post' => :'List-Unsubscribe=One-Click',
     :'List-Unsubscribe' => unsubscribe_url,
     :subject => t('.subject', org_name: ORGANIZATION_NAME, app_name: APPLICATION_NAME),
     :to => "#{@recipient.email}",
     :date => Time.now }
  end

  def unsubscribe_url
    params = { :locale => I18n.locale,
               :user_id => @recipient.id,
               :unsubscribe_code => @recipient.refresh_unsubscribe_code,
               :protocol => :https }
    @unsubscribe_url = admin_unsubscribe_url( params )
  end

Upvotes: 1

Related Questions