Sign in to fill these examples with your own keys. Sign in Start free trial

Send emails with PHP

Install the PHP SDK from GitHub and call emails.send.

What you'll build

A PHP script that sends a receipt email with the postshiba/postshiba client and prints the queued message.

Before you start

  • An API key with Send only access. Full access also works. See Create an API key. Support keys cannot send.
  • A verified sending domain. See Add and verify a domain. from must be an address on it.
  • A cluster that is ready to send (sending_ready is true). See Clusters.
  • PHP 8.1 or later and Composer.

Install

The package is on GitHub, not Packagist. Add the repository, then require it.

install.sh
1composer config repositories.postshiba vcs https://github.com/postshiba/postshiba-php 2composer require postshiba/postshiba:dev-main

Send your first email

Pass your API key to PostShiba, then call emails->send with an array. teamId is only needed for team-scoped calls such as listing clusters, which need a Full access key kept out of your deployed app. It is on the API keys page.

send.php
1<?php 2 3require __DIR__ . '/vendor/autoload.php'; 4 5use PostShiba\PostShiba; 6 7$client = new PostShiba("YOUR_API_KEY", teamId: "KjkAJW"); 8 9$sent = $client->emails->send([ 10 'from' => 'hello@mail.example.com', 11 'to' => ['you@example.com'], 12 'reply_to' => 'support@example.com', 13 'subject' => 'Your receipt', 14 'text' => 'Thanks for your order.', 15 'html' => '<p>Thanks for your order.</p>', 16 'unique_args' => ['order_id' => 'ord_123'], 17]); 18 19print_r($sent);

emails->send returns the JSON body of the 201 response as an associative array. The message then shows under Events in the dashboard.

response.json
1{ 2 "queued": true, 3 "message_id": "abc@capsule.test", 4 "from": "hello@mail.example.com", 5 "to": ["you@example.com"], 6 "subject": "Your receipt", 7 "unique_args": {"order_id": "ord_123"} 8}

Pin a cluster

Without a pin, PostShiba uses your first cluster that is ready to send. Pass clusterId to choose one.

pin.php
1$client->emails->send($message, clusterId: "NmQpXr");

Attachments and templates

Attach a file

Add attachments with filename, content_type, and base64 content. Up to 10 files, 18 MB in total. See Attachments for allowed types.

attachment.php
1$client->emails->send([ 2 'from' => 'hello@mail.example.com', 3 'to' => ['you@example.com'], 4 'subject' => 'Your invoice', 5 'text' => 'Your invoice is attached.', 6 'attachments' => [ 7 [ 8 'filename' => 'invoice.pdf', 9 'content_type' => 'application/pdf', 10 'content' => base64_encode(file_get_contents('invoice.pdf')), 11 ], 12 ], 13]);

Send a published template

Pass template with the alias or public id and its variables. Leave out html and text.

template.php
1$client->emails->send([ 2 'from' => 'hello@mail.example.com', 3 'to' => ['you@example.com'], 4 'template' => ['id' => 'welcome', 'variables' => ['name' => 'Ada']], 5]);

Keep the key safe

Use a Send only key in your app. It can send email and call GET /api/v1/users/me. Every other request returns 403 forbidden.

If this key leaks Send only Full access
Send mail from your verified domains Yes Yes
Read events, recipients, templates, and sent messages No Yes
See SMTP passwords and webhook secrets No Yes
Change clusters, domains, credentials, webhooks, and suppressions No Yes
  • Keep Full access keys for setup scripts, the CLI, and MCP. Don't deploy one with your app.
  • Read the key from an environment variable, such as new PostShiba(getenv('POSTSHIBA_API_KEY')). Don't commit it, log it, or put it in a URL. Keep any file that holds it outside the web root.
  • Send from your server. Never put a key, even a Send only key, in browser or mobile code.
  • Create one key per app and environment. If a key leaks, delete it on API keys and create a new one. Deleting stops it straight away.

Troubleshooting

A response outside 2xx throws PostShiba\Error with the public properties error, field, and status. getMessage() holds the explanation.

errors.php
1try { 2 $client->emails->send($message); 3} catch (PostShiba\Error $e) { 4 error_log("{$e->status} {$e->error} {$e->field} {$e->getMessage()}"); 5 throw $e; 6}
Status error What to do
401 Invalid token or no user signed in The API key is missing, mistyped, or deleted. Copy it again from API keys.
403 forbidden Support keys cannot send. Use a Send only or Full access key. A Send only key gets this on any call other than sending.
403 domain_unverified from is not on a verified domain.
403 kyc_required Your team is pending production approval. Until then, send only to team members, live inbox addresses, and inbox forward-to addresses.
422 invalid field names the problem, such as a missing to, an unpublished template, or a rejected attachment.
429 throttled The cluster hit its hourly throttle or 24-hour cap. The client does not retry, and retrying right away hits the same cap. See What happens at your cap.
429 rate_limited More than 300 send requests in one minute on one API key. Slow down.

Every other status is on Errors.

Next steps