Send emails with Rails
Prerequisites
Section titled “Prerequisites”Before you start, you'll need:
- A Resend API key
- A verified domain
Install
Get the Resend Ruby SDK.
RubyGems gem install resendGemfile gem 'resend'Send email using Rails Action Mailer
This gem can be used as an Action Mailer delivery method.
First, let's update or create your mailer initializer file with your Resend API Key.
config/initializers/mailer.rb Resend.api_key = "re_xxxxxxxxx"Add these lines of code into your environment config file.
config/environments/environment.rb config.action_mailer.delivery_method = :resendThen create a
UserMailerclass definition.app/mailers/user_mailer.rb class UserMailer < ApplicationMailer default from: 'Acme <onboarding@resend.dev>' # this domain must be verified with Resend def welcome_email @user = params[:user] @url = 'http://example.com/login' mail(to: ["delivered@resend.dev"], subject: 'hello world') end endAnd create your ERB email template.
app/views/user_mailer/welcome_email.html.erb <!doctype html> <html> <head> <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" /> </head> <body> <h1>Welcome to example.com, <%= @user.name %></h1> <p>You have successfully signed up to example.com,</p> <p>To log in to the site, just follow this link: <%= @url %>.</p> <p>Thanks for joining and have a great day!</p> </body> </html>Initialize your
UserMailerclass. This returns anActionMailer::MessageDeliveryinstance.Ruby u = User.new name: "derich" mailer = UserMailer.with(user: u).welcome_email # => #<ActionMailer::MessageDelivery:153700, @mail_message=#<Mail::Message...>, @processed_mailer=...>Finally, you can now send emails using the
deliver_now!method:Ruby mailer.deliver_now! # => {:id=>"a193c81e-9ac5-4708-a569-5caf14220539", :from=>....}
Examples
Section titled “Examples”Rails App
Full Rails web application
Basic Send
Basic email sending
Attachments
Send emails with file attachments
Templates
Send emails using Resend hosted templates
Scheduling
Schedule emails for future delivery
Audiences
Manage contacts and audiences
Domains
Create and manage sending domains
Inbound Webhooks
Receive and process inbound emails
Double Opt-in
Double opt-in subscription flow