Rails and sending transactional emails

Rails and sending transactional emails

Yesterday I was working on configuring the default Rails mailer to work with 3rd party SMTP server providers like Gmail, Zoho and so on.

First, I tried Gmail since it gives you quota of 500 mails per day for the trial users but it is paid.

Then I remembered that we used Zoho services at NaughtySpirit and decided to give it a shot.

Rails Mailer Configuration

First of all, you have to configure your mailer to work with the SMTP server and it's an really easy task! 🤘🏻

There're 3 environment configuration files in the Project -> config -> environments directory you should edit: development.rb, production.rb and test.rb.
These files contains environment specific configurations. If we're intending to use the mailer in all of these environments - we should include its configs in these 3 files.

So, the configurations are these:

config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true 
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              ENV['MAIL_PROVIDER_SMTP'],
  port:                 ENV['MAIL_PROVIDER_PORT'],
  domain:               ENV['MAIL_PROVIDER_DOMAIN'],
  user_name:            ENV['MAIL_PROVIDER_USERNAME'],
  password:             ENV['MAIL_PROVIDER_PASSWORD'],
  authentication:       ENV['MAIL_PROVIDER_AUTH_TYPE']
config.action_mailer.default_url_options = { :host => ENV['HOST'] }

As you can see, I'm storing the needed information in environment config variables.
If you're using Heroku to host your web apps, you can set these config variables in the settings page within the dashboard: Personal Apps -> YOUR_APP -> Settings -> Config Variables -> Reveal Config Vars

If you're using Zoho as SMTP server for sending transactional emails, you should provide the following information:

  • address: smtp.zoho.eu
  • port: 587
  • domain: your domain, i.e. seishin.me
  • user_name: your zoho account, i.e. support@seishin.me
  • password: YOUR_PASSWORD
  • authentication: login

And that is it! So simple! 👻

What about the actual sending of emails?

First we have to generate a mailer by executing this command in the Terminal:
rails generate mailer UserMailer
This will generate the following files:

create  app/mailers/user_mailer.rb
create  app/mailers/application_mailer.rb
invoke  erb
create    app/views/user_mailer
create    app/views/layouts/mailer.text.erb
create    app/views/layouts/mailer.html.erb
invoke  test_unit
create    test/mailers/user_mailer_test.rb
create    test/mailers/previews/user_mailer_preview.rb

Now we're interested in the application_mailer.rb file:

# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout 'mailer'

default from sets the mail the receiver will see for the sender, so here you should put your email.

Lets create a new method which we will use for sending a welcome email:

# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
  default from: "from@example.com"
  layout 'mailer'

  def welcome_email(user)
    @user = user
    @url = 'http://example.com/login'
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')

All of the class variables (i.e. all of the vars with @ are passed to the erb template file. But... We don't have a template file for our welcome email, right? Now we're going to create it!
Create a file called welcome_email.html.erb in app/views/user_mailer/ with the following content:

<!DOCTYPE html>
    <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
    <h1>Welcome to example.com, <%= @user.name %></h1>
      You have successfully signed up to example.com,
      your username is: <%= @user.login %>.<br>
      To login to the site, just follow this link: <%= @url %>.
    <p>Thanks for joining and have a great day!</p>

We're almost ready now! What is next - sending an email, right? 🙂
So whenever you want to send an email and in our case it should be after the user successfully signed up, we should call the UserMailer class and pass the user object:

The two most used options for the delivery are deliver_later and deliver_now.

In Conclusion

It's not a difficult task, but you could have some issues with the SMTP configuration. You've to read throughly the SMTP provider's documentation.
As for the Rails' Mailer... It's straightforward and not so difficult to understand and use as most of the Rails functionalities. 🙃

For more information, please review the official Action Mailer Basics documentation