Sign inGet Started

PHP SDK

Send email with Symfony

Register the Parcel Wing PHP SDK as a Symfony service and inject it into controllers.

Install the SDK

The official PHP SDK is installed with Composer.

Terminal

composer require parcelwing/parcelwing-php

Configure services

Store secrets in environment variables and wire the SDK through Symfony's service container.

.env.local

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

config/services.yaml

services:
ParcelWing\ParcelWing:
arguments:
$apiKey: '%env(PARCELWING_API_KEY)%'
 
App\Service\ParcelWingMail:
arguments:
$fromEmail: '%env(PARCELWING_FROM_EMAIL)%'

Create a mail service

A small service keeps template names, tags, and error logging in one place.

src/Service/ParcelWingMail.php

<?php
 
namespace App\Service;
 
use ParcelWing\ParcelWing;
 
class ParcelWingMail
{
public function __construct(
private ParcelWing $parcelwing,
private string $fromEmail,
) {
}
 
public function sendWelcome(string $email, ?string $firstName = null): array
{
$emails = $this->parcelwing->emails->send([
'from' => $this->fromEmail,
'to' => $email,
'template_alias' => 'welcome',
'template_params' => [
'first_name' => $firstName ?: 'friend',
],
]);
 
return $emails[0];
}
}

Send from a controller

Use the service from a controller or message handler.

src/Controller/WelcomeEmailController.php

<?php
 
namespace App\Controller;
 
use App\Service\ParcelWingMail;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
 
class WelcomeEmailController
{
#[Route('/send-welcome', methods: ['POST'])]
public function __invoke(Request $request, ParcelWingMail $mail): JsonResponse
{
$payload = $request->toArray();
$message = $mail->sendWelcome(
$payload['email'],
$payload['first_name'] ?? null,
);
 
return new JsonResponse(['id' => $message['id']]);
}
}