Prompt
How do I send emails from a Rails app?
Latest observation
Sep 23, 2026
OpenAI APIWeb search: off
In Rails, you typically send email with Action Mailer, which is built into Rails.
1. Create a mailer
Generate one with:
rails generate mailer UserMailer
This creates:
app/mailers/user_mailer.rbapp/views/user_mailer/- test files
2. Define an email method
Example:
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: "Welcome to My App")
end
end
3. Create the email view
Rails will look for a view matching the method name:
<!-- app/views/user_mailer/welcome_email.html.erb -->
<h1>Welcome, <%= @user.name %>!</h1>
<p>Thanks for signing up.</p>
You can also create a text version:
<!-- app/views/user_mailer/welcome_email.text.erb -->
Welcome, <%= @user.name %>!
Thanks for signing up.
4. Send the email
From a controller, job, or anywhere in your app:
UserMailer.welcome_email(@user).deliver_now
Or asynchronously:
UserMailer.welcome_email(@user).deliver_later
5. Configure delivery settings
You need to configure a mail delivery method in your environment files.
Example for SMTP in config/environments/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.gmail.com",
port: 587,
user_name: ENV["SMTP_USERNAME"],
password: ENV["SMTP_PASSWORD"],
authentication: :login,
enable_starttls_auto: true
}
config.action_mailer.default_url_options = { host: "yourdomain.com" }
For development, you might use a tool like letter_opener or mailcatcher to preview emails instead of sending real ones.
6. Set defaults in ApplicationMailer
# app/mailers/application_mailer.rb
class ApplicationMailer < ActionMailer::Base
default from: "no-reply@yourdomain.com"
layout "mailer"
end
Example: full flow
# app/models/user.rb
class User < ApplicationRecord
end
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: "Welcome!")
end
end
# app/controllers/users_controller.rb
class UsersController < ApplicationController
def create
@user = User.create!(user_params)
UserMailer.welcome_email(@user).deliver_later
redirect_to root_path, notice: "Account created!"
end
end
If you want, I can also show you:
- how to send emails with Devise
- how to set up SMTP with Gmail/SendGrid
- how to build HTML + text multipart emails
- how to send emails from background jobs