1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/Izumi-kun/yii2-spoolmailer |
4
|
|
|
* @copyright Copyright (c) 2017 Viktor Khokhryakov |
5
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace izumi\spoolmailer\spools; |
9
|
|
|
|
10
|
|
|
use Swift_FileSpool; |
11
|
|
|
use Swift_SpoolTransport; |
12
|
|
|
use Yii; |
13
|
|
|
use yii\helpers\FileHelper; |
14
|
|
|
use yii\mail\BaseMailer; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Stores Messages on the filesystem. |
18
|
|
|
* |
19
|
|
|
* @author Viktor Khokhryakov <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class FileSpool extends BaseSpool |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var string the spool directory |
25
|
|
|
*/ |
26
|
|
|
public $path = '@runtime/mail_spool'; |
27
|
|
|
/** |
28
|
|
|
* @var int the permission to be set for newly created spool directory. |
29
|
|
|
*/ |
30
|
|
|
public $dirMode = 0777; |
31
|
|
|
/** |
32
|
|
|
* @var int the maximum number of messages to send per flush |
33
|
|
|
*/ |
34
|
|
|
public $messageLimit = 0; |
35
|
|
|
/** |
36
|
|
|
* @var int the time limit (in seconds) per flush. |
37
|
|
|
*/ |
38
|
|
|
public $timeLimit = 30; |
39
|
|
|
/** |
40
|
|
|
* @var int in second Defaults is for very slow smtp responses |
41
|
|
|
*/ |
42
|
|
|
public $recoverTimeout = 900; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @inheritdoc |
46
|
|
|
*/ |
47
|
2 |
|
public function init() |
48
|
|
|
{ |
49
|
2 |
|
parent::init(); |
50
|
2 |
|
$this->path = Yii::getAlias($this->path); |
51
|
2 |
|
if (!is_dir($this->path)) { |
52
|
1 |
|
FileHelper::createDirectory($this->path, $this->dirMode, true); |
53
|
|
|
} |
54
|
2 |
|
$this->setTransport([ |
55
|
2 |
|
'class' => Swift_SpoolTransport::class, |
56
|
|
|
'constructArgs' => [ |
57
|
|
|
[ |
58
|
|
|
'class' => Swift_FileSpool::class, |
59
|
2 |
|
'constructArgs' => [$this->path], |
60
|
|
|
], |
61
|
|
|
], |
62
|
|
|
]); |
63
|
2 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritdoc |
67
|
|
|
*/ |
68
|
3 |
|
public function flush(BaseMailer $mailer, &$failedRecipients = null) |
69
|
|
|
{ |
70
|
|
|
/** @var Swift_FileSpool $spool */ |
71
|
3 |
|
$spool = $this->getTransport()->getSpool(); |
72
|
3 |
|
$spool->setTimeLimit($this->timeLimit); |
73
|
3 |
|
$spool->setMessageLimit($this->messageLimit); |
74
|
|
|
|
75
|
3 |
|
$spool->recover($this->recoverTimeout); |
76
|
|
|
|
77
|
3 |
|
return parent::flush($mailer, $failedRecipients); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|