> ## 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.

# Laravel

> Send transactional emails from your Laravel 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 Laravel:

**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 Laravel application.

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

**2. Add SMTP variables**

To configure Loops SMTP in your Laravel project, add the following values to your `.env` file.

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

```bash .env theme={"dark"}
MAIL_MAILER=smtp
MAIL_HOST=smtp.loops.so
MAIL_PORT=587
MAIL_USERNAME=loops
MAIL_PASSWORD=<API_KEY>
MAIL_ENCRYPTION=tls
```

**3. Send emails from Laravel**

Now you can send emails from your application.

If you haven't already, create a mailable class, for example `AuthEmail`:

```bash theme={"dark"}
php artisan make:mail AuthEmail
```

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 view for your email, like below.

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

```json resources/views/mail/auth-email-text.blade.php theme={"dark"}
{
  "transactionalId": "clomzp89u635xl30px7wrl0ri",
  "email": "{{ $email }}", /* recipient */
  "dataVariables": {
    "loginUrl": "https://myapp.com/login?code={{ $auth_code }}"
  }
}
```

Then add a reference to your template in the `Content` definition using the `text` key.

You also need to pass the values for the recipient email address and any data variables in your email. In this case we are using a `$user` property added to the constructor.

```php app/Mail/AuthEmail.php theme={"dark"}
use App\Models\User;

class AuthEmail extends Mailable
{
  /**
   * Create a new message instance.
   */
  public function __construct(
      private User $user,
  ) {}

  /**
   * Get the message content definition.
   */
  public function content(): Content
  {
      return new Content(
          text: 'mail.auth-email-text',
          with: [
            'email' => $this->user->email,
            'auth_code' => $this->user->auth_code,
          ]
      );
  }
}
```

> [!TIP]
>
> You can omit the `view` option typically required for HTML emails in Laravel. Loops handles HTML rendering using the provided payload.

You can skip adding values to the `Envelope` because the "from" address and subject are all defined within Loops on your transactional email.

Now you can send transactional emails.

```php theme={"dark"}
use \App\Mail\AuthEmail;

Mail::to('test@example.com')->send(new AuthEmail($user));
```

Note that the email address defined in `to()` will not be used for sending the email even though it's a required parameter. **You have to provide the recipient's email to the template itself**.

You can read more about sending emails from Laravel [in their docs](https://laravel.com/docs/11.x/mail#writing-mailables).

> [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)
- [Loops SDKs](https://loops.so/docs/sdks.md)
- [API Introduction](https://loops.so/docs/api-reference/intro.md)
- [Social icons](https://loops.so/docs/creating-emails/editor/social-icons.md)
