Prompt
How do I send emails from a Rails app?
Latest observation
Nov 20, 2026
OpenAI APIWeb search: off
In Rails, the usual way to send email is with Action Mailer.
1) Generate a mailer
rails generate mailer UserMailer
This creates:
app/mailers/user_mailer.rbapp/views/user_mailer/- test files
2) Define a mailer method
# app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
def welcome_email(user)
@user = user
mail(to: @user.email, subject: "Welcome to our app!")
end
end
3) Create the email view
<!-- app/views/user_mailer/welcome_email.html.erb -->
<h1>Welcome, <%= @user.name %>!</h1>
<p>Thanks for signing up.</p>
You can also add a text version:
<!-- app/views/user_mailer/welcome_email.text.erb -->
Welcome, <%= @user.name %>!
Thanks for signing up.
4) Deliver the email
Call it from your app:
UserMailer.welcome_email(@user).deliver_now
Or send it asynchronously:
UserMailer.welcome_email(@user).deliver_later
5) Configure SMTP
In production, you’ll usually configure a mail provider like SendGrid, Mailgun, Amazon SES, or Postmark.
Example for SMTP in config/environments/production.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: "smtp.sendgrid.net",
port: 587,
domain: "yourdomain.com",
user_name: Rails.application.credentials.dig(:sendgrid, :username),
password: Rails.application.credentials.dig(:sendgrid, :password),
authentication: :plain,
enable_starttls_auto: true
}
Also set:
config.action_mailer.default_url_options = { host: "example.com", protocol: "https" }
6) Set default sender
In app/mailers/application_mailer.rb:
class ApplicationMailer < ActionMailer::Base
default from: "no-reply@example.com"
layout "mailer"
end
7) Use credentials for secrets
Store email provider creds securely:
bin/rails credentials:edit
Example:
sendgrid:
username: apikey
password: your-secret-key
8) Test locally
For development, you can use:
config.action_mailer.delivery_method = :letter_opener
Then add the gem:
gem "letter_opener"
This opens emails in your browser instead of sending them.
Common gotchas
- Make sure
default_url_optionsis set for links in emails. - Use
deliver_laterif you don’t want email sending to block requests. - In production, use a real email service instead of plain SMTP from your own server.
If you want, I can also show you:
- a full example with Devise
- how to send password reset emails
- how to configure Action Mailer for production with a specific provider