Sign inGet Started

PHP SDK

Send email with PHP

Install the official Parcel Wing PHP SDK, configure your API key, and send your first email.

Requirements

  • PHP 8.1 or newer.
  • Composer installed locally or in your deployment environment.
  • A Parcel Wing API key stored as PARCELWING_API_KEY.
  • A verified sending domain for your from address.

Install the SDK

The official PHP SDK is installed with Composer.

Terminal

composer require parcelwing/parcelwing-php

Send your first email

Create a ParcelWing client and call $parcelwing->emails->send().

send.php

<?php
 
require __DIR__ . '/vendor/autoload.php';
 
use ParcelWing\ParcelWing;
use ParcelWing\Exceptions\ParcelWingException;
 
$parcelwing = new ParcelWing($_ENV['PARCELWING_API_KEY']);
 
try {
$emails = $parcelwing->emails->send([
'from' => 'Acme <[email protected]>',
'to' => '[email protected]',
'subject' => 'Hello from Parcel Wing',
'html' => '<strong>It works!</strong>',
]);
 
echo $emails[0]['id'] . PHP_EOL;
} catch (ParcelWingException $error) {
echo $error->getMessage() . PHP_EOL;
}

Use a template

Use template_alias and template_params for reusable product and lifecycle messages.

templates.php

<?php
 
$emails = $parcelwing->emails->send([
'from' => 'Acme <[email protected]>',
'to' => '[email protected]',
'template_alias' => 'welcome',
'template_params' => [
'first_name' => 'Ada',
'workspace_name' => 'Acme',
],
]);

Handle errors

Failed API calls throw ParcelWing\\Exceptions\\ParcelWingException.

errors.php

<?php
 
use ParcelWing\Exceptions\ParcelWingException;
 
try {
$parcelwing->emails->send([
'from' => 'bad',
'to' => 'not-an-email',
]);
} catch (ParcelWingException $error) {
echo $error->status . PHP_EOL;
echo $error->type . PHP_EOL;
echo ($error->errorCode ?? '') . PHP_EOL;
print_r($error->details);
}

Framework guides