Send transactional emails using SendGrid with Ruby on Rails

sendgrid ruby on rails

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.

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.

and prepare a view for email content

Configure ActionMailer to use SendGrid

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

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.

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.

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.

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.

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!

Related posts

How To Setup Up Rspec In A Rails Application For Testing  – A Lean Version

byAmit Patel
4 years ago

The Complete Guide to Hiring Python Developers in 2022

byParth Barot
3 years ago

Top 5 Python Programming Mistakes and How to Fix Them

byShardul Bhatt
10 months ago
Exit mobile version