Before you start
Accounts are set up with you during onboarding. For the DKIM record and the rest of your sending setup, see transactional email deliverability.
Send email from PHP, step by step
The example sends a receipt and attaches the order and invoice IDs as metadata.
1. Store your API key
Keep the key out of your code. Set it as an environment variable on the server that calls tinyRelay.
export TINYRELAY_API_KEY="your-api-key"$env:TINYRELAY_API_KEY = "your-api-key"2. Build the request body
Build an associative array. Keys match the JSON field names exactly, including replyTo and campaignId.
<?php
$email = [
'subject' => 'Your receipt for order SO-1001',
'body' => '<p>Hi [FIRST_NAME], thanks for your order. Your receipt is below.</p>',
'sender' => [
'from' => ['name' => 'Acme Store', 'email' => 'orders@acme.com'],
'replyTo' => ['name' => 'Acme Support', 'email' => 'support@acme.com'],
],
'recipients' => [
'to' => [
[
'name' => 'Ann Lee',
'email' => 'ann@example.com',
'attributes' => ['FIRST_NAME' => 'Ann'],
],
],
],
'campaignId' => 'receipt_SO-1001',
'metadata' => ['order_id' => 'SO-1001', 'invoice_id' => 'INV-88'],
];3. POST it to /email/campaign
Both tabs define a send_email() function that returns the requestId and throws on anything other than 202.
<?php
// tinyrelay.php (needs the curl extension)
function send_email(array $email): string
{
$ch = curl_init('https://api.tinyemail.com/v1/relay/email/campaign');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 10,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'X-RELAY-ACCESS-TOKEN: ' . getenv('TINYRELAY_API_KEY'),
],
CURLOPT_POSTFIELDS => json_encode($email, JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR),
]);
$response = curl_exec($ch);
if ($response === false) {
throw new RuntimeException('tinyRelay request failed: ' . curl_error($ch));
}
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
if ($status !== 202) {
throw new RuntimeException("tinyRelay $status: $response");
}
return json_decode($response, true)['requestId'];
}<?php
// tinyrelay.php (composer require guzzlehttp/guzzle)
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
function send_email(array $email): string
{
$client = new Client([
'base_uri' => 'https://api.tinyemail.com/v1/relay/',
'timeout' => 10,
]);
try {
$response = $client->post('email/campaign', [
'headers' => ['X-RELAY-ACCESS-TOKEN' => getenv('TINYRELAY_API_KEY')],
'json' => $email,
]);
} catch (RequestException $e) {
// Guzzle throws on 4xx and 5xx by default
$res = $e->getResponse();
$detail = $res ? $res->getStatusCode() . ': ' . $res->getBody() : $e->getMessage();
throw new RuntimeException('tinyRelay ' . $detail, 0, $e);
}
return json_decode((string) $response->getBody(), true)['requestId'];
}Base URL from current site samples; confirm in the docs.
4. Check for a 202
send_email($email) returns the requestId. Store it on the order so a support agent can trace the receipt.
The 202 response
A request that passes validation returns 202. That means tinyRelay accepted the send; what happens to each email afterwards arrives as webhook events.
{
"requestId": "833c5151-5a98-4bad-a30f-7cc816352b7a",
"message": "SUCCESS"
}requestIdidentifies this send. Store it next to your own record.- If you did not set
campaignId, therequestIdis used as the campaign ID, and every webhook event for the send carries it. - A
202is not delivery. Watch forDELIVEREDorBOUNCEDevents.
Send receipts to many customers in one PHP request
Loop over your orders to build recipients.to. Each customer gets their own merge attributes and their own order_id in metadata.
<?php
$to = [];
foreach ($orders as $order) {
$to[] = [
'email' => $order['customer_email'],
'attributes' => ['FIRST_NAME' => $order['first_name'], 'ORDER_ID' => $order['id']],
'metadata' => ['order_id' => $order['id']],
];
}
$requestId = send_email([
'subject' => 'Your Acme receipt',
'body' => '<p>Hi [FIRST_NAME], here is your receipt for order [ORDER_ID].</p>',
'sender' => ['from' => ['name' => 'Acme Store', 'email' => 'orders@acme.com']],
'metadata' => ['tenant_id' => 'acme', 'app' => 'billing'],
'recipients' => ['to' => $to],
]);The rules, from the Send Email docs:
Request-level and recipient-level maps merge. The cap is on the merged map.
At most 500 recipients in one request may carry their own metadata.
Each metadata key can be up to 64 characters.
Values up to 256 characters: flat strings, numbers or booleans.
attributes are merge tags rendered into the body, such as [FIRST_NAME], and never echoed on webhooks. metadata is never rendered into the email and is always echoed on webhooks.
"attributes": { "FIRST_NAME": "Ann" } // in the email: "Hi Ann"
"metadata": { "user_id": "u_1042" } // on every webhook eventRequest-level and recipient-level maps merge, and the recipient's keys win.
// request { "tenant_id": "acme", "plan": "pro" }
// recipient { "plan": "team" }
// merged { "tenant_id": "acme", "plan": "team" }Values must be flat strings, numbers or booleans.
{ "order_id": "SO-1", "total": 42, "gift": true } // flat: fine
{ "order": { "id": "SO-1" } } // nested object: not flatMetadata is scoped to campaignId. Reusing a campaignId across batches replaces the earlier metadata, so use a unique one per batch or leave it out.
"campaignId": "password_reset_batch_0142" // unique per batch
// or leave it out: the requestId is used as the campaign IDPHP pitfall: empty arrays become JSON arrays
metadata and attributes must be JSON objects. json_encode turns an empty PHP array into [], not {}, so either leave the key out or pass an empty object.
<?php
json_encode(['metadata' => []]); // {"metadata":[]} an array, not an object
json_encode(['metadata' => new stdClass()]); // {"metadata":{}} an empty object
// Simplest: leave the key out when you have no metadataHandle delivery webhooks
tinyRelay posts an event to your webhook endpoint for each thing that happens to an email. There are seven event types:
- SENDSent to the receiving server
- DELIVEREDEmail delivered
- BOUNCEDHARD_BOUNCED or SOFT_BOUNCED · adds bounceCategory
- OPENOpened by the recipient
- CLICKA tracked link was clicked · adds url
- UNSUBSCRIBEUnsubscribe link clicked
- SPAMMarked as spam
SEND: the email was sent. Apps usually mark the message as sent against their record.
Each event carries eventType, receiver, requestId, campaignId and your merged metadata. BOUNCED events add bounceCategory, and CLICK events add the url that was clicked.
svix-id, svix-timestamp and svix-signature headers and delivered at least once, so verify each request and deduplicate on svix-id.Read email webhooks and delivery events, then verify webhook signatures. Bounce categories are explained in the email bounce codes reference; field-level detail is in the webhook docs.
Troubleshooting
The Send Email endpoint documents four error statuses. Match the status you get to the row below.
| Status | Meaning | What to check |
|---|---|---|
400 | MeaningBad Request | What to checkThe body failed validation. The response carries a message field; read it, fix the field it names and resend. |
401 | MeaningAPI key missing or invalid | What to checkCheck the header is named X-RELAY-ACCESS-TOKEN and holds the key issued at onboarding. If the key is right, confirm the server you are calling from has an IP on your allowlist. |
405 | MeaningMethod Not Allowed | What to checkSend a POST to /email/campaign. Other methods are refused. |
412 | MeaningMetadata limits broken | What to checkThe merged metadata map has more than 10 keys, or more than 500 recipients carry their own metadata. Nothing is sent; trim the metadata and resend. |
Full error schemas are in the Send Email API reference.
FAQ
No. The curl extension is enough. Guzzle is optional and gives you a shorter call with JSON encoding built in.
Check that metadata is a JSON object. An empty PHP array encodes as [], so leave the key out or pass new stdClass(). Also keep the merged map to 10 keys or fewer.
Yes, for the headers object in the request body. Send List-Unsubscribe and List-Unsubscribe-Post exactly as written, not in lowercase.
In an environment variable or your framework's secrets store, read with getenv or your config layer. Never commit it or expose it to the browser.
Next steps
- Laravel SMTP settings
- Transactional email API overview
- Verify webhook signatures in PHP
- One-click unsubscribe headers
- Send Email API reference (docs)