Skip to content

Commit 5646a4e

Browse files
committed
Implement QueueTransport to enqueue emails automatically Fixes #64
1 parent 6470345 commit 5646a4e

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/Job/SendMailJob.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
7+
*
8+
* Licensed under The MIT License
9+
* For full copyright and license information, please see the LICENSE.txt
10+
* Redistributions of files must retain the above copyright notice.
11+
*
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @since 0.1.9
15+
* @license https://opensource.org/licenses/MIT MIT License
16+
*/
17+
namespace Cake\Queue\Job;
18+
19+
use Cake\Queue\Queue\Processor;
20+
21+
/**
22+
* SendMailJob class to be used by QueueTransport to enqueue emails
23+
*/
24+
class SendMailJob implements JobInterface
25+
{
26+
/**
27+
* @inheritDoc
28+
*/
29+
public function execute(Message $message): ?string
30+
{
31+
$transportClassName = $message->getArgument('transport');
32+
$config = $message->getArgument('config');
33+
$emailMessage = unserialize($message->getArgument('emailMessage'));
34+
try {
35+
/** @var \Cake\Mailer\AbstractTransport $transport */
36+
$transport = new $transportClassName($config);
37+
$result = $transport->send($emailMessage);
38+
} catch (\Exception $e) {
39+
return Processor::REJECT;
40+
}
41+
42+
return Processor::ACK;
43+
}
44+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/**
5+
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
6+
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
7+
*
8+
* Licensed under The MIT License
9+
* For full copyright and license information, please see the LICENSE.txt
10+
* Redistributions of files must retain the above copyright notice.
11+
*
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org/)
13+
* @link https://cakephp.org CakePHP(tm) Project
14+
* @since 0.1.9
15+
* @license https://opensource.org/licenses/MIT MIT License
16+
*/
17+
namespace Cake\Queue\Mailer\Transport;
18+
19+
use Cake\Mailer\Message;
20+
use Cake\Mailer\Transport\MailTransport;
21+
use Cake\Queue\Job\SendMailJob;
22+
use Cake\Queue\QueueManager;
23+
24+
class QueueTransport extends \Cake\Mailer\AbstractTransport
25+
{
26+
/**
27+
* Default config for this class
28+
*
29+
* @var array<string, mixed>
30+
*/
31+
protected $_defaultConfig = [
32+
'options' => [],
33+
'transport' => MailTransport::class,
34+
];
35+
36+
/**
37+
* @inheritDoc
38+
*/
39+
public function send(Message $message): array
40+
{
41+
QueueManager::push(
42+
[SendMailJob::class, 'execute'],
43+
[
44+
'transport' => $this->getConfig('transport'),
45+
'config' => $this->getConfig(),
46+
'emailMessage' => serialize($message),
47+
],
48+
$this->getConfig('options')
49+
);
50+
51+
$headers = $message->getHeadersString(
52+
[
53+
'from',
54+
'to',
55+
'subject',
56+
'sender',
57+
'replyTo',
58+
'readReceipt',
59+
'returnPath',
60+
'cc',
61+
'bcc',
62+
],
63+
);
64+
65+
return ['headers' => $headers, 'message' => 'Message has been enqueued'];
66+
}
67+
}

0 commit comments

Comments
 (0)