PHP

Send an email from PHP with the usenotix/notix-php package.

Send your first email from PHP with usenotix/notix-php, the official SDK. It needs PHP 8.1 or newer with the curl and json extensions. Using Laravel? Follow the Laravel quickstart instead.

Prerequisites

  1. Install the SDK

    composer require usenotix/notix-php
    
  2. Set your API key

    Keep the key in your environment, not in your code.

    NOTIX_API_KEY=notix_12345
    
  3. Send an email

    <?php
    
    require __DIR__ . '/vendor/autoload.php';
    
    use Notix\Notix;
    
    $notix = new Notix(getenv('NOTIX_API_KEY'));
    
    $email = $notix->emails->send([
        'from' => 'Acme <receipts@acme.com>',
        'to' => 'maria@example.com',
        'subject' => 'Your receipt for order 4471',
        'html' => '<p>Thanks for your order, Maria.</p>',
        'text' => 'Thanks for your order, Maria.',
    ]);
    
    echo $email['emailId'];
    
  4. Handle errors

    The SDK throws when the API answers with an error. Every exception extends Notix\Exception\NotixException, with a class per error, such as UnauthorizedException and RateLimitedException.

    use Notix\Exception\NotixException;
    
    try {
        $email = $notix->emails->send([
            'from' => 'receipts@acme.com',
            'to' => 'maria@example.com',
            'subject' => 'Your receipt',
            'html' => '<p>Thanks for your order.</p>',
        ]);
    } catch (NotixException $e) {
        echo $e->getHttpStatus() . ' ' . $e->getErrorCode() . ': ' . $e->getMessage();
    }
    

Next steps

  • Send many emails at once with batch sending: $notix->emails->batch([...]).
  • Add files with attachments.
  • Make retries safe with idempotency keys. Pass the key as the second argument: $notix->emails->send($payload, 'order-4471-receipt').
  • Hear about deliveries and bounces with webhooks.
Open in ClaudeOpen in ChatGPT