Send Email from PHP with the Robotomail API
A PHP cURL sending example with private credentials, error handling, attachment guidance and a path for incoming replies.
John Joubert
Founder, Robotomail

Table of contents
A PHP application can submit email with mail(), an SMTP library or an HTTP API. The useful distinction is what the application needs after submission: delivery feedback, an address for replies and a way to connect those replies to a workflow.
This guide uses Robotomail's HTTPS API from server-side PHP. It explains the credentials, request fields and error handling without assuming that a successful function call means a person received the message. See the email API for agents.
Prepare the account and mailbox
Create the account, complete verification and obtain an API key. List the account's mailboxes through GET /v1/mailboxes to obtain the mailbox ID for this workflow. Use a recipient you control for the first test.
Store ROBOTOMAIL_API_KEY, ROBOTOMAIL_MAILBOX_ID and TEST_RECIPIENT in the server environment or secret store. Never place a real API key in a public PHP example, client-side script or model prompt.
On Free, set TEST_RECIPIENT to your verified signup email address. Free includes one mailbox, 10 sends and 10 receives per calendar month with that address only. Upgrade to contact other recipients or use a custom domain.
Send a message with PHP and cURL
This example uses PHP with the cURL extension and JSON exception support:
<?php
$key = getenv('ROBOTOMAIL_API_KEY');
$mailboxId = getenv('ROBOTOMAIL_MAILBOX_ID');
$recipient = getenv('TEST_RECIPIENT');
if (!$key || !$mailboxId || !$recipient) {
throw new RuntimeException('Missing email configuration');
}
$payload = json_encode([
'to' => [$recipient],
'subject' => 'Hello from PHP',
'bodyText' => 'This is a controlled test. Reply here with any questions.',
], JSON_THROW_ON_ERROR);
$handle = curl_init(
'https://api.robotomail.com/v1/mailboxes/'
. rawurlencode($mailboxId) . '/messages'
);
curl_setopt_array($handle, [
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTPHEADER => [
'Authorization: Bearer ' . $key,
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => $payload,
]);
$result = curl_exec($handle);
$status = curl_getinfo($handle, CURLINFO_HTTP_CODE);
curl_close($handle);
if ($result === false) {
throw new RuntimeException('Network failure; reconcile before retrying');
}
if ($status < 200 || $status >= 300) {
throw new RuntimeException('Send rejected with HTTP ' . $status);
}
$message = json_decode($result, true, 512, JSON_THROW_ON_ERROR)['message'];
echo 'Accepted message ID: ' . $message['id'] . PHP_EOL;
Keep TLS verification enabled. The message body uses bodyText; optional HTML belongs in bodyHtml. The mailbox ID in the URL selects the sending mailbox. Do not invent a /send endpoint or copy fields from a different provider's API.
HTML, attachments and user input
Retain a complete text version when adding HTML. Escape untrusted values before rendering them into HTML and avoid using incoming message content as application markup.
For an attachment, upload the file first and use the returned identifier in attachments. The upload endpoint requires a full-access key; check file and account limits. See sending attachments.
If a website form calls this code, authenticate or otherwise authorize the operation, restrict the destination and add anti-abuse controls. Do not let a visitor turn your key into an unrestricted sender. Contact-form intake explains the boundary.
What mail() and SMTP libraries provide
PHP's mail() passes a message to the configured mail system. A successful return indicates acceptance for that step, not final delivery. The hosting environment must have a correctly configured transport and sender identity.
PHPMailer and similar SMTP libraries add MIME construction, attachments and authenticated SMTP connections. They still need a provider's host, port, TLS and credential settings. Keep those credentials secret and check the library's official documentation for the version in use.
Robotomail's documented integration is an HTTP API. Its API key should be used in the Authorization header, not pasted into an SMTP-password field. The SMTP-to-API guide describes the change in integration shape.
Connect incoming replies
A mailbox makes the PHP application part of a conversation. Register a webhook for incoming mail, or use polling where a public webhook endpoint is not suitable.
Verify webhook HMAC using the raw request bytes before parsing JSON. Record processed events or message IDs and queue slow processing. Fetch the relevant thread, validate the proposed response and send with the original RFC Message-ID in inReplyTo.
Your application owns ticket state, account authorization and the agent's business rules. Robotomail handles the email interface. Follow receive and reply for the current payload schema.
Diagnose delivery failures
Record the accepted message ID and follow later delivery or bounce events. Inspect account verification, domain authentication, recipient validity and quotas when a request fails. Avoid logging keys, full private bodies or activation tokens.
If a request times out after submission, reconcile its outcome before sending again. A retry can create duplicate customer messages. Respect suppressions and complaints, and distinguish recipient-server delivery from a human read.
Use email delivery troubleshooting before scaling and current pricing to size the workflow around sending, inbound volume, storage and mailbox count.
Give your AI agent a real email address
Create a mailbox, connect your agent and test a conversation. Send, receive and retrieve the thread through one API.
Related posts

10 Email Automation Best Practices for 2026
Master our top 10 email automation best practices for AI agents. Learn about DKIM, webhooks, threading, and more for secure, reliable agent-native email.
Read post
Inbound Package Meaning: From Warehouse Box to API Payload
Confused by the 'inbound package meaning'? Learn the critical difference between the logistics term and the API payload concept AI developers must know.
Read post
What Is Email Api: A Developer's Guide for 2026
Discover what is email api and how it works for developers. This 2026 guide covers REST vs SMTP, data flows, security, and AI use cases.
Read post