-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMail.inc.php
571 lines (489 loc) · 14.8 KB
/
Mail.inc.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
<?php
/**
* @defgroup mail
*/
/**
* @file classes/mail/Mail.inc.php
*
* Copyright (c) 2000-2013 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class Mail
* @ingroup mail
*
* @brief Class defining basic operations for handling and sending emails.
*/
define('MAIL_EOL', Core::isWindows() ? "\r\n" : "\n");
define('MAIL_WRAP', 76);
class Mail extends DataObject {
/** @var array List of key => value private parameters for this message */
var $privateParams;
/**
* Constructor.
*/
function Mail() {
parent::DataObject();
$this->privateParams = array();
if (Config::getVar('email', 'allow_envelope_sender')) {
$defaultEnvelopeSender = Config::getVar('email', 'default_envelope_sender');
if (!empty($defaultEnvelopeSender)) $this->setEnvelopeSender($defaultEnvelopeSender);
}
}
/**
* Add a private parameter to this email. Private parameters are
* replaced just before sending and are never available via getBody etc.
*/
function addPrivateParam($name, $value) {
$this->privateParams[$name] = $value;
}
/**
* Set the entire list of private parameters.
* @see addPrivateParam
*/
function setPrivateParams($privateParams) {
$this->privateParams = $privateParams;
}
/**
* Add a recipient.
* @param $email string
* @param $name string optional
*/
function addRecipient($email, $name = '') {
if (($recipients = $this->getData('recipients')) == null) {
$recipients = array();
}
array_push($recipients, array('name' => $name, 'email' => $email));
return $this->setData('recipients', $recipients);
}
/**
* Set the envelope sender (bounce address) for the message,
* if supported.
* @param $envelopeSender string Email address
*/
function setEnvelopeSender($envelopeSender) {
$this->setData('envelopeSender', $envelopeSender);
}
/**
* Get the envelope sender (bounce address) for the message, if set.
* @return string
*/
function getEnvelopeSender() {
return $this->getData('envelopeSender');
}
/**
* Get the message content type (MIME)
* @return string
*/
function getContentType() {
return $this->getData('content_type');
}
/**
* Set the message content type (MIME)
* @param $contentType string
*/
function setContentType($contentType) {
return $this->setData('content_type', $contentType);
}
/**
* Get the recipients for the message.
* @return array
*/
function getRecipients() {
return $this->getData('recipients');
}
/**
* Set the recipients for the message.
* @param $recipients array
*/
function setRecipients($recipients) {
return $this->setData('recipients', $recipients);
}
/**
* Add a carbon-copy (CC) recipient to the message.
* @param $email string
* @param $name string optional
*/
function addCc($email, $name = '') {
if (($ccs = $this->getData('ccs')) == null) {
$ccs = array();
}
array_push($ccs, array('name' => $name, 'email' => $email));
return $this->setData('ccs', $ccs);
}
/**
* Get the carbon-copy (CC) recipients for the message.
* @return array
*/
function getCcs() {
return $this->getData('ccs');
}
/**
* Set the carbon-copy (CC) recipients for the message.
* @param $ccs array
*/
function setCcs($ccs) {
return $this->setData('ccs', $ccs);
}
/**
* Add a blind carbon copy (BCC) recipient to the message.
* @param $email string
* @param $name optional
*/
function addBcc($email, $name = '') {
if (($bccs = $this->getData('bccs')) == null) {
$bccs = array();
}
array_push($bccs, array('name' => $name, 'email' => $email));
return $this->setData('bccs', $bccs);
}
/**
* Get the blind carbon copy (BCC) recipients for the message
* @return array
*/
function getBccs() {
return $this->getData('bccs');
}
/**
* Set the blind carbon copy (BCC) recipients for the message.
* @param $bccs array
*/
function setBccs($bccs) {
return $this->setData('bccs', $bccs);
}
/**
* If no recipients for this message, promote CC'd accounts to
* recipients. If recipients exist, no effect.
* @return boolean true iff CCs were promoted
*/
function promoteCcsIfNoRecipients() {
$ccs = $this->getCcs();
$recipients = $this->getRecipients();
if (empty($recipients)) {
$this->setRecipients($ccs);
$this->setCcs(array());
return true;
}
return false;
}
/**
* Clear all recipients for this message (To, CC, and BCC).
*/
function clearAllRecipients() {
$this->setRecipients(array());
$this->setCcs(array());
$this->setBccs(array());
}
/**
* Add an SMTP header to the message.
* @param $name string
* @param $content string
*/
function addHeader($name, $content) {
$updated = false;
if (($headers = $this->getData('headers')) == null) {
$headers = array();
}
foreach ($headers as $key => $value) {
if ($headers[$key]['name'] == $name) {
$headers[$key]['content'] = $content;
$updated = true;
}
}
if (!$updated) {
array_push($headers, array('name' => $name,'content' => $content));
}
return $this->setData('headers', $headers);
}
/**
* Get the SMTP headers for the message.
* @return array
*/
function getHeaders() {
return $this->getData('headers');
}
/**
* Set the SMTP headers for the message.
* @param $headers array
*/
function setHeaders(&$headers) {
return $this->setData('headers', $headers);
}
/**
* Adds a file attachment to the email.
* @param $filePath string complete path to the file to attach
* @param $fileName string attachment file name (optional)
* @param $contentType string attachment content type (optional)
* @param $contentDisposition string attachment content disposition, inline or attachment (optional, default attachment)
*/
function addAttachment($filePath, $fileName = '', $contentType = '', $contentDisposition = 'attachment') {
if ($attachments =& $this->getData('attachments') == null) {
$attachments = array();
}
/* If the arguments $fileName and $contentType are not specified,
then try and determine them automatically. */
if (empty($fileName)) {
$fileName = basename($filePath);
}
if (empty($contentType)) {
$contentType = String::mime_content_type($filePath);
if (empty($contentType)) $contentType = 'application/x-unknown-content-type';
}
// Open the file and read contents into $attachment
if (is_readable($filePath) && is_file($filePath)) {
$fp = fopen($filePath, 'rb');
if ($fp) {
$content = '';
while (!feof($fp)) {
$content .= fread($fp, 4096);
}
fclose($fp);
}
}
if (isset($content)) {
/* Encode the contents in base64. */
$content = chunk_split(base64_encode($content), MAIL_WRAP, MAIL_EOL);
array_push($attachments, array('filename' => $fileName, 'content-type' => $contentType, 'disposition' => $contentDisposition, 'content' => $content));
return $this->setData('attachments', $attachments);
} else {
return false;
}
}
/**
* Get the attachments currently on the message.
* @return array
*/
function &getAttachments() {
$attachments =& $this->getData('attachments');
return $attachments;
}
/**
* Return true iff attachments are included in this message.
* @return boolean
*/
function hasAttachments() {
$attachments =& $this->getAttachments();
return ($attachments != null && count($attachments) != 0);
}
/**
* Set the sender of the message.
* @param $email string
* @param $name string optional
*/
function setFrom($email, $name = '') {
return $this->setData('from', array('name' => $name, 'email' => $email));
}
/**
* Get the sender of the message.
* @return array
*/
function getFrom() {
return $this->getData('from');
}
/**
* Set the subject of the message.
* @param $subject string
*/
function setSubject($subject) {
return $this->setData('subject', $subject);
}
/**
* Get the subject of the message.
* @return string
*/
function getSubject() {
return $this->getData('subject');
}
/**
* Set the body of the message.
* @param $body string
*/
function setBody($body) {
return $this->setData('body', $body);
}
/**
* Get the body of the message.
* @return string
*/
function getBody() {
return $this->getData('body');
}
/**
* Return a string containing the from address.
* @return string
*/
function getFromString($send = false) {
$from = $this->getFrom();
if ($from == null) {
return null;
} else {
return (Mail::encodeDisplayName($from['name'], $send) . ' <'.$from['email'].'>');
}
}
/**
* Return a string from an array of (name, email) pairs.
* @param $includeNames boolean
* @return string;
*/
function getAddressArrayString($addresses, $includeNames = true, $send = false) {
if ($addresses == null) {
return null;
} else {
$addressString = '';
foreach ($addresses as $address) {
if (!empty($addressString)) {
$addressString .= ', ';
}
if (Core::isWindows() || empty($address['name']) || !$includeNames) {
$addressString .= $address['email'];
} else {
$addressString .= Mail::encodeDisplayName($address['name'], $send) . ' <'.$address['email'].'>';
}
}
return $addressString;
}
}
/**
* Return a string containing the recipients.
* @return string
*/
function getRecipientString() {
return $this->getAddressArrayString($this->getRecipients());
}
/**
* Return a string containing the Cc recipients.
* @return string
*/
function getCcString() {
return $this->getAddressArrayString($this->getCcs());
}
/**
* Return a string containing the Bcc recipients.
* @return string
*/
function getBccString() {
return $this->getAddressArrayString($this->getBccs(), false);
}
/**
* Send the email.
* @return boolean
*/
function send() {
$recipients = $this->getAddressArrayString($this->getRecipients(), true, true);
$from = $this->getFromString(true);
$subject = String::encode_mime_header($this->getSubject());
$body = $this->getBody();
// FIXME Some *nix mailers won't work with CRLFs
if (Core::isWindows()) {
// Convert LFs to CRLFs for Windows
$body = String::regexp_replace("/([^\r]|^)\n/", "\$1\r\n", $body);
} else {
// Convert CRLFs to LFs for *nix
$body = String::regexp_replace("/\r\n/", "\n", $body);
}
if ($this->getContentType() != null) {
$this->addHeader('Content-Type', $this->getContentType());
} elseif ($this->hasAttachments()) {
// Only add MIME headers if sending an attachment
$mimeBoundary = '==boundary_'.md5(microtime());
/* Add MIME-Version and Content-Type as headers. */
$this->addHeader('MIME-Version', '1.0');
$this->addHeader('Content-Type', 'multipart/mixed; boundary="'.$mimeBoundary.'"');
} else {
$this->addHeader('Content-Type', 'text/plain; charset="'.Config::getVar('i18n', 'client_charset').'"');
}
$this->addHeader('X-Mailer', 'Public Knowledge Project Suite v2');
$remoteAddr = Request::getRemoteAddr();
if ($remoteAddr != '') $this->addHeader('X-Originating-IP', $remoteAddr);
$this->addHeader('Date', date('D, d M Y H:i:s O'));
/* Add $from, $ccs, and $bccs as headers. */
if ($from != null) {
$this->addHeader('From', $from);
}
$ccs = $this->getAddressArrayString($this->getCcs(), true, true);
if ($ccs != null) {
$this->addHeader('Cc', $ccs);
}
$bccs = $this->getAddressArrayString($this->getBccs(), false, true);
if ($bccs != null) {
$this->addHeader('Bcc', $bccs);
}
$headers = '';
foreach ($this->getHeaders() as $header) {
if (!empty($headers)) {
$headers .= MAIL_EOL;
}
$headers .= $header['name'].': '. str_replace(array("\r", "\n"), '', $header['content']);
}
if ($this->hasAttachments()) {
// Add the body
$mailBody = 'This message is in MIME format and requires a MIME-capable mail client to view.'.MAIL_EOL.MAIL_EOL;
$mailBody .= '--'.$mimeBoundary.MAIL_EOL;
$mailBody .= sprintf('Content-Type: text/plain; charset=%s', Config::getVar('i18n', 'client_charset')) . MAIL_EOL.MAIL_EOL;
$mailBody .= wordwrap($body, MAIL_WRAP, MAIL_EOL).MAIL_EOL.MAIL_EOL;
// Add the attachments
$attachments = $this->getAttachments();
foreach ($attachments as $attachment) {
$mailBody .= '--'.$mimeBoundary.MAIL_EOL;
$mailBody .= 'Content-Type: '.$attachment['content-type'].'; name="'.str_replace('"', '', $attachment['filename']).'"'.MAIL_EOL;
$mailBody .= 'Content-transfer-encoding: base64'.MAIL_EOL;
$mailBody .= 'Content-disposition: '.$attachment['disposition'].MAIL_EOL.MAIL_EOL;
$mailBody .= $attachment['content'].MAIL_EOL.MAIL_EOL;
}
$mailBody .= '--'.$mimeBoundary.'--';
} else {
// Just add the body
$mailBody = wordwrap($body, MAIL_WRAP, MAIL_EOL);
}
if ($this->getEnvelopeSender() != null) {
$additionalParameters = '-f ' . $this->getEnvelopeSender();
} else {
$additionalParameters = null;
}
if (HookRegistry::call('Mail::send', array(&$this, &$recipients, &$subject, &$mailBody, &$headers, &$additionalParameters))) return;
// Replace all the private parameters for this message.
if (is_array($this->privateParams)) {
foreach ($this->privateParams as $name => $value) {
$mailBody = str_replace($name, $value, $mailBody);
}
}
if (Config::getVar('email', 'smtp')) {
$smtp =& Registry::get('smtpMailer', true, null);
if ($smtp === null) {
import('lib.pkp.classes.mail.SMTPMailer');
$smtp = new SMTPMailer();
}
$sent = $smtp->mail($this, $recipients, $subject, $mailBody, $headers);
} else {
$sent = String::mail($recipients, $subject, $mailBody, $headers, $additionalParameters);
}
if (!$sent) {
if (Config::getVar('debug', 'display_errors')) {
if (Config::getVar('email', 'smtp')) {
fatalError("There was an error sending this email. Please check your PHP error log for more information.");
return false;
} else {
fatalError("There was an error sending this email. Please check your mail log (/var/log/maillog).");
return false;
}
} else return false;
} else return true;
}
/**
* Encode a display name for proper inclusion with an email address.
* @param $displayName string
* @return string
*/
function encodeDisplayName($displayName, $send = false) {
if (String::regexp_match('!^[-A-Za-z0-9\!#\$%&\'\*\+\/=\?\^_\`\{\|\}~]+$!', $displayName)) return $displayName;
return ('"' . ($send ? String::encode_mime_header(str_replace(
array('"', '\\'),
'',
$displayName
)) : str_replace(
array('"', '\\'),
'',
$displayName
)) . '"');
}
}
?>