-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mail.php
117 lines (103 loc) · 3.17 KB
/
Mail.php
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
<?php
namespace Colibri\Mail;
use Colibri\View\PhpTemplate;
use Swift_Mailer;
use Swift_Message;
use Swift_SmtpTransport;
class Mail
{
/** @var array */
private static $config = [];
/** @var \Swift_Mailer */
private static $mailer = null;
/**
* @param array $config
*/
public static function setConfig(array $config)
{
self::$config = array_replace_recursive(self::$config, $config); // array overwrite
}
/**
* @param string $to
* @param string $subject
* @param string $view
* @param array $viewVars
* @param callable $builder
* @param string $from
*
* @throws \Swift_SwiftException
*/
public static function send(string $to, string $subject, string $view, array $viewVars = [], $builder = null, $from = 'default')
{
$message = static::createMessage($to, $subject, $from);
if (is_callable($builder)) {
$builder($message);
}
$message->setBody(static::view($view, $viewVars));
self::getMailer()->send($message);
}
/**
* @return \Swift_Mailer
*/
final protected static function getMailer(): Swift_Mailer
{
return self::$mailer === null
? self::$mailer = static::createMailer()
: self::$mailer;
}
/**
* @return \Swift_Mailer
*/
protected static function createMailer(): Swift_Mailer
{
$class = self::$config['transport']['driver'] ?? Swift_SmtpTransport::class;
$params = self::$config['transport']['params'] ?? [];
/** @var \Swift_SmtpTransport $transport */
$transport = new $class(...array_values($params));
if (isset(self::$config['transport']['username'])) {
$transport->setUsername(self::$config['transport']['username']);
}
if (isset(self::$config['transport']['password'])) {
$transport->setPassword(self::$config['transport']['password']);
}
return new Swift_Mailer($transport);
}
/**
* @param string $name
* @param array $vars
*
* @return string
*/
protected static function view(string $name, array $vars): string
{
static $path = null;
$path = $path ?? (
self::$config['views'] ?? realpath(dirname(__DIR__ . '/../../../application/') . 'views/mail')
);
return (new PhpTemplate("$path/$name.php"))
->setVars($vars)
->compile()
;
}
/**
* @param string $to
* @param string $subject
* @param string $from
*
* @return \Swift_Message
*
* @throws \Swift_DependencyException
* @throws \Swift_SwiftException
*/
protected static function createMessage(string $to, string $subject, string $from = 'default'): Swift_Message
{
$fromName = $from === 'default' ? self::$config['from']['default'] : $from;
$from = self::$config['from'][$fromName];
return (new Swift_Message())
->setContentType('text/html')
->setSubject($subject)
->setFrom([$from['address'] => $from['name']])
->setTo($to)
;
}
}