SendGrid

Send Emails with a Custom Domain Using SendGrid and Rails

68 VIEWS

SendGrid is a cloud-based email delivery service that assists businesses with email delivery. That may sound like one of the most boring descriptions ever, considering the powerhouse of tools that SendGrid provides.

With SendGrid, you don’t just get to send emails—You get to monitor link clicks, unsubscribe rates, geographical tracking, and a lot more. In this tutorial, I’ll walk you through building your own email sending service on Rails with SendGrid.

The first thought that probably pops into your mind is Why SendGrid? Believe me, you’ll figure it out by the end of this tutorial.

Let’s first create a Rails project via command line. I’ll be using Rails 5 here.

(How creative of a name is sendemail though?)

We would like for users to sign up and then maintain a personal profile of all the emails they send. So, we’ll be adding the Devise gem here. I won’t be covering how to add Gems and customizing Devise in this tutorial, but here are a few sources that cover it.

We’ll then create a scaffold called email. The model attributes will be the receiver field, subject field and body field—everything necessary to create an email! Oh, and we’ll also be adding a user_id mapping column, which will be used to query emails belonging to a user.

Here’s where the fun starts.

To send emails from your Rails application, you need to:

1. Add email provider credentials to your development.rb (or production.rb if in a production environment). I’ll be using Gmail here.

ProTip: If you’ve enabled two-factor authentication on your account, you need to create an app password to bypass it.

It’ll look something like this:

2. Add this to your app/mailers/application_mailer.rb. This will send the email.

Notice that I have specified the From address as well. Also, I have NOT provided a default_from in ApplicationMailer.

You’ll also have to create an app/views/application_mailer/send_it.html.erb and add this to it:

This prints the body of the email into what is being sent out.

3. I’ve also added the method to trigger email sending to our model. Here’s how it looks:

I’ve created simple after_create and after_update callbacks that send the email.

Let’s test and see if this works.

I’ll create a user by signing up with a swaathi @ skcript email address, and then send an email to swaathikakarla @ gmail.

Let’s see what happens.

Hey! It was sent from swaathi16 @ gmail. But, the from section was set to swaathi @ skcript. Why did this happen?

It’s because of the credentials in development.rb. We added Gmail credentials belonging to swaathi16 @ gmail. So, no matter what you override, it’ll be sent from the email that’s tied to the credentials.

This works perfect when you don’t want someone masking your identity. But it often sucks when you just want to send an email from another address, with no way of tracking incoming emails. With SendGrid, you can send emails as other addresses, though it’s not as scary as it sounds. It’ll be appended with “via Domain” text. And, any replies to the email will automatically be sent to the address itself.

Let’s see how that works!

1. Sign up on SendGrid
Head over to the signup page of SendGrid, and to test things out, select the free trial. This allows you to send 40,0000 emails per day for 30 days.

2. Head over to the Whitelabels section
After that, visit the Settings section in the sidebar, and then click on the Whitelabels link from the dropdown.

3. Add a Domain
Click on the Add Domain button and fill in the form. You need to do this so that users who receive an email that your app sends will be shown the location of origin. So even if a user on your app has xxx@yyy.com, it’ll get sent from his/her email, but will also include text that says “via DomainName.”

You’ll see “via” and a website name next to the sender’s name if the domain it was sent from doesn’t match the domain in the From: address. (For example, you got an email from john.smith@gmail.com, but it could’ve been sent through a social networking site and not Gmail.)

In the form, you’ll have to enter a subdomain and a domain you’d like to send emails through. I suggest you create a new subdomain, so that you don’t run into any weird conflicts. (In fact, it shouldn’t even exist at this stage.)

4. Add to your CNAMES registry
Once you add a domain, you’ll be taken to a page that looks like this:

All you have to do is navigate to your hosting (like GoDaddy) or CDN (like Cloudflare) provider, whichever manages your domain. You can then take the three subdomains SendGrid gives you and map them to your website.

It should take about a minute or more to reflect the subdomain changes on the DNS.

Then, head back to your SendGrid page and click on the Validate Record button, and it should look like this now:

Three tick marks! Yay!

… If not, wait awhile for your DNS to get updated and try again.

5. Add SendGrid credentials to your app
We’re almost there. All you have to do now is add SendGrid credentials to your development.rb (or production.rb). It will look something like this:

And that’s it! Five simple steps.

Let’s try it out on the app and see how it looks now.

See the “via”? (Best part? It shows your domain, not SendGrid.)

That’s it! You can now send emails from custom domains.

You can find all the code I used here, and the Rails app on GitHub.

Do you think you can beat this Sweet post?

If so, you may have what it takes to become a Sweetcode contributor... Learn More.

Swaathi Kakarla is the co-founder and CTO at Skcript She enjoys talking and writing about code efficiency, performance and startups. In her free time she finds solace in yoga, bicycling and contributing to open source.


Discussion

Click on a tab to select how you'd like to leave your comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Menu