> ## Documentation Index
> Fetch the complete documentation index at: https://docs.mailglyph.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Overview

> Install and initialize the MailGlyph PHP SDK.

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

* Packagist package: [mailglyph/mailglyph-php](https://packagist.org/packages/mailglyph/mailglyph-php)
* GitHub repository: [MailGlyph PHP SDK](https://github.com/MailGlyph/mailglyph-php)

## 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:

* [MailGlyph API keys dashboard](https://app.mailglyph.com/api-keys)
* [MailGlyph domains dashboard](https://app.mailglyph.com/domains)
* [API keys guide](https://docs.mailglyph.com/guides/api-keys.md)
* [Domain verification guide](https://docs.mailglyph.com/guides/verifying-domains.md)

## Install

```bash theme={null}
composer require mailglyph/mailglyph-php
```

## Initialize clients

Create one SDK client for your secret key and one for your public key:

```php theme={null}
<?php

use MailGlyph\MailGlyph;

$secretClient = new MailGlyph($_ENV['MAILGLYPH_SECRET_KEY']);
$publicClient = new MailGlyph($_ENV['MAILGLYPH_PUBLIC_KEY']);
```

Set environment variables:

```bash theme={null}
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 theme={null}
<?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 theme={null}
<?php

use MailGlyph\Exceptions\AuthenticationException;
use MailGlyph\MailGlyph;

$secretClient = new MailGlyph($_ENV['MAILGLYPH_SECRET_KEY']);

try {
    $secretClient->events->track([
        'email' => 'user@example.com',
        '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

* [Emails examples](https://docs.mailglyph.com/sdks/php/emails.md)
* [Email Validation examples](https://docs.mailglyph.com/sdks/php/email-validation.md)
* [Events examples](https://docs.mailglyph.com/sdks/php/events.md)
* [Contacts examples](https://docs.mailglyph.com/sdks/php/contacts.md)
* [Segments examples](https://docs.mailglyph.com/sdks/php/segments.md)
* [Campaigns examples](https://docs.mailglyph.com/sdks/php/campaigns.md)
* [Error handling](https://docs.mailglyph.com/sdks/php/errors.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.