-
Notifications
You must be signed in to change notification settings - Fork 6
/
emailer.php
288 lines (275 loc) · 7.18 KB
/
emailer.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
function email_types(){
return array('sendmail'=>'Default','smtp'=>'SMTP mail server');
}
function gmail_send($p,&$error){
$mail=mailer_setup($error);
if (empty($mail)){
return false;
}
$mail->Host='smtp.gmail.com';
$mail->Port=465;
$mail->SMTPSecure='ssl';
$mail->SMTPAuth=true;
if (function_exists('send_user')){
$mail=send_user($mail,'gmail');
}
if (!smtp_send_email($mail,$p,$error)){
return false;
}
return true;
}
function log_email($email,$subject,$message,$headers){
$file=LOG.'email-sent.log';
$content='---- Email sent on '.date('r').' ----'."\r\n";
$content.="\r\n".'Recipient: '.$email;
$content.="\r\n".'Subject: '.$subject;
$content.="\r\n".print_r($message, true);
$content.="\r\n".$headers."\r\n\r\n";
$fh=fopen($file,'a+');
if (!empty($fh)){
fwrite($fh,$content);
fclose($fh);
}
elseif (!empty($_SERVER['SHELL'])){
echo 'Email log failed to '.$file.PHP_EOL;
}
return true;
}
function mail_headers(&$p=null,&$extra=null){
if (function_exists('mail_headers_config')){
$p=mail_headers_config($p);
}
$headers=[];
$headers[]='From: '.$p['from'].' ('.$p['name'].')';
$headers[]='Return-Path: <'.$p['from'].'>';
$headers[]='Reply-to: "'.$p['name'].'" <'.$p['reply'].'> ';
$headers[]='X-Mailer: php';
$headers[]='MIME-Version: 1.0';
if (!$extra['multi']){
$headers[]='Content-type: text/html; charset=utf-8';
}
elseif (!$extra['boundary']){
$extra['boundary']=md5(date('U'));
$headers[]='Content-Type: multipart/alternative; boundary="'.$extra['boundary'].'"';
}
$headers=implode("\r\n",$headers);
return $headers;
}
/**
* @param $error
*
* @return bool|PHPMailer
*/
function mailer_setup(&$error){
if (!class_exists('PHPMailer')){
$error='The mailing system cannot be loaded to send this email.';
return false;
}
$mail=new PHPMailer();
$mail->IsSMTP();
if (defined('SMTP_DEBUG')){
$mail->SMTPDebug=1;
}
return $mail;
}
function send_email($p,&$error=null,$mail_type=null){
if (empty($mail_type) and function_exists('send_email_type')){
$mail_type=send_email_type();
}
if (function_exists('send_email_subject')){
$p['subject']=send_email_subject($p);
}
$n=0;
if (!isset($p['emails']) or !is_array_full(array_keys($p['emails']))){
$error='You must send emails to the emailer as an array, even for single email addresses. If you don't know what this means, contact your website manager.';
return false;
}
// this bit is only needed until we've updated all other sites to use new email assoc format
$first=reset($p['emails']);
if (make_email($first)){
$temp=array();
foreach ($p['emails'] as $name => $email){
$temp[$email]=$name;
}
$p['emails']=$temp;
unset($temp);
}
//
if (defined('BETA') && function_exists('send_email_whitelist')){
$p['emails'] = send_email_whitelist($p['emails']);
if (!empty($p['cc'])){
$p['cc'] = send_email_whitelist($p['cc'], false);
}
if (!empty($p['bcc'])){
$p['bcc'] = send_email_whitelist($p['bcc'], false);
}
}
if (!defined('EMAIL_SEND')){
if (!isset($p['headers'])){
$headers=mail_headers();
}
foreach ($p['emails'] as $email => $name){
log_email($name.' <'.$email.'>',$p['subject'],$p['message'],$headers);
$n++;
}
}
else {
switch ($mail_type){
case 'func':
$func=send_email_func();
if (!$func($p,$error)){
return false;
}
break;
case 'gmail':
if (!gmail_send($p,$error)){
return false;
}
break;
case 'sendgrid':
if (!sendgrid_send($p,$error)){
return false;
}
break;
case 'smtp':
if (!smtp_send($p,$error)){
return false;
}
break;
case 'sendmail':
default:
if (!is_array_full($p['emails'])){
$error='You must send emails to the emailer as an array, even for single email addresses. If you don't know what this means, contact your website manager.';
return false;
}
if (!isset($p['headers'])){
$p['headers']=mail_headers();
}
foreach ($p['emails'] as $email => $name){
if (@mail($email,$p['subject'],$p['message'],$p['headers'])){
$n++;
}
else {
$errors[]=$email;
}
}
if (!empty($errors)){
$error='The email message could not be sent to the following addresses.</p><ul><li>'.implode('</li><li>',$errors).'</li></ul><p>';
return false;
}
break;
}
}
return true;
}
function sendgrid_send($p,&$error){
$mail=mailer_setup($error);
if (empty($mail)){
return false;
}
$mail->Host='smtp.sendgrid.net';
$mail->Port=587;
$mail->SMTPAuth=true;
if (function_exists('send_user')){
$mail=send_user($mail,'sendgrid');
}
$mail->Encoding='base64';
if (function_exists('sendgrid_send_extend')){
$mail = sendgrid_send_extend($mail);
}
if (!smtp_send_email($mail,$p,$error)){
return false;
}
return true;
}
/**
* @param PHPMailer $mail
* @param array $filter
*
* $filter = [
'footer' => [
'setting' => [
'enable' => 1,
'text/plain' => 'You can haz footers!',
],
],
];
*
* @return PHPMailer
*/
function sendgrid_add_filter(PHPMailer $mail, array $filter){
$header = new Smtpapi\Header();
$header->setFilters($filter);
$mail->addCustomHeader(Smtpapi\Header::NAME, $header->jsonString());
return $mail;
}
function smtp_send($p,&$error){
$mail=mailer_setup($error);
if (empty($mail)){
return false;
}
if (function_exists('smtp_send_details')){
$mail=smtp_send_details($mail);
}
if (function_exists('send_user')){
$mail=send_user($mail,'smtp');
}
if (!smtp_send_email($mail,$p,$error)){
return false;
}
return true;
}
function smtp_send_email(PHPMailer $mail,$p,&$error){
if (function_exists('smtp_send_config')){
$p=smtp_send_config($p);
}
$mail->FromName=$p['from-name'];
$mail->AddReplyTo($p['replyto'],$p['replyto-name']);
$mail->Subject=$p['subject'];
if (is_array($p['message'])){
$mail->Body=$p['message']['html'];
$mail->AltBody=$p['message']['plain'];
}
else {
$mail->Body=$p['message']; //HTML Body
$mail->AltBody=strip_tags($p['message']); //Text Body
}
$mail->IsHTML(true); // send as HTML
if (!empty($p['cc'])){
foreach ($p['cc'] as $email => $name){
$mail->AddCC($email,$name);
}
}
if (!empty($p['bcc'])){
foreach ($p['bcc'] as $email => $name){
$mail->AddBCC($email,$name);
}
}
if (!empty($p['attach'])){
foreach($p['attach'] as $attach){
$mail->AddAttachment($attach['file'],$attach['name']);
}
}
if (!empty($p['emails'])){
foreach ($p['emails'] as $email => $name){
$mail->AddAddress($email,$name);
if (is_on('EMAIL_SERIALIZE')){
$mail_serialize = serialize($mail);
file_save(LOG.'emails/by-date/'.date('Y-m-d-H:i:s').'_'.$email, $mail_serialize);
}
if (!$mail->Send()){
$errors[]=$mail->ErrorInfo.' occured for address '.$email;
}
$mail->ClearAddresses();
}
}
elseif (!$mail->Send()){
$errors[]='The error '.$mail->ErrorInfo.' occured for address '.$email;
}
if (count($errors)>0){
$error='The following SMTP errors were reported:<ul><li>'.implode('</li><li>',$errors).'</li></ul>';
return false;
}
return true;
}