Skip to content

Commit 6ced309

Browse files
authored
Merge pull request #23 from BBrunekreeft/master
Store complete Swift_Message object in mail_queue database
2 parents 776cc1f + 5204935 commit 6ced309

4 files changed

Lines changed: 64 additions & 84 deletions

File tree

MailQueue.php

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php
22

33
/**
44
* MailQueue.php
@@ -13,11 +13,11 @@
1313
use nterms\mailqueue\models\Queue;
1414

1515
/**
16-
* MailQueue is a sub class of [yii\switmailer\Mailer](https://github.com/yiisoft/yii2-swiftmailer/blob/master/Mailer.php)
16+
* MailQueue is a sub class of [yii\switmailer\Mailer](https://github.com/yiisoft/yii2-swiftmailer/blob/master/Mailer.php)
1717
* which intends to replace it.
18-
*
18+
*
1919
* Configuration is the same as in `yii\switmailer\Mailer` with some additional properties to control the mail queue
20-
*
20+
*
2121
* ~~~
2222
* 'components' => [
2323
* ...
@@ -38,49 +38,49 @@
3838
* ...
3939
* ],
4040
* ~~~
41-
*
41+
*
4242
* @see http://www.yiiframework.com/doc-2.0/yii-swiftmailer-mailer.html
4343
* @see http://www.yiiframework.com/doc-2.0/ext-swiftmailer-index.html
44-
*
45-
* This extension replaces `yii\switmailer\Message` with `nterms\mailqueue\Message'
44+
*
45+
* This extension replaces `yii\switmailer\Message` with `nterms\mailqueue\Message'
4646
* to enable queuing right from the message.
47-
*
47+
*
4848
*/
4949
class MailQueue extends Mailer
5050
{
5151
const NAME = 'mailqueue';
52-
52+
5353
/**
5454
* @var string message default class name.
5555
*/
5656
public $messageClass = 'nterms\mailqueue\Message';
57-
57+
5858
/**
5959
* @var string the name of the database table to store the mail queue.
6060
*/
6161
public $table = '{{%mail_queue}}';
62-
62+
6363
/**
6464
* @var integer the default value for the number of mails to be sent out per processing round.
6565
*/
6666
public $mailsPerRound = 10;
67-
67+
6868
/**
6969
* @var integer maximum number of attempts to try sending an email out.
7070
*/
7171
public $maxAttempts = 3;
72-
72+
7373
/**
7474
* Initializes the MailQueue component.
7575
*/
7676
public function init()
7777
{
7878
parent::init();
7979
}
80-
80+
8181
/**
8282
* Sends out the messages in email queue and update the database.
83-
*
83+
*
8484
* @return boolean true if all messages are successfully sent out
8585
*/
8686
public function process()
@@ -90,33 +90,26 @@ public function process()
9090
}
9191

9292
$success = true;
93-
94-
$items = Queue::find()->where(['and', ['sent_time' => NULL], ['!=', 'to', 'a:0:{}'], ['<', 'attempts', $this->maxAttempts], ['<=', 'time_to_send', date('Y-m-d H:i:s')]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound)->all();
95-
// dd($items);
96-
if (!empty($items)) {
97-
$n = count($items);
98-
$pad = strlen($this->mailsPerRound);
99-
foreach ($items as $i => $item) {
100-
$j = str_pad($i + 1, $pad, ' ', STR_PAD_LEFT);
101-
if ($message = $item->toMessage()) {
102-
$attributes = ['attempts', 'last_attempt_time'];
103-
$to = array_keys($message->to);
104-
$to = $to[0];
105-
if ($message->send($this)) {
106-
$item->sent_time = new \yii\db\Expression('NOW()');
107-
$attributes[] = 'sent_time';
108-
} else {
109-
$success = false;
110-
}
111-
112-
$item->attempts++;
113-
$item->last_attempt_time = new \yii\db\Expression('NOW()');
114-
115-
$item->updateAttributes($attributes);
116-
}
93+
94+
$items = Queue::find()->where(['and', ['sent_time' => NULL], ['!=', 'to', 'a:0:{}'], ['<', 'attempts', $this->maxAttempts], ['<=', 'time_to_send', date('Y-m-d H:i:s')]])->orderBy(['created_at' => SORT_ASC])->limit($this->mailsPerRound);
95+
foreach ($items->each() as $item) {
96+
if ($message = $item->toMessage()) {
97+
$attributes = ['attempts', 'last_attempt_time'];
98+
if ($this->send($message)) {
99+
$item->sent_time = new \yii\db\Expression('NOW()');
100+
$attributes[] = 'sent_time';
101+
} else {
102+
$success = false;
117103
}
118-
}
119-
104+
105+
$item->attempts++;
106+
$item->last_attempt_time = new \yii\db\Expression('NOW()');
107+
108+
$item->updateAttributes($attributes);
109+
}
110+
}
111+
112+
120113
return $success;
121114
}
122115
}

Message.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function queue($time_to_send = 'now')
3939
$item->charset = $this->getCharset();
4040
$item->subject = $this->getSubject();
4141
$item->attempts = 0;
42+
$item->swift_message = serialize($this);
4243
$item->time_to_send = date('Y-m-d H:i:s', $time_to_send);
4344

4445
$parts = $this->getSwiftMessage()->getChildren();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use yii\db\Migration;
4+
5+
/**
6+
* Handles adding swift_message to table `mail_queue`.
7+
*/
8+
class m161111_080914_add_swift_message_column_to_mail_queue_table extends Migration
9+
{
10+
/**
11+
* @inheritdoc
12+
*/
13+
public function up()
14+
{
15+
$this->addColumn(Yii::$app->get(MailQueue::NAME)->table, 'swift_message', $this->longtext());
16+
}
17+
18+
/**
19+
* @inheritdoc
20+
*/
21+
public function down()
22+
{
23+
$this->dropColumn(Yii::$app->get(MailQueue::NAME)->table, 'swift_message');
24+
}
25+
}

models/Queue.php

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* @property integer $last_attempt_time
2525
* @property integer $sent_time
2626
* @property string $time_to_send
27+
* @property string swift_message
2728
*/
2829
class Queue extends ActiveRecord
2930
{
@@ -59,53 +60,13 @@ public function rules()
5960
{
6061
return [
6162
[['created_at', 'attempts', 'last_attempt_time', 'sent_time'], 'integer'],
62-
[['time_to_send'], 'required'],
63+
[['time_to_send', 'swift_message'], 'required'],
6364
[['to', 'cc', 'bcc', 'subject', 'html_body', 'text_body', 'charset'], 'safe'],
6465
];
6566
}
66-
67+
6768
public function toMessage()
6869
{
69-
$from = unserialize($this->from);
70-
$to = unserialize($this->to);
71-
72-
if( !empty($from) && !empty($to) ) {
73-
$cc = unserialize($this->cc);
74-
$bcc = unserialize($this->bcc);
75-
$reply_to = unserialize($this->reply_to);
76-
77-
$message = new Message();
78-
$message->setFrom($from)->setTo($to);
79-
80-
if(!empty($cc)) {
81-
$message->setCc($cc);
82-
}
83-
84-
if(!empty($bcc)) {
85-
$message->setBcc($bcc);
86-
}
87-
88-
if(!empty($reply_to)) {
89-
$message->setReplyTo($reply_to);
90-
}
91-
92-
if(!empty($this->charset)) {
93-
$message->setCharset($this->charset);
94-
}
95-
96-
$message->setSubject($this->subject);
97-
98-
if(!empty($this->html_body)) {
99-
$message->setHtmlBody($this->html_body);
100-
}
101-
102-
if(!empty($this->text_body)) {
103-
$message->setTextBody($this->text_body);
104-
}
105-
106-
return $message;
107-
}
108-
109-
return null;
70+
return unserialize($this->swift_message);
11071
}
11172
}

0 commit comments

Comments
 (0)