PHP Transactional Email API (2026): Send App Mail from Vanilla PHP with POST /emails
Send transactional email from vanilla PHP with Rovela’s HTTP API: a cURL helper, environment variables for the Bearer key, Idempotency-Key, and documented errors. Not a Laravel tutorial and not a language tour.
Vanilla PHP already speaks HTTP; transactional email should too
A PHP application already posts JSON to payment providers, writes rows to a database, and returns a receipt page. The natural way to send transactional email — a payment receipt, a “your export is ready” note, a shipping update, a security alert — is the same pattern: one HTTPS POST with a Bearer key, not mail() or PHPMailer pointed at a mailbox.
Rovela’s Email API is built for that. There is no official SDK and no Composer package you have to pin. The base URL is https://api.rovela.dev. You authenticate with Authorization: Bearer re_… (Authentication) and queue a message with POST /emails. The response is a message id. HTTP 200 means the message was accepted and queued, not that it has already arrived (Quick Start).
This is not a Laravel tutorial, and it is not a language-tour rewrite. Everyday Laravel sending already has its own guide (Laravel transactional email API); one-time codes belong in the Laravel OTP post, not here. If you only wanted a first request in PHP syntax, the send email in PHP tour is already published. This article is vanilla PHP app mail: a small helper, environment variables for the key, an Idempotency-Key on anything a retry might repeat, and documented error handling around the {statusCode, name, message} envelope.
If you are still choosing between HTTP and SMTP, read Email API vs SMTP. Rovela’s SMTP is mailbox SMTP for mail apps (mx.rovela.dev:465, a device credential, send-as-self, 30 messages a minute from one client address). Programmatic volume from a PHP process belongs on POST /emails.
What you need before the first PHP send
The first-email guide and the Quick Start are the same five steps, whether your caller is curl on the command line or PHP’s cURL extension:
- Create an account. You start on the free plan, which sends real mail. There is no separate sandbox or test key.
- Add and verify a sending domain (a subdomain such as
mail.example.comis a common choice). You cannot send from a domain you have not verified; an unverified or foreignfromis refused with a 400 that names the domain. See the domain setup guide and what domain email is. - Create an API key. A
sending_accesskey can send and nothing else — the right default for production. Afull_accesskey can also read messages and events, and manage domains, templates, and webhooks. The full key is shown once. - Send with
POST /emails. - Confirm delivery in the dashboard, with
GET /emails/:idorGET /emails/:id/events(those reads needfull_accessor a dashboard session), or with a webhook.
from is a string in the documented form "Acme <hello@yourdomain.com>". The address must sit on a verified sending domain. That is the same rule that makes domain email trustworthy: you prove you control the domain before you speak as it. How the display name and the address work is covered in the From name guide.
At least one of html, text, or template is required. Send both html and text where you can — many clients preview the text part (HTML email API).
A cURL helper with environment variables
Keep the key on the server. Never put a Rovela key in a browser bundle, a mobile app, or a public repository. A leaked sending_access key can send mail as your domain. Environment variables (or a server-only config file that is not committed) are the right place in a PHP app.
The helper below uses PHP’s cURL extension, which most application servers already have enabled. The JSON body and headers are identical if you prefer file_get_contents with a stream context; there is still no official SDK to install.
<?php
// emails.php
class EmailsError extends RuntimeException
{
public int $statusCode;
public array $payload;
public function __construct(int $statusCode, array $payload)
{
$this->statusCode = $statusCode;
$this->payload = $payload; // {statusCode, name, message}
parent::__construct($payload['message'] ?? ('HTTP ' . $statusCode));
}
}
function rovela_send_email(array $input): array
{
$payload = [
'from' => getenv('ROVELA_FROM') ?: 'Acme <hello@mail.acme.com>',
'to' => $input['to'],
'subject' => $input['subject'],
'html' => $input['html'],
'text' => $input['text'],
];
if (!empty($input['reply_to'])) {
$payload['reply_to'] = $input['reply_to'];
}
if (array_key_exists('tags', $input)) {
$payload['tags'] = $input['tags'];
}
$headers = [
'Authorization: Bearer ' . getenv('ROVELA_API_KEY'), // sending_access
'Content-Type: application/json',
];
if (!empty($input['idempotency_key'])) {
$headers[] = 'Idempotency-Key: ' . $input['idempotency_key'];
}
$base = rtrim(getenv('ROVELA_BASE_URL') ?: 'https://api.rovela.dev', '/');
$ch = curl_init($base . '/emails');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
]);
$raw = curl_exec($ch);
if ($raw === false) {
$err = curl_error($ch);
curl_close($ch);
throw new RuntimeException($err);
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
$decoded = json_decode($raw, true) ?? [];
if ($code !== 200) {
throw new EmailsError($code, $decoded);
}
return $decoded; // {"object":"email","id":"..."}
}
The documented success body is {"object": "email", "id": "9f1c2e40-..."}. Store that id on the payment or export job. Do not invent other response fields.
The same request in curl is useful when you are checking the domain and the key before you wire the helper:
curl -X POST https://api.rovela.dev/emails \
-H "Authorization: Bearer re_your_api_key" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: receipt-pay_1042" \
-d '{
"from": "Acme <hello@mail.acme.com>",
"to": "alice@example.com",
"subject": "Your payment receipt",
"html": "<p>We received payment for order ORD-1042.</p>",
"text": "We received payment for order ORD-1042."
}'
A checkout script that sends after the payment is saved
Call the helper after the work that matters has already succeeded — the payment row is persisted, the export file is on disk, the shipment is marked dispatched. If the request should return a receipt page quickly, enqueue the send and reuse the same Idempotency-Key whenever that job is retried. The header may be up to 256 characters and is remembered for 24 hours (Idempotency-Key).
<?php
require __DIR__ . '/emails.php';
// $payment has already been written to the database.
function notify_payment_received(array $payment): array
{
return rovela_send_email([
'to' => $payment['email'],
'subject' => 'Your payment receipt',
'html' => '<p>We received payment for order ' . htmlspecialchars($payment['order_id'], ENT_QUOTES) . '.</p>',
'text' => 'We received payment for order ' . $payment['order_id'] . '.',
'reply_to' => 'billing@mail.acme.com',
'tags' => [
'kind' => 'payment_receipt',
'orderId' => $payment['order_id'],
],
'idempotency_key' => 'receipt-' . $payment['order_id'],
]);
}
tags are optional labels returned with the message; they are not a substitute for Idempotency-Key and they are not topic_id. The Email API tags guide covers the documented shapes. reply_to is the right field when a human should be able to answer; see Email API reply-to. Recipients across to, cc, and bcc are capped at 50, and quota is charged per recipient (cc and bcc).
Do not invent a sandbox. Free-plan keys send real mail. During development, use a verified staging subdomain and recipients you control (how to test a transactional email API).
Errors you should handle in the helper
Every error uses the same shape (Quick Start, Email API error handling):
{
"statusCode": 400,
"name": "Bad Request",
"message": "html or text body is required"
}
Documented cases you will actually hit from a PHP script:
- 400 — a missing required field, an unverified or foreign
fromdomain, a template that does not exist, a base64 attachment that does not decode, or more than 50 recipients acrossto,cc, andbcc. - 401 — missing or invalid key, or a
sending_accesskey used to read, reschedule, or cancel a message. Reading events is afull_access(or session) operation. - 429 — rate limit exceeded. Each entry in a batch counts individually, so a large batch can trip this partway through.
Surface the message field in your own logs as-is. Do not invent a /logs API; delivery history lives in the dashboard and, with a full_access key, on GET /emails/:id and GET /emails/:id/events.
A 403 can appear on management endpoints when the credential is valid but lacks permission — for example, a sending_access key used against the Domains API. That is a different failure from a 401. See Email API authentication and the sending domains API guide in this batch.
Delivery is asynchronous — do not poll from the request path
A 200 from POST /emails is not a delivery receipt. Confirm later:
- The Emails view in the dashboard.
GET /emails/:idandGET /emails/:id/eventswith afull_accesskey. You are looking for accepted, delivered, or bounced events on that message (Email delivery status API).- A webhook for anything in production. Verify the HMAC-SHA256 signature (webhook security). Event types include sent, delivered, delayed, bounced, complained, opened, and clicked.
Do not turn a checkout script into a polling loop against /emails/:id/events. The docs are explicit: the events endpoint is useful for debugging a single message; systematic handling belongs on a webhook.
POST /emails/batch accepts up to 100 messages, each with the same schema, processed independently. template and topic_id are rejected on the batch endpoint rather than ignored — send those through POST /emails. If you schedule with scheduled_at, reading, rescheduling, and cancelling need full_access or a dashboard session; a sending_access key can only send. The dedicated scheduled email and batch transactional email guides cover those flows in full; this article will not redo them.
Templates are available if you would rather store HTML in Rovela and send { id, variables } (Templates API, templates guide). template is mutually exclusive with html and text. You can also render a PHP template on the server and pass the resulting strings as html and text — that is ordinary PHP, not a Rovela SDK.
What not to do in a PHP codebase
Do not point mail(), PHPMailer, or Symfony Mailer at mx.rovela.dev and send application volume that way. That path is for mailboxes and mail apps. A mailbox can send only as its own address, and the documented client limit is 30 messages a minute. You also lose the HTTP API’s tracking, logs, and webhooks (SMTP, Email API vs SMTP relay). If you are on Laravel, use the Laravel guide instead of this one; the HTTP call is the same, the wiring is not.
Do not create a “Rovela SDK” package that invents endpoints, official clients, numeric HTTP rate (req/s) figures, a sandbox flag, or a dedicated IP. Call the documented HTTP API with cURL or a stream context.
Do not put the key in query parameters, in frontend code, or in a committed .env that ships to git. Create a sending_access key for the PHP service. If you send for several customers from one organization, a key can be restricted to a single domain (Authentication, Email API for multi-tenant SaaS).
Do not treat a fire-and-forget register_shutdown_function as a substitute for idempotency. A background call that retries after a timeout still needs an Idempotency-Key. A background call also does not confirm delivery; only the events API or a webhook does. For retry policy around timeouts and 429s, see Email API retries.
A compact PHP checklist
- Verify the sending domain before any production
from. - Store a
sending_accesskey in the environment; constructAuthorization: Bearer …in server-side PHP. - Send both
htmlandtext. Setreply_towhen a human should be able to answer. - Put an
Idempotency-Keyon any send a queue, payment webhook, or double submit might repeat. - Handle 400, 401, and 429 from the documented error envelope. Remember that 200 means queued.
- Store the returned message
idon the payment or export job. - Subscribe to webhooks for delivery, bounces, and complaints; use
GET /emails/:id/eventsonly to debug a single message. - Leave mailbox SMTP to people and mail apps. If you are writing Laravel, use the Laravel guide.
Once that helper is in place, every other PHP script can call it the same way — receipts, security alerts, “your export is ready” notes — without growing a second email stack. That is the point of a transactional email API: one authenticated HTTP call from the application you already run. For a first request without PHP at all, the curl sending guide and HTTP email API pages are the shortest path.