Send email from PHP

Send email from any PHP app with TrivSend's SDK, or point PHPMailer or Symfony Mailer at TrivSend's SMTP server. Every email is signed for your own domain and logged with its delivery result.

Start free · PHP in the docs

Install and send

bash
composer require trivsend/trivsend
php
use TrivSend\TrivSend;
use TrivSend\TrivSendException;

$trivsend = new TrivSend(); // reads TRIVSEND_API_KEY

try {
    $email = $trivsend->emails->send([
        'from' => 'Acme <noreply@notify.example.com>',
        'to' => 'asha@example.com',
        'subject' => 'Your order has shipped',
        'html' => '<p>Order #1042 is on its way.</p>',
        'text' => 'Order #1042 is on its way.',
    ], idempotencyKey: 'shipped-1042');
    echo $email['id'];
} catch (TrivSendException $e) {
    echo $e->name, ': ', $e->apiMessage();
}

The SDK needs PHP 8.1 or later and nothing else. It uses the API's own field names, so the API reference applies as written.

PHPMailer and other SMTP libraries

Already using PHPMailer? Change the server settings and keep the rest of your code:

php
use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer(true);
$mail->isSMTP();
$mail->Host = 'smtp.trivsend.com';
$mail->Port = 587;
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
$mail->SMTPAuth = true;
$mail->Username = 'trivsend';
$mail->Password = getenv('TRIVSEND_API_KEY');

$mail->setFrom('noreply@notify.example.com', 'Acme');
$mail->addAddress('asha@example.com');
$mail->Subject = 'TrivSend test';
$mail->Body = 'It works';
$mail->send();

Symfony Mailer takes the same settings as one DSN: MAILER_DSN=smtp://trivsend:ts_...@smtp.trivsend.com:587. For Laravel, see Laravel email; for WordPress, WordPress SMTP.

Over SMTP, add the header X-TrivSend-Priority: high to sign-in codes and password resets so they go out before bulk mail (SMTP relay).

Know what happened to each email

TrivSend calls your app when an email bounces, is marked as spam or fails. Verify the signature with the SDK, using the raw body:

php
use TrivSend\Webhook;

$event = Webhook::verify(file_get_contents('php://input'), getallheaders(), getenv('TRIVSEND_WEBHOOK_SECRET'));
if ($event['type'] === 'email.bounced') {
    // mark the address as bad in your database
}
http_response_code(204);

Webhook::verify throws InvalidArgumentException when the signature doesn't match or the timestamp is more than 5 minutes old (webhooks).

Why TrivSend for PHP apps

Also see: SMTP relay · Transactional email · Email API for India