Sending Emails in Rails Application

592
SHARES
2.7k
VIEWS

In this tutorial, we will learn how you can send emails from your rails applications. Sending emails is very important for nearly every web application today, from filling out contact forms to user’s account verification, etc. For this tutorial’s sake, we are sending emails to the user for confirming his account. But make sure you have set up the authentication in the rails app.

What is Action Mailer?

Action Mailer allows you to send emails without installing the specified gem or creating an API. It is very easy to use.

Creating the Mailer

Open the terminal and go to the related app directory and type

rails g mailer user

It will creates some files in your app. You have to open

app/mailer/user_mailer.rb

You will see some code like this

class UserMailer < ApplicationMailer

end

Make sure the user mailer is inherited with application mailer. Now open the application mailer and type

class ApplicationMailer < ActionMailer::Base
  default from: "no-replay@example.com"
  layout 'mailer'
end
 

Rails action mailer are very similar to the controls. They have methods named “actions”. We can send messages using this action to the client.

Open your user_mailer.rb file and type


class UserMailer < ApplicationMailer
  default from: 'no-replay@example.com'
 
  def welcome
    @user = params[:user]
    @url  = 'https://www.codemeals.com'
    mail(to: @user.email, subject: 'Welcome to My Awesome Site')
  end
end

You can see we have defined the welcome action. Whenever the user hits this action it performs the functionalities that we have defined into it.

Now we have to create an HTML file for the email template. Go to the app/views/user_mailer/. Create a file and name it welcome.html.erb

Open the file in code editor and type the following code

<!DOCTYPE html>
<html>
  <head>
    <title> Welcome </titile>
  </head>
  <body>
    <h1>Welcome to codemeals.com, <%= @user.name %></h1>
    <p>
      You have successfully signed up to codemeals.com,
      Great Job
    </p>
    <p>
    
    </p>
    <p>Thanks for joining and have a great day!</p>
  </body>
</html>

Now we have to call the action mailer. For doing that go the user controller where you have defined the create action. Type this code

class UsersController < ApplicationController
def create
  @user = User.new(parans[:user]);
  if @user.save
   UserMailer.with(user :@user).welcome.deliver_now;
   redirect_to @user, notice: "Account was created"
   else
   redirect_to @user.new, notice: "Invalid Data"
  end
end
end

Now you have all setup for sending emails. When you hit the signup button it will display you a message in the terminal but it’s actually not sending it. Because we haven’t configured our email yet. We have to configure our SMTP settings. Open config/environments

Add the following code


config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:              'smtp.gmail.com',
  port:                 587,
  domain:               'example.com',
  user_name:            '<Enter your user name>',
  password:             '<enter your password>',
  authentication:       'plain',
  enable_starttls_auto: true }

This configuration will send email using your Gmail account. That’s it, for now, I hope this will help you in learning purpose. If you have a question write it down in comment box below.

Leave a Reply

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

Trending