PHP code example of mattstein / craft-usesend

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

    

mattstein / craft-usesend example snippets




return [
    // Extra attempts after a connection failure, server error, or rate limit.
    'retries' => 2,
    // Milliseconds between attempts.
    'retryDelay' => 200,
    // Send embedded (cid:) images as ordinary attachments.
    'attachInlineImages' => false,
];

use craft\mail\Mailer;
use craft\mail\Message;
use mattstein\usesend\UseSendTransport;
use yii\base\Event;
use yii\mail\MailEvent;

Event::on(Mailer::class, Mailer::EVENT_BEFORE_SEND, function(MailEvent $event) {
    $message = $event->message;

    if (!$message instanceof Message || $message->key !== 'account_activation') {
        return;
    }

    $message->setHeader(UseSendTransport::TEMPLATE_ID_HEADER, 'cm8activation');
    $message->setHeader(UseSendTransport::VARIABLES_HEADER, json_encode([
        'name' => ($message->variables['user'] ?? null)?->friendlyName,
        'link' => (string) ($message->variables['link'] ?? ''),
    ]));
});

Craft::$app->getMailer()->compose()
    ->setTo($customer->email)
    ->setSubject('Your order')
    ->setHtmlBody($html)
    ->setHeader(UseSendTransport::SCHEDULED_AT_HEADER, $sendAt->format(DATE_ATOM))
    ->setHeader(UseSendTransport::IDEMPOTENCY_KEY_HEADER, 'order-confirmation-' . $order->id)
    ->send();

return [
    'retries' => 2,
];

use mattstein\usesend\UseSendTransport;

$sent = (new UseSendTransport(App::env('USESEND_API_KEY')))->send($email);
$emailId = $sent?->getMessageId();