Skip to content

Latest commit

 

History

History
90 lines (67 loc) · 1.93 KB

File metadata and controls

90 lines (67 loc) · 1.93 KB

SQS Backend Usage

Add email job to queue

use Da\Mailer\Model\MailMessage;
use Da\Mailer\Queue\MailQueue;
use Da\Mailer\Queue\Backend\Sqs\SqsMailJob;
use Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreAdapter;
use Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreConnection;

$message = new MailMessage([
    'from' => '[email protected]',
    'to' => '[email protected]',
    'subject' => 'What is up?',
    'bodyText' => 'New mailing'
]);

$conn = new SqsQueueStoreConnection([
    'key' => 'AKIA...',
    'secret' => '...',
    'region' => 'eu-west-1', // https://docs.aws.amazon.com/general/latest/gr/rande.html
]);

$adapter = new SqsQueueStoreAdapter($conn);

$queue = new MailQueue($adapter);

$job = new SqsMailJob([
    'message' => json_encode($message),
]);

if (!$queue->enqueue($job)) {
    // ... queue operation failed
}

Fetch email job from queue

use Da\Mailer\Queue\MailQueue;
use Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreAdapter;
use Da\Mailer\Queue\Backend\Sqs\SqsQueueStoreConnection;

$conn = new SqsQueueStoreConnection([
    'key' => 'AKIA...',
    'secret' => '...',
    'region' => 'eu-west-1', // https://docs.aws.amazon.com/general/latest/gr/rande.html
]);

$adapter = new SqsQueueStoreAdapter($conn);

$queue = new MailQueue($adapter);

if (($job = $queue->dequeue()) !== null) {
    // ... do something with received job
    // ... send it using `mail()` function for example

    $job->setDeleted(true);
    $queue->ack($job);
}

Send email job with mail() function

use Da\Mailer\Mailer;
use Da\Mailer\Model\MailMessage;
use Da\Mailer\Transport\MailTransport;

$transport = new MailTransport();

$mailer = new Mailer($transport);

$mailJob = /* ... get mail job here ... */;

$status = null;

try {
    $result = $mailer->send(
        new MailMessage(json_decode($mailJob->getMessage(), true))
    );
} catch (Exception $e) {
    // log exception
}

if (is_null($status)) {
    // ... cannot send email
}