Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

ror
ruby

What is action mailer in ROR?

Behzad Ahmad

Applications use action mailer to send and receive emails in ROR using mailer classes and views.

To send an email with the help of the action mailer, we can perform the following steps:

  1. We have to set up a mailer with rails generate mailer.
  2. We have to create the template for the email.
  3. Tell the suitable controller to send the email.
  4. Set the email preview.
  5. Configure mail settings.

Generate mailer

To generate a mailer we have to type the following command:

tp> cd emails
emails> rails generate mailer UserMail

This command will create UserMail.rb.

Now, we add some functionality:

class UserMail < ApplicationMailer
   default from: 'hr@educative.com'
   
   def welcome(user)
      @user = user
      @url  = 'http://www.educative.io'
      mail(to: @user.email, subject: 'Welcome to Educative')
   end
   
end

After this, we have to create a file named welcome.html.erb in app/views/user_mailer/.

<html>
   
   <head>
      <meta content = 'text/html; charset = UTF-8' http-equiv = 'Content-Type' />
   </head>
   
   <body>
      <h1>Welcome to Educative, <%= @user.name %></h1>
      
      <p>
         Your account is created successfully. Your username is : 
         <%= @user.login %>.<br>
      </p>
        
   </body>
</html>

The text part of the email is:

Welcome to Educative, <%= @user.name %

Your account is created successfully. Your username is :  <%= @user.login %>

In the place of <%= @user.name % and <%= @user.login %>, the actual name of the user is printed.

RELATED TAGS

ror
ruby

CONTRIBUTOR

Behzad Ahmad
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring