1
|
|
|
<?php |
2
|
|
|
namespace Yiisoft\Mailer; |
3
|
|
|
|
4
|
|
|
use Psr\EventDispatcher\EventDispatcherInterface; |
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* FileMailer is a mock mailer that save email messages in files instead of sending them. |
9
|
|
|
*/ |
10
|
|
|
class FileMailer extends BaseMailer |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* The path where message files located. |
14
|
|
|
* @var string $path |
15
|
|
|
*/ |
16
|
|
|
private $path; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Returns path. |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
2 |
|
public function getPath(): string |
23
|
|
|
{ |
24
|
2 |
|
return $this->path; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Sets path. |
29
|
|
|
* @param string $path |
30
|
|
|
*/ |
31
|
2 |
|
public function setPath(string $path): void |
32
|
|
|
{ |
33
|
2 |
|
$this->path = $path; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param MessageFactoryInterface $messageFactory |
39
|
|
|
* @param Composer $composer |
40
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
41
|
|
|
* @param LoggerInterface $logger |
42
|
|
|
* @param string $path |
43
|
|
|
*/ |
44
|
12 |
|
public function __construct( |
45
|
|
|
MessageFactoryInterface $messageFactory, |
46
|
|
|
Composer $composer, |
47
|
|
|
EventDispatcherInterface $eventDispatcher, |
48
|
|
|
LoggerInterface $logger, |
49
|
|
|
string $path |
50
|
|
|
) |
51
|
|
|
{ |
52
|
12 |
|
parent::__construct($messageFactory, $composer, $eventDispatcher, $logger); |
53
|
12 |
|
$this->path = $path; |
54
|
12 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var callable a PHP callback that return a file name which will be used to |
58
|
|
|
* save the email message. |
59
|
|
|
* If not set, the file name will be generated based on the current timestamp. |
60
|
|
|
* |
61
|
|
|
* The signature of the callback is: |
62
|
|
|
* |
63
|
|
|
* ```php |
64
|
|
|
* function ($mailer, $message) |
65
|
|
|
* ``` |
66
|
|
|
*/ |
67
|
|
|
private $filenameCallback; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Sets filename callback. |
71
|
|
|
* @param callable $callback |
72
|
|
|
*/ |
73
|
1 |
|
public function setFilenameCallback(Callable $callback): void |
74
|
|
|
{ |
75
|
1 |
|
$this->filenameCallback = $callback; |
76
|
1 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string the filename for saving the message. |
80
|
|
|
* @throws \Exception |
81
|
|
|
*/ |
82
|
5 |
|
protected function generateMessageFilename(): string |
83
|
|
|
{ |
84
|
5 |
|
$time = microtime(true); |
85
|
|
|
|
86
|
5 |
|
return date('Ymd-His-', $time) . sprintf('%04d', (int) (($time - (int) $time) * 10000)) . '-' . sprintf('%04d', random_int(0, 10000)) . '.eml'; |
87
|
|
|
} |
88
|
|
|
|
89
|
6 |
|
protected function sendMessage(MessageInterface $message): void |
90
|
|
|
{ |
91
|
6 |
|
if ($this->filenameCallback !== null) { |
92
|
1 |
|
$filename = call_user_func($this->filenameCallback, $this, $message); |
93
|
|
|
} else { |
94
|
5 |
|
$filename = $this->generateMessageFilename(); |
95
|
|
|
} |
96
|
6 |
|
$filename = $this->path . DIRECTORY_SEPARATOR . $filename; |
97
|
6 |
|
$filepath = dirname($filename); |
98
|
6 |
|
if (!is_dir($filepath) && !mkdir($filepath, 0777, true) && !is_dir($filepath)) { |
99
|
|
|
throw new \RuntimeException(sprintf('Directory "%s" was not created', $filepath)); |
100
|
|
|
} |
101
|
6 |
|
file_put_contents($filename, $message->toString()); |
102
|
6 |
|
} |
103
|
|
|
} |
104
|
|
|
|