Action Mailer helps your Rails app send emails easily. It organizes email sending like writing and sending letters.
Send welcome emails when users sign up.
Notify users about password changes.
Send order confirmations in an online store.
Deliver newsletters or updates to subscribers.
Syntax
Ruby on Rails
class UserMailer < ApplicationMailer
default from: 'notifications@example.com'def welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
Define a mailer class inheriting from ApplicationMailer.
Use mail method to set recipient and subject.
Examples
A simple mailer method sending an alert email.
Ruby on Rails
class NotificationMailer < ApplicationMailer
def alert_email(user)
mail(to: user.email, subject: 'Alert!')
end
end
Sets a default sender email for all mails in this mailer.
Ruby on Rails
class UserMailer < ApplicationMailer
default from: 'support@example.com'def reset_password_email(user)
@user = user
mail(to: @user.email, subject: 'Reset your password')
end
end
Sample Program
This example shows how to set up SMTP settings in development, create a mailer, and send a welcome email immediately.
Ruby on Rails
# config/environments/development.rb
Rails.application.configure do
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.example.com',
port: 587,
user_name: 'user@example.com',
password: 'password',
authentication: :plain,
enable_starttls_auto: true
}
end
# app/mailers/user_mailer.rbclass UserMailer < ApplicationMailer
default from: 'notifications@example.com'def welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome to My Awesome Site')
end
end
# Usage in Rails console or controller
user = OpenStruct.new(email: 'newuser@example.com')
email = UserMailer.welcome_email(user)
email.deliver_now
puts "Email sent to: #{email.to.first} with subject: '#{email.subject}'"
OutputSuccess
Important Notes
Remember to configure SMTP settings for your environment to send emails.
Use deliver_now to send immediately or deliver_later to send asynchronously.
Test emails in development with tools like Letter Opener or Mailcatcher to avoid sending real emails.
Summary
Action Mailer organizes email sending in Rails apps.
Define mailer classes and methods to create emails.
Configure SMTP settings to send emails through a mail server.
Practice
(1/5)
1. What is the main purpose of Action Mailer in a Rails application?
easy
A. To manage database migrations automatically
B. To send emails by defining mailer classes and methods
C. To handle user authentication and sessions
D. To style HTML views with CSS
Solution
Step 1: Understand Action Mailer role
Action Mailer is designed to send emails by organizing mailer classes and methods.
Step 2: Compare with other Rails features
Database migrations, authentication, and styling are handled by other parts of Rails, not Action Mailer.
Final Answer:
To send emails by defining mailer classes and methods -> Option B
Quick Check:
Action Mailer = send emails [OK]
Hint: Remember: Action Mailer is for emails, not database or styling [OK]
Common Mistakes:
Confusing Action Mailer with Active Record migrations
Thinking it handles user login or CSS styling
Assuming it manages background jobs directly
2. Which of the following is the correct way to define a mailer method in a Rails mailer class?
easy
A. def welcome_email(user) mail to: user.email, subject: 'Welcome' end
B. def welcome_email(user) { send_email(to: user.email) }
C. def welcome_email(user) mail(to user.email, subject 'Welcome') end
D. def welcome_email(user); mail(to: user.email, subject: 'Welcome'); end
Solution
Step 1: Check Ruby method syntax
Method definitions use def name(params); ...; end with parentheses and semicolons optional but recommended for clarity.
Step 2: Verify mail method usage
The mail method requires keyword arguments with colons, e.g., to: user.email, and commas between arguments.
Final Answer:
def welcome_email(user); mail(to: user.email, subject: 'Welcome'); end -> Option D
Quick Check:
Correct Ruby method and mail syntax = def welcome_email(user); mail(to: user.email, subject: 'Welcome'); end [OK]
Hint: Use parentheses and colons for keyword args in mail method [OK]
Common Mistakes:
Missing parentheses or commas in method definition
Using braces {} instead of do...end or semicolons
Omitting colons in keyword arguments
3. Given this mailer method in Rails:
class UserMailer < ApplicationMailer
def notify(user)
mail(to: user.email, subject: 'Notice')
end
end
What will happen when you call UserMailer.notify(user).deliver_now?
medium
A. An email is sent immediately to the user's email address
B. An email is queued but not sent until later
C. A syntax error occurs because deliver_now is invalid
D. Nothing happens because notify method lacks a body
Solution
Step 1: Understand deliver_now behavior
The deliver_now method sends the email immediately when called on a mail object.
Step 2: Check mailer method correctness
The notify method calls mail with valid parameters, so it returns a mail object ready to send.
Final Answer:
An email is sent immediately to the user's email address -> Option A
Quick Check:
deliver_now sends email immediately = An email is sent immediately to the user's email address [OK]
Hint: deliver_now sends email immediately, deliver_later queues it [OK]
Common Mistakes:
Confusing deliver_now with deliver_later
Thinking mail method sends email by itself
Assuming notify method must have email body text
4. You wrote this mailer method:
def alert(user)
mail(to: user.email, subject: 'Alert')
end
But emails are not sending. Which is the most likely cause?
medium
A. SMTP settings are not configured properly in environment files
B. The mail method is missing a block with email body content
C. You forgot to call deliver_now or deliver_later
D. Mailer class must inherit from ActionMailer::Base explicitly
Solution
Step 1: Check SMTP configuration
Without proper SMTP settings, emails cannot be sent even if mailer code is correct.
Step 2: Review other options
Mailer inheritance from ApplicationMailer is standard; mail method can use templates for body; calling deliver_now is needed but question implies method is called.
Final Answer:
SMTP settings are not configured properly in environment files -> Option A
Quick Check:
SMTP config missing = emails not sent [OK]
Hint: Check SMTP setup first if emails don't send [OK]
Common Mistakes:
Assuming mail method sends email without deliver_now
Forgetting to configure SMTP in config/environments
Thinking mailer must inherit directly from ActionMailer::Base
5. You want to configure Action Mailer to send emails via Gmail SMTP in production. Which configuration snippet in config/environments/production.rb is correct?