forked from nterms/yii2-mailqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMessage.php
More file actions
58 lines (47 loc) · 1.29 KB
/
Message.php
File metadata and controls
58 lines (47 loc) · 1.29 KB
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
<?php
/**
* Message.php
* @author Saranga Abeykoon http://nterms.com
*/
namespace nterms\mailqueue;
use Yii;
use nterms\mailqueue\models\Queue;
/**
* Extends `yii\swiftmailer\Message` to enable queuing.
*
* @see http://www.yiiframework.com/doc-2.0/yii-swiftmailer-message.html
*/
class Message extends \yii\swiftmailer\Message {
private $db;
/**
* Method to set db instance to use. It is going to be passed down to Queue.
*/
public function setDb(\yii\db\Connection $db) {
$this->db = $db;
return $this;
}
/**
* Method to get db instance.
*/
public function getDb() {
return $this->db ? $this->db : Yii::$app->db;
}
/**
* Enqueue the message storing it in database.
*
* @param timestamp $time_to_send
* @return boolean true on success, false otherwise
*/
public function queue($time_to_send = 'now') {
if ($time_to_send == 'now') {
$time_to_send = time();
}
$item = new Queue();
$item->setDb($this->getDb());
$item->subject = $this->getSubject();
$item->attempts = 0;
$item->swift_message = base64_encode(serialize($this));
$item->time_to_send = date('Y-m-d H:i:s', $time_to_send);
return $item->save();
}
}