Skip to main content
Use the official PHP SDK to call the Mailrify API from server-side apps.

Prerequisites

Before you start, make sure you have:
  • A Mailrify secret API key (sk_...) for server-side API requests
  • A Mailrify public API key (pk_...) for event tracking (/v1/track)
  • At least one verified sending domain for email sending
Setup links:

Install

composer require mailrify/mailrify-php

Initialize clients

Create one SDK client for your secret key and one for your public key:
<?php

use Mailrify\Mailrify;

$secretClient = new Mailrify($_ENV['MAILRIFY_SECRET_KEY']);
$publicClient = new Mailrify($_ENV['MAILRIFY_PUBLIC_KEY']);
Set environment variables:
export MAILRIFY_SECRET_KEY="sk_your_secret_key"
export MAILRIFY_PUBLIC_KEY="pk_your_public_key"

Configure base URL, timeout, and retries

Use this when testing against staging or local environments:
<?php

use Mailrify\Mailrify;

$client = new Mailrify($_ENV['MAILRIFY_SECRET_KEY'], [
    'baseUrl' => 'https://api.mailrify.com',
    'timeout' => 30000,
    'maxRetries' => 3,
    'userAgent' => 'your-app/1.0',
]);

Key usage rules

The PHP SDK enforces key restrictions automatically:
  • pk_* keys can only call events->track()
  • sk_* keys can call non-track endpoints, including events->listNames() and events->getNames()
<?php

use Mailrify\Exceptions\AuthenticationException;
use Mailrify\Mailrify;

$secretClient = new Mailrify($_ENV['MAILRIFY_SECRET_KEY']);

try {
    $secretClient->events->track([
        'email' => '[email protected]',
        'event' => 'purchase',
    ]);
} catch (AuthenticationException $error) {
    echo $error->getMessage();
}
Unlike the Node.js SDK, the PHP SDK does not expose a keyType property on the client.

Next pages