Action Mailer in Rails 4

Step 1
Create the Mailer called “UserMailer” with the action “sendMail” using following command:

$ rails generate mailer UserMailer sendMail

Step 2
Modify the “sendMail” action according to your requirement. Below is the example content of the/app/mailers/user_mailer.rb:

class UserMailer < ActionMailer::Base
default from: “sender@example.com”
def sendMail(email)
@greeting = “Hi”

mail to: email, subject: “Your Subject”
end
end

Step 3
You may modify the text content of the message at /app/views/user_mailer/sendMail.text.erb. You may also create the HTML version of the message by creating /app/views/user_mailer/sendMail.html.erb file and add the required html content. Below is the example content of the text message:

<%= @greeting %>, This is a text message.

Step 4
Configure the SMTP settings in the development.rb or production.rb or test.rb configuration file according to your requirement. These files are located at /config/environments/ folder.

config.action_mailer.raise_delivery_errors = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: “smtpcorp.com”,
port: 2525,
# 8025, 587 and 25 can also be used.

domain: “yourdomain.com”,
authentication: “plain”,
enable_starttls_auto: true,
user_name: “USERNAME”,
password: “PASSWORD”
}

Step 5
You may test the settings by running the following command from rails console.

UserMailer.sendMail(“recipient@example.com”).deliver
In this way you have to follow the steps to setup action mailer in rails application

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s