PHP
Laravel
PHP
Laravel
Learn how to send your first email using Laravel.
Prerequisites
To get the most out of this guide, you will need to:
1. Install
First, install Resend for Laravel using the Composer package manager:
Composer
composer require resend/resend-laravel
2. Configuration
API key
Next, you should configure your Resend API key in your application’s .env
file:
.env
RESEND_API_KEY=re_123456789
Mail driver
To use Resend as your mail driver, first create a new mailer definition, in the mailers
array within your application’s config/mail.php
configuration file:
mail.php
'resend' => [
'transport' => 'resend',
],
Next, update your application’s .env
file to use the Resend mail driver:
.env
MAIL_MAILER=resend
MAIL_FROM_ADDRESS="noreply@resend.dev"
MAIL_FROM_NAME=Resend
3. Send an email
Resend for Laravel provides two convenient ways to send emails, using Laravel’s email service or the Resend
API facade.
Using the Mail Facade
OrderShipmentController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Mail\OrderShipped;
use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
class OrderShipmentController extends Controller
{
/**
* Ship the given order.
*/
public function store(Request $request): RedirectResponse
{
$order = Order::findOrFail($request->order_id);
// Ship the order...
Mail::to($request->user())->send(new OrderShipped($order));
return redirect('/orders');
}
}
Using the Resend Facade
OrderShipmentController.php
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Mail\OrderShipped;
use App\Models\Order;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Resend\Laravel\Facades\Resend;
class OrderShipmentController extends Controller
{
/**
* Ship the given order.
*/
public function store(Request $request): RedirectResponse
{
$order = Order::findOrFail($request->order_id);
// Ship the order...
Resend::email()->send([
'from' => 'from@example.com',
'to' => $request->user()->email,
'subject' => 'hello world',
'html' => (new OrderShipped($order))->render(),
])
return redirect('/orders');
}
}
4. Try it yourself
Laravel Example
See the full source code.