Ruby on Rails - Send Emails
Ruby on Rails - Send Emails
Section titled “Ruby on Rails - Send Emails”Action Mailer is the Rails component responsible for sending and receiving emails. It allows you to create mailer classes that inherit from ApplicationMailer (which in turn inherits from ActionMailer::Base) to handle email delivery.
Let’s create a simple application, mail_sender_app, to demonstrate sending emails.
$ rails new mail_sender_app$ cd mail_sender_appAction Mailer Configuration
Section titled “Action Mailer Configuration”Configuration for Action Mailer is typically done in environment-specific files (e.g., config/environments/development.rb, config/environments/production.rb).
- Set Delivery Method:
In
config/environments/development.rb, specify how emails should be delivered. For development,letter_opener_webis a great gem that opens sent emails in your browser. For production, you’d use:smtpor a service like SendGrid, Postmark, etc.
# Gemfilegem 'letter_opener_web', group: :development# Then run: $ bundle installRails.application.configure do # ... other configurations ...
config.action_mailer.delivery_method = :letter_opener_web # For development config.action_mailer.perform_deliveries = true # Ensure emails are actually sent config.action_mailer.raise_delivery_errors = true config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } # Important for links in emailsend- SMTP Settings (for production or if using SMTP in development): If using SMTP, configure settings like address, port, domain, username, and password. Never hardcode credentials; use Rails credentials or environment variables.
# config/environments/production.rb (example for Gmail SMTP - use with caution, app passwords recommended)config.action_mailer.delivery_method = :smtpconfig.action_mailer.smtp_settings = { address: 'smtp.gmail.com', port: 587, domain: 'yourdomain.com', # Or your app's domain user_name: ENV['GMAIL_USERNAME'], password: ENV['GMAIL_APP_PASSWORD'], authentication: 'plain', enable_starttls_auto: true}Generate a Mailer
Section titled “Generate a Mailer”Use the Rails generator to create a mailer. Let’s create a UserMailer with a welcome_email action:
$ bin/rails generate mailer UserMailer welcome_emailThis creates app/mailers/user_mailer.rb and view templates in app/views/user_mailer/.
Edit app/mailers/user_mailer.rb:
class UserMailer < ApplicationMailer default from: 'notifications@example.com' # Default sender address
def welcome_email(user) @user = user @url = 'http://example.com/login' # Example URL for the email mail(to: @user.email, subject: 'Welcome to Our Awesome Site!') # Rails will look for templates in app/views/user_mailer/welcome_email.html.erb and .text.erb endenddefault from:sets a default sender for all emails from this mailer.- In
welcome_email(user),@usermakes the user object available to the view templates. mail(to: ..., subject: ...)sends the email. Rails automatically creates multipart emails if both HTML and text templates exist.
Create Email View Templates
Section titled “Create Email View Templates”- HTML Template (
app/views/user_mailer/welcome_email.html.erb):
<!DOCTYPE html><html><head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></head><body> <h1>Welcome to example.com, <%= @user.name %>!</h1> <p> You have successfully signed up to example.com, your username is: <%= @user.login %>.<br> </p> <p> To login to the site, just follow this link: <%= link_to 'Login', @url %>. </p> <p>Thanks for joining and have a great day!</p></body></html>- Text Template (
app/views/user_mailer/welcome_email.text.erb):
Welcome to example.com, <%= @user.name %>!===============================================
You have successfully signed up to example.com,your username is: <%= @user.login %>.
To login to the site, just follow this link: <%= @url %>
Thanks for joining and have a great day!Calling the Mailer
Section titled “Calling the Mailer”You’ll typically send emails from your controllers or background jobs after certain events (e.g., user registration).
First, let’s set up a simple User model and controller:
$ bin/rails generate scaffold User name:string email:string login:string$ bin/rails db:migrateModify the create action in app/controllers/users_controller.rb to send the welcome email:
class UsersController < ApplicationController # ... other actions (index, new, show, edit, update, destroy) ...
# POST /users def create @user = User.new(user_params)
if @user.save # Tell the UserMailer to send a welcome email after save # .deliver_later sends email asynchronously using Active Job (recommended) UserMailer.welcome_email(@user).deliver_later
redirect_to @user, notice: 'User was successfully created. Check your email!' else render :new, status: :unprocessable_entity end end
private def user_params params.require(:user).permit(:name, :email, :login) endendTesting Email Delivery
Section titled “Testing Email Delivery”- Start your Rails server:
$ bin/rails s - If using
letter_opener_web, navigate tohttp://localhost:3000/letter_openerin your browser. - Navigate to
http://localhost:3000/users/newto create a new user. - Fill in the form and submit. If successful, the email should be “sent”.
- Check
letter_opener_web(or your configured email delivery system) to see the email.
The log file (log/development.log) will also show details of the email being sent:
Rendering user_mailer/welcome_email.html.erb within layouts/mailer Rendered user_mailer/welcome_email.html.erb within layouts/mailer (Duration: X.Xms) Rendering user_mailer/welcome_email.text.erb within layouts/mailer Rendered user_mailer/welcome_email.text.erb within layouts/mailer (Duration: X.Xms)UserMailer#welcome_email: processed outbound mail in XXX.XmsSent mail to user@example.com (XXX.Xms)[ActiveJob] Enqueued ActionMailer::MailDeliveryJob (Job ID: xxxxx) to SendGrid(mail)...Further Information: For advanced topics like attachments, inline images, and mailer testing, refer to the official Action Mailer Basics Guide.