unsent
unsent.dev
Guides

Laravel

How to send emails with Unsent in Laravel

Introduction

This guide shows how to send emails using Unsent in a Laravel application. You can use the Unsent PHP SDK or SMTP.

Install dependencies

Install the Unsent PHP SDK via Composer.

composer require unsent/unsent-php

Configure API Key

Add your Unsent API key to your .env file.

.env
UNSENT_API_KEY=un_...

Send Email

You can send emails using the Unsent client.

routes/web.php
use Unsent\Unsent;
use Illuminate\Support\Facades\Route;

Route::get('/send-email', function () {
    $unsent = Unsent::client(env('UNSENT_API_KEY'));

    $result = $unsent->emails->send([
        'from' => 'Acme <onboarding@unsent.dev>',
        'to' => ['delivered@unsent.dev'],
        'subject' => 'Hello world',
        'html' => '<strong>It works!</strong>',
    ]);

    return $result;
});

Using SMTP

Alternatively, you can use Laravel's built-in SMTP driver.

.env
MAIL_MAILER=smtp
MAIL_HOST=smtp.unsent.dev
MAIL_PORT=587
MAIL_USERNAME=unsent
MAIL_PASSWORD=un_...
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="onboarding@unsent.dev"
MAIL_FROM_NAME="${APP_NAME}"