โšก
LightningRails
  • ๐Ÿ‘‹Welcome
  • Access the Repo
  • Getting Started
    • ๐Ÿš€Quickstart
    • ๐ŸŽจThemes
    • ๐Ÿ–ผ๏ธCustomize the views
    • ๐Ÿ”ฎDaisyUI Library
    • โšกLightning Landing
      • Quickstart
      • Theme and branding
      • Page structure
      • Publish your landing page
  • Features setup
    • ๐Ÿ“ธImages & media
    • ๐Ÿ”Admin Dashboard
    • Search Engine Optimization
    • ๐Ÿ“งAutomatic Emails
      • ๐ŸŸจPostmark
      • ๐Ÿ”ฒResend
    • ๐ŸšชLogin with Devise
    • ๐Ÿช„Magic Link Signup
    • ๐Ÿ’ณStripe Payment Gateway
    • Github Signup
    • Lucide icons
    • ๐Ÿค–Multi-provider AI
    • Open AI API
  • UI Components
    • ๐ŸฆธHeros
    • โ”FAQs
    • ๐Ÿƒcards
    • ๐Ÿ’ฌTestimonials
    • ๐Ÿ‘‹Call To Actions
    • ๐Ÿ”ฆFeatures
  • Deploying to production
    • โฌ†๏ธHeroku Deploy
    • ๐Ÿ›ก๏ธSecurity
      • ๐ŸŽ›๏ธRate Limiting
  • RESOURCES
    • ๐Ÿš€Vote for new features
    • Report an issue
    • ๐Ÿ†˜Get help on Slack
    • ๐ŸญDesign Resources
      • Maria Ba Illustrations
      • Assets Mockup Generator
      • Logo Generator
      • Tailwind Cheatsheet
      • HyperUI Tailwind Library
Powered by GitBook
On this page
  • Step 1: Create a Resend Account
  • Step 2: Add the Resend Gem
  • Step 3: Configure Your API Key
  • Step 4: Set Up Action Mailer
  • (Optional) Welcome Email
  • Step 5: Create Your First Mailer
  • Step 6: Add HTML Content for the Email
  • Step 7: Initialize and Send the Mailer
  • Step 8: Verify the Email

Was this helpful?

  1. Features setup
  2. Automatic Emails

Resend

This guide outlines how to install and configure the Resend Gem for sending transactional emails in your Lightning Rails project.

PreviousPostmarkNextLogin with Devise

Last updated 3 months ago

Was this helpful?

Step 1: Create a Resend Account

  1. Go to the .

  2. Sign up and create an account to access your API key.


Step 2: Add the Resend Gem

  1. Open your Gemfile.

  2. Add the Resend gem in your Gemfile:

    gem "resend"
  3. Run bundle install in your terminal to install the gem.

    bundle install

Step 3: Configure Your API Key

  1. Locate your API key from the Resend dashboard.

  2. Add the API key to your .env file:

    RESEND_API_KEY="re_***************"

Step 4: Set Up Action Mailer

  1. Development Environment: Open config/environments/development.rb and set the delivery method to :letter_opener for testing emails locally (The Gem is already installed in the boilerplate):

    config.action_mailer.delivery_method = :letter_opener
  2. Production Environment delivery method: Open config/environments/production.rb and set the delivery method to :resend:

    config.action_mailer.delivery_method = :resend
  3. API key in production

    1. Create a new initializer file: touch app/config/initializers/mailer.rb

    2. Paste your API key, and make sure your API key is set in your hosting provider:

      Resend.api_key = ENV["RESEND_API_KEY"]

(Optional) Welcome Email

Step 5: Create Your First Mailer

  1. Generate a new mailer:

    rails generate mailer UserMailer
  2. Open the newly created file app/mailers/user_mailer.rb and define a mailer method. For example:

    class UserMailer < ApplicationMailer
      def welcome_email
        @user = params[:user]
        mail(to: @user.email, subject: "Welcome to Our App!")
      end
    end

Step 6: Add HTML Content for the Email

  1. Create a corresponding view file for the mailer:

touch app/views/user_mailer/welcome_email.html.erb
  • Path: app/views/user_mailer/welcome_email.html.erb

  • Content example:

    <h1>Welcome to Our App, <%= @user.name %>!</h1>
    <p>Weโ€™re excited to have you on board.</p>

Step 7: Initialize and Send the Mailer

  1. Open the Rails console:

    rails console
  2. Initialize the mailer with a sample user object:

    u = User.new(name: "Derich", email: "derich@example.com")
    mailer = UserMailer.with(user: u).welcome_email
  3. Send the email:

    mailer.deliver_now!
  4. Add an auto-welcome message to welcome your users in user.rb

class User < ApplicationRecord
#ย [...]
  after_create :send_welcome_email

  def send_welcome_email
    UserMailer.with(user: self).welcome_email.deliver_now
  end
end

Step 8: Verify the Email

  • Check the recipient's inbox to confirm the email has been sent successfully.

  • If testing in development, verify the email opens correctly using the letter_opener gem.


That's it! Youโ€™ve successfully set up the Resend Gem for sending transactional an marketing emails in your Lightning Rails project, you can enjoy 100 free emails per month.

Ensure you have the .env file setup as specified in the guide.

๐Ÿ“ง
๐Ÿ”ฒ
Resend website
quick
start