Configure Action Mailer
In config/environments/production.rb (or an initializer):
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.sendcorex.com',
port: 587,
user_name: ENV['SENDCOREX_API_KEY'],
password: ENV['SENDCOREX_API_KEY'],
authentication: 'plain',
enable_starttls_auto: true,
}
config.action_mailer.default_url_options = { host: 'yourdomain.com' }
Generate and send a mailer
rails generate mailer Welcome
class WelcomeMailer < ApplicationMailer
default from: 'Your App <hello@yourdomain.com>'
def welcome_email(user)
@user = user
mail(to: @user.email, subject: 'Welcome!')
end
end
# from anywhere in your app
WelcomeMailer.welcome_email(user).deliver_later
Use deliver_later with a queue adapter (Sidekiq, Solid Queue) instead of deliver_now so a slow SMTP connection never blocks a web request.
Common errors
| Symptom | Fix |
|---|---|
Net::SMTPAuthenticationError | Confirm the env var is loaded and used for both user_name and password. |
| Mail never leaves the queue | Check your queue adapter (e.g. Sidekiq) is actually running in that environment. |