“Welcome email” for new user using Action Mailer

Pascales Kurniawan
3 min readJun 9, 2018

--

Hello gaes…

Today I want to share about sending email from rails application using Action mailer. Recently I worked on a project with my friend, and I wanted to add a feature that automatically send a new registered user a Welcome email…mmm.. No I was lying..haha. The truth is that I was trying to build a “forgot password” mechanism but I accidentally bumped to this feature.

anyway, let’s get on with it..

Generate and set up mailer

The first thing we should do is

rails generate mailer UserMailer

it will generate files that we need for sending mail. after that go to app/mailers/application_mailer.rb and change the value according to your mail account

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

Okay then, open app/mailers/user_mailer.rb and add the method bellow

# app/mailers/user_mailer.rbclass UserMailer < ApplicationMailerdef welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end

and again change the value as needed by your application.

Create the body of the email in app/views/user_mailer/ name it welcome_email.html.erb and add these :

# app/views/user_mailer/welcome_email.html.erb<h1>Welcome to example.com, <%= @user.nickname %></h1>
<p>
You have successfully signed up to example.com,
your username is: <%= @user.username %>.<br>
</p>
<p>Thanks for joining and have a great day!</p>

customize it again according your application, it is basically a ruby html file so you can make it more attractive by adding some html codes

Add a text only version as well, in app/views/user_mailer/ , create a file called welcome_email.text.erb

# app/views/user_mailer/welcome_email.text.erbWelcome to example.com, <%= @user.nickname %>
===============================================
You have successfully signed up to example.com,
your username is: <%= @user.username %>.
Thanks for joining and have a great day!

Gmail Configuration

Copy and paste the following into config/environments/development.rb ,

# config/environments/development.rb
config.action_mailer.delivery_method = :sendmail
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_options = {from: 'no-reply@example.com'}

Change the no-reply@example.com to your gmail account. Then copy and paste this part, which is specific for Gmail:

# config/environments/development.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'example.com',
user_name: '<username>',
password: '<password>',
authentication: 'plain',
enable_starttls_auto: true }

Send The mail

Okay, after all set up correctly, we’re going to tell the mailer to send the mail by executing this

UserMailer.welcome_email(@user).deliver_now

We want to send the “Welcome email” after user is successfully registered, so we should put it on the create method in our users controller.

def create
@user = User.create(create_param)
if @user.save
UserMailer.welcome_email(@user).deliver_now
....

Well done! we’re sending our new user a “Welcome email” to greet them.

Problems with gmail account

If you working with more than one application that using the same gmail account, probably you will see this issue:

Net::SMTPAuthenticationError 535-5.7.8 Username and Password not accepted

There are some possible causes:

  • you have 2-step authentication setup on you gmail account
  • Your Gmail account is not valid
  • You have more than one application using the same gmail account.

Work around.

  • Disable 2-step authentication on your gmail account
  • Make sure your gmail account/password is valid
  • goto https://www.google.com/settings/security/lesssecureapps You should change the “Access for less secure apps” to Enabled (change it to disabled and than back to enabled). Also, bear in mind that it might take some times to enable the less secure apps. so be patient and wait for a while until they work

You can use sendGrid, mailchimp or mailgun for mail server, its up to you but I use gmail account on my development environment.

Okay, If you have any correction or suggestion, please feel free to put it on responses bellow…

You can find more detail of Action mailer on:

--

--

Pascales Kurniawan

"In times of change, learners inherit the earth, while the learned find themselves beautifully equipped to deal with a world that no longer exists."