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:
rails 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.
Free Resources