Send email from Ruby on Rails (Action Mailer) over SMTP
Rails Action Mailer SMTP configuration: config.action_mailer.smtp_settings with address, port, authentication and enable_starttls, credentials from Rails credentials or ENV, a mailer class and deliver_later. Plain Ruby net/smtp snippet included.
Install
# Action Mailer ships with Rails — plain Ruby: gem "net-smtp"
Complete example
# config/environments/production.rb
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: ENV.fetch("SMTP_HOST", "smtp.queensmtp.com"),
port: ENV.fetch("SMTP_PORT", 587).to_i,
user_name: ENV.fetch("SMTP_USER"),
password: ENV.fetch("SMTP_PASS"),
authentication: :plain,
enable_starttls: true, # 587; for 465 use tls: true instead
open_timeout: 20,
read_timeout: 20,
}
config.action_mailer.default_url_options = { host: "yourdomain.com" }
config.action_mailer.raise_delivery_errors = true
# app/mailers/order_mailer.rb
class OrderMailer < ApplicationMailer
default from: "Your App <hello@yourdomain.com>"
def shipped(order)
@order = order
mail(to: order.customer_email, subject: "Your order has shipped")
end
end
# anywhere
OrderMailer.shipped(order).deliver_later # via Active Job; deliver_now to send inline
# plain Ruby (no Rails)
require "net/smtp"
msg = "From: hello@yourdomain.com\nTo: customer@example.com\nSubject: test\n\nSMTP works\n"
Net::SMTP.start("smtp.queensmtp.com", 587, "localhost", ENV["SMTP_USER"], ENV["SMTP_PASS"], :plain, tls: false, starttls: :always) do |smtp|
smtp.send_message msg, "hello@yourdomain.com", "customer@example.com"
endWhat to know
- enable_starttls: true (Rails 7.1+; older: enable_starttls_auto) for 587; tls: true for 465 — not both.
- deliver_later needs a job backend (Sidekiq, Solid Queue, GoodJob) in production.
- Use the letter_opener gem in development and the Mailtrap sandbox host in staging.
Same code, different provider
Only the host, port and credentials change. The rules per provider are on the SMTP settings hub.
| Provider | Host | Port | Username | Password | Note |
|---|---|---|---|---|---|
| QueenSMTP | smtp.queensmtp.com | 587 (STARTTLS) / 465 (SSL) / 2525 | SMTP username from the dashboard | SMTP password | $5/year + $0.10 per 1,000; 100 free/day |
| Gmail | smtp.gmail.com | 587 / 465 | you@gmail.com | App Password | 500/day, From must be the account |
| Amazon SES | email-smtp.<region>.amazonaws.com | 587 / 465 / 2587 | SES SMTP username | SES SMTP password | sandbox 200/day until approved |
| Office 365 | smtp.office365.com | 587 | user@yourdomain.com | mailbox password | SMTP AUTH must be enabled; 30/min |
| SendGrid | smtp.sendgrid.net | 587 / 465 / 2525 | apikey | API key | no free plan since 2025 |
Credentials for the example above
Create a free account, verify your sending domain (SPF + DKIM shown in the dashboard), add an SMTP credential, and paste it into the code. 100 emails a day free; $5/year plus $0.10 per 1,000 after that. Prefer HTTPS? The same account has a REST email API.
Get SMTP credentials Test your settings firstOther languages and frameworks
Ruby on Rails SMTP — questions
The SMTP credentials are wrong for that host. Gmail needs an App Password; relays need the credential from the dashboard, not the login password.
Related Guides
Continue learning with these related articles