https://github.com/resend/resend-rails-example
Example Rails APP showcasing how to use the Resend Rails SDK with Active Mailer
https://github.com/resend/resend-rails-example
Last synced: about 1 year ago
JSON representation
Example Rails APP showcasing how to use the Resend Rails SDK with Active Mailer
- Host: GitHub
- URL: https://github.com/resend/resend-rails-example
- Owner: resend
- Created: 2022-12-30T14:47:03.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2025-04-11T14:15:40.000Z (about 1 year ago)
- Last Synced: 2025-04-13T05:45:26.735Z (about 1 year ago)
- Language: Ruby
- Size: 98.6 KB
- Stars: 13
- Watchers: 2
- Forks: 0
- Open Issues: 12
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
This is an example rails app on how to configure and use the [Resend Rails SDK](https://github.com/drish/resend-ruby) with Rails Action Mailer
# Setup
Add the gem to your Gemfile:
```ruby
# Gemfile
gem "resend"
```
Create a `/config/initializers/mailer.rb` file and add the following configuration line.
```
Resend.api_key = ENV['RESEND_API_KEY']
```
Add these lines of code into your environment config file.
```rb
# Setup resend as the email method
config.action_mailer.delivery_method = :resend
```
Checkout the [Rails example app with SMTP setup](https://github.com/resendlabs/resend-rails-smtp-example) for SMTP support example.
Create your mailer class
```ruby
# /app/mailers/user_mailer.rb
class UserMailer < ApplicationMailer
default from: 'you@domain.io' # this domain must be verified with Resend
def welcome_email
@user = params[:user]
attachments["invoice.pdf"] = File.read(Rails.root.join("resources","invoice.pdf"))
@url = "http://example.com/login"
mail(
to: ["to@email.com"],
cc: ["cc@email.com"],
bcc: ["cc@email.com"],
reply_to: "to@email.com",
subject: "Hello from Rails",
)
end
end
```
Create your `ERB Template` for `UserMailer`
```ruby
# /app/views/welcome_email.html.erb
Welcome to example.com, <%= @user.name %>
You have successfully signed up to example.com,
To login to the site, just follow this link: <%= @url %>.
Thanks for joining and have a great day!
```
Now you can send your emails, lets send it using Rails console.
```sh
bundle exec rails c
```
Initialize your `UserMailer` class, this should return a `UserMailer` instance.
```ruby
u = User.new name: "derich"
mailer = UserMailer.with(user: u).welcome_email
# => #, , , ...
```
You can now send emails with:
```ruby
mailer.deliver_now!
# => {:id=>"a193c81e-9ac5-4708-a569-5caf14220539", :from=>....}
```