SendGrid provides a very simple API to send transactional and marketing emails in Rails applications.

There are simple steps to avail the SendGrid in a Rails application.

Sign up in SendGrid

First, create an account on SendGrid and you will receive a verification email. You should see the dashboard after clicking on verification link in the email.

sendgrid dashboard

Setup ActionMailer

Generate a Mailer class.
$ rails generate mailer UserMailer
Open the mailer we’ve just created and write a mailer action that would send mails.

sendgrid signin

and prepare a view for email content

sendgrid email

Configure ActionMailer to use SendGrid

In config/environment.rb specify your ActionMailer settings to point to SendGrid’s servers.

sendgrid email configure

That’s it! Now you should be able to use UserMailer#send_signup_email to send emails which will sent through SendGrid.

You can see various Statistics on SendGrid dashboard like number of email sent, bounced emails etc.

sendgrid dashboard update

Super easy, huh!

Transactional Template

SendGrid also provide many other services like using custom templates for mailer. What if you need to change your email content / style frequently? It is very cumbersome if we have to keep on changing rails mailer views. Here, send grid helps. You can create template using interactive editor online and use them in ActionMailer.

Configure Template

Go to template tab and click on create template.

sendgrid template configure

Specify the name of the template and save.

This will just create a template. To add / edit content, click on Action button and then select Add Version option.

You will get a page with editor to edit the template. Write the content. You can also preview content in design view.

sendgrid design

Save the template and it is now ready for use.

Configure the application

Now to use the template feature of SendGrid, you first need to add gem in your Gemfile and bundle it.

gem 'smtpapi'

Now, you need to tell UserMailer action to use your custom template.

user mailer code

Where template_id is the id of the template you just created under Templates tab on the Dashboard.

Now, your application is using custom template which is residing on cloud and not your source code. Flexible, right!

Click here for more details…

At BoTree Technologies, we build enterprise applications with our RoR team of 25+ engineers.

We also specialize in RPA, AI, Python, Django, JavaScript and ReactJS.

Consulting is free – let us help you grow!