PHP SDK
Send email with Slim
Add Parcel Wing to a small Slim API route with Composer and server-side environment variables.
Install the SDK
The official PHP SDK is installed with Composer.
Terminal
composer require parcelwing/parcelwing-php
Send from a route
Create the client once and reuse it from your route handlers.
public/index.php
<?phpuse ParcelWing\ParcelWing;use Psr\Http\Message\ResponseInterface as Response;use Psr\Http\Message\ServerRequestInterface as Request;use Slim\Factory\AppFactory;require __DIR__ . '/../vendor/autoload.php';$app = AppFactory::create();$parcelwing = new ParcelWing($_ENV['PARCELWING_API_KEY']);$app->post('/send-welcome', function (Request $request, Response $response) use ($parcelwing) {$payload = (array) $request->getParsedBody();$emails = $parcelwing->emails->send(['from' => $_ENV['PARCELWING_FROM_EMAIL'],'to' => $payload['email'],'template_alias' => 'welcome','template_params' => ['first_name' => $payload['first_name'] ?? 'friend',],]);$response->getBody()->write(json_encode(['id' => $emails[0]['id']]));return $response->withHeader('Content-Type', 'application/json');});$app->run();
Production notes
- Validate request payloads before passing values into templates.
- Move client construction into your dependency container as the app grows.
- Log
ParcelWingExceptiondetails with your request ID for easier support.