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

Prerequisites

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

Install

composer require mailglyph/mailglyph-php

Initialize clients

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

use MailGlyph\MailGlyph;

$secretClient = new MailGlyph($_ENV['MAILGLYPH_SECRET_KEY']);
$publicClient = new MailGlyph($_ENV['MAILGLYPH_PUBLIC_KEY']);
Set environment variables:
export MAILGLYPH_SECRET_KEY="sk_your_secret_key"
export MAILGLYPH_PUBLIC_KEY="pk_your_public_key"

Configure base URL, timeout, and retries

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

use MailGlyph\MailGlyph;

$client = new MailGlyph($_ENV['MAILGLYPH_SECRET_KEY'], [
    'baseUrl' => 'https://api.mailglyph.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 MailGlyph\Exceptions\AuthenticationException;
use MailGlyph\MailGlyph;

$secretClient = new MailGlyph($_ENV['MAILGLYPH_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