1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
<?php
namespace BEdita\Core\Mailer\Transport;
use BEdita\Core\Mailer\Email as BeditaEmail;
use Cake\Mailer\AbstractTransport;
use Cake\Mailer\Email;
use Cake\Mailer\Transport\DebugTransport;
use Cake\ORM\TableRegistry;
class AsyncJobsTransport extends AbstractTransport
{
protected $_defaultConfig = [
'service' => 'mail',
'max_attempts' => 3,
];
public function send(Email $email)
{
$table = TableRegistry::getTableLocator()->get('AsyncJobs');
$asyncJob = $table->newEntity();
$asyncJob->service = $this->getConfig('service');
$asyncJob->max_attempts = $this->getConfig('max_attempts');
if ($this->getConfig('priority') !== null) {
$asyncJob->priority = $this->getConfig('priority');
}
$payload = $email->jsonSerialize();
$payload += [
'_boundary' => BeditaEmail::getBoundary($email),
'_message' => $email->message(),
'_htmlMessage' => $email->message(Email::MESSAGE_HTML),
'_textMessage' => $email->message(Email::MESSAGE_TEXT),
];
unset($payload['viewVars'], $payload['viewConfig']);
$asyncJob->payload = $payload;
$table->saveOrFail($asyncJob);
return (new DebugTransport())->send($email);
}
}