> ## Documentation Index
> Fetch the complete documentation index at: https://resend.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Send emails using Rails with SMTP

> Learn how to integrate Rails with Resend SMTP.

## Prerequisites

Before you start, you'll need:

* A Resend [API key](/create-an-api-key)
* A [verified domain](/add-a-domain)

## 1. Setup your environment

Add these lines of code into your environment config file.

```rb config/environments/environment.rb theme={"theme":{"light":"github-light","dark":"vesper"}}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  :address   => 'smtp.resend.com',
  :port      => 465,
  :user_name => 'resend',
  :password  => ENV['RESEND_API_KEY'],
  :tls => true
}
```

## 2. Send email using Rails Action Mailer

Then create a `UserMailer` class definition.

```rb app/mailers/user_mailer.rb theme={"theme":{"light":"github-light","dark":"vesper"}}
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
end
```

And create your ERB email template.

```html app/views/user_mailer/welcome_email.html.erb theme={"theme":{"light":"github-light","dark":"vesper"}}
<!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 `UserMailer` class. This should return a `UserMailer` instance.

```rb theme={"theme":{"light":"github-light","dark":"vesper"}}
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:

```rb theme={"theme":{"light":"github-light","dark":"vesper"}}
mailer.deliver_now!

# => {:id=>"a193c81e-9ac5-4708-a569-5caf14220539", :from=>....}
```

## 3. Try it yourself

<Card title="Rails SMTP Example" icon="arrow-up-right-from-square" href="https://github.com/resend/resend-rails-smtp-example">
  See the full source code.
</Card>
