PHP code example of mailhive / mailhive-php

1. Go to this page and download the library: Download mailhive/mailhive-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/ */

    

mailhive / mailhive-php example snippets


use Mailhive\Mailhive;

$mailhive = new Mailhive(); // reads MAILHIVE_API_KEY

$email = $mailhive->emails->send([
    'from' => 'Acme <[email protected]>',
    'to' => '[email protected]',
    'subject' => 'Your receipt',
    'html' => '<p>Thanks for your order.</p>',
]);
echo $email['id'];

'attachments' => [
    ['filename' => 'invoice.pdf', 'content' => file_get_contents('invoice.pdf'), 'content_type' => 'application/pdf'],
],

$mailhive->emails->sendBatch([$email1, $email2]); // up to 100; all accepted or none
$mailhive->emails->get($id);                      // ['status' => 'delivered', …]

$verdicts = $mailhive->emails->validate(['[email protected]']);
// [['email' => '[email protected]', 'valid' => false, 'reason' => 'domain_typo', 'suggestion' => '[email protected]', 'message' => 'The domain looks mistyped.', …]]

$contact = $mailhive->contacts->upsert($audienceId, [
    'email' => '[email protected]',
    'first_name' => 'Ada',
    'fields' => ['plan' => 'pro'],
]);

$mailhive->contacts->upsert($audienceId, [
    'email' => '[email protected]',
    'double_opt_in' => ['from' => 'Acme News <[email protected]>', 'subject' => 'Confirm your subscription'],
]);

// config/services.php
'mailhive' => ['key' => env('MAILHIVE_API_KEY')],

// config/mail.php, under 'mailers'
'mailhive' => ['transport' => 'mailhive'],

public function headers(): Headers
{
    return new Headers(text: ['X-Mailhive-Idempotency-Key' => "order-{$this->order->id}-receipt"]);
}

$mailhive->emails->send($email, ['idempotency_key' => "order-{$order->id}-receipt"]);

use Mailhive\Exception\RateLimitException;
use Mailhive\Exception\ValidationException;

try {
    $mailhive->emails->send($email);
} catch (ValidationException $e) {
    echo $e->errorCode(), $e->getMessage(), json_encode($e->details()), $e->requestId();
} catch (RateLimitException $e) {
    // $e->errorCode() === 'monthly_quota_reached', or $e->retryAfter()
}

use Mailhive\Exception\WebhookVerificationException;
use Mailhive\Webhook;

try {
    $event = Webhook::verify(
        file_get_contents('php://input'),   // Laravel: $request->getContent()
        $_SERVER['HTTP_MAILHIVE_SIGNATURE'] ?? null,  // Laravel: $request->header('Mailhive-Signature')
        getenv('MAILHIVE_WEBHOOK_SECRET'),
    );
} catch (WebhookVerificationException $e) {
    http_response_code(400);
    exit;
}

new Mailhive('mhs_…', [
    'base_url' => 'https://api-beta.mailhive.africa/v1', // default: MAILHIVE_BASE_URL, then production
    'timeout' => 30.0,
    'max_retries' => 2,
]);
sh
git subtree split --prefix php -b php-split
git push [email protected]:Naszat/mailhive-php.git php-split:main
git tag vX.Y.Z php-split && git push [email protected]:Naszat/mailhive-php.git vX.Y.Z