1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package CleverStyle Framework |
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
5
|
|
|
* @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi |
6
|
|
|
* @license MIT License, see license.txt |
7
|
|
|
*/ |
8
|
|
|
namespace cs; |
9
|
|
|
use |
10
|
|
|
h, |
11
|
|
|
PHPMailer, |
12
|
|
|
phpmailerException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @method static $this instance($check = false) |
16
|
|
|
*/ |
17
|
|
|
class Mail { |
18
|
|
|
use Singleton; |
19
|
|
|
/** |
20
|
|
|
* Sending of email |
21
|
|
|
* |
22
|
|
|
* @param array|string|string[] $email if emails without names - string (may be several emails separated by comma) or |
23
|
|
|
* 1-dimensional array(<i>email</i>)<br> |
24
|
|
|
* 2-dimensional of emails or array(<i>email</i>, <i>name</i>) must be given |
25
|
|
|
* @param string $subject Mail subject |
26
|
|
|
* @param string $body html body |
27
|
|
|
* @param string|null $body_text plain text body |
28
|
|
|
* @param array|null|string $attachments 1- or 2-dimensional array of array(<i>path</i>, <i>name</i>) or simply string with path to the file in |
29
|
|
|
* file system |
30
|
|
|
* @param array|null|string|string[] $reply_to Similar to <b>$email</b>, but multiple emails are not supported |
31
|
|
|
* @param bool|string $signature <b>true</b> - add system signature<br> |
32
|
|
|
* <b>false</b> - without signature<br> |
33
|
|
|
* <b>string</b> - custom signature |
34
|
|
|
* |
35
|
|
|
* @return bool |
36
|
|
|
*/ |
37
|
2 |
|
public function send_to ($email, $subject, $body, $body_text = null, $attachments = null, $reply_to = null, $signature = true) { |
38
|
2 |
|
if (!$email || !$subject || !$body) { |
39
|
2 |
|
return false; |
40
|
|
|
} |
41
|
2 |
|
$PHPMailer = $this->phpmailer_instance(); |
42
|
2 |
|
foreach ($this->normalize_email($email) as $e) { |
43
|
2 |
|
$PHPMailer->addAddress(...$e); |
44
|
|
|
} |
45
|
2 |
|
foreach ($this->normalize_email($reply_to) as $r) { |
46
|
2 |
|
$PHPMailer->addReplyTo(...$r); |
47
|
|
|
} |
48
|
2 |
|
foreach ($this->normalize_attachment($attachments) as $a) { |
49
|
|
|
try { |
50
|
2 |
|
$PHPMailer->addAttachment(...$a); |
51
|
2 |
|
} catch (phpmailerException $e) { |
52
|
2 |
|
trigger_error($e->getMessage(), E_USER_WARNING); |
53
|
|
|
} |
54
|
|
|
} |
55
|
2 |
|
$PHPMailer->Subject = $subject; |
56
|
2 |
|
$signature = $this->make_signature($signature); |
57
|
2 |
|
$PHPMailer->Body = $this->normalize_body($body, $signature); |
58
|
2 |
|
if ($body_text) { |
59
|
2 |
|
$PHPMailer->AltBody = $body_text.strip_tags($signature); |
60
|
|
|
} |
61
|
|
|
try { |
62
|
2 |
|
$result = $PHPMailer->send(); |
63
|
2 |
|
} catch (phpmailerException $e) { |
64
|
2 |
|
trigger_error($e->getMessage(), E_USER_WARNING); |
65
|
2 |
|
$result = false; |
66
|
|
|
} |
67
|
2 |
|
return $result; |
68
|
|
|
} |
69
|
|
|
/** |
70
|
|
|
* Create PHPMailer instance with parameters according to system configuration |
71
|
|
|
* |
72
|
|
|
* @return PHPMailer |
73
|
|
|
*/ |
74
|
2 |
|
protected function phpmailer_instance () { |
75
|
2 |
|
$PHPMailer = new PHPMailer(true); |
76
|
2 |
|
$Config = Config::instance(); |
77
|
2 |
|
if ($Config->core['smtp']) { |
78
|
2 |
|
$PHPMailer->isSMTP(); |
79
|
2 |
|
$PHPMailer->Host = $Config->core['smtp_host']; |
80
|
2 |
|
$PHPMailer->Port = $Config->core['smtp_port']; |
81
|
2 |
|
$PHPMailer->SMTPSecure = $Config->core['smtp_secure']; |
82
|
2 |
|
if ($Config->core['smtp_auth']) { |
83
|
2 |
|
$PHPMailer->SMTPAuth = true; |
84
|
2 |
|
$PHPMailer->Username = $Config->core['smtp_user']; |
85
|
2 |
|
$PHPMailer->Password = $Config->core['smtp_password']; |
86
|
|
|
} |
87
|
|
|
} |
88
|
2 |
|
$PHPMailer->From = $Config->core['mail_from']; |
89
|
2 |
|
$PHPMailer->FromName = get_core_ml_text('mail_from_name'); |
90
|
2 |
|
$PHPMailer->CharSet = 'utf-8'; |
91
|
2 |
|
$PHPMailer->isHTML(); |
92
|
2 |
|
return $PHPMailer; |
93
|
|
|
} |
94
|
|
|
/** |
95
|
|
|
* @param array|string|string[] $email |
96
|
|
|
* |
97
|
|
|
* @return string[][] |
98
|
|
|
*/ |
99
|
2 |
|
protected function normalize_email ($email) { |
100
|
2 |
|
if (!$email) { |
101
|
2 |
|
return []; |
102
|
|
|
} |
103
|
2 |
|
if (!is_array($email)) { |
104
|
2 |
|
$email = _trim(explode(',', $email)); |
105
|
2 |
|
} elseif (!is_array($email[0])) { |
106
|
2 |
|
$email = [$email]; |
107
|
|
|
} |
108
|
2 |
|
return _array($email); |
109
|
|
|
} |
110
|
2 |
|
protected function make_signature ($signature) { |
|
|
|
|
111
|
2 |
|
if ($signature === true) { |
112
|
2 |
|
$signature = get_core_ml_text('mail_signature'); |
113
|
2 |
|
return $signature ? "<br>\n<br>\n-- \n<br>$signature" : ''; |
114
|
|
|
} |
115
|
2 |
|
return $signature ? "<br>\n<br>\n-- \n<br>".xap($signature, true) : ''; |
116
|
|
|
} |
117
|
|
|
/** |
118
|
|
|
* @param string $body |
119
|
|
|
* @param string $signature |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
2 |
|
protected function normalize_body ($body, $signature) { |
124
|
2 |
|
if (strpos($body, '<html') === false) { |
125
|
2 |
|
if (strpos($body, '<body') === false) { |
126
|
2 |
|
$body = h::body($body.$signature); |
127
|
|
|
} else { |
128
|
2 |
|
$body = str_replace('</body>', "$signature</body>", $body); |
129
|
|
|
} |
130
|
2 |
|
$body = h::html( |
131
|
2 |
|
h::{'head meta'}( |
132
|
|
|
[ |
133
|
2 |
|
'content' => 'text/html; charset=utf-8', |
134
|
|
|
'http-equiv' => 'Content-Type' |
135
|
|
|
] |
136
|
|
|
). |
137
|
2 |
|
$body |
138
|
|
|
); |
139
|
|
|
} else { |
140
|
2 |
|
$body = str_replace('</body>', "$signature</body>", $body); |
141
|
|
|
} |
142
|
2 |
|
if (strpos($body, '<!doctype') === false) { |
143
|
2 |
|
$body = "<!doctype html>\n$body"; |
144
|
|
|
} |
145
|
2 |
|
return $body; |
146
|
|
|
} |
147
|
|
|
/** |
148
|
|
|
* @param array|null|string $attachments |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
2 |
|
protected function normalize_attachment ($attachments) { |
153
|
2 |
|
if (!$attachments) { |
154
|
2 |
|
return []; |
155
|
|
|
} |
156
|
2 |
|
if (!is_array($attachments) || !is_array($attachments[0])) { |
157
|
2 |
|
$attachments = [$attachments]; |
158
|
|
|
} |
159
|
2 |
|
return _array($attachments); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.