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

# Ruby on Rails

> Send transactional emails from your Rails project using Loops' SMTP service.

Transactional emails with Loops simplify your code. With our WYSIWYG editor and API-like payloads, you can design and manage email templates outside of your codebase, ensuring cleaner code and easier template maintenance.

> [!NOTE]
>
> Unlike older SMTP services, Loops requires the body of emails sent via SMTP to
> be formatted as an [API-like payload](https://loops.so/docs/smtp.md#how-it-works). This approach
> allows you to use Loops' [powerful email editor](https://loops.so/docs/creating-emails/editor.md) to
> craft your emails and keep email templating outside of your application code.

> [!WARNING]
>
> Every email sent over Loops SMTP requires a transactional email to be set up
> in your Loops account. Note the `transactionalId` value in the email payload.

Here's how you can set up transactional emails with Loops SMTP in Rails:

**1. Create emails in Loops**

Create transactional emails in Loops using the [editor](https://loops.so/docs/creating-emails/editor.md).

Add [data variables](https://loops.so/docs/transactional.md#add-data-variables) to your emails for any dynamic content you want to send from your Rails application.

![Add a data variable](https://mintcdn.com/loops/aOtgyczWtTw7x5Xz/images/terminal.png?fit=max\&auto=format\&n=aOtgyczWtTw7x5Xz\&q=85\&s=22f7b0d6876f4e3b56d9b67a421c9996)

**2. Configure Action Mailer**

To use Loops SMTP, configure Action Mailer using the following settings.

> [!TIP]
>
> The `password` value should be an API key from your [API Settings](https://app.loops.so/settings?page=api) page.

```ruby config/environments/production.rb theme={"dark"}
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
  address:         'smtp.loops.so',
  port:            587,
  user_name:       'loops',
  password:        '<API_KEY>',
  authentication:  'plain',
  enable_starttls: true
}
```

**3. Send emails from Rails**

Now you can send emails from your application.

Here's an example Mailer:

```ruby app/mailers/user_mailer.rb theme={"dark"}
class UserMailer < ApplicationMailer

  def login_email
    @user = params[:user]
    # Note: the "to" address is required by Action Mailer
    #  but is overwritten by the email provided in the view
    #  (see below). Likewise, a subject is not required here
    #  because Loops will use the subject provided in the editor.
    mail(to: @user.email)
  end

end
```

Loops' SMTP system doesn't send full HTML emails directly. Instead, you should provide a structured API-like payload, which Loops will then use to render an HTML email.

Create a text view for your email, like below.

You can copy an example payload from the **Publish** page of your transactional email in Loops.

```json app/views/user_mailer/login_email.text.erb theme={"dark"}
{
  "transactionalId": "clomzp89u635xl30px7wrl0ri",
  "email": "<%= @user.email %>", /* recipient */
  "dataVariables": {
    "loginUrl": "https://myapp.com/login?code=<%= @user.auth_code %>"
  }
}
```

> [!TIP]
>
> You do not need to provide an HTML view for your emails when using Loops SMTP (i.e.: `login_email.html.erb` is not required).

> [card] [Send transactional email](https://loops.so/docs/api-reference/send-transactional-email.md)
>
> Read how to send transactional email with our API.

> [card] [Transactional email guide](https://loops.so/docs/transactional.md)
>
> Learn how to send transactional email with Loops.


## Related topics

- [Send with SMTP](https://loops.so/docs/smtp.md)
- [API Introduction](https://loops.so/docs/api-reference/intro.md)
- [Loops SDKs](https://loops.so/docs/sdks.md)
- [Ruby SDK](https://loops.so/docs/sdks/ruby.md)
- [API Changelog](https://loops.so/docs/api-reference/changelog.md)
