Ruby
Ruby and Rails
Ruby
Ruby and Rails
Learn how to send your first email using Rails and the Resend Ruby SDK.
Prerequisites
To get the most out of this guide, you’ll need to:
1. Install
Get the Resend Ruby SDK.
gem install resend
2. 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_123456789"
Add these lines of code into your environment config file.
config/environments/environment.rb
config.action_mailer.delivery_method = :resend
Then create a UserMailer
class definition.
app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'from@example.com' # this domain must be verified with Resend
def welcome_email
@user = params[:user]
@url = 'http://example.com/login'
mail(to: ["to@example.com"], subject: 'hello world')
end
end
And create your ERB email template.
app/views/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 login to the site, just follow this link: <%= @url %>.</p>
<p>Thanks for joining and have a great day!</p>
</body>
</html>
Initialize your UserMailer
class. This should return a UserMailer
instance.
u = User.new name: "derich"
mailer = UserMailer.with(user: u).welcome_email
# => #<Mail::Message:153700, Multipart: false, Headers: <From: from@example.com>, <To: to@example.com>, <Subject: hello world>, <Mime-Version: 1.0>...
Finally, you can now send emails using the deliver_now!
method:
mailer.deliver_now!
# => {:id=>"a193c81e-9ac5-4708-a569-5caf14220539", :from=>....}
3. Try it yourself
Rails Example
See the full source code.