Send

Send Email with PHP

Send transactional email from PHP using a REST email API or SMTP relay, with authentication, retries, and webhook tracking.

Updated

TL;DR

For new PHP code, call the REST email API from a small mail adapter. For older PHP apps, use SMTP relay through a maintained mailer library and keep delivery status in webhooks.

What you will learn

  • Send transactional email from PHP over HTTP
  • Know when SMTP is still the right PHP integration
  • Track final delivery state through webhooks

Recommended approach

Use a small application mail adapter. The rest of your PHP code calls that adapter with normalized fields, and the adapter decides whether to send through Postscale REST API or SMTP relay.

This keeps provider details out of controllers, jobs, and model hooks.

REST API example

<?php

$payload = [
    "from" => "Acme <alerts@example.com>",
    "to" => ["user@example.com"],
    "subject" => "Welcome",
    "html_body" => "<p>Hello from Acme</p>",
    "text_body" => "Hello from Acme",
    "tags" => ["onboarding"],
];

$ch = curl_init("https://api.postscale.io/v1/send");
curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => [
        "Authorization: Bearer " . getenv("POSTSCALE_API_KEY"),
        "Content-Type: application/json",
    ],
    CURLOPT_POSTFIELDS => json_encode($payload, JSON_THROW_ON_ERROR),
]);

$response = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);

if ($status >= 400) {
    throw new RuntimeException("Email send failed: " . $response);
}

Store your own application event ID with the message so a webhook can update the right user, order, or notification later.

SMTP relay for legacy PHP

Use SMTP when the app already has mail settings or uses a framework/plugin that expects SMTP.

SettingValue
Hostsmtp.postscale.io
Port587
SecuritySTARTTLS
UsernameSMTP credential username
PasswordSMTP credential secret

Avoid unauthenticated local sendmail or PHP mail() for production transactional email. Those paths make deliverability, DKIM, logs, suppressions, and support harder.

Retry rules

  1. Retry network timeouts and temporary 5xx API responses with backoff.
  2. Do not retry validation errors until the payload is fixed.
  3. Do not generate a new password reset token on every retry.
  4. Use webhook retries and idempotency for final state.

Migration checklist

  1. Verify the From domain.
  2. Publish SPF, DKIM, and DMARC-compatible records.
  3. Import suppressions from the old provider.
  4. Move one transactional workflow first.
  5. Confirm delivery, bounce, defer, and complaint webhooks.
  6. Expand after bounce rates and support tickets look normal.

References

Frequently asked questions

Should PHP apps use mail() for transactional email?
No for production product mail. Use an authenticated email API or SMTP relay so authentication, suppressions, logs, and webhooks work reliably.
Can I use SMTP first and move to the API later?
Yes. That is a common staged migration for older PHP products.

Related guides

Put the guide into production

Postscale brings sending, inbound processing, DMARC reporting, and masked addresses behind one API so the operational pieces stay connected.