Action Mailer in Ruby on Rails
Action Mailer is a powerful component of Ruby on Rails that enables you to send emails directly from your application using mailer classes and associated views. Much like controllers, mailers are essential for managing email communication within your Rails application. In this guide, we’ll explore the key features of Action Mailer, how to set it up, and how to send emails using various methods.
Introduction to Action Mailer
Mailers in Ruby on Rails are analogous to controllers in the sense that they inherit from ActionMailer::Base
and are typically located in the app/mailers
directory. They share several similarities with controllers, including:
Actions and Views: Mailers have actions, just like controllers, and they also have associated views that reside in the
app/views
directory.Instance Variables: You can define instance variables in mailers, and they are accessible within the associated views.
Layouts and Partials: Like controllers, mailers can utilize layouts and partials to structure and format email content.
Access to Params: Mailers can access a
params
hash, making it easy to pass information to email templates.
How to Send Emails with Action Mailer
Sending emails with Action Mailer is straightforward and can be achieved in two main ways:
1. Sending Email from an Anonymous Email Address
To send emails from an anonymous email address, follow these steps:
- Create a file named
setup_mail.rb
in theconfig/initializers
directory and add the following code:
# config/initializers/setup_mail.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.ntc.net.np",
:port => 25
}
Be sure to replace "smtp.ntc.net.np"
with the SMTP address of your Internet Service Provider (ISP) and verify the correct port number for your ISP.
- Generate a mailer (similar to creating a controller) by running the following command:
rails generate mailer mailer_name
For example, to create a mailer named user_mailer
, you would use:
rails generate mailer user_mailer
This command generates a file named user_mailer.rb
in the app/mailers
directory and a directory named user_mailer
in the app/views
directory.
- Replace the code in
user_mailer.rb
with the following:
# app/mailers/user_mailer.rb
class UserMailer < ActionMailer::Base
def registration_confirmation(user)
mail(:to => 'destination@example.com', :subject => "Newly Registered", :from => "sourceemail@example.com")
end
end
You can replace 'destination@example.com'
with user.email
if you have passed a User
object via a method call.
- Create a view for your mailer. In this example, create a file named
registration_confirmation.html.erb
in theuser_mailer
directory within theapp/views
directory. Customize the content as needed:
<h1>Thank you for registering</h1>
<p>Welcome to our community!</p>
You can embed Ruby code in these views, just like in other Rails views. Additionally, you can render these views to preview the email content.
- Finally, trigger Rails to send the email by invoking the mailer action in your application code. For example, in a controller:
# app/controllers/user_controller.rb
def create
@user = User.new(params[:user])
if @user.save
UserMailer.registration_confirmation(@user).deliver
end
end
2. Sending Email via Email Servers
To send emails through email servers (e.g., Gmail, Outlook), you’ll need to adjust the settings in the setup_mail.rb
file. Here’s an example for sending emails via Gmail:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => "587",
:domain => "gmail.com",
:user_name => "xxx@gmail.com",
:password => "yyy",
:authentication => "plain",
:enable_starttls_auto => true
}
For Outlook or other email providers, modify the attributes accordingly.
With Action Mailer in Ruby on Rails, you can easily incorporate email communication into your web application, whether you need to send notifications, confirmations, or any other type of email correspondence. This powerful feature simplifies the process of handling email in your Rails projects.