Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom email headers

I'm trying to create a custom email header to use the SendGrid api.

Here's what I'm doing - but its not working:

class Mailman < ActionMailer::Base
  default :from => "[email protected]"

  def send_message(name, email, message)
    @name = name
    @email = email
    @message = message

    mail(:to => '[email protected]',
     :from => email,
     :subject => "Message from the site",
     :headers['X-SMTPAPI'] => "category: Drip Email"
    )
  end

end

Any help appreciated.

Thanks, Adam

like image 487
Northband Avatar asked Sep 05 '25 13:09

Northband


2 Answers

You can use the #headers method of ActionMailer, I've edited your example to show how:

class Mailman < ActionMailer::Base
  default :from => "[email protected]"

  def send_message(name, email, message)
    @name = name
    @email = email
    @message = message

    headers['X-SMTPAPI'] = '{"category": "Drip Email"}'

    mail(
     :to => '[email protected]',
     :from => email,
     :subject => "Message from the site"
    )
  end

end

Alternatively, you can pass a hash as an argument (to the method #headers) too:

headers {"SPECIFIC-HEADER-1" => "value", "ANOTHER-HEADER" => "and so..."}

I hope this can help you, and if not you always can check the rails guides: http://edgeguides.rubyonrails.org/action_mailer_basics.html.

like image 156
23 revs, 14 users 49% Avatar answered Sep 08 '25 12:09

23 revs, 14 users 49%


I am using below code and works fine, just convert the hash to json with to_json

headers['X-SMTPAPI'] = { 
  category: "Weekly Newsletter",
  unique_args: { user_id: user.id } 
}.to_json
like image 26
overallduka Avatar answered Sep 08 '25 12:09

overallduka