Sending Emails in Rails Applications with Resend and Devise.

ukaypromise

Engr. Promise

Posted on November 13, 2023

Sending Emails in Rails Applications with Resend and Devise.

In one of the projects I worked on for a client, I needed to configure an email service for the Rails application, and I wanted to try something different than the standard SendGrid or Gmail smtp configurations.
My client had already provided domain-specific emails from Namecheap, and I wanted to set up the configuration using those customized domain emails.

I asked a coworker, who suggested I try Resend. I wasn't sure if it offered any Rails assistance, so I read through the documentation and was surprised to find a Rails section. It was extremely simple to set up with minimal configuration.

For the purposes of this article, I'm assuming you've already configured user authentication with devise.

Resend is a powerful email service that can handle transactional emails, such as password reset emails or email confirmations, which are often required in a Devise setup. In this article, we'll walk you through how to integrate Resend with Devise in a Rails application.

You can visit Resend and sign up for an account.

Step 1: Install Resend Gem

First, you need to install the Resend gem. You can do this by running the following command:

gem install resend

Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Resend

Next, you need to configure Resend with your API keys. This is typically done in an initializer, create a resend.rb file inside the config/initializers :

# config/initializers/resend.rb
Resend.api_key = "your-api-key"
Enter fullscreen mode Exit fullscreen mode

To generate your api_key, you can navigate to the API key section and create an api_key, kindly copy it and save it somewhere.

Resend

Replace "your-api-key" with the API key you received from Resend. You can find your API key in the Resend dashboard.
This would be bad practice to push our api_key to github, so we would be using the rails credentials.

EDITOR="code --wait" rails credentials:edit
Enter fullscreen mode Exit fullscreen mode

This would open the credentials.yml file in your vscode, simple add your api_key like this:

resend:
  api_key: your_api_key_goes_here
Enter fullscreen mode Exit fullscreen mode

Step 3: Set Up Email Delivery Method

After that, you need to set up the email delivery method in your Rails application. This is done by configuring the action_mailer settings in your environment configuration files. Here's how you can do it:

# config/environments/production.rb
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.default_url_options = { host: "this_is_your_domain.com", protocol:"https" }
config.action_mailer.delivery_method = :resend
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure Devise

In order to use Resend with Devise, you need to configure Devise's mailer to use the correct delivery method. This is done in the Devise initializer:

# config/initializers/devise.rb
config.mailer_sender = 'promise@this_is_your_domain.com'
Enter fullscreen mode Exit fullscreen mode

Replace the email address with the email address you want to use for sending emails.

For my case i have my domain from Namecheap, so let us do some more settings to make this work.
On the domain section on Resend, let us add our domain.

Add domain

After adding a domain, you would need to verify it by setting up MX record and TXT record on your Namecheap Advanced DNS settings. Resend would provide this records and you only need to copy to your Namecheap Advanced DNS.

DNS setup

Once it is all green, then it should work as expected. You can test and see it works great.

Email test
Finally, you can test if the email sending works by triggering an email send. For example, you can try resetting a user's password, which should send a password reset email to the user's email address.

Conclusion
In this article, we went through how to set up Resend with Devise in a Rails application. While this guide should cover most use cases, always refer to the official documentation of Resend and Devise for the most accurate information. If you encounter any issues during the setup process, don't hesitate to leave a comment or ask a question. We're here to help!
If this was helpfull, kindly leave a comment.

💖 💪 🙅 🚩
ukaypromise
Engr. Promise

Posted on November 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related