PHP code example of mailengin / mailengin-php

1. Go to this page and download the library: Download mailengin/mailengin-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mailengin / mailengin-php example snippets




declare(strict_types=1);

use MailEngin\Model\SendEmailRequest;

$client = new MailEnginClient($_ENV['MAILENGIN_API_KEY']);

$email = $client->emails()->send(new SendEmailRequest(
    to: '[email protected]',
    fromEmail: '[email protected]',
    templateName: 'welcome-email',
    variables: ['first_name' => 'Asha'],
));

echo $email->id;

use MailEngin\Model\SendEmailRequest;

$email = $client->emails()->send(new SendEmailRequest(
    to: '[email protected]',
    fromEmail: '[email protected]',
    templateName: 'account-verification',
    variables: [
        'first_name' => 'Asha',
        'verification_url' => 'https://yourapp.com/verify/token',
    ],
    replyToMailEngin: true,
));

printf("Queued email %s at %s\n", $email->id, $email->createdAt);

use MailEngin\Model\BulkRecipient;
use MailEngin\Model\SendBulkEmailRequest;

$job = $client->emails()->sendBulk(new SendBulkEmailRequest(
    to: [
        new BulkRecipient(
            email: '[email protected]',
            variables: ['first_name' => 'Asha'],
        ),
        new BulkRecipient(
            email: '[email protected]',
            variables: ['first_name' => 'Ben'],
        ),
    ],
    fromEmail: '[email protected]',
    templateName: 'product-update',
    variables: ['product_name' => 'MailEngin'],
));

printf("Queued %d recipients in job %s\n", $job->queuedCount, $job->jobId);

$job = $client->emails()->sendBulk(new SendBulkEmailRequest(
    to: ['[email protected]', '[email protected]'],
    templateName: 'maintenance-notice',
));

$email = $client->emails()->send(new SendEmailRequest(
    to: '[email protected]',
    fromEmail: '[email protected]',
    subject: 'Your report is ready',
    html: '<h1>Report ready</h1><p>You can download it now.</p>',
));

use MailEngin\Exception\MailEnginError;
use MailEngin\Model\SendEmailRequest;

try {
    $client->emails()->send(new SendEmailRequest(
        to: '[email protected]',
        templateName: 'welcome-email',
    ));
} catch (MailEnginError $error) {
    echo $error->getMessage();
    var_dump($error->status);       // HTTP status, when available
    var_dump($error->errorCode);    // Machine-readable error code
    var_dump($error->requestId);    // Include when contacting support
    var_dump($error->retryAfter);   // Seconds supplied with HTTP 429
    var_dump($error->body);         // Parsed JSON or response text
    var_dump($error->isRetryable());
}

use MailEngin\MailEnginClient;

$client = new MailEnginClient(
    apiKey: $_ENV['MAILENGIN_API_KEY'],
    baseUrl: 'https://api.mailengin.app',
    timeout: 15.0,
);

use Illuminate\Contracts\Foundation\Application;
use MailEngin\MailEnginClient;

$this->app->singleton(MailEnginClient::class, function (Application $app): MailEnginClient {
    return new MailEnginClient((string) config('services.mailengin.api_key'));
});

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use MailEngin\MailEnginClient;

$mock = new MockHandler([
    new Response(200, ['Content-Type' => 'application/json'], json_encode([
        'id' => 'email_123',
        'from' => '[email protected]',
        'to' => '[email protected]',
        'template_name' => 'welcome-email',
        'created_at' => '2026-08-31T12:00:00Z',
    ], JSON_THROW_ON_ERROR)),
]);

$httpClient = new Client(['handler' => HandlerStack::create($mock)]);
$client = new MailEnginClient('test_key', httpClient: $httpClient);
bash
composer