Sign inGet Started

PHP SDK

Send email with Laravel

Use the Parcel Wing PHP SDK from Laravel services, controllers, and queued jobs.

Install the SDK

The official PHP SDK is installed with Composer.

Terminal

composer require parcelwing/parcelwing-php

Configure Laravel

Add your API key and default sender to .env, then expose them through config/services.php.

.env

PARCELWING_API_KEY=pw_live_...
PARCELWING_FROM_EMAIL="Acme <[email protected]>"

config/services.php

'parcelwing' => [
'key' => env('PARCELWING_API_KEY'),
'from' => env('PARCELWING_FROM_EMAIL'),
],

Create a mail service

Bind the SDK once, then wrap your common message flows in a service class.

AppServiceProvider.php

<?php
 
use ParcelWing\ParcelWing;
 
$this->app->singleton(ParcelWing::class, function () {
return new ParcelWing(config('services.parcelwing.key'));
});

app/Services/ParcelWingMail.php

<?php
 
namespace App\Services;
 
use ParcelWing\ParcelWing;
use ParcelWing\Exceptions\ParcelWingException;
 
class ParcelWingMail
{
public function __construct(private ParcelWing $parcelwing)
{
}
 
/** @throws ParcelWingException */
public function sendWelcome(string $email, ?string $firstName = null): array
{
$emails = $this->parcelwing->emails->send([
'from' => config('services.parcelwing.from'),
'to' => $email,
'template_alias' => 'welcome',
'template_params' => [
'first_name' => $firstName ?: 'friend',
],
]);
 
return $emails[0];
}
}

Send from a controller

Keep API keys server-side and return the queued Parcel Wing message ID to your app.

WelcomeEmailController.php

<?php
 
namespace App\Http\Controllers;
 
use App\Services\ParcelWingMail;
use Illuminate\Http\Request;
 
class WelcomeEmailController
{
public function store(Request $request, ParcelWingMail $mail)
{
$data = $request->validate([
'email' => ['required', 'email'],
'first_name' => ['nullable', 'string'],
]);
 
$message = $mail->sendWelcome($data['email'], $data['first_name'] ?? null);
 
return response()->json(['id' => $message['id']]);
}
}