1- <?php
1+ <?php
22
33/**
44 * MailQueue.php
1313use 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 * ...
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 */
4949class 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}
0 commit comments