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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\swiftmailer;
use yii\helpers\FileHelper;
use yii\mail\BaseMailer;
use Yii;
/**
* FileMailer implements a mailer that saves mails as files under runtime directory.
*
* To use FileMailer, you should configure it in the application configuration like the following,
*
* ~~~
* 'components' => array(
* ...
* 'email' => array(
* 'class' => 'yii\swiftmailer\FileMailer'
* ),
* ...
* ),
* ~~~
*
* To send an email, you may use the following code:
*
* ~~~
* Yii::$app->mail->compose('contact/html', ['contactForm' => $form])
* ->setFrom('from@domain.com')
* ->setTo($form->email)
* ->setSubject($form->subject)
* ->send();
* ~~~
*
* @author Dmitry Erofeev <dmeroff@gmail.com>
* @since 2.0
*/
class FileMailer extends BaseMailer
{
/**
* @var string message default class name.
*/
public $messageClass = 'yii\swiftmailer\Message';
/**
* @var string The path under which mail files will be written. Defaults to "@app/runtime/mail".
*/
protected $path;
/**
* @var callable Callback that will be used to generate name of the message to save.
*/
protected $callback;
/**
* Sets path under which mail files will be written.
* Any trailing '/' and '\' characters in the given path will be trimmed.
*
* @param $path
* @throws \InvalidArgumentException
*/
public function setPath($path)
{
$path = Yii::getAlias($path);
if (!is_dir($path) || !is_writable($path)) {
throw new \InvalidArgumentException('Filemailer::setPath expects a valid path in which to write mail files');
}
$this->path = rtrim($path, '\\/');
}
/**
* Gets path under which mail files will be written.
*
* @return string
*/
public function getPath()
{
if ($this->path == null) {
$path = Yii::getAlias('@app/runtime/mail');
if (!is_dir($path)) {
FileHelper::createDirectory($path);
}
$this->setPath($path);
}
return $this->path;
}
/**
* Sets callback that will be used to generate name of the message to save.
*
* @param callable $callback
*/
public function setCallback(callable $callback)
{
$this->callback = $callback;
}
/**
* Gets callback that will be used to generate name of the message to save.
*
* @return callable
*/
public function getCallback()
{
if ($this->callback == null) {
$this->setCallback(function () {
return uniqid('Message_') . '.txt';
});
}
return $this->callback;
}
/**
* @inheritdoc
*/
public function send($message)
{
$address = $message->getTo();
if (is_array($address)) {
$address = implode(', ', array_keys($address));
}
Yii::trace('Sending email "' . $message->getSubject() . '" to "' . $address . '"', __METHOD__);
$filename = $this->getPath() . DIRECTORY_SEPARATOR . call_user_func($this->getCallback());
return file_put_contents($filename, $message->toString()) !== false;
}
}