Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Mailer using SendGrid Template

How do I tie in my rails mailer to Sendgrid using the smtpapi-ruby gem? I've followed their limited documentation, but my emails aren't going through, I've verified that my SendGrid implementation works fine when just sending a plain email, so that's not it. This is what I have:

user_controller.rb

def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }


        header = Smtpapi::Header.new
        header.add_to(@user.email)
        header.add_substitution('user', [@user.name])
        header.add_substitution('body', "You've registered! This is from the controller.")
        header.add_filter('templates', 'enable', 1)
        header.add_filter('templates', 'template_id', 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx')
        header.to_json

        UserNotifier.welcome(header).deliver
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

mailers/user_notifier.rb

class UserNotifier < ApplicationMailer
  default from: "[email protected]"

  def welcome(header)
    headers['X-SMTPAPI'] = hdr.to_json
    mail(subject: "Welcome to the site!")
  end
end

views/user_notifier/welcome.html.erb

<html>
 <body>
   Hi -user-<br />
   Thanks so much for joining us!

   <p>-body-</p>

   Thanks,<br />
   The Microblog Team
 </body>
</html>

I'm not seeing anything come through on the SendGrid activity log, so it's not even getting sent there, at least that's my guess.

What am I doing wrong?

like image 811
Godzilla74 Avatar asked Sep 25 '15 21:09

Godzilla74


2 Answers

I think you've mixed up your variables. You call hdr.to_json and the param name is header which is also already converted into json.

You should include the header meta data directly into UserNotifier:

headers "X-SMTPAPI" => {
    "sub": {
      "%name%" => [user.name]
    },
    "filters": {
      "templates": {
        "settings": {
          "enable": 1,
          "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'
        }
      }
    }
  }.to_json

# the 'to' email can be overridden per action
mail(
  from: '[email protected]',
  to: '[email protected]',
  subject: "Hello World"
)

You can also pass the content if UserNotifier.welcome is used in other parts of your app:

UserNotifier.welcome(user: @user, subject: "Welcome!").deliver_now

# user_notifier.rb

class UserNotifier < ApplicationMailer
  default from: "[email protected]"

  def welcome(user: , subject: , template: "default" )

    # template's default view is "default"
    headers "X-SMTPAPI" => {
     "sub": {
       "%name%" => [user.name]
     },
     "filters": {
       "templates": {
         "settings": {
           "enable": 1,
           "template_id": 'xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'
         }
       }
     }
    }.to_json

    mail(
      from: '[email protected]',
      to: user.email,
      subject: subject,
      template_path: 'path/to/view',
      template_name: template
    )
    # this would try to render the view: `path/to/view/default.erb`
  end
end

In your template, you can include your substitution tags by including the name of the tag:

<h1>Hello %name%!</h1>

More information about substitution tags

Check out the Sendgrid docs on using their template system

like image 167
Myk Klemme Avatar answered Nov 02 '22 04:11

Myk Klemme


You have to change the code in your mailer to something like:

class UserNotifier < ApplicationMailer
  default from: "[email protected]"

  def welcome(hdr)
    headers['X-SMTPAPI'] =  hdr.asJSON()
    mail(subject: "Welcome to the site!")
  end
end

Example: https://sendgrid.com/docs/Integrate/Code_Examples/SMTP_API_Header_Examples/ruby.html

like image 3
Aguardientico Avatar answered Nov 02 '22 04:11

Aguardientico