30 | | /** |
31 | | * The PHPMailer Version number. |
32 | | * @var string |
33 | | */ |
34 | | public $Version = '5.2.27'; |
35 | | |
36 | | /** |
37 | | * Email priority. |
38 | | * Options: null (default), 1 = High, 3 = Normal, 5 = low. |
39 | | * When null, the header is not set at all. |
40 | | * @var integer |
41 | | */ |
42 | | public $Priority = null; |
43 | | |
44 | | /** |
45 | | * The character set of the message. |
46 | | * @var string |
47 | | */ |
48 | | public $CharSet = 'iso-8859-1'; |
49 | | |
50 | | /** |
51 | | * The MIME Content-type of the message. |
52 | | * @var string |
53 | | */ |
54 | | public $ContentType = 'text/plain'; |
55 | | |
56 | | /** |
57 | | * The message encoding. |
58 | | * Options: "8bit", "7bit", "binary", "base64", and "quoted-printable". |
59 | | * @var string |
60 | | */ |
61 | | public $Encoding = '8bit'; |
62 | | |
63 | | /** |
64 | | * Holds the most recent mailer error message. |
65 | | * @var string |
66 | | */ |
67 | | public $ErrorInfo = ''; |
68 | | |
69 | | /** |
70 | | * The From email address for the message. |
71 | | * @var string |
72 | | */ |
73 | | public $From = 'root@localhost'; |
74 | | |
75 | | /** |
76 | | * The From name of the message. |
77 | | * @var string |
78 | | */ |
79 | | public $FromName = 'Root User'; |
80 | | |
81 | | /** |
82 | | * The Sender email (Return-Path) of the message. |
83 | | * If not empty, will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. |
84 | | * @var string |
85 | | */ |
86 | | public $Sender = ''; |
87 | | |
88 | | /** |
89 | | * The Return-Path of the message. |
90 | | * If empty, it will be set to either From or Sender. |
91 | | * @var string |
92 | | * @deprecated Email senders should never set a return-path header; |
93 | | * it's the receiver's job (RFC5321 section 4.4), so this no longer does anything. |
94 | | * @link https://tools.ietf.org/html/rfc5321#section-4.4 RFC5321 reference |
95 | | */ |
96 | | public $ReturnPath = ''; |
97 | | |
98 | | /** |
99 | | * The Subject of the message. |
100 | | * @var string |
101 | | */ |
102 | | public $Subject = ''; |
103 | | |
104 | | /** |
105 | | * An HTML or plain text message body. |
106 | | * If HTML then call isHTML(true). |
107 | | * @var string |
108 | | */ |
109 | | public $Body = ''; |
110 | | |
111 | | /** |
112 | | * The plain-text message body. |
113 | | * This body can be read by mail clients that do not have HTML email |
114 | | * capability such as mutt & Eudora. |
115 | | * Clients that can read HTML will view the normal Body. |
116 | | * @var string |
117 | | */ |
118 | | public $AltBody = ''; |
119 | | |
120 | | /** |
121 | | * An iCal message part body. |
122 | | * Only supported in simple alt or alt_inline message types |
123 | | * To generate iCal events, use the bundled extras/EasyPeasyICS.php class or iCalcreator |
124 | | * @link http://sprain.ch/blog/downloads/php-class-easypeasyics-create-ical-files-with-php/ |
125 | | * @link http://kigkonsult.se/iCalcreator/ |
126 | | * @var string |
127 | | */ |
128 | | public $Ical = ''; |
129 | | |
130 | | /** |
131 | | * The complete compiled MIME message body. |
132 | | * @access protected |
133 | | * @var string |
134 | | */ |
135 | | protected $MIMEBody = ''; |
136 | | |
137 | | /** |
138 | | * The complete compiled MIME message headers. |
139 | | * @var string |
140 | | * @access protected |
141 | | */ |
142 | | protected $MIMEHeader = ''; |
143 | | |
144 | | /** |
145 | | * Extra headers that createHeader() doesn't fold in. |
146 | | * @var string |
147 | | * @access protected |
148 | | */ |
149 | | protected $mailHeader = ''; |
150 | | |
151 | | /** |
152 | | * Word-wrap the message body to this number of chars. |
153 | | * Set to 0 to not wrap. A useful value here is 78, for RFC2822 section 2.1.1 compliance. |
154 | | * @var integer |
155 | | */ |
156 | | public $WordWrap = 0; |
157 | | |
158 | | /** |
159 | | * Which method to use to send mail. |
160 | | * Options: "mail", "sendmail", or "smtp". |
161 | | * @var string |
162 | | */ |
163 | | public $Mailer = 'mail'; |
164 | | |
165 | | /** |
166 | | * The path to the sendmail program. |
167 | | * @var string |
168 | | */ |
169 | | public $Sendmail = '/usr/sbin/sendmail'; |
170 | | |
171 | | /** |
172 | | * Whether mail() uses a fully sendmail-compatible MTA. |
173 | | * One which supports sendmail's "-oi -f" options. |
174 | | * @var boolean |
175 | | */ |
176 | | public $UseSendmailOptions = true; |
177 | | |
178 | | /** |
179 | | * Path to PHPMailer plugins. |
180 | | * Useful if the SMTP class is not in the PHP include path. |
181 | | * @var string |
182 | | * @deprecated Should not be needed now there is an autoloader. |
183 | | */ |
184 | | public $PluginDir = ''; |
185 | | |
186 | | /** |
187 | | * The email address that a reading confirmation should be sent to, also known as read receipt. |
188 | | * @var string |
189 | | */ |
190 | | public $ConfirmReadingTo = ''; |
191 | | |
192 | | /** |
193 | | * The hostname to use in the Message-ID header and as default HELO string. |
194 | | * If empty, PHPMailer attempts to find one with, in order, |
195 | | * $_SERVER['SERVER_NAME'], gethostname(), php_uname('n'), or the value |
196 | | * 'localhost.localdomain'. |
197 | | * @var string |
198 | | */ |
199 | | public $Hostname = ''; |
200 | | |
201 | | /** |
202 | | * An ID to be used in the Message-ID header. |
203 | | * If empty, a unique id will be generated. |
204 | | * You can set your own, but it must be in the format "<id@domain>", |
205 | | * as defined in RFC5322 section 3.6.4 or it will be ignored. |
206 | | * @see https://tools.ietf.org/html/rfc5322#section-3.6.4 |
207 | | * @var string |
208 | | */ |
209 | | public $MessageID = ''; |
210 | | |
211 | | /** |
212 | | * The message Date to be used in the Date header. |
213 | | * If empty, the current date will be added. |
214 | | * @var string |
215 | | */ |
216 | | public $MessageDate = ''; |
217 | | |
218 | | /** |
219 | | * SMTP hosts. |
220 | | * Either a single hostname or multiple semicolon-delimited hostnames. |
221 | | * You can also specify a different port |
222 | | * for each host by using this format: [hostname:port] |
223 | | * (e.g. "smtp1.example.com:25;smtp2.example.com"). |
224 | | * You can also specify encryption type, for example: |
225 | | * (e.g. "tls://smtp1.example.com:587;ssl://smtp2.example.com:465"). |
226 | | * Hosts will be tried in order. |
227 | | * @var string |
228 | | */ |
229 | | public $Host = 'localhost'; |
230 | | |
231 | | /** |
232 | | * The default SMTP server port. |
233 | | * @var integer |
234 | | * @TODO Why is this needed when the SMTP class takes care of it? |
235 | | */ |
236 | | public $Port = 25; |
237 | | |
238 | | /** |
239 | | * The SMTP HELO of the message. |
240 | | * Default is $Hostname. If $Hostname is empty, PHPMailer attempts to find |
241 | | * one with the same method described above for $Hostname. |
242 | | * @var string |
243 | | * @see PHPMailer::$Hostname |
244 | | */ |
245 | | public $Helo = ''; |
246 | | |
247 | | /** |
248 | | * What kind of encryption to use on the SMTP connection. |
249 | | * Options: '', 'ssl' or 'tls' |
250 | | * @var string |
251 | | */ |
252 | | public $SMTPSecure = ''; |
253 | | |
254 | | /** |
255 | | * Whether to enable TLS encryption automatically if a server supports it, |
256 | | * even if `SMTPSecure` is not set to 'tls'. |
257 | | * Be aware that in PHP >= 5.6 this requires that the server's certificates are valid. |
258 | | * @var boolean |
259 | | */ |
260 | | public $SMTPAutoTLS = true; |
261 | | |
262 | | /** |
263 | | * Whether to use SMTP authentication. |
264 | | * Uses the Username and Password properties. |
265 | | * @var boolean |
266 | | * @see PHPMailer::$Username |
267 | | * @see PHPMailer::$Password |
268 | | */ |
269 | | public $SMTPAuth = false; |
270 | | |
271 | | /** |
272 | | * Options array passed to stream_context_create when connecting via SMTP. |
273 | | * @var array |
274 | | */ |
275 | | public $SMTPOptions = array(); |
276 | | |
277 | | /** |
278 | | * SMTP username. |
279 | | * @var string |
280 | | */ |
281 | | public $Username = ''; |
282 | | |
283 | | /** |
284 | | * SMTP password. |
285 | | * @var string |
286 | | */ |
287 | | public $Password = ''; |
288 | | |
289 | | /** |
290 | | * SMTP auth type. |
291 | | * Options are CRAM-MD5, LOGIN, PLAIN, attempted in that order if not specified |
292 | | * @var string |
293 | | */ |
294 | | public $AuthType = ''; |
295 | | |
296 | | /** |
297 | | * SMTP realm. |
298 | | * Used for NTLM auth |
299 | | * @var string |
300 | | */ |
301 | | public $Realm = ''; |
302 | | |
303 | | /** |
304 | | * SMTP workstation. |
305 | | * Used for NTLM auth |
306 | | * @var string |
307 | | */ |
308 | | public $Workstation = ''; |
309 | | |
310 | | /** |
311 | | * The SMTP server timeout in seconds. |
312 | | * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2 |
313 | | * @var integer |
314 | | */ |
315 | | public $Timeout = 300; |
316 | | |
317 | | /** |
318 | | * SMTP class debug output mode. |
319 | | * Debug output level. |
320 | | * Options: |
321 | | * * `0` No output |
322 | | * * `1` Commands |
323 | | * * `2` Data and commands |
324 | | * * `3` As 2 plus connection status |
325 | | * * `4` Low-level data output |
326 | | * @var integer |
327 | | * @see SMTP::$do_debug |
328 | | */ |
329 | | public $SMTPDebug = 0; |
330 | | |
331 | | /** |
332 | | * How to handle debug output. |
333 | | * Options: |
334 | | * * `echo` Output plain-text as-is, appropriate for CLI |
335 | | * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output |
336 | | * * `error_log` Output to error log as configured in php.ini |
337 | | * |
338 | | * Alternatively, you can provide a callable expecting two params: a message string and the debug level: |
339 | | * <code> |
340 | | * $mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; |
341 | | * </code> |
342 | | * @var string|callable |
343 | | * @see SMTP::$Debugoutput |
344 | | */ |
345 | | public $Debugoutput = 'echo'; |
346 | | |
347 | | /** |
348 | | * Whether to keep SMTP connection open after each message. |
349 | | * If this is set to true then to close the connection |
350 | | * requires an explicit call to smtpClose(). |
351 | | * @var boolean |
352 | | */ |
353 | | public $SMTPKeepAlive = false; |
354 | | |
355 | | /** |
356 | | * Whether to split multiple to addresses into multiple messages |
357 | | * or send them all in one message. |
358 | | * Only supported in `mail` and `sendmail` transports, not in SMTP. |
359 | | * @var boolean |
360 | | */ |
361 | | public $SingleTo = false; |
362 | | |
363 | | /** |
364 | | * Storage for addresses when SingleTo is enabled. |
365 | | * @var array |
366 | | * @TODO This should really not be public |
367 | | */ |
368 | | public $SingleToArray = array(); |
369 | | |
370 | | /** |
371 | | * Whether to generate VERP addresses on send. |
372 | | * Only applicable when sending via SMTP. |
373 | | * @link https://en.wikipedia.org/wiki/Variable_envelope_return_path |
374 | | * @link http://www.postfix.org/VERP_README.html Postfix VERP info |
375 | | * @var boolean |
376 | | */ |
377 | | public $do_verp = false; |
378 | | |
379 | | /** |
380 | | * Whether to allow sending messages with an empty body. |
381 | | * @var boolean |
382 | | */ |
383 | | public $AllowEmpty = false; |
384 | | |
385 | | /** |
386 | | * The default line ending. |
387 | | * @note The default remains "\n". We force CRLF where we know |
388 | | * it must be used via self::CRLF. |
389 | | * @var string |
390 | | */ |
391 | | public $LE = "\n"; |
392 | | |
393 | | /** |
394 | | * DKIM selector. |
395 | | * @var string |
396 | | */ |
397 | | public $DKIM_selector = ''; |
398 | | |
399 | | /** |
400 | | * DKIM Identity. |
401 | | * Usually the email address used as the source of the email. |
402 | | * @var string |
403 | | */ |
404 | | public $DKIM_identity = ''; |
405 | | |
406 | | /** |
407 | | * DKIM passphrase. |
408 | | * Used if your key is encrypted. |
409 | | * @var string |
410 | | */ |
411 | | public $DKIM_passphrase = ''; |
412 | | |
413 | | /** |
414 | | * DKIM signing domain name. |
415 | | * @example 'example.com' |
416 | | * @var string |
417 | | */ |
418 | | public $DKIM_domain = ''; |
419 | | |
420 | | /** |
421 | | * DKIM private key file path. |
422 | | * @var string |
423 | | */ |
424 | | public $DKIM_private = ''; |
425 | | |
426 | | /** |
427 | | * DKIM private key string. |
428 | | * If set, takes precedence over `$DKIM_private`. |
429 | | * @var string |
430 | | */ |
431 | | public $DKIM_private_string = ''; |
432 | | |
433 | | /** |
434 | | * Callback Action function name. |
435 | | * |
436 | | * The function that handles the result of the send email action. |
437 | | * It is called out by send() for each email sent. |
438 | | * |
439 | | * Value can be any php callable: http://www.php.net/is_callable |
440 | | * |
441 | | * Parameters: |
442 | | * boolean $result result of the send action |
443 | | * array $to email addresses of the recipients |
444 | | * array $cc cc email addresses |
445 | | * array $bcc bcc email addresses |
446 | | * string $subject the subject |
447 | | * string $body the email body |
448 | | * string $from email address of sender |
449 | | * @var string |
450 | | */ |
451 | | public $action_function = ''; |
452 | | |
453 | | /** |
454 | | * What to put in the X-Mailer header. |
455 | | * Options: An empty string for PHPMailer default, whitespace for none, or a string to use |
456 | | * @var string |
457 | | */ |
458 | | public $XMailer = ''; |
459 | | |
460 | | /** |
461 | | * Which validator to use by default when validating email addresses. |
462 | | * May be a callable to inject your own validator, but there are several built-in validators. |
463 | | * @see PHPMailer::validateAddress() |
464 | | * @var string|callable |
465 | | * @static |
466 | | */ |
467 | | public static $validator = 'auto'; |
468 | | |
469 | | /** |
470 | | * An instance of the SMTP sender class. |
471 | | * @var SMTP |
472 | | * @access protected |
473 | | */ |
474 | | protected $smtp = null; |
475 | | |
476 | | /** |
477 | | * The array of 'to' names and addresses. |
478 | | * @var array |
479 | | * @access protected |
480 | | */ |
481 | | protected $to = array(); |
482 | | |
483 | | /** |
484 | | * The array of 'cc' names and addresses. |
485 | | * @var array |
486 | | * @access protected |
487 | | */ |
488 | | protected $cc = array(); |
489 | | |
490 | | /** |
491 | | * The array of 'bcc' names and addresses. |
492 | | * @var array |
493 | | * @access protected |
494 | | */ |
495 | | protected $bcc = array(); |
496 | | |
497 | | /** |
498 | | * The array of reply-to names and addresses. |
499 | | * @var array |
500 | | * @access protected |
501 | | */ |
502 | | protected $ReplyTo = array(); |
503 | | |
504 | | /** |
505 | | * An array of all kinds of addresses. |
506 | | * Includes all of $to, $cc, $bcc |
507 | | * @var array |
508 | | * @access protected |
509 | | * @see PHPMailer::$to @see PHPMailer::$cc @see PHPMailer::$bcc |
510 | | */ |
511 | | protected $all_recipients = array(); |
512 | | |
513 | | /** |
514 | | * An array of names and addresses queued for validation. |
515 | | * In send(), valid and non duplicate entries are moved to $all_recipients |
516 | | * and one of $to, $cc, or $bcc. |
517 | | * This array is used only for addresses with IDN. |
518 | | * @var array |
519 | | * @access protected |
520 | | * @see PHPMailer::$to @see PHPMailer::$cc @see PHPMailer::$bcc |
521 | | * @see PHPMailer::$all_recipients |
522 | | */ |
523 | | protected $RecipientsQueue = array(); |
524 | | |
525 | | /** |
526 | | * An array of reply-to names and addresses queued for validation. |
527 | | * In send(), valid and non duplicate entries are moved to $ReplyTo. |
528 | | * This array is used only for addresses with IDN. |
529 | | * @var array |
530 | | * @access protected |
531 | | * @see PHPMailer::$ReplyTo |
532 | | */ |
533 | | protected $ReplyToQueue = array(); |
534 | | |
535 | | /** |
536 | | * The array of attachments. |
537 | | * @var array |
538 | | * @access protected |
539 | | */ |
540 | | protected $attachment = array(); |
541 | | |
542 | | /** |
543 | | * The array of custom headers. |
544 | | * @var array |
545 | | * @access protected |
546 | | */ |
547 | | protected $CustomHeader = array(); |
548 | | |
549 | | /** |
550 | | * The most recent Message-ID (including angular brackets). |
551 | | * @var string |
552 | | * @access protected |
553 | | */ |
554 | | protected $lastMessageID = ''; |
555 | | |
556 | | /** |
557 | | * The message's MIME type. |
558 | | * @var string |
559 | | * @access protected |
560 | | */ |
561 | | protected $message_type = ''; |
562 | | |
563 | | /** |
564 | | * The array of MIME boundary strings. |
565 | | * @var array |
566 | | * @access protected |
567 | | */ |
568 | | protected $boundary = array(); |
569 | | |
570 | | /** |
571 | | * The array of available languages. |
572 | | * @var array |
573 | | * @access protected |
574 | | */ |
575 | | protected $language = array(); |
576 | | |
577 | | /** |
578 | | * The number of errors encountered. |
579 | | * @var integer |
580 | | * @access protected |
581 | | */ |
582 | | protected $error_count = 0; |
583 | | |
584 | | /** |
585 | | * The S/MIME certificate file path. |
586 | | * @var string |
587 | | * @access protected |
588 | | */ |
589 | | protected $sign_cert_file = ''; |
590 | | |
591 | | /** |
592 | | * The S/MIME key file path. |
593 | | * @var string |
594 | | * @access protected |
595 | | */ |
596 | | protected $sign_key_file = ''; |
597 | | |
598 | | /** |
599 | | * The optional S/MIME extra certificates ("CA Chain") file path. |
600 | | * @var string |
601 | | * @access protected |
602 | | */ |
603 | | protected $sign_extracerts_file = ''; |
604 | | |
605 | | /** |
606 | | * The S/MIME password for the key. |
607 | | * Used only if the key is encrypted. |
608 | | * @var string |
609 | | * @access protected |
610 | | */ |
611 | | protected $sign_key_pass = ''; |
612 | | |
613 | | /** |
614 | | * Whether to throw exceptions for errors. |
615 | | * @var boolean |
616 | | * @access protected |
617 | | */ |
618 | | protected $exceptions = false; |
619 | | |
620 | | /** |
621 | | * Unique ID used for message ID and boundaries. |
622 | | * @var string |
623 | | * @access protected |
624 | | */ |
625 | | protected $uniqueid = ''; |
626 | | |
627 | | /** |
628 | | * Error severity: message only, continue processing. |
629 | | */ |
630 | | const STOP_MESSAGE = 0; |
631 | | |
632 | | /** |
633 | | * Error severity: message, likely ok to continue processing. |
634 | | */ |
635 | | const STOP_CONTINUE = 1; |
636 | | |
637 | | /** |
638 | | * Error severity: message, plus full stop, critical error reached. |
639 | | */ |
640 | | const STOP_CRITICAL = 2; |
641 | | |
642 | | /** |
643 | | * SMTP RFC standard line ending. |
644 | | */ |
645 | | const CRLF = "\r\n"; |
646 | | |
647 | | /** |
648 | | * The maximum line length allowed by RFC 2822 section 2.1.1 |
649 | | * @var integer |
650 | | */ |
651 | | const MAX_LINE_LENGTH = 998; |
652 | | |
653 | | /** |
654 | | * Constructor. |
655 | | * @param boolean $exceptions Should we throw external exceptions? |
656 | | */ |
657 | | public function __construct($exceptions = null) |
658 | | { |
659 | | if ($exceptions !== null) { |
660 | | $this->exceptions = (boolean)$exceptions; |
661 | | } |
662 | | //Pick an appropriate debug output format automatically |
663 | | $this->Debugoutput = (strpos(PHP_SAPI, 'cli') !== false ? 'echo' : 'html'); |
664 | | } |
665 | | |
666 | | /** |
667 | | * Destructor. |
668 | | */ |
669 | | public function __destruct() |
670 | | { |
671 | | //Close any open SMTP connection nicely |
672 | | $this->smtpClose(); |
673 | | } |
674 | | |
675 | | /** |
676 | | * Call mail() in a safe_mode-aware fashion. |
677 | | * Also, unless sendmail_path points to sendmail (or something that |
678 | | * claims to be sendmail), don't pass params (not a perfect fix, |
679 | | * but it will do) |
680 | | * @param string $to To |
681 | | * @param string $subject Subject |
682 | | * @param string $body Message Body |
683 | | * @param string $header Additional Header(s) |
684 | | * @param string $params Params |
685 | | * @access private |
686 | | * @return boolean |
687 | | */ |
688 | | private function mailPassthru($to, $subject, $body, $header, $params) |
689 | | { |
690 | | //Check overloading of mail function to avoid double-encoding |
691 | | if (ini_get('mbstring.func_overload') & 1) { |
692 | | $subject = $this->secureHeader($subject); |
693 | | } else { |
694 | | $subject = $this->encodeHeader($this->secureHeader($subject)); |
695 | | } |
696 | | |
697 | | //Can't use additional_parameters in safe_mode, calling mail() with null params breaks |
698 | | //@link http://php.net/manual/en/function.mail.php |
699 | | if (ini_get('safe_mode') or !$this->UseSendmailOptions or is_null($params)) { |
700 | | $result = @mail($to, $subject, $body, $header); |
701 | | } else { |
702 | | $result = @mail($to, $subject, $body, $header, $params); |
703 | | } |
704 | | return $result; |
705 | | } |
706 | | /** |
707 | | * Output debugging info via user-defined method. |
708 | | * Only generates output if SMTP debug output is enabled (@see SMTP::$do_debug). |
709 | | * @see PHPMailer::$Debugoutput |
710 | | * @see PHPMailer::$SMTPDebug |
711 | | * @param string $str |
712 | | */ |
713 | | protected function edebug($str) |
714 | | { |
715 | | if ($this->SMTPDebug <= 0) { |
716 | | return; |
717 | | } |
718 | | //Avoid clash with built-in function names |
719 | | if (!in_array($this->Debugoutput, array('error_log', 'html', 'echo')) and is_callable($this->Debugoutput)) { |
720 | | call_user_func($this->Debugoutput, $str, $this->SMTPDebug); |
721 | | return; |
722 | | } |
723 | | switch ($this->Debugoutput) { |
724 | | case 'error_log': |
725 | | //Don't output, just log |
726 | | error_log($str); |
727 | | break; |
728 | | case 'html': |
729 | | //Cleans up output a bit for a better looking, HTML-safe output |
730 | | echo htmlentities( |
731 | | preg_replace('/[\r\n]+/', '', $str), |
732 | | ENT_QUOTES, |
733 | | 'UTF-8' |
734 | | ) |
735 | | . "<br>\n"; |
736 | | break; |
737 | | case 'echo': |
738 | | default: |
739 | | //Normalize line breaks |
740 | | $str = preg_replace('/\r\n?/ms', "\n", $str); |
741 | | echo gmdate('Y-m-d H:i:s') . "\t" . str_replace( |
742 | | "\n", |
743 | | "\n \t ", |
744 | | trim($str) |
745 | | ) . "\n"; |
746 | | } |
747 | | } |
748 | | |
749 | | /** |
750 | | * Sets message type to HTML or plain. |
751 | | * @param boolean $isHtml True for HTML mode. |
752 | | * @return void |
753 | | */ |
754 | | public function isHTML($isHtml = true) |
755 | | { |
756 | | if ($isHtml) { |
757 | | $this->ContentType = 'text/html'; |
758 | | } else { |
759 | | $this->ContentType = 'text/plain'; |
760 | | } |
761 | | } |
762 | | |
763 | | /** |
764 | | * Send messages using SMTP. |
765 | | * @return void |
766 | | */ |
767 | | public function isSMTP() |
768 | | { |
769 | | $this->Mailer = 'smtp'; |
770 | | } |
771 | | |
772 | | /** |
773 | | * Send messages using PHP's mail() function. |
774 | | * @return void |
775 | | */ |
776 | | public function isMail() |
777 | | { |
778 | | $this->Mailer = 'mail'; |
779 | | } |
780 | | |
781 | | /** |
782 | | * Send messages using $Sendmail. |
783 | | * @return void |
784 | | */ |
785 | | public function isSendmail() |
786 | | { |
787 | | $ini_sendmail_path = ini_get('sendmail_path'); |
788 | | |
789 | | if (!stristr($ini_sendmail_path, 'sendmail')) { |
790 | | $this->Sendmail = '/usr/sbin/sendmail'; |
791 | | } else { |
792 | | $this->Sendmail = $ini_sendmail_path; |
793 | | } |
794 | | $this->Mailer = 'sendmail'; |
795 | | } |
796 | | |
797 | | /** |
798 | | * Send messages using qmail. |
799 | | * @return void |
800 | | */ |
801 | | public function isQmail() |
802 | | { |
803 | | $ini_sendmail_path = ini_get('sendmail_path'); |
804 | | |
805 | | if (!stristr($ini_sendmail_path, 'qmail')) { |
806 | | $this->Sendmail = '/var/qmail/bin/qmail-inject'; |
807 | | } else { |
808 | | $this->Sendmail = $ini_sendmail_path; |
809 | | } |
810 | | $this->Mailer = 'qmail'; |
811 | | } |
812 | | |
813 | | /** |
814 | | * Add a "To" address. |
815 | | * @param string $address The email address to send to |
816 | | * @param string $name |
817 | | * @return boolean true on success, false if address already used or invalid in some way |
818 | | */ |
819 | | public function addAddress($address, $name = '') |
820 | | { |
821 | | return $this->addOrEnqueueAnAddress('to', $address, $name); |
822 | | } |
823 | | |
824 | | /** |
825 | | * Add a "CC" address. |
826 | | * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer. |
827 | | * @param string $address The email address to send to |
828 | | * @param string $name |
829 | | * @return boolean true on success, false if address already used or invalid in some way |
830 | | */ |
831 | | public function addCC($address, $name = '') |
832 | | { |
833 | | return $this->addOrEnqueueAnAddress('cc', $address, $name); |
834 | | } |
835 | | |
836 | | /** |
837 | | * Add a "BCC" address. |
838 | | * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer. |
839 | | * @param string $address The email address to send to |
840 | | * @param string $name |
841 | | * @return boolean true on success, false if address already used or invalid in some way |
842 | | */ |
843 | | public function addBCC($address, $name = '') |
844 | | { |
845 | | return $this->addOrEnqueueAnAddress('bcc', $address, $name); |
846 | | } |
847 | | |
848 | | /** |
849 | | * Add a "Reply-To" address. |
850 | | * @param string $address The email address to reply to |
851 | | * @param string $name |
852 | | * @return boolean true on success, false if address already used or invalid in some way |
853 | | */ |
854 | | public function addReplyTo($address, $name = '') |
855 | | { |
856 | | return $this->addOrEnqueueAnAddress('Reply-To', $address, $name); |
857 | | } |
858 | | |
859 | | /** |
860 | | * Add an address to one of the recipient arrays or to the ReplyTo array. Because PHPMailer |
861 | | * can't validate addresses with an IDN without knowing the PHPMailer::$CharSet (that can still |
862 | | * be modified after calling this function), addition of such addresses is delayed until send(). |
863 | | * Addresses that have been added already return false, but do not throw exceptions. |
864 | | * @param string $kind One of 'to', 'cc', 'bcc', or 'ReplyTo' |
865 | | * @param string $address The email address to send, resp. to reply to |
866 | | * @param string $name |
867 | | * @throws phpmailerException |
868 | | * @return boolean true on success, false if address already used or invalid in some way |
869 | | * @access protected |
870 | | */ |
871 | | protected function addOrEnqueueAnAddress($kind, $address, $name) |
872 | | { |
873 | | $address = trim($address); |
874 | | $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim |
875 | | if (($pos = strrpos($address, '@')) === false) { |
876 | | // At-sign is misssing. |
877 | | $error_message = $this->lang('invalid_address') . " (addAnAddress $kind): $address"; |
878 | | $this->setError($error_message); |
879 | | $this->edebug($error_message); |
880 | | if ($this->exceptions) { |
881 | | throw new phpmailerException($error_message); |
882 | | } |
883 | | return false; |
884 | | } |
885 | | $params = array($kind, $address, $name); |
886 | | // Enqueue addresses with IDN until we know the PHPMailer::$CharSet. |
887 | | if ($this->has8bitChars(substr($address, ++$pos)) and $this->idnSupported()) { |
888 | | if ($kind != 'Reply-To') { |
889 | | if (!array_key_exists($address, $this->RecipientsQueue)) { |
890 | | $this->RecipientsQueue[$address] = $params; |
891 | | return true; |
892 | | } |
893 | | } else { |
894 | | if (!array_key_exists($address, $this->ReplyToQueue)) { |
895 | | $this->ReplyToQueue[$address] = $params; |
896 | | return true; |
897 | | } |
898 | | } |
899 | | return false; |
900 | | } |
901 | | // Immediately add standard addresses without IDN. |
902 | | return call_user_func_array(array($this, 'addAnAddress'), $params); |
903 | | } |
904 | | |
905 | | /** |
906 | | * Add an address to one of the recipient arrays or to the ReplyTo array. |
907 | | * Addresses that have been added already return false, but do not throw exceptions. |
908 | | * @param string $kind One of 'to', 'cc', 'bcc', or 'ReplyTo' |
909 | | * @param string $address The email address to send, resp. to reply to |
910 | | * @param string $name |
911 | | * @throws phpmailerException |
912 | | * @return boolean true on success, false if address already used or invalid in some way |
913 | | * @access protected |
914 | | */ |
915 | | protected function addAnAddress($kind, $address, $name = '') |
916 | | { |
917 | | if (!in_array($kind, array('to', 'cc', 'bcc', 'Reply-To'))) { |
918 | | $error_message = $this->lang('Invalid recipient kind: ') . $kind; |
919 | | $this->setError($error_message); |
920 | | $this->edebug($error_message); |
921 | | if ($this->exceptions) { |
922 | | throw new phpmailerException($error_message); |
923 | | } |
924 | | return false; |
925 | | } |
926 | | if (!$this->validateAddress($address)) { |
927 | | $error_message = $this->lang('invalid_address') . " (addAnAddress $kind): $address"; |
928 | | $this->setError($error_message); |
929 | | $this->edebug($error_message); |
930 | | if ($this->exceptions) { |
931 | | throw new phpmailerException($error_message); |
932 | | } |
933 | | return false; |
934 | | } |
935 | | if ($kind != 'Reply-To') { |
936 | | if (!array_key_exists(strtolower($address), $this->all_recipients)) { |
937 | | array_push($this->$kind, array($address, $name)); |
938 | | $this->all_recipients[strtolower($address)] = true; |
939 | | return true; |
940 | | } |
941 | | } else { |
942 | | if (!array_key_exists(strtolower($address), $this->ReplyTo)) { |
943 | | $this->ReplyTo[strtolower($address)] = array($address, $name); |
944 | | return true; |
945 | | } |
946 | | } |
947 | | return false; |
948 | | } |
949 | | |
950 | | /** |
951 | | * Parse and validate a string containing one or more RFC822-style comma-separated email addresses |
952 | | * of the form "display name <address>" into an array of name/address pairs. |
953 | | * Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available. |
954 | | * Note that quotes in the name part are removed. |
955 | | * @param string $addrstr The address list string |
956 | | * @param bool $useimap Whether to use the IMAP extension to parse the list |
957 | | * @return array |
958 | | * @link http://www.andrew.cmu.edu/user/agreen1/testing/mrbs/web/Mail/RFC822.php A more careful implementation |
959 | | */ |
960 | | public function parseAddresses($addrstr, $useimap = true) |
961 | | { |
962 | | $addresses = array(); |
963 | | if ($useimap and function_exists('imap_rfc822_parse_adrlist')) { |
964 | | //Use this built-in parser if it's available |
965 | | $list = imap_rfc822_parse_adrlist($addrstr, ''); |
966 | | foreach ($list as $address) { |
967 | | if ($address->host != '.SYNTAX-ERROR.') { |
968 | | if ($this->validateAddress($address->mailbox . '@' . $address->host)) { |
969 | | $addresses[] = array( |
970 | | 'name' => (property_exists($address, 'personal') ? $address->personal : ''), |
971 | | 'address' => $address->mailbox . '@' . $address->host |
972 | | ); |
973 | | } |
974 | | } |
975 | | } |
976 | | } else { |
977 | | //Use this simpler parser |
978 | | $list = explode(',', $addrstr); |
979 | | foreach ($list as $address) { |
980 | | $address = trim($address); |
981 | | //Is there a separate name part? |
982 | | if (strpos($address, '<') === false) { |
983 | | //No separate name, just use the whole thing |
984 | | if ($this->validateAddress($address)) { |
985 | | $addresses[] = array( |
986 | | 'name' => '', |
987 | | 'address' => $address |
988 | | ); |
989 | | } |
990 | | } else { |
991 | | list($name, $email) = explode('<', $address); |
992 | | $email = trim(str_replace('>', '', $email)); |
993 | | if ($this->validateAddress($email)) { |
994 | | $addresses[] = array( |
995 | | 'name' => trim(str_replace(array('"', "'"), '', $name)), |
996 | | 'address' => $email |
997 | | ); |
998 | | } |
999 | | } |
1000 | | } |
1001 | | } |
1002 | | return $addresses; |
1003 | | } |
1004 | | |
1005 | | /** |
1006 | | * Set the From and FromName properties. |
1007 | | * @param string $address |
1008 | | * @param string $name |
1009 | | * @param boolean $auto Whether to also set the Sender address, defaults to true |
1010 | | * @throws phpmailerException |
1011 | | * @return boolean |
1012 | | */ |
1013 | | public function setFrom($address, $name = '', $auto = true) |
1014 | | { |
1015 | | $address = trim($address); |
1016 | | $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim |
1017 | | // Don't validate now addresses with IDN. Will be done in send(). |
1018 | | if (($pos = strrpos($address, '@')) === false or |
1019 | | (!$this->has8bitChars(substr($address, ++$pos)) or !$this->idnSupported()) and |
1020 | | !$this->validateAddress($address)) { |
1021 | | $error_message = $this->lang('invalid_address') . " (setFrom) $address"; |
1022 | | $this->setError($error_message); |
1023 | | $this->edebug($error_message); |
1024 | | if ($this->exceptions) { |
1025 | | throw new phpmailerException($error_message); |
1026 | | } |
1027 | | return false; |
1028 | | } |
1029 | | $this->From = $address; |
1030 | | $this->FromName = $name; |
1031 | | if ($auto) { |
1032 | | if (empty($this->Sender)) { |
1033 | | $this->Sender = $address; |
1034 | | } |
1035 | | } |
1036 | | return true; |
1037 | | } |
1038 | | |
1039 | | /** |
1040 | | * Return the Message-ID header of the last email. |
1041 | | * Technically this is the value from the last time the headers were created, |
1042 | | * but it's also the message ID of the last sent message except in |
1043 | | * pathological cases. |
1044 | | * @return string |
1045 | | */ |
1046 | | public function getLastMessageID() |
1047 | | { |
1048 | | return $this->lastMessageID; |
1049 | | } |
1050 | | |
1051 | | /** |
1052 | | * Check that a string looks like an email address. |
1053 | | * @param string $address The email address to check |
1054 | | * @param string|callable $patternselect A selector for the validation pattern to use : |
1055 | | * * `auto` Pick best pattern automatically; |
1056 | | * * `pcre8` Use the squiloople.com pattern, requires PCRE > 8.0, PHP >= 5.3.2, 5.2.14; |
1057 | | * * `pcre` Use old PCRE implementation; |
1058 | | * * `php` Use PHP built-in FILTER_VALIDATE_EMAIL; |
1059 | | * * `html5` Use the pattern given by the HTML5 spec for 'email' type form input elements. |
1060 | | * * `noregex` Don't use a regex: super fast, really dumb. |
1061 | | * Alternatively you may pass in a callable to inject your own validator, for example: |
1062 | | * PHPMailer::validateAddress('user@example.com', function($address) { |
1063 | | * return (strpos($address, '@') !== false); |
1064 | | * }); |
1065 | | * You can also set the PHPMailer::$validator static to a callable, allowing built-in methods to use your validator. |
1066 | | * @return boolean |
1067 | | * @static |
1068 | | * @access public |
1069 | | */ |
1070 | | public static function validateAddress($address, $patternselect = null) |
1071 | | { |
1072 | | if (is_null($patternselect)) { |
1073 | | $patternselect = self::$validator; |
1074 | | } |
1075 | | if (is_callable($patternselect)) { |
1076 | | return call_user_func($patternselect, $address); |
1077 | | } |
1078 | | //Reject line breaks in addresses; it's valid RFC5322, but not RFC5321 |
1079 | | if (strpos($address, "\n") !== false or strpos($address, "\r") !== false) { |
1080 | | return false; |
1081 | | } |
1082 | | if (!$patternselect or $patternselect == 'auto') { |
1083 | | //Check this constant first so it works when extension_loaded() is disabled by safe mode |
1084 | | //Constant was added in PHP 5.2.4 |
1085 | | if (defined('PCRE_VERSION')) { |
1086 | | //This pattern can get stuck in a recursive loop in PCRE <= 8.0.2 |
1087 | | if (version_compare(PCRE_VERSION, '8.0.3') >= 0) { |
1088 | | $patternselect = 'pcre8'; |
1089 | | } else { |
1090 | | $patternselect = 'pcre'; |
1091 | | } |
1092 | | } elseif (function_exists('extension_loaded') and extension_loaded('pcre')) { |
1093 | | //Fall back to older PCRE |
1094 | | $patternselect = 'pcre'; |
1095 | | } else { |
1096 | | //Filter_var appeared in PHP 5.2.0 and does not require the PCRE extension |
1097 | | if (version_compare(PHP_VERSION, '5.2.0') >= 0) { |
1098 | | $patternselect = 'php'; |
1099 | | } else { |
1100 | | $patternselect = 'noregex'; |
1101 | | } |
1102 | | } |
1103 | | } |
1104 | | switch ($patternselect) { |
1105 | | case 'pcre8': |
1106 | | /** |
1107 | | * Uses the same RFC5322 regex on which FILTER_VALIDATE_EMAIL is based, but allows dotless domains. |
1108 | | * @link http://squiloople.com/2009/12/20/email-address-validation/ |
| 33 | const CHARSET_ASCII = 'us-ascii'; |
| 34 | const CHARSET_ISO88591 = 'iso-8859-1'; |
| 35 | const CHARSET_UTF8 = 'utf-8'; |
| 36 | |
| 37 | const CONTENT_TYPE_PLAINTEXT = 'text/plain'; |
| 38 | const CONTENT_TYPE_TEXT_CALENDAR = 'text/calendar'; |
| 39 | const CONTENT_TYPE_TEXT_HTML = 'text/html'; |
| 40 | const CONTENT_TYPE_MULTIPART_ALTERNATIVE = 'multipart/alternative'; |
| 41 | const CONTENT_TYPE_MULTIPART_MIXED = 'multipart/mixed'; |
| 42 | const CONTENT_TYPE_MULTIPART_RELATED = 'multipart/related'; |
| 43 | |
| 44 | const ENCODING_7BIT = '7bit'; |
| 45 | const ENCODING_8BIT = '8bit'; |
| 46 | const ENCODING_BASE64 = 'base64'; |
| 47 | const ENCODING_BINARY = 'binary'; |
| 48 | const ENCODING_QUOTED_PRINTABLE = 'quoted-printable'; |
| 49 | |
| 50 | const ENCRYPTION_STARTTLS = 'tls'; |
| 51 | const ENCRYPTION_SMTPS = 'ssl'; |
| 52 | |
| 53 | const ICAL_METHOD_REQUEST = 'REQUEST'; |
| 54 | const ICAL_METHOD_PUBLISH = 'PUBLISH'; |
| 55 | const ICAL_METHOD_REPLY = 'REPLY'; |
| 56 | const ICAL_METHOD_ADD = 'ADD'; |
| 57 | const ICAL_METHOD_CANCEL = 'CANCEL'; |
| 58 | const ICAL_METHOD_REFRESH = 'REFRESH'; |
| 59 | const ICAL_METHOD_COUNTER = 'COUNTER'; |
| 60 | const ICAL_METHOD_DECLINECOUNTER = 'DECLINECOUNTER'; |
| 61 | |
| 62 | /** |
| 63 | * Email priority. |
| 64 | * Options: null (default), 1 = High, 3 = Normal, 5 = low. |
| 65 | * When null, the header is not set at all. |
| 66 | * |
| 67 | * @var int |
| 68 | */ |
| 69 | public $Priority; |
| 70 | |
| 71 | /** |
| 72 | * The character set of the message. |
| 73 | * |
| 74 | * @var string |
| 75 | */ |
| 76 | public $CharSet = self::CHARSET_ISO88591; |
| 77 | |
| 78 | /** |
| 79 | * The MIME Content-type of the message. |
| 80 | * |
| 81 | * @var string |
| 82 | */ |
| 83 | public $ContentType = self::CONTENT_TYPE_PLAINTEXT; |
| 84 | |
| 85 | /** |
| 86 | * The message encoding. |
| 87 | * Options: "8bit", "7bit", "binary", "base64", and "quoted-printable". |
| 88 | * |
| 89 | * @var string |
| 90 | */ |
| 91 | public $Encoding = self::ENCODING_8BIT; |
| 92 | |
| 93 | /** |
| 94 | * Holds the most recent mailer error message. |
| 95 | * |
| 96 | * @var string |
| 97 | */ |
| 98 | public $ErrorInfo = ''; |
| 99 | |
| 100 | /** |
| 101 | * The From email address for the message. |
| 102 | * |
| 103 | * @var string |
| 104 | */ |
| 105 | public $From = 'root@localhost'; |
| 106 | |
| 107 | /** |
| 108 | * The From name of the message. |
| 109 | * |
| 110 | * @var string |
| 111 | */ |
| 112 | public $FromName = 'Root User'; |
| 113 | |
| 114 | /** |
| 115 | * The envelope sender of the message. |
| 116 | * This will usually be turned into a Return-Path header by the receiver, |
| 117 | * and is the address that bounces will be sent to. |
| 118 | * If not empty, will be passed via `-f` to sendmail or as the 'MAIL FROM' value over SMTP. |
| 119 | * |
| 120 | * @var string |
| 121 | */ |
| 122 | public $Sender = ''; |
| 123 | |
| 124 | /** |
| 125 | * The Subject of the message. |
| 126 | * |
| 127 | * @var string |
| 128 | */ |
| 129 | public $Subject = ''; |
| 130 | |
| 131 | /** |
| 132 | * An HTML or plain text message body. |
| 133 | * If HTML then call isHTML(true). |
| 134 | * |
| 135 | * @var string |
| 136 | */ |
| 137 | public $Body = ''; |
| 138 | |
| 139 | /** |
| 140 | * The plain-text message body. |
| 141 | * This body can be read by mail clients that do not have HTML email |
| 142 | * capability such as mutt & Eudora. |
| 143 | * Clients that can read HTML will view the normal Body. |
| 144 | * |
| 145 | * @var string |
| 146 | */ |
| 147 | public $AltBody = ''; |
| 148 | |
| 149 | /** |
| 150 | * An iCal message part body. |
| 151 | * Only supported in simple alt or alt_inline message types |
| 152 | * To generate iCal event structures, use classes like EasyPeasyICS or iCalcreator. |
| 153 | * |
| 154 | * @see http://sprain.ch/blog/downloads/php-class-easypeasyics-create-ical-files-with-php/ |
| 155 | * @see http://kigkonsult.se/iCalcreator/ |
| 156 | * |
| 157 | * @var string |
| 158 | */ |
| 159 | public $Ical = ''; |
| 160 | |
| 161 | /** |
| 162 | * Value-array of "method" in Contenttype header "text/calendar" |
| 163 | * |
| 164 | * @var string[] |
| 165 | */ |
| 166 | protected static $IcalMethods = [ |
| 167 | self::ICAL_METHOD_REQUEST, |
| 168 | self::ICAL_METHOD_PUBLISH, |
| 169 | self::ICAL_METHOD_REPLY, |
| 170 | self::ICAL_METHOD_ADD, |
| 171 | self::ICAL_METHOD_CANCEL, |
| 172 | self::ICAL_METHOD_REFRESH, |
| 173 | self::ICAL_METHOD_COUNTER, |
| 174 | self::ICAL_METHOD_DECLINECOUNTER, |
| 175 | ]; |
| 176 | |
| 177 | /** |
| 178 | * The complete compiled MIME message body. |
| 179 | * |
| 180 | * @var string |
| 181 | */ |
| 182 | protected $MIMEBody = ''; |
| 183 | |
| 184 | /** |
| 185 | * The complete compiled MIME message headers. |
| 186 | * |
| 187 | * @var string |
| 188 | */ |
| 189 | protected $MIMEHeader = ''; |
| 190 | |
| 191 | /** |
| 192 | * Extra headers that createHeader() doesn't fold in. |
| 193 | * |
| 194 | * @var string |
| 195 | */ |
| 196 | protected $mailHeader = ''; |
| 197 | |
| 198 | /** |
| 199 | * Word-wrap the message body to this number of chars. |
| 200 | * Set to 0 to not wrap. A useful value here is 78, for RFC2822 section 2.1.1 compliance. |
| 201 | * |
| 202 | * @see static::STD_LINE_LENGTH |
| 203 | * |
| 204 | * @var int |
| 205 | */ |
| 206 | public $WordWrap = 0; |
| 207 | |
| 208 | /** |
| 209 | * Which method to use to send mail. |
| 210 | * Options: "mail", "sendmail", or "smtp". |
| 211 | * |
| 212 | * @var string |
| 213 | */ |
| 214 | public $Mailer = 'mail'; |
| 215 | |
| 216 | /** |
| 217 | * The path to the sendmail program. |
| 218 | * |
| 219 | * @var string |
| 220 | */ |
| 221 | public $Sendmail = '/usr/sbin/sendmail'; |
| 222 | |
| 223 | /** |
| 224 | * Whether mail() uses a fully sendmail-compatible MTA. |
| 225 | * One which supports sendmail's "-oi -f" options. |
| 226 | * |
| 227 | * @var bool |
| 228 | */ |
| 229 | public $UseSendmailOptions = true; |
| 230 | |
| 231 | /** |
| 232 | * The email address that a reading confirmation should be sent to, also known as read receipt. |
| 233 | * |
| 234 | * @var string |
| 235 | */ |
| 236 | public $ConfirmReadingTo = ''; |
| 237 | |
| 238 | /** |
| 239 | * The hostname to use in the Message-ID header and as default HELO string. |
| 240 | * If empty, PHPMailer attempts to find one with, in order, |
| 241 | * $_SERVER['SERVER_NAME'], gethostname(), php_uname('n'), or the value |
| 242 | * 'localhost.localdomain'. |
| 243 | * |
| 244 | * @see PHPMailer::$Helo |
| 245 | * |
| 246 | * @var string |
| 247 | */ |
| 248 | public $Hostname = ''; |
| 249 | |
| 250 | /** |
| 251 | * An ID to be used in the Message-ID header. |
| 252 | * If empty, a unique id will be generated. |
| 253 | * You can set your own, but it must be in the format "<id@domain>", |
| 254 | * as defined in RFC5322 section 3.6.4 or it will be ignored. |
| 255 | * |
| 256 | * @see https://tools.ietf.org/html/rfc5322#section-3.6.4 |
| 257 | * |
| 258 | * @var string |
| 259 | */ |
| 260 | public $MessageID = ''; |
| 261 | |
| 262 | /** |
| 263 | * The message Date to be used in the Date header. |
| 264 | * If empty, the current date will be added. |
| 265 | * |
| 266 | * @var string |
| 267 | */ |
| 268 | public $MessageDate = ''; |
| 269 | |
| 270 | /** |
| 271 | * SMTP hosts. |
| 272 | * Either a single hostname or multiple semicolon-delimited hostnames. |
| 273 | * You can also specify a different port |
| 274 | * for each host by using this format: [hostname:port] |
| 275 | * (e.g. "smtp1.example.com:25;smtp2.example.com"). |
| 276 | * You can also specify encryption type, for example: |
| 277 | * (e.g. "tls://smtp1.example.com:587;ssl://smtp2.example.com:465"). |
| 278 | * Hosts will be tried in order. |
| 279 | * |
| 280 | * @var string |
| 281 | */ |
| 282 | public $Host = 'localhost'; |
| 283 | |
| 284 | /** |
| 285 | * The default SMTP server port. |
| 286 | * |
| 287 | * @var int |
| 288 | */ |
| 289 | public $Port = 25; |
| 290 | |
| 291 | /** |
| 292 | * The SMTP HELO/EHLO name used for the SMTP connection. |
| 293 | * Default is $Hostname. If $Hostname is empty, PHPMailer attempts to find |
| 294 | * one with the same method described above for $Hostname. |
| 295 | * |
| 296 | * @see PHPMailer::$Hostname |
| 297 | * |
| 298 | * @var string |
| 299 | */ |
| 300 | public $Helo = ''; |
| 301 | |
| 302 | /** |
| 303 | * What kind of encryption to use on the SMTP connection. |
| 304 | * Options: '', static::ENCRYPTION_STARTTLS, or static::ENCRYPTION_SMTPS. |
| 305 | * |
| 306 | * @var string |
| 307 | */ |
| 308 | public $SMTPSecure = ''; |
| 309 | |
| 310 | /** |
| 311 | * Whether to enable TLS encryption automatically if a server supports it, |
| 312 | * even if `SMTPSecure` is not set to 'tls'. |
| 313 | * Be aware that in PHP >= 5.6 this requires that the server's certificates are valid. |
| 314 | * |
| 315 | * @var bool |
| 316 | */ |
| 317 | public $SMTPAutoTLS = true; |
| 318 | |
| 319 | /** |
| 320 | * Whether to use SMTP authentication. |
| 321 | * Uses the Username and Password properties. |
| 322 | * |
| 323 | * @see PHPMailer::$Username |
| 324 | * @see PHPMailer::$Password |
| 325 | * |
| 326 | * @var bool |
| 327 | */ |
| 328 | public $SMTPAuth = false; |
| 329 | |
| 330 | /** |
| 331 | * Options array passed to stream_context_create when connecting via SMTP. |
| 332 | * |
| 333 | * @var array |
| 334 | */ |
| 335 | public $SMTPOptions = []; |
| 336 | |
| 337 | /** |
| 338 | * SMTP username. |
| 339 | * |
| 340 | * @var string |
| 341 | */ |
| 342 | public $Username = ''; |
| 343 | |
| 344 | /** |
| 345 | * SMTP password. |
| 346 | * |
| 347 | * @var string |
| 348 | */ |
| 349 | public $Password = ''; |
| 350 | |
| 351 | /** |
| 352 | * SMTP auth type. |
| 353 | * Options are CRAM-MD5, LOGIN, PLAIN, XOAUTH2, attempted in that order if not specified. |
| 354 | * |
| 355 | * @var string |
| 356 | */ |
| 357 | public $AuthType = ''; |
| 358 | |
| 359 | /** |
| 360 | * An instance of the PHPMailer OAuth class. |
| 361 | * |
| 362 | * @var OAuth |
| 363 | */ |
| 364 | protected $oauth; |
| 365 | |
| 366 | /** |
| 367 | * The SMTP server timeout in seconds. |
| 368 | * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2. |
| 369 | * |
| 370 | * @var int |
| 371 | */ |
| 372 | public $Timeout = 300; |
| 373 | |
| 374 | /** |
| 375 | * Comma separated list of DSN notifications |
| 376 | * 'NEVER' under no circumstances a DSN must be returned to the sender. |
| 377 | * If you use NEVER all other notifications will be ignored. |
| 378 | * 'SUCCESS' will notify you when your mail has arrived at its destination. |
| 379 | * 'FAILURE' will arrive if an error occurred during delivery. |
| 380 | * 'DELAY' will notify you if there is an unusual delay in delivery, but the actual |
| 381 | * delivery's outcome (success or failure) is not yet decided. |
| 382 | * |
| 383 | * @see https://tools.ietf.org/html/rfc3461 See section 4.1 for more information about NOTIFY |
| 384 | */ |
| 385 | public $dsn = ''; |
| 386 | |
| 387 | /** |
| 388 | * SMTP class debug output mode. |
| 389 | * Debug output level. |
| 390 | * Options: |
| 391 | * * SMTP::DEBUG_OFF: No output |
| 392 | * * SMTP::DEBUG_CLIENT: Client messages |
| 393 | * * SMTP::DEBUG_SERVER: Client and server messages |
| 394 | * * SMTP::DEBUG_CONNECTION: As SERVER plus connection status |
| 395 | * * SMTP::DEBUG_LOWLEVEL: Noisy, low-level data output, rarely needed |
| 396 | * |
| 397 | * @see SMTP::$do_debug |
| 398 | * |
| 399 | * @var int |
| 400 | */ |
| 401 | public $SMTPDebug = 0; |
| 402 | |
| 403 | /** |
| 404 | * How to handle debug output. |
| 405 | * Options: |
| 406 | * * `echo` Output plain-text as-is, appropriate for CLI |
| 407 | * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output |
| 408 | * * `error_log` Output to error log as configured in php.ini |
| 409 | * By default PHPMailer will use `echo` if run from a `cli` or `cli-server` SAPI, `html` otherwise. |
| 410 | * Alternatively, you can provide a callable expecting two params: a message string and the debug level: |
| 411 | * |
| 412 | * ```php |
| 413 | * $mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; |
| 414 | * ``` |
| 415 | * |
| 416 | * Alternatively, you can pass in an instance of a PSR-3 compatible logger, though only `debug` |
| 417 | * level output is used: |
| 418 | * |
| 419 | * ```php |
| 420 | * $mail->Debugoutput = new myPsr3Logger; |
| 421 | * ``` |
| 422 | * |
| 423 | * @see SMTP::$Debugoutput |
| 424 | * |
| 425 | * @var string|callable|\Psr\Log\LoggerInterface |
| 426 | */ |
| 427 | public $Debugoutput = 'echo'; |
| 428 | |
| 429 | /** |
| 430 | * Whether to keep SMTP connection open after each message. |
| 431 | * If this is set to true then to close the connection |
| 432 | * requires an explicit call to smtpClose(). |
| 433 | * |
| 434 | * @var bool |
| 435 | */ |
| 436 | public $SMTPKeepAlive = false; |
| 437 | |
| 438 | /** |
| 439 | * Whether to split multiple to addresses into multiple messages |
| 440 | * or send them all in one message. |
| 441 | * Only supported in `mail` and `sendmail` transports, not in SMTP. |
| 442 | * |
| 443 | * @var bool |
| 444 | */ |
| 445 | public $SingleTo = false; |
| 446 | |
| 447 | /** |
| 448 | * Storage for addresses when SingleTo is enabled. |
| 449 | * |
| 450 | * @var array |
| 451 | */ |
| 452 | protected $SingleToArray = []; |
| 453 | |
| 454 | /** |
| 455 | * Whether to generate VERP addresses on send. |
| 456 | * Only applicable when sending via SMTP. |
| 457 | * |
| 458 | * @see https://en.wikipedia.org/wiki/Variable_envelope_return_path |
| 459 | * @see http://www.postfix.org/VERP_README.html Postfix VERP info |
| 460 | * |
| 461 | * @var bool |
| 462 | */ |
| 463 | public $do_verp = false; |
| 464 | |
| 465 | /** |
| 466 | * Whether to allow sending messages with an empty body. |
| 467 | * |
| 468 | * @var bool |
| 469 | */ |
| 470 | public $AllowEmpty = false; |
| 471 | |
| 472 | /** |
| 473 | * DKIM selector. |
| 474 | * |
| 475 | * @var string |
| 476 | */ |
| 477 | public $DKIM_selector = ''; |
| 478 | |
| 479 | /** |
| 480 | * DKIM Identity. |
| 481 | * Usually the email address used as the source of the email. |
| 482 | * |
| 483 | * @var string |
| 484 | */ |
| 485 | public $DKIM_identity = ''; |
| 486 | |
| 487 | /** |
| 488 | * DKIM passphrase. |
| 489 | * Used if your key is encrypted. |
| 490 | * |
| 491 | * @var string |
| 492 | */ |
| 493 | public $DKIM_passphrase = ''; |
| 494 | |
| 495 | /** |
| 496 | * DKIM signing domain name. |
| 497 | * |
| 498 | * @example 'example.com' |
| 499 | * |
| 500 | * @var string |
| 501 | */ |
| 502 | public $DKIM_domain = ''; |
| 503 | |
| 504 | /** |
| 505 | * DKIM Copy header field values for diagnostic use. |
| 506 | * |
| 507 | * @var bool |
| 508 | */ |
| 509 | public $DKIM_copyHeaderFields = true; |
| 510 | |
| 511 | /** |
| 512 | * DKIM Extra signing headers. |
| 513 | * |
| 514 | * @example ['List-Unsubscribe', 'List-Help'] |
| 515 | * |
| 516 | * @var array |
| 517 | */ |
| 518 | public $DKIM_extraHeaders = []; |
| 519 | |
| 520 | /** |
| 521 | * DKIM private key file path. |
| 522 | * |
| 523 | * @var string |
| 524 | */ |
| 525 | public $DKIM_private = ''; |
| 526 | |
| 527 | /** |
| 528 | * DKIM private key string. |
| 529 | * |
| 530 | * If set, takes precedence over `$DKIM_private`. |
| 531 | * |
| 532 | * @var string |
| 533 | */ |
| 534 | public $DKIM_private_string = ''; |
| 535 | |
| 536 | /** |
| 537 | * Callback Action function name. |
| 538 | * |
| 539 | * The function that handles the result of the send email action. |
| 540 | * It is called out by send() for each email sent. |
| 541 | * |
| 542 | * Value can be any php callable: http://www.php.net/is_callable |
| 543 | * |
| 544 | * Parameters: |
| 545 | * bool $result result of the send action |
| 546 | * array $to email addresses of the recipients |
| 547 | * array $cc cc email addresses |
| 548 | * array $bcc bcc email addresses |
| 549 | * string $subject the subject |
| 550 | * string $body the email body |
| 551 | * string $from email address of sender |
| 552 | * string $extra extra information of possible use |
| 553 | * "smtp_transaction_id' => last smtp transaction id |
| 554 | * |
| 555 | * @var string |
| 556 | */ |
| 557 | public $action_function = ''; |
| 558 | |
| 559 | /** |
| 560 | * What to put in the X-Mailer header. |
| 561 | * Options: An empty string for PHPMailer default, whitespace/null for none, or a string to use. |
| 562 | * |
| 563 | * @var string|null |
| 564 | */ |
| 565 | public $XMailer = ''; |
| 566 | |
| 567 | /** |
| 568 | * Which validator to use by default when validating email addresses. |
| 569 | * May be a callable to inject your own validator, but there are several built-in validators. |
| 570 | * The default validator uses PHP's FILTER_VALIDATE_EMAIL filter_var option. |
| 571 | * |
| 572 | * @see PHPMailer::validateAddress() |
| 573 | * |
| 574 | * @var string|callable |
| 575 | */ |
| 576 | public static $validator = 'php'; |
| 577 | |
| 578 | /** |
| 579 | * An instance of the SMTP sender class. |
| 580 | * |
| 581 | * @var SMTP |
| 582 | */ |
| 583 | protected $smtp; |
| 584 | |
| 585 | /** |
| 586 | * The array of 'to' names and addresses. |
| 587 | * |
| 588 | * @var array |
| 589 | */ |
| 590 | protected $to = []; |
| 591 | |
| 592 | /** |
| 593 | * The array of 'cc' names and addresses. |
| 594 | * |
| 595 | * @var array |
| 596 | */ |
| 597 | protected $cc = []; |
| 598 | |
| 599 | /** |
| 600 | * The array of 'bcc' names and addresses. |
| 601 | * |
| 602 | * @var array |
| 603 | */ |
| 604 | protected $bcc = []; |
| 605 | |
| 606 | /** |
| 607 | * The array of reply-to names and addresses. |
| 608 | * |
| 609 | * @var array |
| 610 | */ |
| 611 | protected $ReplyTo = []; |
| 612 | |
| 613 | /** |
| 614 | * An array of all kinds of addresses. |
| 615 | * Includes all of $to, $cc, $bcc. |
| 616 | * |
| 617 | * @see PHPMailer::$to |
| 618 | * @see PHPMailer::$cc |
| 619 | * @see PHPMailer::$bcc |
| 620 | * |
| 621 | * @var array |
| 622 | */ |
| 623 | protected $all_recipients = []; |
| 624 | |
| 625 | /** |
| 626 | * An array of names and addresses queued for validation. |
| 627 | * In send(), valid and non duplicate entries are moved to $all_recipients |
| 628 | * and one of $to, $cc, or $bcc. |
| 629 | * This array is used only for addresses with IDN. |
| 630 | * |
| 631 | * @see PHPMailer::$to |
| 632 | * @see PHPMailer::$cc |
| 633 | * @see PHPMailer::$bcc |
| 634 | * @see PHPMailer::$all_recipients |
| 635 | * |
| 636 | * @var array |
| 637 | */ |
| 638 | protected $RecipientsQueue = []; |
| 639 | |
| 640 | /** |
| 641 | * An array of reply-to names and addresses queued for validation. |
| 642 | * In send(), valid and non duplicate entries are moved to $ReplyTo. |
| 643 | * This array is used only for addresses with IDN. |
| 644 | * |
| 645 | * @see PHPMailer::$ReplyTo |
| 646 | * |
| 647 | * @var array |
| 648 | */ |
| 649 | protected $ReplyToQueue = []; |
| 650 | |
| 651 | /** |
| 652 | * The array of attachments. |
| 653 | * |
| 654 | * @var array |
| 655 | */ |
| 656 | protected $attachment = []; |
| 657 | |
| 658 | /** |
| 659 | * The array of custom headers. |
| 660 | * |
| 661 | * @var array |
| 662 | */ |
| 663 | protected $CustomHeader = []; |
| 664 | |
| 665 | /** |
| 666 | * The most recent Message-ID (including angular brackets). |
| 667 | * |
| 668 | * @var string |
| 669 | */ |
| 670 | protected $lastMessageID = ''; |
| 671 | |
| 672 | /** |
| 673 | * The message's MIME type. |
| 674 | * |
| 675 | * @var string |
| 676 | */ |
| 677 | protected $message_type = ''; |
| 678 | |
| 679 | /** |
| 680 | * The array of MIME boundary strings. |
| 681 | * |
| 682 | * @var array |
| 683 | */ |
| 684 | protected $boundary = []; |
| 685 | |
| 686 | /** |
| 687 | * The array of available languages. |
| 688 | * |
| 689 | * @var array |
| 690 | */ |
| 691 | protected $language = []; |
| 692 | |
| 693 | /** |
| 694 | * The number of errors encountered. |
| 695 | * |
| 696 | * @var int |
| 697 | */ |
| 698 | protected $error_count = 0; |
| 699 | |
| 700 | /** |
| 701 | * The S/MIME certificate file path. |
| 702 | * |
| 703 | * @var string |
| 704 | */ |
| 705 | protected $sign_cert_file = ''; |
| 706 | |
| 707 | /** |
| 708 | * The S/MIME key file path. |
| 709 | * |
| 710 | * @var string |
| 711 | */ |
| 712 | protected $sign_key_file = ''; |
| 713 | |
| 714 | /** |
| 715 | * The optional S/MIME extra certificates ("CA Chain") file path. |
| 716 | * |
| 717 | * @var string |
| 718 | */ |
| 719 | protected $sign_extracerts_file = ''; |
| 720 | |
| 721 | /** |
| 722 | * The S/MIME password for the key. |
| 723 | * Used only if the key is encrypted. |
| 724 | * |
| 725 | * @var string |
| 726 | */ |
| 727 | protected $sign_key_pass = ''; |
| 728 | |
| 729 | /** |
| 730 | * Whether to throw exceptions for errors. |
| 731 | * |
| 732 | * @var bool |
| 733 | */ |
| 734 | protected $exceptions = false; |
| 735 | |
| 736 | /** |
| 737 | * Unique ID used for message ID and boundaries. |
| 738 | * |
| 739 | * @var string |
| 740 | */ |
| 741 | protected $uniqueid = ''; |
| 742 | |
| 743 | /** |
| 744 | * The PHPMailer Version number. |
| 745 | * |
| 746 | * @var string |
| 747 | */ |
| 748 | const VERSION = '6.1.4'; |
| 749 | |
| 750 | /** |
| 751 | * Error severity: message only, continue processing. |
| 752 | * |
| 753 | * @var int |
| 754 | */ |
| 755 | const STOP_MESSAGE = 0; |
| 756 | |
| 757 | /** |
| 758 | * Error severity: message, likely ok to continue processing. |
| 759 | * |
| 760 | * @var int |
| 761 | */ |
| 762 | const STOP_CONTINUE = 1; |
| 763 | |
| 764 | /** |
| 765 | * Error severity: message, plus full stop, critical error reached. |
| 766 | * |
| 767 | * @var int |
| 768 | */ |
| 769 | const STOP_CRITICAL = 2; |
| 770 | |
| 771 | /** |
| 772 | * SMTP RFC standard line ending. |
| 773 | * |
| 774 | * @var string |
| 775 | */ |
| 776 | protected static $LE = "\r\n"; |
| 777 | |
| 778 | /** |
| 779 | * The maximum line length supported by mail(). |
| 780 | * |
| 781 | * Background: mail() will sometimes corrupt messages |
| 782 | * with headers headers longer than 65 chars, see #818. |
| 783 | * |
| 784 | * @var int |
| 785 | */ |
| 786 | const MAIL_MAX_LINE_LENGTH = 63; |
| 787 | |
| 788 | /** |
| 789 | * The maximum line length allowed by RFC 2822 section 2.1.1. |
| 790 | * |
| 791 | * @var int |
| 792 | */ |
| 793 | const MAX_LINE_LENGTH = 998; |
| 794 | |
| 795 | /** |
| 796 | * The lower maximum line length allowed by RFC 2822 section 2.1.1. |
| 797 | * This length does NOT include the line break |
| 798 | * 76 means that lines will be 77 or 78 chars depending on whether |
| 799 | * the line break format is LF or CRLF; both are valid. |
| 800 | * |
| 801 | * @var int |
| 802 | */ |
| 803 | const STD_LINE_LENGTH = 76; |
| 804 | |
| 805 | /** |
| 806 | * Constructor. |
| 807 | * |
| 808 | * @param bool $exceptions Should we throw external exceptions? |
| 809 | */ |
| 810 | public function __construct($exceptions = null) |
| 811 | { |
| 812 | if (null !== $exceptions) { |
| 813 | $this->exceptions = (bool) $exceptions; |
| 814 | } |
| 815 | //Pick an appropriate debug output format automatically |
| 816 | $this->Debugoutput = (strpos(PHP_SAPI, 'cli') !== false ? 'echo' : 'html'); |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * Destructor. |
| 821 | */ |
| 822 | public function __destruct() |
| 823 | { |
| 824 | //Close any open SMTP connection nicely |
| 825 | $this->smtpClose(); |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * Call mail() in a safe_mode-aware fashion. |
| 830 | * Also, unless sendmail_path points to sendmail (or something that |
| 831 | * claims to be sendmail), don't pass params (not a perfect fix, |
| 832 | * but it will do). |
| 833 | * |
| 834 | * @param string $to To |
| 835 | * @param string $subject Subject |
| 836 | * @param string $body Message Body |
| 837 | * @param string $header Additional Header(s) |
| 838 | * @param string|null $params Params |
| 839 | * |
| 840 | * @return bool |
| 841 | */ |
| 842 | private function mailPassthru($to, $subject, $body, $header, $params) |
| 843 | { |
| 844 | //Check overloading of mail function to avoid double-encoding |
| 845 | if (ini_get('mbstring.func_overload') & 1) { |
| 846 | $subject = $this->secureHeader($subject); |
| 847 | } else { |
| 848 | $subject = $this->encodeHeader($this->secureHeader($subject)); |
| 849 | } |
| 850 | //Calling mail() with null params breaks |
| 851 | if (!$this->UseSendmailOptions || null === $params) { |
| 852 | $result = @mail($to, $subject, $body, $header); |
| 853 | } else { |
| 854 | $result = @mail($to, $subject, $body, $header, $params); |
| 855 | } |
| 856 | |
| 857 | return $result; |
| 858 | } |
| 859 | |
| 860 | /** |
| 861 | * Output debugging info via user-defined method. |
| 862 | * Only generates output if SMTP debug output is enabled (@see SMTP::$do_debug). |
| 863 | * |
| 864 | * @see PHPMailer::$Debugoutput |
| 865 | * @see PHPMailer::$SMTPDebug |
| 866 | * |
| 867 | * @param string $str |
| 868 | */ |
| 869 | protected function edebug($str) |
| 870 | { |
| 871 | if ($this->SMTPDebug <= 0) { |
| 872 | return; |
| 873 | } |
| 874 | //Is this a PSR-3 logger? |
| 875 | if ($this->Debugoutput instanceof \Psr\Log\LoggerInterface) { |
| 876 | $this->Debugoutput->debug($str); |
| 877 | |
| 878 | return; |
| 879 | } |
| 880 | //Avoid clash with built-in function names |
| 881 | if (is_callable($this->Debugoutput) && !in_array($this->Debugoutput, ['error_log', 'html', 'echo'])) { |
| 882 | call_user_func($this->Debugoutput, $str, $this->SMTPDebug); |
| 883 | |
| 884 | return; |
| 885 | } |
| 886 | switch ($this->Debugoutput) { |
| 887 | case 'error_log': |
| 888 | //Don't output, just log |
| 889 | error_log($str); |
| 890 | break; |
| 891 | case 'html': |
| 892 | //Cleans up output a bit for a better looking, HTML-safe output |
| 893 | echo htmlentities( |
| 894 | preg_replace('/[\r\n]+/', '', $str), |
| 895 | ENT_QUOTES, |
| 896 | 'UTF-8' |
| 897 | ), "<br>\n"; |
| 898 | break; |
| 899 | case 'echo': |
| 900 | default: |
| 901 | //Normalize line breaks |
| 902 | $str = preg_replace('/\r\n|\r/m', "\n", $str); |
| 903 | echo gmdate('Y-m-d H:i:s'), |
| 904 | "\t", |
| 905 | //Trim trailing space |
| 906 | trim( |
| 907 | //Indent for readability, except for trailing break |
| 908 | str_replace( |
| 909 | "\n", |
| 910 | "\n \t ", |
| 911 | trim($str) |
| 912 | ) |
| 913 | ), |
| 914 | "\n"; |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * Sets message type to HTML or plain. |
| 920 | * |
| 921 | * @param bool $isHtml True for HTML mode |
| 922 | */ |
| 923 | public function isHTML($isHtml = true) |
| 924 | { |
| 925 | if ($isHtml) { |
| 926 | $this->ContentType = static::CONTENT_TYPE_TEXT_HTML; |
| 927 | } else { |
| 928 | $this->ContentType = static::CONTENT_TYPE_PLAINTEXT; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * Send messages using SMTP. |
| 934 | */ |
| 935 | public function isSMTP() |
| 936 | { |
| 937 | $this->Mailer = 'smtp'; |
| 938 | } |
| 939 | |
| 940 | /** |
| 941 | * Send messages using PHP's mail() function. |
| 942 | */ |
| 943 | public function isMail() |
| 944 | { |
| 945 | $this->Mailer = 'mail'; |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * Send messages using $Sendmail. |
| 950 | */ |
| 951 | public function isSendmail() |
| 952 | { |
| 953 | $ini_sendmail_path = ini_get('sendmail_path'); |
| 954 | |
| 955 | if (false === stripos($ini_sendmail_path, 'sendmail')) { |
| 956 | $this->Sendmail = '/usr/sbin/sendmail'; |
| 957 | } else { |
| 958 | $this->Sendmail = $ini_sendmail_path; |
| 959 | } |
| 960 | $this->Mailer = 'sendmail'; |
| 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Send messages using qmail. |
| 965 | */ |
| 966 | public function isQmail() |
| 967 | { |
| 968 | $ini_sendmail_path = ini_get('sendmail_path'); |
| 969 | |
| 970 | if (false === stripos($ini_sendmail_path, 'qmail')) { |
| 971 | $this->Sendmail = '/var/qmail/bin/qmail-inject'; |
| 972 | } else { |
| 973 | $this->Sendmail = $ini_sendmail_path; |
| 974 | } |
| 975 | $this->Mailer = 'qmail'; |
| 976 | } |
| 977 | |
| 978 | /** |
| 979 | * Add a "To" address. |
| 980 | * |
| 981 | * @param string $address The email address to send to |
| 982 | * @param string $name |
| 983 | * |
| 984 | * @throws Exception |
| 985 | * |
| 986 | * @return bool true on success, false if address already used or invalid in some way |
| 987 | */ |
| 988 | public function addAddress($address, $name = '') |
| 989 | { |
| 990 | return $this->addOrEnqueueAnAddress('to', $address, $name); |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * Add a "CC" address. |
| 995 | * |
| 996 | * @param string $address The email address to send to |
| 997 | * @param string $name |
| 998 | * |
| 999 | * @throws Exception |
| 1000 | * |
| 1001 | * @return bool true on success, false if address already used or invalid in some way |
| 1002 | */ |
| 1003 | public function addCC($address, $name = '') |
| 1004 | { |
| 1005 | return $this->addOrEnqueueAnAddress('cc', $address, $name); |
| 1006 | } |
| 1007 | |
| 1008 | /** |
| 1009 | * Add a "BCC" address. |
| 1010 | * |
| 1011 | * @param string $address The email address to send to |
| 1012 | * @param string $name |
| 1013 | * |
| 1014 | * @throws Exception |
| 1015 | * |
| 1016 | * @return bool true on success, false if address already used or invalid in some way |
| 1017 | */ |
| 1018 | public function addBCC($address, $name = '') |
| 1019 | { |
| 1020 | return $this->addOrEnqueueAnAddress('bcc', $address, $name); |
| 1021 | } |
| 1022 | |
| 1023 | /** |
| 1024 | * Add a "Reply-To" address. |
| 1025 | * |
| 1026 | * @param string $address The email address to reply to |
| 1027 | * @param string $name |
| 1028 | * |
| 1029 | * @throws Exception |
| 1030 | * |
| 1031 | * @return bool true on success, false if address already used or invalid in some way |
| 1032 | */ |
| 1033 | public function addReplyTo($address, $name = '') |
| 1034 | { |
| 1035 | return $this->addOrEnqueueAnAddress('Reply-To', $address, $name); |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * Add an address to one of the recipient arrays or to the ReplyTo array. Because PHPMailer |
| 1040 | * can't validate addresses with an IDN without knowing the PHPMailer::$CharSet (that can still |
| 1041 | * be modified after calling this function), addition of such addresses is delayed until send(). |
| 1042 | * Addresses that have been added already return false, but do not throw exceptions. |
| 1043 | * |
| 1044 | * @param string $kind One of 'to', 'cc', 'bcc', or 'ReplyTo' |
| 1045 | * @param string $address The email address to send, resp. to reply to |
| 1046 | * @param string $name |
| 1047 | * |
| 1048 | * @throws Exception |
| 1049 | * |
| 1050 | * @return bool true on success, false if address already used or invalid in some way |
| 1051 | */ |
| 1052 | protected function addOrEnqueueAnAddress($kind, $address, $name) |
| 1053 | { |
| 1054 | $address = trim($address); |
| 1055 | $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim |
| 1056 | $pos = strrpos($address, '@'); |
| 1057 | if (false === $pos) { |
| 1058 | // At-sign is missing. |
| 1059 | $error_message = sprintf( |
| 1060 | '%s (%s): %s', |
| 1061 | $this->lang('invalid_address'), |
| 1062 | $kind, |
| 1063 | $address |
| 1064 | ); |
| 1065 | $this->setError($error_message); |
| 1066 | $this->edebug($error_message); |
| 1067 | if ($this->exceptions) { |
| 1068 | throw new \Exception($error_message); |
| 1069 | } |
| 1070 | |
| 1071 | return false; |
| 1072 | } |
| 1073 | $params = [$kind, $address, $name]; |
| 1074 | // Enqueue addresses with IDN until we know the PHPMailer::$CharSet. |
| 1075 | if (static::idnSupported() && $this->has8bitChars(substr($address, ++$pos))) { |
| 1076 | if ('Reply-To' !== $kind) { |
| 1077 | if (!array_key_exists($address, $this->RecipientsQueue)) { |
| 1078 | $this->RecipientsQueue[$address] = $params; |
| 1079 | |
| 1080 | return true; |
| 1081 | } |
| 1082 | } elseif (!array_key_exists($address, $this->ReplyToQueue)) { |
| 1083 | $this->ReplyToQueue[$address] = $params; |
| 1084 | |
| 1085 | return true; |
| 1086 | } |
| 1087 | |
| 1088 | return false; |
| 1089 | } |
| 1090 | |
| 1091 | // Immediately add standard addresses without IDN. |
| 1092 | return call_user_func_array([$this, 'addAnAddress'], $params); |
| 1093 | } |
| 1094 | |
| 1095 | /** |
| 1096 | * Add an address to one of the recipient arrays or to the ReplyTo array. |
| 1097 | * Addresses that have been added already return false, but do not throw exceptions. |
| 1098 | * |
| 1099 | * @param string $kind One of 'to', 'cc', 'bcc', or 'ReplyTo' |
| 1100 | * @param string $address The email address to send, resp. to reply to |
| 1101 | * @param string $name |
| 1102 | * |
| 1103 | * @throws Exception |
| 1104 | * |
| 1105 | * @return bool true on success, false if address already used or invalid in some way |
| 1106 | */ |
| 1107 | protected function addAnAddress($kind, $address, $name = '') |
| 1108 | { |
| 1109 | if (!in_array($kind, ['to', 'cc', 'bcc', 'Reply-To'])) { |
| 1110 | $error_message = sprintf( |
| 1111 | '%s: %s', |
| 1112 | $this->lang('Invalid recipient kind'), |
| 1113 | $kind |
| 1114 | ); |
| 1115 | $this->setError($error_message); |
| 1116 | $this->edebug($error_message); |
| 1117 | if ($this->exceptions) { |
| 1118 | throw new Exception($error_message); |
| 1119 | } |
| 1120 | |
| 1121 | return false; |
| 1122 | } |
| 1123 | if (!static::validateAddress($address)) { |
| 1124 | $error_message = sprintf( |
| 1125 | '%s (%s): %s', |
| 1126 | $this->lang('invalid_address'), |
| 1127 | $kind, |
| 1128 | $address |
| 1129 | ); |
| 1130 | $this->setError($error_message); |
| 1131 | $this->edebug($error_message); |
| 1132 | if ($this->exceptions) { |
| 1133 | throw new Exception($error_message); |
| 1134 | } |
| 1135 | |
| 1136 | return false; |
| 1137 | } |
| 1138 | if ('Reply-To' !== $kind) { |
| 1139 | if (!array_key_exists(strtolower($address), $this->all_recipients)) { |
| 1140 | $this->{$kind}[] = [$address, $name]; |
| 1141 | $this->all_recipients[strtolower($address)] = true; |
| 1142 | |
| 1143 | return true; |
| 1144 | } |
| 1145 | } elseif (!array_key_exists(strtolower($address), $this->ReplyTo)) { |
| 1146 | $this->ReplyTo[strtolower($address)] = [$address, $name]; |
| 1147 | |
| 1148 | return true; |
| 1149 | } |
| 1150 | |
| 1151 | return false; |
| 1152 | } |
| 1153 | |
| 1154 | /** |
| 1155 | * Parse and validate a string containing one or more RFC822-style comma-separated email addresses |
| 1156 | * of the form "display name <address>" into an array of name/address pairs. |
| 1157 | * Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available. |
| 1158 | * Note that quotes in the name part are removed. |
| 1159 | * |
| 1160 | * @see http://www.andrew.cmu.edu/user/agreen1/testing/mrbs/web/Mail/RFC822.php A more careful implementation |
| 1161 | * |
| 1162 | * @param string $addrstr The address list string |
| 1163 | * @param bool $useimap Whether to use the IMAP extension to parse the list |
| 1164 | * |
| 1165 | * @return array |
| 1166 | */ |
| 1167 | public static function parseAddresses($addrstr, $useimap = true) |
| 1168 | { |
| 1169 | $addresses = []; |
| 1170 | if ($useimap && function_exists('imap_rfc822_parse_adrlist')) { |
| 1171 | //Use this built-in parser if it's available |
| 1172 | $list = imap_rfc822_parse_adrlist($addrstr, ''); |
| 1173 | foreach ($list as $address) { |
| 1174 | if (('.SYNTAX-ERROR.' !== $address->host) && static::validateAddress( |
| 1175 | $address->mailbox . '@' . $address->host |
| 1176 | )) { |
| 1177 | $addresses[] = [ |
| 1178 | 'name' => (property_exists($address, 'personal') ? $address->personal : ''), |
| 1179 | 'address' => $address->mailbox . '@' . $address->host, |
| 1180 | ]; |
| 1181 | } |
| 1182 | } |
| 1183 | } else { |
| 1184 | //Use this simpler parser |
| 1185 | $list = explode(',', $addrstr); |
| 1186 | foreach ($list as $address) { |
| 1187 | $address = trim($address); |
| 1188 | //Is there a separate name part? |
| 1189 | if (strpos($address, '<') === false) { |
| 1190 | //No separate name, just use the whole thing |
| 1191 | if (static::validateAddress($address)) { |
| 1192 | $addresses[] = [ |
| 1193 | 'name' => '', |
| 1194 | 'address' => $address, |
| 1195 | ]; |
| 1196 | } |
| 1197 | } else { |
| 1198 | list($name, $email) = explode('<', $address); |
| 1199 | $email = trim(str_replace('>', '', $email)); |
| 1200 | if (static::validateAddress($email)) { |
| 1201 | $addresses[] = [ |
| 1202 | 'name' => trim(str_replace(['"', "'"], '', $name)), |
| 1203 | 'address' => $email, |
| 1204 | ]; |
| 1205 | } |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | return $addresses; |
| 1211 | } |
| 1212 | |
| 1213 | /** |
| 1214 | * Set the From and FromName properties. |
| 1215 | * |
| 1216 | * @param string $address |
| 1217 | * @param string $name |
| 1218 | * @param bool $auto Whether to also set the Sender address, defaults to true |
| 1219 | * |
| 1220 | * @throws Exception |
| 1221 | * |
| 1222 | * @return bool |
| 1223 | */ |
| 1224 | public function setFrom($address, $name = '', $auto = true) |
| 1225 | { |
| 1226 | $address = trim($address); |
| 1227 | $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim |
| 1228 | // Don't validate now addresses with IDN. Will be done in send(). |
| 1229 | $pos = strrpos($address, '@'); |
| 1230 | if ((false === $pos) |
| 1231 | || ((!$this->has8bitChars(substr($address, ++$pos)) || !static::idnSupported()) |
| 1232 | && !static::validateAddress($address)) |
| 1233 | ) { |
| 1234 | $error_message = sprintf( |
| 1235 | '%s (From): %s', |
| 1236 | $this->lang('invalid_address'), |
| 1237 | $address |
| 1238 | ); |
| 1239 | $this->setError($error_message); |
| 1240 | $this->edebug($error_message); |
| 1241 | if ($this->exceptions) { |
| 1242 | throw new Exception($error_message); |
| 1243 | } |
| 1244 | |
| 1245 | return false; |
| 1246 | } |
| 1247 | $this->From = $address; |
| 1248 | $this->FromName = $name; |
| 1249 | if ($auto && empty($this->Sender)) { |
| 1250 | $this->Sender = $address; |
| 1251 | } |
| 1252 | |
| 1253 | return true; |
| 1254 | } |
| 1255 | |
| 1256 | /** |
| 1257 | * Return the Message-ID header of the last email. |
| 1258 | * Technically this is the value from the last time the headers were created, |
| 1259 | * but it's also the message ID of the last sent message except in |
| 1260 | * pathological cases. |
| 1261 | * |
| 1262 | * @return string |
| 1263 | */ |
| 1264 | public function getLastMessageID() |
| 1265 | { |
| 1266 | return $this->lastMessageID; |
| 1267 | } |
| 1268 | |
| 1269 | /** |
| 1270 | * Check that a string looks like an email address. |
| 1271 | * Validation patterns supported: |
| 1272 | * * `auto` Pick best pattern automatically; |
| 1273 | * * `pcre8` Use the squiloople.com pattern, requires PCRE > 8.0; |
| 1274 | * * `pcre` Use old PCRE implementation; |
| 1275 | * * `php` Use PHP built-in FILTER_VALIDATE_EMAIL; |
| 1276 | * * `html5` Use the pattern given by the HTML5 spec for 'email' type form input elements. |
| 1277 | * * `noregex` Don't use a regex: super fast, really dumb. |
| 1278 | * Alternatively you may pass in a callable to inject your own validator, for example: |
| 1279 | * |
| 1280 | * ```php |
| 1281 | * PHPMailer::validateAddress('user@example.com', function($address) { |
| 1282 | * return (strpos($address, '@') !== false); |
| 1283 | * }); |
| 1284 | * ``` |
| 1285 | * |
| 1286 | * You can also set the PHPMailer::$validator static to a callable, allowing built-in methods to use your validator. |
| 1287 | * |
| 1288 | * @param string $address The email address to check |
| 1289 | * @param string|callable $patternselect Which pattern to use |
| 1290 | * |
| 1291 | * @return bool |
| 1292 | */ |
| 1293 | public static function validateAddress($address, $patternselect = null) |
| 1294 | { |
| 1295 | if (null === $patternselect) { |
| 1296 | $patternselect = static::$validator; |
| 1297 | } |
| 1298 | if (is_callable($patternselect)) { |
| 1299 | return $patternselect($address); |
| 1300 | } |
| 1301 | //Reject line breaks in addresses; it's valid RFC5322, but not RFC5321 |
| 1302 | if (strpos($address, "\n") !== false || strpos($address, "\r") !== false) { |
| 1303 | return false; |
| 1304 | } |
| 1305 | switch ($patternselect) { |
| 1306 | case 'pcre': //Kept for BC |
| 1307 | case 'pcre8': |
| 1308 | /* |
| 1309 | * A more complex and more permissive version of the RFC5322 regex on which FILTER_VALIDATE_EMAIL |
| 1310 | * is based. |
| 1311 | * In addition to the addresses allowed by filter_var, also permits: |
| 1312 | * * dotless domains: `a@b` |
| 1313 | * * comments: `1234 @ local(blah) .machine .example` |
| 1314 | * * quoted elements: `'"test blah"@example.org'` |
| 1315 | * * numeric TLDs: `a@b.123` |
| 1316 | * * unbracketed IPv4 literals: `a@192.168.0.1` |
| 1317 | * * IPv6 literals: 'first.last@[IPv6:a1::]' |
| 1318 | * Not all of these will necessarily work for sending! |
| 1319 | * |
| 1320 | * @see http://squiloople.com/2009/12/20/email-address-validation/ |
1144 | | return (boolean)preg_match( |
1145 | | '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' . |
1146 | | '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD', |
1147 | | $address |
1148 | | ); |
1149 | | case 'noregex': |
1150 | | //No PCRE! Do something _very_ approximate! |
1151 | | //Check the address is 3 chars or longer and contains an @ that's not the first or last char |
1152 | | return (strlen($address) >= 3 |
1153 | | and strpos($address, '@') >= 1 |
1154 | | and strpos($address, '@') != strlen($address) - 1); |
1155 | | case 'php': |
1156 | | default: |
1157 | | return (boolean)filter_var($address, FILTER_VALIDATE_EMAIL); |
1158 | | } |
1159 | | } |
1160 | | |
1161 | | /** |
1162 | | * Tells whether IDNs (Internationalized Domain Names) are supported or not. This requires the |
1163 | | * "intl" and "mbstring" PHP extensions. |
1164 | | * @return bool "true" if required functions for IDN support are present |
1165 | | */ |
1166 | | public function idnSupported() |
1167 | | { |
1168 | | // @TODO: Write our own "idn_to_ascii" function for PHP <= 5.2. |
1169 | | return function_exists('idn_to_ascii') and function_exists('mb_convert_encoding'); |
1170 | | } |
1171 | | |
1172 | | /** |
1173 | | * Converts IDN in given email address to its ASCII form, also known as punycode, if possible. |
1174 | | * Important: Address must be passed in same encoding as currently set in PHPMailer::$CharSet. |
1175 | | * This function silently returns unmodified address if: |
1176 | | * - No conversion is necessary (i.e. domain name is not an IDN, or is already in ASCII form) |
1177 | | * - Conversion to punycode is impossible (e.g. required PHP functions are not available) |
1178 | | * or fails for any reason (e.g. domain has characters not allowed in an IDN) |
1179 | | * @see PHPMailer::$CharSet |
1180 | | * @param string $address The email address to convert |
1181 | | * @return string The encoded address in ASCII form |
1182 | | */ |
1183 | | public function punyencodeAddress($address) |
1184 | | { |
1185 | | // Verify we have required functions, CharSet, and at-sign. |
1186 | | if ($this->idnSupported() and |
1187 | | !empty($this->CharSet) and |
1188 | | ($pos = strrpos($address, '@')) !== false) { |
1189 | | $domain = substr($address, ++$pos); |
1190 | | // Verify CharSet string is a valid one, and domain properly encoded in this CharSet. |
1191 | | if ($this->has8bitChars($domain) and @mb_check_encoding($domain, $this->CharSet)) { |
1192 | | $domain = mb_convert_encoding($domain, 'UTF-8', $this->CharSet); |
1193 | | if (($punycode = defined('INTL_IDNA_VARIANT_UTS46') ? |
1194 | | idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46) : |
1195 | | idn_to_ascii($domain)) !== false) { |
1196 | | return substr($address, 0, $pos) . $punycode; |
1197 | | } |
1198 | | } |
1199 | | } |
1200 | | return $address; |
1201 | | } |
1202 | | |
1203 | | /** |
1204 | | * Create a message and send it. |
1205 | | * Uses the sending method specified by $Mailer. |
1206 | | * @throws phpmailerException |
1207 | | * @return boolean false on error - See the ErrorInfo property for details of the error. |
1208 | | */ |
1209 | | public function send() |
1210 | | { |
1211 | | try { |
1212 | | if (!$this->preSend()) { |
1213 | | return false; |
1214 | | } |
1215 | | return $this->postSend(); |
1216 | | } catch (phpmailerException $exc) { |
1217 | | $this->mailHeader = ''; |
1218 | | $this->setError($exc->getMessage()); |
1219 | | if ($this->exceptions) { |
1220 | | throw $exc; |
1221 | | } |
1222 | | return false; |
1223 | | } |
1224 | | } |
1225 | | |
1226 | | /** |
1227 | | * Prepare a message for sending. |
1228 | | * @throws phpmailerException |
1229 | | * @return boolean |
1230 | | */ |
1231 | | public function preSend() |
1232 | | { |
1233 | | try { |
1234 | | $this->error_count = 0; // Reset errors |
1235 | | $this->mailHeader = ''; |
1236 | | |
1237 | | // Dequeue recipient and Reply-To addresses with IDN |
1238 | | foreach (array_merge($this->RecipientsQueue, $this->ReplyToQueue) as $params) { |
1239 | | $params[1] = $this->punyencodeAddress($params[1]); |
1240 | | call_user_func_array(array($this, 'addAnAddress'), $params); |
1241 | | } |
1242 | | if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) { |
1243 | | throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL); |
1244 | | } |
1245 | | |
1246 | | // Validate From, Sender, and ConfirmReadingTo addresses |
1247 | | foreach (array('From', 'Sender', 'ConfirmReadingTo') as $address_kind) { |
1248 | | $this->$address_kind = trim($this->$address_kind); |
1249 | | if (empty($this->$address_kind)) { |
1250 | | continue; |
1251 | | } |
1252 | | $this->$address_kind = $this->punyencodeAddress($this->$address_kind); |
1253 | | if (!$this->validateAddress($this->$address_kind)) { |
1254 | | $error_message = $this->lang('invalid_address') . ' (punyEncode) ' . $this->$address_kind; |
1255 | | $this->setError($error_message); |
1256 | | $this->edebug($error_message); |
1257 | | if ($this->exceptions) { |
1258 | | throw new phpmailerException($error_message); |
1259 | | } |
1260 | | return false; |
1261 | | } |
1262 | | } |
1263 | | |
1264 | | // Set whether the message is multipart/alternative |
1265 | | if ($this->alternativeExists()) { |
1266 | | $this->ContentType = 'multipart/alternative'; |
1267 | | } |
1268 | | |
1269 | | $this->setMessageType(); |
1270 | | // Refuse to send an empty message unless we are specifically allowing it |
1271 | | if (!$this->AllowEmpty and empty($this->Body)) { |
1272 | | throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL); |
1273 | | } |
1274 | | |
1275 | | // Create body before headers in case body makes changes to headers (e.g. altering transfer encoding) |
1276 | | $this->MIMEHeader = ''; |
1277 | | $this->MIMEBody = $this->createBody(); |
1278 | | // createBody may have added some headers, so retain them |
1279 | | $tempheaders = $this->MIMEHeader; |
1280 | | $this->MIMEHeader = $this->createHeader(); |
1281 | | $this->MIMEHeader .= $tempheaders; |
1282 | | |
1283 | | // To capture the complete message when using mail(), create |
1284 | | // an extra header list which createHeader() doesn't fold in |
1285 | | if ($this->Mailer == 'mail') { |
1286 | | if (count($this->to) > 0) { |
1287 | | $this->mailHeader .= $this->addrAppend('To', $this->to); |
1288 | | } else { |
1289 | | $this->mailHeader .= $this->headerLine('To', 'undisclosed-recipients:;'); |
1290 | | } |
1291 | | $this->mailHeader .= $this->headerLine( |
1292 | | 'Subject', |
1293 | | $this->encodeHeader($this->secureHeader(trim($this->Subject))) |
1294 | | ); |
1295 | | } |
1296 | | |
1297 | | // Sign with DKIM if enabled |
1298 | | if (!empty($this->DKIM_domain) |
1299 | | and !empty($this->DKIM_selector) |
1300 | | and (!empty($this->DKIM_private_string) |
1301 | | or (!empty($this->DKIM_private) |
1302 | | and self::isPermittedPath($this->DKIM_private) |
1303 | | and file_exists($this->DKIM_private) |
1304 | | ) |
1305 | | ) |
1306 | | ) { |
1307 | | $header_dkim = $this->DKIM_Add( |
1308 | | $this->MIMEHeader . $this->mailHeader, |
1309 | | $this->encodeHeader($this->secureHeader($this->Subject)), |
1310 | | $this->MIMEBody |
1311 | | ); |
1312 | | $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . self::CRLF . |
1313 | | str_replace("\r\n", "\n", $header_dkim) . self::CRLF; |
1314 | | } |
1315 | | return true; |
1316 | | } catch (phpmailerException $exc) { |
1317 | | $this->setError($exc->getMessage()); |
1318 | | if ($this->exceptions) { |
1319 | | throw $exc; |
1320 | | } |
1321 | | return false; |
1322 | | } |
1323 | | } |
1324 | | |
1325 | | /** |
1326 | | * Actually send a message. |
1327 | | * Send the email via the selected mechanism |
1328 | | * @throws phpmailerException |
1329 | | * @return boolean |
1330 | | */ |
1331 | | public function postSend() |
1332 | | { |
1333 | | try { |
1334 | | // Choose the mailer and send through it |
1335 | | switch ($this->Mailer) { |
1336 | | case 'sendmail': |
1337 | | case 'qmail': |
1338 | | return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody); |
1339 | | case 'smtp': |
1340 | | return $this->smtpSend($this->MIMEHeader, $this->MIMEBody); |
1341 | | case 'mail': |
1342 | | return $this->mailSend($this->MIMEHeader, $this->MIMEBody); |
1343 | | default: |
1344 | | $sendMethod = $this->Mailer.'Send'; |
1345 | | if (method_exists($this, $sendMethod)) { |
1346 | | return $this->$sendMethod($this->MIMEHeader, $this->MIMEBody); |
1347 | | } |
1348 | | |
1349 | | return $this->mailSend($this->MIMEHeader, $this->MIMEBody); |
1350 | | } |
1351 | | } catch (phpmailerException $exc) { |
1352 | | $this->setError($exc->getMessage()); |
1353 | | $this->edebug($exc->getMessage()); |
1354 | | if ($this->exceptions) { |
1355 | | throw $exc; |
1356 | | } |
1357 | | } |
1358 | | return false; |
1359 | | } |
1360 | | |
1361 | | /** |
1362 | | * Send mail using the $Sendmail program. |
1363 | | * @param string $header The message headers |
1364 | | * @param string $body The message body |
1365 | | * @see PHPMailer::$Sendmail |
1366 | | * @throws phpmailerException |
1367 | | * @access protected |
1368 | | * @return boolean |
1369 | | */ |
1370 | | protected function sendmailSend($header, $body) |
1371 | | { |
1372 | | // CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. |
1373 | | if (!empty($this->Sender) and self::isShellSafe($this->Sender)) { |
1374 | | if ($this->Mailer == 'qmail') { |
1375 | | $sendmailFmt = '%s -f%s'; |
1376 | | } else { |
1377 | | $sendmailFmt = '%s -oi -f%s -t'; |
1378 | | } |
1379 | | } else { |
1380 | | if ($this->Mailer == 'qmail') { |
1381 | | $sendmailFmt = '%s'; |
1382 | | } else { |
1383 | | $sendmailFmt = '%s -oi -t'; |
1384 | | } |
1385 | | } |
1386 | | |
1387 | | // TODO: If possible, this should be changed to escapeshellarg. Needs thorough testing. |
1388 | | $sendmail = sprintf($sendmailFmt, escapeshellcmd($this->Sendmail), $this->Sender); |
1389 | | |
1390 | | if ($this->SingleTo) { |
1391 | | foreach ($this->SingleToArray as $toAddr) { |
1392 | | if (!@$mail = popen($sendmail, 'w')) { |
1393 | | throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
1394 | | } |
1395 | | fputs($mail, 'To: ' . $toAddr . "\n"); |
1396 | | fputs($mail, $header); |
1397 | | fputs($mail, $body); |
1398 | | $result = pclose($mail); |
1399 | | $this->doCallback( |
1400 | | ($result == 0), |
1401 | | array($toAddr), |
1402 | | $this->cc, |
1403 | | $this->bcc, |
1404 | | $this->Subject, |
1405 | | $body, |
1406 | | $this->From |
1407 | | ); |
1408 | | if ($result != 0) { |
1409 | | throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
1410 | | } |
1411 | | } |
1412 | | } else { |
1413 | | if (!@$mail = popen($sendmail, 'w')) { |
1414 | | throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
1415 | | } |
1416 | | fputs($mail, $header); |
1417 | | fputs($mail, $body); |
1418 | | $result = pclose($mail); |
1419 | | $this->doCallback( |
1420 | | ($result == 0), |
1421 | | $this->to, |
1422 | | $this->cc, |
1423 | | $this->bcc, |
1424 | | $this->Subject, |
1425 | | $body, |
1426 | | $this->From |
1427 | | ); |
1428 | | if ($result != 0) { |
1429 | | throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
1430 | | } |
1431 | | } |
1432 | | return true; |
1433 | | } |
1434 | | |
1435 | | /** |
1436 | | * Fix CVE-2016-10033 and CVE-2016-10045 by disallowing potentially unsafe shell characters. |
1437 | | * |
1438 | | * Note that escapeshellarg and escapeshellcmd are inadequate for our purposes, especially on Windows. |
1439 | | * @param string $string The string to be validated |
1440 | | * @see https://github.com/PHPMailer/PHPMailer/issues/924 CVE-2016-10045 bug report |
1441 | | * @access protected |
1442 | | * @return boolean |
1443 | | */ |
1444 | | protected static function isShellSafe($string) |
1445 | | { |
1446 | | // Future-proof |
1447 | | if (escapeshellcmd($string) !== $string |
1448 | | or !in_array(escapeshellarg($string), array("'$string'", "\"$string\"")) |
1449 | | ) { |
1450 | | return false; |
1451 | | } |
1452 | | |
1453 | | $length = strlen($string); |
1454 | | |
1455 | | for ($i = 0; $i < $length; $i++) { |
1456 | | $c = $string[$i]; |
1457 | | |
1458 | | // All other characters have a special meaning in at least one common shell, including = and +. |
1459 | | // Full stop (.) has a special meaning in cmd.exe, but its impact should be negligible here. |
1460 | | // Note that this does permit non-Latin alphanumeric characters based on the current locale. |
1461 | | if (!ctype_alnum($c) && strpos('@_-.', $c) === false) { |
1462 | | return false; |
1463 | | } |
1464 | | } |
1465 | | |
1466 | | return true; |
1467 | | } |
1468 | | |
1469 | | /** |
1470 | | * Check whether a file path is of a permitted type. |
1471 | | * Used to reject URLs and phar files from functions that access local file paths, |
1472 | | * such as addAttachment. |
1473 | | * @param string $path A relative or absolute path to a file. |
1474 | | * @return bool |
1475 | | */ |
1476 | | protected static function isPermittedPath($path) |
1477 | | { |
1478 | | return !preg_match('#^[a-z]+://#i', $path); |
1479 | | } |
1480 | | |
1481 | | /** |
1482 | | * Send mail using the PHP mail() function. |
1483 | | * @param string $header The message headers |
1484 | | * @param string $body The message body |
1485 | | * @link http://www.php.net/manual/en/book.mail.php |
1486 | | * @throws phpmailerException |
1487 | | * @access protected |
1488 | | * @return boolean |
1489 | | */ |
1490 | | protected function mailSend($header, $body) |
1491 | | { |
1492 | | $toArr = array(); |
1493 | | foreach ($this->to as $toaddr) { |
1494 | | $toArr[] = $this->addrFormat($toaddr); |
1495 | | } |
1496 | | $to = implode(', ', $toArr); |
1497 | | |
1498 | | $params = null; |
1499 | | //This sets the SMTP envelope sender which gets turned into a return-path header by the receiver |
1500 | | if (!empty($this->Sender) and $this->validateAddress($this->Sender)) { |
1501 | | // CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. |
1502 | | if (self::isShellSafe($this->Sender)) { |
1503 | | $params = sprintf('-f%s', $this->Sender); |
1504 | | } |
1505 | | } |
1506 | | if (!empty($this->Sender) and !ini_get('safe_mode') and $this->validateAddress($this->Sender)) { |
1507 | | $old_from = ini_get('sendmail_from'); |
1508 | | ini_set('sendmail_from', $this->Sender); |
1509 | | } |
1510 | | $result = false; |
1511 | | if ($this->SingleTo and count($toArr) > 1) { |
1512 | | foreach ($toArr as $toAddr) { |
1513 | | $result = $this->mailPassthru($toAddr, $this->Subject, $body, $header, $params); |
1514 | | $this->doCallback($result, array($toAddr), $this->cc, $this->bcc, $this->Subject, $body, $this->From); |
1515 | | } |
1516 | | } else { |
1517 | | $result = $this->mailPassthru($to, $this->Subject, $body, $header, $params); |
1518 | | $this->doCallback($result, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From); |
1519 | | } |
1520 | | if (isset($old_from)) { |
1521 | | ini_set('sendmail_from', $old_from); |
1522 | | } |
1523 | | if (!$result) { |
1524 | | throw new phpmailerException($this->lang('instantiate'), self::STOP_CRITICAL); |
1525 | | } |
1526 | | return true; |
1527 | | } |
1528 | | |
1529 | | /** |
1530 | | * Get an instance to use for SMTP operations. |
1531 | | * Override this function to load your own SMTP implementation |
1532 | | * @return SMTP |
1533 | | */ |
1534 | | public function getSMTPInstance() |
1535 | | { |
1536 | | if (!is_object($this->smtp)) { |
1537 | | require_once( 'class-smtp.php' ); |
1538 | | $this->smtp = new SMTP; |
1539 | | } |
1540 | | return $this->smtp; |
1541 | | } |
1542 | | |
1543 | | /** |
1544 | | * Send mail via SMTP. |
1545 | | * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. |
1546 | | * Uses the PHPMailerSMTP class by default. |
1547 | | * @see PHPMailer::getSMTPInstance() to use a different class. |
1548 | | * @param string $header The message headers |
1549 | | * @param string $body The message body |
1550 | | * @throws phpmailerException |
1551 | | * @uses SMTP |
1552 | | * @access protected |
1553 | | * @return boolean |
1554 | | */ |
1555 | | protected function smtpSend($header, $body) |
1556 | | { |
1557 | | $bad_rcpt = array(); |
1558 | | if (!$this->smtpConnect($this->SMTPOptions)) { |
1559 | | throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL); |
1560 | | } |
1561 | | if (!empty($this->Sender) and $this->validateAddress($this->Sender)) { |
1562 | | $smtp_from = $this->Sender; |
1563 | | } else { |
1564 | | $smtp_from = $this->From; |
1565 | | } |
1566 | | if (!$this->smtp->mail($smtp_from)) { |
1567 | | $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError())); |
1568 | | throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL); |
1569 | | } |
1570 | | |
1571 | | // Attempt to send to all recipients |
1572 | | foreach (array($this->to, $this->cc, $this->bcc) as $togroup) { |
1573 | | foreach ($togroup as $to) { |
1574 | | if (!$this->smtp->recipient($to[0])) { |
1575 | | $error = $this->smtp->getError(); |
1576 | | $bad_rcpt[] = array('to' => $to[0], 'error' => $error['detail']); |
1577 | | $isSent = false; |
1578 | | } else { |
1579 | | $isSent = true; |
1580 | | } |
1581 | | $this->doCallback($isSent, array($to[0]), array(), array(), $this->Subject, $body, $this->From); |
1582 | | } |
1583 | | } |
1584 | | |
1585 | | // Only send the DATA command if we have viable recipients |
1586 | | if ((count($this->all_recipients) > count($bad_rcpt)) and !$this->smtp->data($header . $body)) { |
1587 | | throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL); |
1588 | | } |
1589 | | if ($this->SMTPKeepAlive) { |
1590 | | $this->smtp->reset(); |
1591 | | } else { |
1592 | | $this->smtp->quit(); |
1593 | | $this->smtp->close(); |
1594 | | } |
1595 | | //Create error message for any bad addresses |
1596 | | if (count($bad_rcpt) > 0) { |
1597 | | $errstr = ''; |
1598 | | foreach ($bad_rcpt as $bad) { |
1599 | | $errstr .= $bad['to'] . ': ' . $bad['error']; |
1600 | | } |
1601 | | throw new phpmailerException( |
1602 | | $this->lang('recipients_failed') . $errstr, |
1603 | | self::STOP_CONTINUE |
1604 | | ); |
1605 | | } |
1606 | | return true; |
1607 | | } |
1608 | | |
1609 | | /** |
1610 | | * Initiate a connection to an SMTP server. |
1611 | | * Returns false if the operation failed. |
1612 | | * @param array $options An array of options compatible with stream_context_create() |
1613 | | * @uses SMTP |
1614 | | * @access public |
1615 | | * @throws phpmailerException |
1616 | | * @return boolean |
1617 | | */ |
1618 | | public function smtpConnect($options = null) |
1619 | | { |
1620 | | if (is_null($this->smtp)) { |
1621 | | $this->smtp = $this->getSMTPInstance(); |
1622 | | } |
1623 | | |
1624 | | //If no options are provided, use whatever is set in the instance |
1625 | | if (is_null($options)) { |
1626 | | $options = $this->SMTPOptions; |
1627 | | } |
1628 | | |
1629 | | // Already connected? |
1630 | | if ($this->smtp->connected()) { |
1631 | | return true; |
1632 | | } |
1633 | | |
1634 | | $this->smtp->setTimeout($this->Timeout); |
1635 | | $this->smtp->setDebugLevel($this->SMTPDebug); |
1636 | | $this->smtp->setDebugOutput($this->Debugoutput); |
1637 | | $this->smtp->setVerp($this->do_verp); |
1638 | | $hosts = explode(';', $this->Host); |
1639 | | $lastexception = null; |
1640 | | |
1641 | | foreach ($hosts as $hostentry) { |
1642 | | $hostinfo = array(); |
1643 | | if (!preg_match( |
1644 | | '/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*|\[[a-fA-F0-9:]+\]):?([0-9]*)$/', |
1645 | | trim($hostentry), |
1646 | | $hostinfo |
1647 | | )) { |
1648 | | // Not a valid host entry |
1649 | | $this->edebug('Ignoring invalid host: ' . $hostentry); |
1650 | | continue; |
1651 | | } |
1652 | | // $hostinfo[2]: optional ssl or tls prefix |
1653 | | // $hostinfo[3]: the hostname |
1654 | | // $hostinfo[4]: optional port number |
1655 | | // The host string prefix can temporarily override the current setting for SMTPSecure |
1656 | | // If it's not specified, the default value is used |
1657 | | $prefix = ''; |
1658 | | $secure = $this->SMTPSecure; |
1659 | | $tls = ($this->SMTPSecure == 'tls'); |
1660 | | if ('ssl' == $hostinfo[2] or ('' == $hostinfo[2] and 'ssl' == $this->SMTPSecure)) { |
1661 | | $prefix = 'ssl://'; |
1662 | | $tls = false; // Can't have SSL and TLS at the same time |
1663 | | $secure = 'ssl'; |
1664 | | } elseif ($hostinfo[2] == 'tls') { |
1665 | | $tls = true; |
1666 | | // tls doesn't use a prefix |
1667 | | $secure = 'tls'; |
1668 | | } |
1669 | | //Do we need the OpenSSL extension? |
1670 | | $sslext = defined('OPENSSL_ALGO_SHA1'); |
1671 | | if ('tls' === $secure or 'ssl' === $secure) { |
1672 | | //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled |
1673 | | if (!$sslext) { |
1674 | | throw new phpmailerException($this->lang('extension_missing').'openssl', self::STOP_CRITICAL); |
1675 | | } |
1676 | | } |
1677 | | $host = $hostinfo[3]; |
1678 | | $port = $this->Port; |
1679 | | $tport = (integer)$hostinfo[4]; |
1680 | | if ($tport > 0 and $tport < 65536) { |
1681 | | $port = $tport; |
1682 | | } |
1683 | | if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { |
1684 | | try { |
1685 | | if ($this->Helo) { |
1686 | | $hello = $this->Helo; |
1687 | | } else { |
1688 | | $hello = $this->serverHostname(); |
1689 | | } |
1690 | | $this->smtp->hello($hello); |
1691 | | //Automatically enable TLS encryption if: |
1692 | | // * it's not disabled |
1693 | | // * we have openssl extension |
1694 | | // * we are not already using SSL |
1695 | | // * the server offers STARTTLS |
1696 | | if ($this->SMTPAutoTLS and $sslext and $secure != 'ssl' and $this->smtp->getServerExt('STARTTLS')) { |
1697 | | $tls = true; |
1698 | | } |
1699 | | if ($tls) { |
1700 | | if (!$this->smtp->startTLS()) { |
1701 | | throw new phpmailerException($this->lang('connect_host')); |
1702 | | } |
1703 | | // We must resend EHLO after TLS negotiation |
1704 | | $this->smtp->hello($hello); |
1705 | | } |
1706 | | if ($this->SMTPAuth) { |
1707 | | if (!$this->smtp->authenticate( |
1708 | | $this->Username, |
1709 | | $this->Password, |
1710 | | $this->AuthType, |
1711 | | $this->Realm, |
1712 | | $this->Workstation |
1713 | | ) |
1714 | | ) { |
1715 | | throw new phpmailerException($this->lang('authenticate')); |
1716 | | } |
1717 | | } |
1718 | | return true; |
1719 | | } catch (phpmailerException $exc) { |
1720 | | $lastexception = $exc; |
1721 | | $this->edebug($exc->getMessage()); |
1722 | | // We must have connected, but then failed TLS or Auth, so close connection nicely |
1723 | | $this->smtp->quit(); |
1724 | | } |
1725 | | } |
1726 | | } |
1727 | | // If we get here, all connection attempts have failed, so close connection hard |
1728 | | $this->smtp->close(); |
1729 | | // As we've caught all exceptions, just report whatever the last one was |
1730 | | if ($this->exceptions and !is_null($lastexception)) { |
1731 | | throw $lastexception; |
1732 | | } |
1733 | | return false; |
1734 | | } |
1735 | | |
1736 | | /** |
1737 | | * Close the active SMTP session if one exists. |
1738 | | * @return void |
1739 | | */ |
1740 | | public function smtpClose() |
1741 | | { |
1742 | | if (is_a($this->smtp, 'SMTP')) { |
1743 | | if ($this->smtp->connected()) { |
1744 | | $this->smtp->quit(); |
1745 | | $this->smtp->close(); |
1746 | | } |
1747 | | } |
1748 | | } |
1749 | | |
1750 | | /** |
1751 | | * Set the language for error messages. |
1752 | | * Returns false if it cannot load the language file. |
1753 | | * The default language is English. |
1754 | | * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr") |
1755 | | * @param string $lang_path Path to the language file directory, with trailing separator (slash) |
1756 | | * @return boolean |
1757 | | * @access public |
1758 | | */ |
1759 | | public function setLanguage($langcode = 'en', $lang_path = '') |
1760 | | { |
1761 | | // Backwards compatibility for renamed language codes |
1762 | | $renamed_langcodes = array( |
1763 | | 'br' => 'pt_br', |
1764 | | 'cz' => 'cs', |
1765 | | 'dk' => 'da', |
1766 | | 'no' => 'nb', |
1767 | | 'se' => 'sv', |
1768 | | 'sr' => 'rs' |
1769 | | ); |
1770 | | |
1771 | | if (isset($renamed_langcodes[$langcode])) { |
1772 | | $langcode = $renamed_langcodes[$langcode]; |
1773 | | } |
1774 | | |
1775 | | // Define full set of translatable strings in English |
1776 | | $PHPMAILER_LANG = array( |
1777 | | 'authenticate' => 'SMTP Error: Could not authenticate.', |
1778 | | 'connect_host' => 'SMTP Error: Could not connect to SMTP host.', |
1779 | | 'data_not_accepted' => 'SMTP Error: data not accepted.', |
1780 | | 'empty_message' => 'Message body empty', |
1781 | | 'encoding' => 'Unknown encoding: ', |
1782 | | 'execute' => 'Could not execute: ', |
1783 | | 'file_access' => 'Could not access file: ', |
1784 | | 'file_open' => 'File Error: Could not open file: ', |
1785 | | 'from_failed' => 'The following From address failed: ', |
1786 | | 'instantiate' => 'Could not instantiate mail function.', |
1787 | | 'invalid_address' => 'Invalid address: ', |
1788 | | 'mailer_not_supported' => ' mailer is not supported.', |
1789 | | 'provide_address' => 'You must provide at least one recipient email address.', |
1790 | | 'recipients_failed' => 'SMTP Error: The following recipients failed: ', |
1791 | | 'signing' => 'Signing Error: ', |
1792 | | 'smtp_connect_failed' => 'SMTP connect() failed.', |
1793 | | 'smtp_error' => 'SMTP server error: ', |
1794 | | 'variable_set' => 'Cannot set or reset variable: ', |
1795 | | 'extension_missing' => 'Extension missing: ' |
1796 | | ); |
1797 | | if (empty($lang_path)) { |
1798 | | // Calculate an absolute path so it can work if CWD is not here |
1799 | | $lang_path = dirname(__FILE__). DIRECTORY_SEPARATOR . 'language'. DIRECTORY_SEPARATOR; |
1800 | | } |
1801 | | //Validate $langcode |
1802 | | if (!preg_match('/^[a-z]{2}(?:_[a-zA-Z]{2})?$/', $langcode)) { |
1803 | | $langcode = 'en'; |
1804 | | } |
1805 | | $foundlang = true; |
1806 | | $lang_file = $lang_path . 'phpmailer.lang-' . $langcode . '.php'; |
1807 | | // There is no English translation file |
1808 | | if ($langcode != 'en') { |
1809 | | // Make sure language file path is readable |
1810 | | if (!self::isPermittedPath($lang_file) or !is_readable($lang_file)) { |
1811 | | $foundlang = false; |
1812 | | } else { |
1813 | | // Overwrite language-specific strings. |
1814 | | // This way we'll never have missing translation keys. |
1815 | | $foundlang = include $lang_file; |
1816 | | } |
1817 | | } |
1818 | | $this->language = $PHPMAILER_LANG; |
1819 | | return (boolean)$foundlang; // Returns false if language not found |
1820 | | } |
1821 | | |
1822 | | /** |
1823 | | * Get the array of strings for the current language. |
1824 | | * @return array |
1825 | | */ |
1826 | | public function getTranslations() |
1827 | | { |
1828 | | return $this->language; |
1829 | | } |
1830 | | |
1831 | | /** |
1832 | | * Create recipient headers. |
1833 | | * @access public |
1834 | | * @param string $type |
1835 | | * @param array $addr An array of recipient, |
1836 | | * where each recipient is a 2-element indexed array with element 0 containing an address |
1837 | | * and element 1 containing a name, like: |
1838 | | * array(array('joe@example.com', 'Joe User'), array('zoe@example.com', 'Zoe User')) |
1839 | | * @return string |
1840 | | */ |
1841 | | public function addrAppend($type, $addr) |
1842 | | { |
1843 | | $addresses = array(); |
1844 | | foreach ($addr as $address) { |
1845 | | $addresses[] = $this->addrFormat($address); |
1846 | | } |
1847 | | return $type . ': ' . implode(', ', $addresses) . $this->LE; |
1848 | | } |
1849 | | |
1850 | | /** |
1851 | | * Format an address for use in a message header. |
1852 | | * @access public |
1853 | | * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name |
1854 | | * like array('joe@example.com', 'Joe User') |
1855 | | * @return string |
1856 | | */ |
1857 | | public function addrFormat($addr) |
1858 | | { |
1859 | | if (empty($addr[1])) { // No name provided |
1860 | | return $this->secureHeader($addr[0]); |
1861 | | } else { |
1862 | | return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . ' <' . $this->secureHeader( |
1863 | | $addr[0] |
1864 | | ) . '>'; |
1865 | | } |
1866 | | } |
1867 | | |
1868 | | /** |
1869 | | * Word-wrap message. |
1870 | | * For use with mailers that do not automatically perform wrapping |
1871 | | * and for quoted-printable encoded messages. |
1872 | | * Original written by philippe. |
1873 | | * @param string $message The message to wrap |
1874 | | * @param integer $length The line length to wrap to |
1875 | | * @param boolean $qp_mode Whether to run in Quoted-Printable mode |
1876 | | * @access public |
1877 | | * @return string |
1878 | | */ |
1879 | | public function wrapText($message, $length, $qp_mode = false) |
1880 | | { |
1881 | | if ($qp_mode) { |
1882 | | $soft_break = sprintf(' =%s', $this->LE); |
1883 | | } else { |
1884 | | $soft_break = $this->LE; |
1885 | | } |
1886 | | // If utf-8 encoding is used, we will need to make sure we don't |
1887 | | // split multibyte characters when we wrap |
1888 | | $is_utf8 = (strtolower($this->CharSet) == 'utf-8'); |
1889 | | $lelen = strlen($this->LE); |
1890 | | $crlflen = strlen(self::CRLF); |
1891 | | |
1892 | | $message = $this->fixEOL($message); |
1893 | | //Remove a trailing line break |
1894 | | if (substr($message, -$lelen) == $this->LE) { |
1895 | | $message = substr($message, 0, -$lelen); |
1896 | | } |
1897 | | |
1898 | | //Split message into lines |
1899 | | $lines = explode($this->LE, $message); |
1900 | | //Message will be rebuilt in here |
1901 | | $message = ''; |
1902 | | foreach ($lines as $line) { |
1903 | | $words = explode(' ', $line); |
1904 | | $buf = ''; |
1905 | | $firstword = true; |
1906 | | foreach ($words as $word) { |
1907 | | if ($qp_mode and (strlen($word) > $length)) { |
1908 | | $space_left = $length - strlen($buf) - $crlflen; |
1909 | | if (!$firstword) { |
1910 | | if ($space_left > 20) { |
1911 | | $len = $space_left; |
1912 | | if ($is_utf8) { |
1913 | | $len = $this->utf8CharBoundary($word, $len); |
1914 | | } elseif (substr($word, $len - 1, 1) == '=') { |
1915 | | $len--; |
1916 | | } elseif (substr($word, $len - 2, 1) == '=') { |
1917 | | $len -= 2; |
1918 | | } |
1919 | | $part = substr($word, 0, $len); |
1920 | | $word = substr($word, $len); |
1921 | | $buf .= ' ' . $part; |
1922 | | $message .= $buf . sprintf('=%s', self::CRLF); |
1923 | | } else { |
1924 | | $message .= $buf . $soft_break; |
1925 | | } |
1926 | | $buf = ''; |
1927 | | } |
1928 | | while (strlen($word) > 0) { |
1929 | | if ($length <= 0) { |
1930 | | break; |
1931 | | } |
1932 | | $len = $length; |
1933 | | if ($is_utf8) { |
1934 | | $len = $this->utf8CharBoundary($word, $len); |
1935 | | } elseif (substr($word, $len - 1, 1) == '=') { |
1936 | | $len--; |
1937 | | } elseif (substr($word, $len - 2, 1) == '=') { |
1938 | | $len -= 2; |
1939 | | } |
1940 | | $part = substr($word, 0, $len); |
1941 | | $word = substr($word, $len); |
1942 | | |
1943 | | if (strlen($word) > 0) { |
1944 | | $message .= $part . sprintf('=%s', self::CRLF); |
1945 | | } else { |
1946 | | $buf = $part; |
1947 | | } |
1948 | | } |
1949 | | } else { |
1950 | | $buf_o = $buf; |
1951 | | if (!$firstword) { |
1952 | | $buf .= ' '; |
1953 | | } |
1954 | | $buf .= $word; |
1955 | | |
1956 | | if (strlen($buf) > $length and $buf_o != '') { |
1957 | | $message .= $buf_o . $soft_break; |
1958 | | $buf = $word; |
1959 | | } |
1960 | | } |
1961 | | $firstword = false; |
1962 | | } |
1963 | | $message .= $buf . self::CRLF; |
1964 | | } |
1965 | | |
1966 | | return $message; |
1967 | | } |
1968 | | |
1969 | | /** |
1970 | | * Find the last character boundary prior to $maxLength in a utf-8 |
1971 | | * quoted-printable encoded string. |
1972 | | * Original written by Colin Brown. |
1973 | | * @access public |
1974 | | * @param string $encodedText utf-8 QP text |
1975 | | * @param integer $maxLength Find the last character boundary prior to this length |
1976 | | * @return integer |
1977 | | */ |
1978 | | public function utf8CharBoundary($encodedText, $maxLength) |
1979 | | { |
1980 | | $foundSplitPos = false; |
1981 | | $lookBack = 3; |
1982 | | while (!$foundSplitPos) { |
1983 | | $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack); |
1984 | | $encodedCharPos = strpos($lastChunk, '='); |
1985 | | if (false !== $encodedCharPos) { |
1986 | | // Found start of encoded character byte within $lookBack block. |
1987 | | // Check the encoded byte value (the 2 chars after the '=') |
1988 | | $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2); |
1989 | | $dec = hexdec($hex); |
1990 | | if ($dec < 128) { |
1991 | | // Single byte character. |
1992 | | // If the encoded char was found at pos 0, it will fit |
1993 | | // otherwise reduce maxLength to start of the encoded char |
1994 | | if ($encodedCharPos > 0) { |
1995 | | $maxLength = $maxLength - ($lookBack - $encodedCharPos); |
1996 | | } |
1997 | | $foundSplitPos = true; |
1998 | | } elseif ($dec >= 192) { |
1999 | | // First byte of a multi byte character |
2000 | | // Reduce maxLength to split at start of character |
2001 | | $maxLength = $maxLength - ($lookBack - $encodedCharPos); |
2002 | | $foundSplitPos = true; |
2003 | | } elseif ($dec < 192) { |
2004 | | // Middle byte of a multi byte character, look further back |
2005 | | $lookBack += 3; |
2006 | | } |
2007 | | } else { |
2008 | | // No encoded character found |
2009 | | $foundSplitPos = true; |
2010 | | } |
2011 | | } |
2012 | | return $maxLength; |
2013 | | } |
2014 | | |
2015 | | /** |
2016 | | * Apply word wrapping to the message body. |
2017 | | * Wraps the message body to the number of chars set in the WordWrap property. |
2018 | | * You should only do this to plain-text bodies as wrapping HTML tags may break them. |
2019 | | * This is called automatically by createBody(), so you don't need to call it yourself. |
2020 | | * @access public |
2021 | | * @return void |
2022 | | */ |
2023 | | public function setWordWrap() |
2024 | | { |
2025 | | if ($this->WordWrap < 1) { |
2026 | | return; |
2027 | | } |
2028 | | |
2029 | | switch ($this->message_type) { |
2030 | | case 'alt': |
2031 | | case 'alt_inline': |
2032 | | case 'alt_attach': |
2033 | | case 'alt_inline_attach': |
2034 | | $this->AltBody = $this->wrapText($this->AltBody, $this->WordWrap); |
2035 | | break; |
2036 | | default: |
2037 | | $this->Body = $this->wrapText($this->Body, $this->WordWrap); |
2038 | | break; |
2039 | | } |
2040 | | } |
2041 | | |
2042 | | /** |
2043 | | * Assemble message headers. |
2044 | | * @access public |
2045 | | * @return string The assembled headers |
2046 | | */ |
2047 | | public function createHeader() |
2048 | | { |
2049 | | $result = ''; |
2050 | | |
2051 | | $result .= $this->headerLine('Date', $this->MessageDate == '' ? self::rfcDate() : $this->MessageDate); |
2052 | | |
2053 | | // To be created automatically by mail() |
2054 | | if ($this->SingleTo) { |
2055 | | if ($this->Mailer != 'mail') { |
2056 | | foreach ($this->to as $toaddr) { |
2057 | | $this->SingleToArray[] = $this->addrFormat($toaddr); |
2058 | | } |
2059 | | } |
2060 | | } else { |
2061 | | if (count($this->to) > 0) { |
2062 | | if ($this->Mailer != 'mail') { |
2063 | | $result .= $this->addrAppend('To', $this->to); |
2064 | | } |
2065 | | } elseif (count($this->cc) == 0) { |
2066 | | $result .= $this->headerLine('To', 'undisclosed-recipients:;'); |
2067 | | } |
2068 | | } |
2069 | | |
2070 | | $result .= $this->addrAppend('From', array(array(trim($this->From), $this->FromName))); |
2071 | | |
2072 | | // sendmail and mail() extract Cc from the header before sending |
2073 | | if (count($this->cc) > 0) { |
2074 | | $result .= $this->addrAppend('Cc', $this->cc); |
2075 | | } |
2076 | | |
2077 | | // sendmail and mail() extract Bcc from the header before sending |
2078 | | if (( |
2079 | | $this->Mailer == 'sendmail' or $this->Mailer == 'qmail' or $this->Mailer == 'mail' |
2080 | | ) |
2081 | | and count($this->bcc) > 0 |
2082 | | ) { |
2083 | | $result .= $this->addrAppend('Bcc', $this->bcc); |
2084 | | } |
2085 | | |
2086 | | if (count($this->ReplyTo) > 0) { |
2087 | | $result .= $this->addrAppend('Reply-To', $this->ReplyTo); |
2088 | | } |
2089 | | |
2090 | | // mail() sets the subject itself |
2091 | | if ($this->Mailer != 'mail') { |
2092 | | $result .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader($this->Subject))); |
2093 | | } |
2094 | | |
2095 | | // Only allow a custom message ID if it conforms to RFC 5322 section 3.6.4 |
2096 | | // https://tools.ietf.org/html/rfc5322#section-3.6.4 |
2097 | | if ('' != $this->MessageID and preg_match('/^<.*@.*>$/', $this->MessageID)) { |
2098 | | $this->lastMessageID = $this->MessageID; |
2099 | | } else { |
2100 | | $this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->serverHostname()); |
2101 | | } |
2102 | | $result .= $this->headerLine('Message-ID', $this->lastMessageID); |
2103 | | if (!is_null($this->Priority)) { |
2104 | | $result .= $this->headerLine('X-Priority', $this->Priority); |
2105 | | } |
2106 | | if ($this->XMailer == '') { |
2107 | | $result .= $this->headerLine( |
2108 | | 'X-Mailer', |
2109 | | 'PHPMailer ' . $this->Version . ' (https://github.com/PHPMailer/PHPMailer)' |
2110 | | ); |
2111 | | } else { |
2112 | | $myXmailer = trim($this->XMailer); |
2113 | | if ($myXmailer) { |
2114 | | $result .= $this->headerLine('X-Mailer', $myXmailer); |
2115 | | } |
2116 | | } |
2117 | | |
2118 | | if ($this->ConfirmReadingTo != '') { |
2119 | | $result .= $this->headerLine('Disposition-Notification-To', '<' . $this->ConfirmReadingTo . '>'); |
2120 | | } |
2121 | | |
2122 | | // Add custom headers |
2123 | | foreach ($this->CustomHeader as $header) { |
2124 | | $result .= $this->headerLine( |
2125 | | trim($header[0]), |
2126 | | $this->encodeHeader(trim($header[1])) |
2127 | | ); |
2128 | | } |
2129 | | if (!$this->sign_key_file) { |
2130 | | $result .= $this->headerLine('MIME-Version', '1.0'); |
2131 | | $result .= $this->getMailMIME(); |
2132 | | } |
2133 | | |
2134 | | return $result; |
2135 | | } |
2136 | | |
2137 | | /** |
2138 | | * Get the message MIME type headers. |
2139 | | * @access public |
2140 | | * @return string |
2141 | | */ |
2142 | | public function getMailMIME() |
2143 | | { |
2144 | | $result = ''; |
2145 | | $ismultipart = true; |
2146 | | switch ($this->message_type) { |
2147 | | case 'inline': |
2148 | | $result .= $this->headerLine('Content-Type', 'multipart/related;'); |
2149 | | $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"'); |
2150 | | break; |
2151 | | case 'attach': |
2152 | | case 'inline_attach': |
2153 | | case 'alt_attach': |
2154 | | case 'alt_inline_attach': |
2155 | | $result .= $this->headerLine('Content-Type', 'multipart/mixed;'); |
2156 | | $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"'); |
2157 | | break; |
2158 | | case 'alt': |
2159 | | case 'alt_inline': |
2160 | | $result .= $this->headerLine('Content-Type', 'multipart/alternative;'); |
2161 | | $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"'); |
2162 | | break; |
2163 | | default: |
2164 | | // Catches case 'plain': and case '': |
2165 | | $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet); |
2166 | | $ismultipart = false; |
2167 | | break; |
2168 | | } |
2169 | | // RFC1341 part 5 says 7bit is assumed if not specified |
2170 | | if ($this->Encoding != '7bit') { |
2171 | | // RFC 2045 section 6.4 says multipart MIME parts may only use 7bit, 8bit or binary CTE |
2172 | | if ($ismultipart) { |
2173 | | if ($this->Encoding == '8bit') { |
2174 | | $result .= $this->headerLine('Content-Transfer-Encoding', '8bit'); |
2175 | | } |
2176 | | // The only remaining alternatives are quoted-printable and base64, which are both 7bit compatible |
2177 | | } else { |
2178 | | $result .= $this->headerLine('Content-Transfer-Encoding', $this->Encoding); |
2179 | | } |
2180 | | } |
2181 | | |
2182 | | if ($this->Mailer != 'mail') { |
2183 | | $result .= $this->LE; |
2184 | | } |
2185 | | |
2186 | | return $result; |
2187 | | } |
2188 | | |
2189 | | /** |
2190 | | * Returns the whole MIME message. |
2191 | | * Includes complete headers and body. |
2192 | | * Only valid post preSend(). |
2193 | | * @see PHPMailer::preSend() |
2194 | | * @access public |
2195 | | * @return string |
2196 | | */ |
2197 | | public function getSentMIMEMessage() |
2198 | | { |
2199 | | return rtrim($this->MIMEHeader . $this->mailHeader, "\n\r") . self::CRLF . self::CRLF . $this->MIMEBody; |
2200 | | } |
2201 | | |
2202 | | /** |
2203 | | * Create unique ID |
2204 | | * @return string |
2205 | | */ |
2206 | | protected function generateId() { |
2207 | | return md5(uniqid(time())); |
2208 | | } |
2209 | | |
2210 | | /** |
2211 | | * Assemble the message body. |
2212 | | * Returns an empty string on failure. |
2213 | | * @access public |
2214 | | * @throws phpmailerException |
2215 | | * @return string The assembled message body |
2216 | | */ |
2217 | | public function createBody() |
2218 | | { |
2219 | | $body = ''; |
2220 | | //Create unique IDs and preset boundaries |
2221 | | $this->uniqueid = $this->generateId(); |
2222 | | $this->boundary[1] = 'b1_' . $this->uniqueid; |
2223 | | $this->boundary[2] = 'b2_' . $this->uniqueid; |
2224 | | $this->boundary[3] = 'b3_' . $this->uniqueid; |
2225 | | |
2226 | | if ($this->sign_key_file) { |
2227 | | $body .= $this->getMailMIME() . $this->LE; |
2228 | | } |
2229 | | |
2230 | | $this->setWordWrap(); |
2231 | | |
2232 | | $bodyEncoding = $this->Encoding; |
2233 | | $bodyCharSet = $this->CharSet; |
2234 | | //Can we do a 7-bit downgrade? |
2235 | | if ($bodyEncoding == '8bit' and !$this->has8bitChars($this->Body)) { |
2236 | | $bodyEncoding = '7bit'; |
2237 | | //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit |
2238 | | $bodyCharSet = 'us-ascii'; |
2239 | | } |
2240 | | //If lines are too long, and we're not already using an encoding that will shorten them, |
2241 | | //change to quoted-printable transfer encoding for the body part only |
2242 | | if ('base64' != $this->Encoding and self::hasLineLongerThanMax($this->Body)) { |
2243 | | $bodyEncoding = 'quoted-printable'; |
2244 | | } |
2245 | | |
2246 | | $altBodyEncoding = $this->Encoding; |
2247 | | $altBodyCharSet = $this->CharSet; |
2248 | | //Can we do a 7-bit downgrade? |
2249 | | if ($altBodyEncoding == '8bit' and !$this->has8bitChars($this->AltBody)) { |
2250 | | $altBodyEncoding = '7bit'; |
2251 | | //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit |
2252 | | $altBodyCharSet = 'us-ascii'; |
2253 | | } |
2254 | | //If lines are too long, and we're not already using an encoding that will shorten them, |
2255 | | //change to quoted-printable transfer encoding for the alt body part only |
2256 | | if ('base64' != $altBodyEncoding and self::hasLineLongerThanMax($this->AltBody)) { |
2257 | | $altBodyEncoding = 'quoted-printable'; |
2258 | | } |
2259 | | //Use this as a preamble in all multipart message types |
2260 | | $mimepre = "This is a multi-part message in MIME format." . $this->LE . $this->LE; |
2261 | | switch ($this->message_type) { |
2262 | | case 'inline': |
2263 | | $body .= $mimepre; |
2264 | | $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); |
2265 | | $body .= $this->encodeString($this->Body, $bodyEncoding); |
2266 | | $body .= $this->LE . $this->LE; |
2267 | | $body .= $this->attachAll('inline', $this->boundary[1]); |
2268 | | break; |
2269 | | case 'attach': |
2270 | | $body .= $mimepre; |
2271 | | $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); |
2272 | | $body .= $this->encodeString($this->Body, $bodyEncoding); |
2273 | | $body .= $this->LE . $this->LE; |
2274 | | $body .= $this->attachAll('attachment', $this->boundary[1]); |
2275 | | break; |
2276 | | case 'inline_attach': |
2277 | | $body .= $mimepre; |
2278 | | $body .= $this->textLine('--' . $this->boundary[1]); |
2279 | | $body .= $this->headerLine('Content-Type', 'multipart/related;'); |
2280 | | $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); |
2281 | | $body .= $this->LE; |
2282 | | $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding); |
2283 | | $body .= $this->encodeString($this->Body, $bodyEncoding); |
2284 | | $body .= $this->LE . $this->LE; |
2285 | | $body .= $this->attachAll('inline', $this->boundary[2]); |
2286 | | $body .= $this->LE; |
2287 | | $body .= $this->attachAll('attachment', $this->boundary[1]); |
2288 | | break; |
2289 | | case 'alt': |
2290 | | $body .= $mimepre; |
2291 | | $body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding); |
2292 | | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
2293 | | $body .= $this->LE . $this->LE; |
2294 | | $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, 'text/html', $bodyEncoding); |
2295 | | $body .= $this->encodeString($this->Body, $bodyEncoding); |
2296 | | $body .= $this->LE . $this->LE; |
2297 | | if (!empty($this->Ical)) { |
2298 | | $body .= $this->getBoundary($this->boundary[1], '', 'text/calendar; method=REQUEST', ''); |
2299 | | $body .= $this->encodeString($this->Ical, $this->Encoding); |
2300 | | $body .= $this->LE . $this->LE; |
2301 | | } |
2302 | | $body .= $this->endBoundary($this->boundary[1]); |
2303 | | break; |
2304 | | case 'alt_inline': |
2305 | | $body .= $mimepre; |
2306 | | $body .= $this->getBoundary($this->boundary[1], $altBodyCharSet, 'text/plain', $altBodyEncoding); |
2307 | | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
2308 | | $body .= $this->LE . $this->LE; |
2309 | | $body .= $this->textLine('--' . $this->boundary[1]); |
2310 | | $body .= $this->headerLine('Content-Type', 'multipart/related;'); |
2311 | | $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); |
2312 | | $body .= $this->LE; |
2313 | | $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding); |
2314 | | $body .= $this->encodeString($this->Body, $bodyEncoding); |
2315 | | $body .= $this->LE . $this->LE; |
2316 | | $body .= $this->attachAll('inline', $this->boundary[2]); |
2317 | | $body .= $this->LE; |
2318 | | $body .= $this->endBoundary($this->boundary[1]); |
2319 | | break; |
2320 | | case 'alt_attach': |
2321 | | $body .= $mimepre; |
2322 | | $body .= $this->textLine('--' . $this->boundary[1]); |
2323 | | $body .= $this->headerLine('Content-Type', 'multipart/alternative;'); |
2324 | | $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); |
2325 | | $body .= $this->LE; |
2326 | | $body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding); |
2327 | | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
2328 | | $body .= $this->LE . $this->LE; |
2329 | | $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, 'text/html', $bodyEncoding); |
2330 | | $body .= $this->encodeString($this->Body, $bodyEncoding); |
2331 | | $body .= $this->LE . $this->LE; |
2332 | | $body .= $this->endBoundary($this->boundary[2]); |
2333 | | $body .= $this->LE; |
2334 | | $body .= $this->attachAll('attachment', $this->boundary[1]); |
2335 | | break; |
2336 | | case 'alt_inline_attach': |
2337 | | $body .= $mimepre; |
2338 | | $body .= $this->textLine('--' . $this->boundary[1]); |
2339 | | $body .= $this->headerLine('Content-Type', 'multipart/alternative;'); |
2340 | | $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"'); |
2341 | | $body .= $this->LE; |
2342 | | $body .= $this->getBoundary($this->boundary[2], $altBodyCharSet, 'text/plain', $altBodyEncoding); |
2343 | | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
2344 | | $body .= $this->LE . $this->LE; |
2345 | | $body .= $this->textLine('--' . $this->boundary[2]); |
2346 | | $body .= $this->headerLine('Content-Type', 'multipart/related;'); |
2347 | | $body .= $this->textLine("\tboundary=\"" . $this->boundary[3] . '"'); |
2348 | | $body .= $this->LE; |
2349 | | $body .= $this->getBoundary($this->boundary[3], $bodyCharSet, 'text/html', $bodyEncoding); |
2350 | | $body .= $this->encodeString($this->Body, $bodyEncoding); |
2351 | | $body .= $this->LE . $this->LE; |
2352 | | $body .= $this->attachAll('inline', $this->boundary[3]); |
2353 | | $body .= $this->LE; |
2354 | | $body .= $this->endBoundary($this->boundary[2]); |
2355 | | $body .= $this->LE; |
2356 | | $body .= $this->attachAll('attachment', $this->boundary[1]); |
2357 | | break; |
2358 | | default: |
2359 | | // Catch case 'plain' and case '', applies to simple `text/plain` and `text/html` body content types |
2360 | | //Reset the `Encoding` property in case we changed it for line length reasons |
2361 | | $this->Encoding = $bodyEncoding; |
2362 | | $body .= $this->encodeString($this->Body, $this->Encoding); |
2363 | | break; |
2364 | | } |
2365 | | |
2366 | | if ($this->isError()) { |
2367 | | $body = ''; |
2368 | | } elseif ($this->sign_key_file) { |
2369 | | try { |
2370 | | if (!defined('PKCS7_TEXT')) { |
2371 | | throw new phpmailerException($this->lang('extension_missing') . 'openssl'); |
2372 | | } |
2373 | | // @TODO would be nice to use php://temp streams here, but need to wrap for PHP < 5.1 |
2374 | | $file = tempnam(sys_get_temp_dir(), 'mail'); |
2375 | | if (false === file_put_contents($file, $body)) { |
2376 | | throw new phpmailerException($this->lang('signing') . ' Could not write temp file'); |
2377 | | } |
2378 | | $signed = tempnam(sys_get_temp_dir(), 'signed'); |
2379 | | //Workaround for PHP bug https://bugs.php.net/bug.php?id=69197 |
2380 | | if (empty($this->sign_extracerts_file)) { |
2381 | | $sign = @openssl_pkcs7_sign( |
2382 | | $file, |
2383 | | $signed, |
2384 | | 'file://' . realpath($this->sign_cert_file), |
2385 | | array('file://' . realpath($this->sign_key_file), $this->sign_key_pass), |
2386 | | null |
2387 | | ); |
2388 | | } else { |
2389 | | $sign = @openssl_pkcs7_sign( |
2390 | | $file, |
2391 | | $signed, |
2392 | | 'file://' . realpath($this->sign_cert_file), |
2393 | | array('file://' . realpath($this->sign_key_file), $this->sign_key_pass), |
2394 | | null, |
2395 | | PKCS7_DETACHED, |
2396 | | $this->sign_extracerts_file |
2397 | | ); |
2398 | | } |
2399 | | if ($sign) { |
2400 | | @unlink($file); |
2401 | | $body = file_get_contents($signed); |
2402 | | @unlink($signed); |
2403 | | //The message returned by openssl contains both headers and body, so need to split them up |
2404 | | $parts = explode("\n\n", $body, 2); |
2405 | | $this->MIMEHeader .= $parts[0] . $this->LE . $this->LE; |
2406 | | $body = $parts[1]; |
2407 | | } else { |
2408 | | @unlink($file); |
2409 | | @unlink($signed); |
2410 | | throw new phpmailerException($this->lang('signing') . openssl_error_string()); |
2411 | | } |
2412 | | } catch (phpmailerException $exc) { |
2413 | | $body = ''; |
2414 | | if ($this->exceptions) { |
2415 | | throw $exc; |
2416 | | } |
2417 | | } |
2418 | | } |
2419 | | return $body; |
2420 | | } |
2421 | | |
2422 | | /** |
2423 | | * Return the start of a message boundary. |
2424 | | * @access protected |
2425 | | * @param string $boundary |
2426 | | * @param string $charSet |
2427 | | * @param string $contentType |
2428 | | * @param string $encoding |
2429 | | * @return string |
2430 | | */ |
2431 | | protected function getBoundary($boundary, $charSet, $contentType, $encoding) |
2432 | | { |
2433 | | $result = ''; |
2434 | | if ($charSet == '') { |
2435 | | $charSet = $this->CharSet; |
2436 | | } |
2437 | | if ($contentType == '') { |
2438 | | $contentType = $this->ContentType; |
2439 | | } |
2440 | | if ($encoding == '') { |
2441 | | $encoding = $this->Encoding; |
2442 | | } |
2443 | | $result .= $this->textLine('--' . $boundary); |
2444 | | $result .= sprintf('Content-Type: %s; charset=%s', $contentType, $charSet); |
2445 | | $result .= $this->LE; |
2446 | | // RFC1341 part 5 says 7bit is assumed if not specified |
2447 | | if ($encoding != '7bit') { |
2448 | | $result .= $this->headerLine('Content-Transfer-Encoding', $encoding); |
2449 | | } |
2450 | | $result .= $this->LE; |
2451 | | |
2452 | | return $result; |
2453 | | } |
2454 | | |
2455 | | /** |
2456 | | * Return the end of a message boundary. |
2457 | | * @access protected |
2458 | | * @param string $boundary |
2459 | | * @return string |
2460 | | */ |
2461 | | protected function endBoundary($boundary) |
2462 | | { |
2463 | | return $this->LE . '--' . $boundary . '--' . $this->LE; |
2464 | | } |
2465 | | |
2466 | | /** |
2467 | | * Set the message type. |
2468 | | * PHPMailer only supports some preset message types, not arbitrary MIME structures. |
2469 | | * @access protected |
2470 | | * @return void |
2471 | | */ |
2472 | | protected function setMessageType() |
2473 | | { |
2474 | | $type = array(); |
2475 | | if ($this->alternativeExists()) { |
2476 | | $type[] = 'alt'; |
2477 | | } |
2478 | | if ($this->inlineImageExists()) { |
2479 | | $type[] = 'inline'; |
2480 | | } |
2481 | | if ($this->attachmentExists()) { |
2482 | | $type[] = 'attach'; |
2483 | | } |
2484 | | $this->message_type = implode('_', $type); |
2485 | | if ($this->message_type == '') { |
2486 | | //The 'plain' message_type refers to the message having a single body element, not that it is plain-text |
2487 | | $this->message_type = 'plain'; |
2488 | | } |
2489 | | } |
2490 | | |
2491 | | /** |
2492 | | * Format a header line. |
2493 | | * @access public |
2494 | | * @param string $name |
2495 | | * @param string $value |
2496 | | * @return string |
2497 | | */ |
2498 | | public function headerLine($name, $value) |
2499 | | { |
2500 | | return $name . ': ' . $value . $this->LE; |
2501 | | } |
2502 | | |
2503 | | /** |
2504 | | * Return a formatted mail line. |
2505 | | * @access public |
2506 | | * @param string $value |
2507 | | * @return string |
2508 | | */ |
2509 | | public function textLine($value) |
2510 | | { |
2511 | | return $value . $this->LE; |
2512 | | } |
2513 | | |
2514 | | /** |
2515 | | * Add an attachment from a path on the filesystem. |
2516 | | * Never use a user-supplied path to a file! |
2517 | | * Returns false if the file could not be found or read. |
2518 | | * Explicitly *does not* support passing URLs; PHPMailer is not an HTTP client. |
2519 | | * If you need to do that, fetch the resource yourself and pass it in via a local file or string. |
2520 | | * @param string $path Path to the attachment. |
2521 | | * @param string $name Overrides the attachment name. |
2522 | | * @param string $encoding File encoding (see $Encoding). |
2523 | | * @param string $type File extension (MIME) type. |
2524 | | * @param string $disposition Disposition to use |
2525 | | * @throws phpmailerException |
2526 | | * @return boolean |
2527 | | */ |
2528 | | public function addAttachment($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment') |
2529 | | { |
2530 | | try { |
2531 | | if (!self::isPermittedPath($path) or !@is_file($path)) { |
2532 | | throw new phpmailerException($this->lang('file_access') . $path, self::STOP_CONTINUE); |
2533 | | } |
2534 | | |
2535 | | // If a MIME type is not specified, try to work it out from the file name |
2536 | | if ($type == '') { |
2537 | | $type = self::filenameToType($path); |
2538 | | } |
2539 | | |
2540 | | $filename = basename($path); |
2541 | | if ($name == '') { |
2542 | | $name = $filename; |
2543 | | } |
2544 | | |
2545 | | $this->attachment[] = array( |
2546 | | 0 => $path, |
2547 | | 1 => $filename, |
2548 | | 2 => $name, |
2549 | | 3 => $encoding, |
2550 | | 4 => $type, |
2551 | | 5 => false, // isStringAttachment |
2552 | | 6 => $disposition, |
2553 | | 7 => 0 |
2554 | | ); |
2555 | | |
2556 | | } catch (phpmailerException $exc) { |
2557 | | $this->setError($exc->getMessage()); |
2558 | | $this->edebug($exc->getMessage()); |
2559 | | if ($this->exceptions) { |
2560 | | throw $exc; |
2561 | | } |
2562 | | return false; |
2563 | | } |
2564 | | return true; |
2565 | | } |
2566 | | |
2567 | | /** |
2568 | | * Return the array of attachments. |
2569 | | * @return array |
2570 | | */ |
2571 | | public function getAttachments() |
2572 | | { |
2573 | | return $this->attachment; |
2574 | | } |
2575 | | |
2576 | | /** |
2577 | | * Attach all file, string, and binary attachments to the message. |
2578 | | * Returns an empty string on failure. |
2579 | | * @access protected |
2580 | | * @param string $disposition_type |
2581 | | * @param string $boundary |
2582 | | * @return string |
2583 | | */ |
2584 | | protected function attachAll($disposition_type, $boundary) |
2585 | | { |
2586 | | // Return text of body |
2587 | | $mime = array(); |
2588 | | $cidUniq = array(); |
2589 | | $incl = array(); |
2590 | | |
2591 | | // Add all attachments |
2592 | | foreach ($this->attachment as $attachment) { |
2593 | | // Check if it is a valid disposition_filter |
2594 | | if ($attachment[6] == $disposition_type) { |
2595 | | // Check for string attachment |
2596 | | $string = ''; |
2597 | | $path = ''; |
2598 | | $bString = $attachment[5]; |
2599 | | if ($bString) { |
2600 | | $string = $attachment[0]; |
2601 | | } else { |
2602 | | $path = $attachment[0]; |
2603 | | } |
2604 | | |
2605 | | $inclhash = md5(serialize($attachment)); |
2606 | | if (in_array($inclhash, $incl)) { |
2607 | | continue; |
2608 | | } |
2609 | | $incl[] = $inclhash; |
2610 | | $name = $attachment[2]; |
2611 | | $encoding = $attachment[3]; |
2612 | | $type = $attachment[4]; |
2613 | | $disposition = $attachment[6]; |
2614 | | $cid = $attachment[7]; |
2615 | | if ($disposition == 'inline' && array_key_exists($cid, $cidUniq)) { |
2616 | | continue; |
2617 | | } |
2618 | | $cidUniq[$cid] = true; |
2619 | | |
2620 | | $mime[] = sprintf('--%s%s', $boundary, $this->LE); |
2621 | | //Only include a filename property if we have one |
2622 | | if (!empty($name)) { |
2623 | | $mime[] = sprintf( |
2624 | | 'Content-Type: %s; name="%s"%s', |
2625 | | $type, |
2626 | | $this->encodeHeader($this->secureHeader($name)), |
2627 | | $this->LE |
2628 | | ); |
2629 | | } else { |
2630 | | $mime[] = sprintf( |
2631 | | 'Content-Type: %s%s', |
2632 | | $type, |
2633 | | $this->LE |
2634 | | ); |
2635 | | } |
2636 | | // RFC1341 part 5 says 7bit is assumed if not specified |
2637 | | if ($encoding != '7bit') { |
2638 | | $mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, $this->LE); |
2639 | | } |
2640 | | |
2641 | | if ($disposition == 'inline') { |
2642 | | $mime[] = sprintf('Content-ID: <%s>%s', $cid, $this->LE); |
2643 | | } |
2644 | | |
2645 | | // If a filename contains any of these chars, it should be quoted, |
2646 | | // but not otherwise: RFC2183 & RFC2045 5.1 |
2647 | | // Fixes a warning in IETF's msglint MIME checker |
2648 | | // Allow for bypassing the Content-Disposition header totally |
2649 | | if (!(empty($disposition))) { |
2650 | | $encoded_name = $this->encodeHeader($this->secureHeader($name)); |
2651 | | if (preg_match('/[ \(\)<>@,;:\\"\/\[\]\?=]/', $encoded_name)) { |
2652 | | $mime[] = sprintf( |
2653 | | 'Content-Disposition: %s; filename="%s"%s', |
2654 | | $disposition, |
2655 | | $encoded_name, |
2656 | | $this->LE . $this->LE |
2657 | | ); |
2658 | | } else { |
2659 | | if (!empty($encoded_name)) { |
2660 | | $mime[] = sprintf( |
2661 | | 'Content-Disposition: %s; filename=%s%s', |
2662 | | $disposition, |
2663 | | $encoded_name, |
2664 | | $this->LE . $this->LE |
2665 | | ); |
2666 | | } else { |
2667 | | $mime[] = sprintf( |
2668 | | 'Content-Disposition: %s%s', |
2669 | | $disposition, |
2670 | | $this->LE . $this->LE |
2671 | | ); |
2672 | | } |
2673 | | } |
2674 | | } else { |
2675 | | $mime[] = $this->LE; |
2676 | | } |
2677 | | |
2678 | | // Encode as string attachment |
2679 | | if ($bString) { |
2680 | | $mime[] = $this->encodeString($string, $encoding); |
2681 | | if ($this->isError()) { |
2682 | | return ''; |
2683 | | } |
2684 | | $mime[] = $this->LE . $this->LE; |
2685 | | } else { |
2686 | | $mime[] = $this->encodeFile($path, $encoding); |
2687 | | if ($this->isError()) { |
2688 | | return ''; |
2689 | | } |
2690 | | $mime[] = $this->LE . $this->LE; |
2691 | | } |
2692 | | } |
2693 | | } |
2694 | | |
2695 | | $mime[] = sprintf('--%s--%s', $boundary, $this->LE); |
2696 | | |
2697 | | return implode('', $mime); |
2698 | | } |
2699 | | |
2700 | | /** |
2701 | | * Encode a file attachment in requested format. |
2702 | | * Returns an empty string on failure. |
2703 | | * @param string $path The full path to the file |
2704 | | * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' |
2705 | | * @throws phpmailerException |
2706 | | * @access protected |
2707 | | * @return string |
2708 | | */ |
2709 | | protected function encodeFile($path, $encoding = 'base64') |
2710 | | { |
2711 | | try { |
2712 | | if (!self::isPermittedPath($path) or !file_exists($path)) { |
2713 | | throw new phpmailerException($this->lang('file_open') . $path, self::STOP_CONTINUE); |
2714 | | } |
2715 | | $magic_quotes = ( PHP_VERSION_ID < 70400 && get_magic_quotes_runtime() ); // WP: Patched for PHP 7.4. |
2716 | | if ($magic_quotes) { |
2717 | | if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
2718 | | set_magic_quotes_runtime(false); |
2719 | | } else { |
2720 | | //Doesn't exist in PHP 5.4, but we don't need to check because |
2721 | | //get_magic_quotes_runtime always returns false in 5.4+ |
2722 | | //so it will never get here |
2723 | | ini_set('magic_quotes_runtime', false); |
2724 | | } |
2725 | | } |
2726 | | $file_buffer = file_get_contents($path); |
2727 | | $file_buffer = $this->encodeString($file_buffer, $encoding); |
2728 | | if ($magic_quotes) { |
2729 | | if (version_compare(PHP_VERSION, '5.3.0', '<')) { |
2730 | | set_magic_quotes_runtime($magic_quotes); |
2731 | | } else { |
2732 | | ini_set('magic_quotes_runtime', $magic_quotes); |
2733 | | } |
2734 | | } |
2735 | | return $file_buffer; |
2736 | | } catch (Exception $exc) { |
2737 | | $this->setError($exc->getMessage()); |
2738 | | return ''; |
2739 | | } |
2740 | | } |
2741 | | |
2742 | | /** |
2743 | | * Encode a string in requested format. |
2744 | | * Returns an empty string on failure. |
2745 | | * @param string $str The text to encode |
2746 | | * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' |
2747 | | * @access public |
2748 | | * @return string |
2749 | | */ |
2750 | | public function encodeString($str, $encoding = 'base64') |
2751 | | { |
2752 | | $encoded = ''; |
2753 | | switch (strtolower($encoding)) { |
2754 | | case 'base64': |
2755 | | $encoded = chunk_split(base64_encode($str), 76, $this->LE); |
2756 | | break; |
2757 | | case '7bit': |
2758 | | case '8bit': |
2759 | | $encoded = $this->fixEOL($str); |
2760 | | // Make sure it ends with a line break |
2761 | | if (substr($encoded, -(strlen($this->LE))) != $this->LE) { |
2762 | | $encoded .= $this->LE; |
2763 | | } |
2764 | | break; |
2765 | | case 'binary': |
2766 | | $encoded = $str; |
2767 | | break; |
2768 | | case 'quoted-printable': |
2769 | | $encoded = $this->encodeQP($str); |
2770 | | break; |
2771 | | default: |
2772 | | $this->setError($this->lang('encoding') . $encoding); |
2773 | | break; |
2774 | | } |
2775 | | return $encoded; |
2776 | | } |
2777 | | |
2778 | | /** |
2779 | | * Encode a header string optimally. |
2780 | | * Picks shortest of Q, B, quoted-printable or none. |
2781 | | * @access public |
2782 | | * @param string $str |
2783 | | * @param string $position |
2784 | | * @return string |
2785 | | */ |
2786 | | public function encodeHeader($str, $position = 'text') |
2787 | | { |
2788 | | $matchcount = 0; |
2789 | | switch (strtolower($position)) { |
2790 | | case 'phrase': |
2791 | | if (!preg_match('/[\200-\377]/', $str)) { |
2792 | | // Can't use addslashes as we don't know the value of magic_quotes_sybase |
2793 | | $encoded = addcslashes($str, "\0..\37\177\\\""); |
2794 | | if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) { |
2795 | | return ($encoded); |
2796 | | } else { |
2797 | | return ("\"$encoded\""); |
2798 | | } |
2799 | | } |
2800 | | $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches); |
2801 | | break; |
2802 | | /** @noinspection PhpMissingBreakStatementInspection */ |
2803 | | case 'comment': |
2804 | | $matchcount = preg_match_all('/[()"]/', $str, $matches); |
2805 | | // Intentional fall-through |
2806 | | case 'text': |
2807 | | default: |
2808 | | $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); |
2809 | | break; |
2810 | | } |
2811 | | |
2812 | | //There are no chars that need encoding |
2813 | | if ($matchcount == 0) { |
2814 | | return ($str); |
2815 | | } |
2816 | | |
2817 | | $maxlen = 75 - 7 - strlen($this->CharSet); |
2818 | | // Try to select the encoding which should produce the shortest output |
2819 | | if ($matchcount > strlen($str) / 3) { |
2820 | | // More than a third of the content will need encoding, so B encoding will be most efficient |
2821 | | $encoding = 'B'; |
2822 | | if (function_exists('mb_strlen') && $this->hasMultiBytes($str)) { |
2823 | | // Use a custom function which correctly encodes and wraps long |
2824 | | // multibyte strings without breaking lines within a character |
2825 | | $encoded = $this->base64EncodeWrapMB($str, "\n"); |
2826 | | } else { |
2827 | | $encoded = base64_encode($str); |
2828 | | $maxlen -= $maxlen % 4; |
2829 | | $encoded = trim(chunk_split($encoded, $maxlen, "\n")); |
2830 | | } |
2831 | | } else { |
2832 | | $encoding = 'Q'; |
2833 | | $encoded = $this->encodeQ($str, $position); |
2834 | | $encoded = $this->wrapText($encoded, $maxlen, true); |
2835 | | $encoded = str_replace('=' . self::CRLF, "\n", trim($encoded)); |
2836 | | } |
2837 | | |
2838 | | $encoded = preg_replace('/^(.*)$/m', ' =?' . $this->CharSet . "?$encoding?\\1?=", $encoded); |
2839 | | $encoded = trim(str_replace("\n", $this->LE, $encoded)); |
2840 | | |
2841 | | return $encoded; |
2842 | | } |
2843 | | |
2844 | | /** |
2845 | | * Check if a string contains multi-byte characters. |
2846 | | * @access public |
2847 | | * @param string $str multi-byte text to wrap encode |
2848 | | * @return boolean |
2849 | | */ |
2850 | | public function hasMultiBytes($str) |
2851 | | { |
2852 | | if (function_exists('mb_strlen')) { |
2853 | | return (strlen($str) > mb_strlen($str, $this->CharSet)); |
2854 | | } else { // Assume no multibytes (we can't handle without mbstring functions anyway) |
2855 | | return false; |
2856 | | } |
2857 | | } |
2858 | | |
2859 | | /** |
2860 | | * Does a string contain any 8-bit chars (in any charset)? |
2861 | | * @param string $text |
2862 | | * @return boolean |
2863 | | */ |
2864 | | public function has8bitChars($text) |
2865 | | { |
2866 | | return (boolean)preg_match('/[\x80-\xFF]/', $text); |
2867 | | } |
2868 | | |
2869 | | /** |
2870 | | * Encode and wrap long multibyte strings for mail headers |
2871 | | * without breaking lines within a character. |
2872 | | * Adapted from a function by paravoid |
2873 | | * @link http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283 |
2874 | | * @access public |
2875 | | * @param string $str multi-byte text to wrap encode |
2876 | | * @param string $linebreak string to use as linefeed/end-of-line |
2877 | | * @return string |
2878 | | */ |
2879 | | public function base64EncodeWrapMB($str, $linebreak = null) |
2880 | | { |
2881 | | $start = '=?' . $this->CharSet . '?B?'; |
2882 | | $end = '?='; |
2883 | | $encoded = ''; |
2884 | | if ($linebreak === null) { |
2885 | | $linebreak = $this->LE; |
2886 | | } |
2887 | | |
2888 | | $mb_length = mb_strlen($str, $this->CharSet); |
2889 | | // Each line must have length <= 75, including $start and $end |
2890 | | $length = 75 - strlen($start) - strlen($end); |
2891 | | // Average multi-byte ratio |
2892 | | $ratio = $mb_length / strlen($str); |
2893 | | // Base64 has a 4:3 ratio |
2894 | | $avgLength = floor($length * $ratio * .75); |
2895 | | |
2896 | | for ($i = 0; $i < $mb_length; $i += $offset) { |
2897 | | $lookBack = 0; |
2898 | | do { |
2899 | | $offset = $avgLength - $lookBack; |
2900 | | $chunk = mb_substr($str, $i, $offset, $this->CharSet); |
2901 | | $chunk = base64_encode($chunk); |
2902 | | $lookBack++; |
2903 | | } while (strlen($chunk) > $length); |
2904 | | $encoded .= $chunk . $linebreak; |
2905 | | } |
2906 | | |
2907 | | // Chomp the last linefeed |
2908 | | $encoded = substr($encoded, 0, -strlen($linebreak)); |
2909 | | return $encoded; |
2910 | | } |
2911 | | |
2912 | | /** |
2913 | | * Encode a string in quoted-printable format. |
2914 | | * According to RFC2045 section 6.7. |
2915 | | * @access public |
2916 | | * @param string $string The text to encode |
2917 | | * @param integer $line_max Number of chars allowed on a line before wrapping |
2918 | | * @return string |
2919 | | * @link http://www.php.net/manual/en/function.quoted-printable-decode.php#89417 Adapted from this comment |
2920 | | */ |
2921 | | public function encodeQP($string, $line_max = 76) |
2922 | | { |
2923 | | // Use native function if it's available (>= PHP5.3) |
2924 | | if (function_exists('quoted_printable_encode')) { |
2925 | | return quoted_printable_encode($string); |
2926 | | } |
2927 | | // Fall back to a pure PHP implementation |
2928 | | $string = str_replace( |
2929 | | array('%20', '%0D%0A.', '%0D%0A', '%'), |
2930 | | array(' ', "\r\n=2E", "\r\n", '='), |
2931 | | rawurlencode($string) |
2932 | | ); |
2933 | | return preg_replace('/[^\r\n]{' . ($line_max - 3) . '}[^=\r\n]{2}/', "$0=\r\n", $string); |
2934 | | } |
2935 | | |
2936 | | /** |
2937 | | * Backward compatibility wrapper for an old QP encoding function that was removed. |
2938 | | * @see PHPMailer::encodeQP() |
2939 | | * @access public |
2940 | | * @param string $string |
2941 | | * @param integer $line_max |
2942 | | * @param boolean $space_conv |
2943 | | * @return string |
2944 | | * @deprecated Use encodeQP instead. |
2945 | | */ |
2946 | | public function encodeQPphp( |
2947 | | $string, |
2948 | | $line_max = 76, |
2949 | | /** @noinspection PhpUnusedParameterInspection */ $space_conv = false |
2950 | | ) { |
2951 | | return $this->encodeQP($string, $line_max); |
2952 | | } |
2953 | | |
2954 | | /** |
2955 | | * Encode a string using Q encoding. |
2956 | | * @link http://tools.ietf.org/html/rfc2047 |
2957 | | * @param string $str the text to encode |
2958 | | * @param string $position Where the text is going to be used, see the RFC for what that means |
2959 | | * @access public |
2960 | | * @return string |
2961 | | */ |
2962 | | public function encodeQ($str, $position = 'text') |
2963 | | { |
2964 | | // There should not be any EOL in the string |
2965 | | $pattern = ''; |
2966 | | $encoded = str_replace(array("\r", "\n"), '', $str); |
2967 | | switch (strtolower($position)) { |
2968 | | case 'phrase': |
2969 | | // RFC 2047 section 5.3 |
2970 | | $pattern = '^A-Za-z0-9!*+\/ -'; |
2971 | | break; |
2972 | | /** @noinspection PhpMissingBreakStatementInspection */ |
2973 | | case 'comment': |
2974 | | // RFC 2047 section 5.2 |
2975 | | $pattern = '\(\)"'; |
2976 | | // intentional fall-through |
2977 | | // for this reason we build the $pattern without including delimiters and [] |
2978 | | case 'text': |
2979 | | default: |
2980 | | // RFC 2047 section 5.1 |
2981 | | // Replace every high ascii, control, =, ? and _ characters |
2982 | | $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern; |
2983 | | break; |
2984 | | } |
2985 | | $matches = array(); |
2986 | | if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) { |
2987 | | // If the string contains an '=', make sure it's the first thing we replace |
2988 | | // so as to avoid double-encoding |
2989 | | $eqkey = array_search('=', $matches[0]); |
2990 | | if (false !== $eqkey) { |
2991 | | unset($matches[0][$eqkey]); |
2992 | | array_unshift($matches[0], '='); |
2993 | | } |
2994 | | foreach (array_unique($matches[0]) as $char) { |
2995 | | $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded); |
2996 | | } |
2997 | | } |
2998 | | // Replace every spaces to _ (more readable than =20) |
2999 | | return str_replace(' ', '_', $encoded); |
3000 | | } |
3001 | | |
3002 | | /** |
3003 | | * Add a string or binary attachment (non-filesystem). |
3004 | | * This method can be used to attach ascii or binary data, |
3005 | | * such as a BLOB record from a database. |
3006 | | * @param string $string String attachment data. |
3007 | | * @param string $filename Name of the attachment. |
3008 | | * @param string $encoding File encoding (see $Encoding). |
3009 | | * @param string $type File extension (MIME) type. |
3010 | | * @param string $disposition Disposition to use |
3011 | | * @return void |
3012 | | */ |
3013 | | public function addStringAttachment( |
3014 | | $string, |
3015 | | $filename, |
3016 | | $encoding = 'base64', |
3017 | | $type = '', |
3018 | | $disposition = 'attachment' |
3019 | | ) { |
3020 | | // If a MIME type is not specified, try to work it out from the file name |
3021 | | if ($type == '') { |
3022 | | $type = self::filenameToType($filename); |
3023 | | } |
3024 | | // Append to $attachment array |
3025 | | $this->attachment[] = array( |
3026 | | 0 => $string, |
3027 | | 1 => $filename, |
3028 | | 2 => basename($filename), |
3029 | | 3 => $encoding, |
3030 | | 4 => $type, |
3031 | | 5 => true, // isStringAttachment |
3032 | | 6 => $disposition, |
3033 | | 7 => 0 |
3034 | | ); |
3035 | | } |
3036 | | |
3037 | | /** |
3038 | | * Add an embedded (inline) attachment from a file. |
3039 | | * This can include images, sounds, and just about any other document type. |
3040 | | * These differ from 'regular' attachments in that they are intended to be |
3041 | | * displayed inline with the message, not just attached for download. |
3042 | | * This is used in HTML messages that embed the images |
3043 | | * the HTML refers to using the $cid value. |
3044 | | * Never use a user-supplied path to a file! |
3045 | | * @param string $path Path to the attachment. |
3046 | | * @param string $cid Content ID of the attachment; Use this to reference |
3047 | | * the content when using an embedded image in HTML. |
3048 | | * @param string $name Overrides the attachment name. |
3049 | | * @param string $encoding File encoding (see $Encoding). |
3050 | | * @param string $type File MIME type. |
3051 | | * @param string $disposition Disposition to use |
3052 | | * @return boolean True on successfully adding an attachment |
3053 | | */ |
3054 | | public function addEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = '', $disposition = 'inline') |
3055 | | { |
3056 | | if (!self::isPermittedPath($path) or !@is_file($path)) { |
3057 | | $this->setError($this->lang('file_access') . $path); |
3058 | | return false; |
3059 | | } |
3060 | | |
3061 | | // If a MIME type is not specified, try to work it out from the file name |
3062 | | if ($type == '') { |
3063 | | $type = self::filenameToType($path); |
3064 | | } |
3065 | | |
3066 | | $filename = basename($path); |
3067 | | if ($name == '') { |
3068 | | $name = $filename; |
3069 | | } |
3070 | | |
3071 | | // Append to $attachment array |
3072 | | $this->attachment[] = array( |
3073 | | 0 => $path, |
3074 | | 1 => $filename, |
3075 | | 2 => $name, |
3076 | | 3 => $encoding, |
3077 | | 4 => $type, |
3078 | | 5 => false, // isStringAttachment |
3079 | | 6 => $disposition, |
3080 | | 7 => $cid |
3081 | | ); |
3082 | | return true; |
3083 | | } |
3084 | | |
3085 | | /** |
3086 | | * Add an embedded stringified attachment. |
3087 | | * This can include images, sounds, and just about any other document type. |
3088 | | * Be sure to set the $type to an image type for images: |
3089 | | * JPEG images use 'image/jpeg', GIF uses 'image/gif', PNG uses 'image/png'. |
3090 | | * @param string $string The attachment binary data. |
3091 | | * @param string $cid Content ID of the attachment; Use this to reference |
3092 | | * the content when using an embedded image in HTML. |
3093 | | * @param string $name |
3094 | | * @param string $encoding File encoding (see $Encoding). |
3095 | | * @param string $type MIME type. |
3096 | | * @param string $disposition Disposition to use |
3097 | | * @return boolean True on successfully adding an attachment |
3098 | | */ |
3099 | | public function addStringEmbeddedImage( |
3100 | | $string, |
3101 | | $cid, |
3102 | | $name = '', |
3103 | | $encoding = 'base64', |
3104 | | $type = '', |
3105 | | $disposition = 'inline' |
3106 | | ) { |
3107 | | // If a MIME type is not specified, try to work it out from the name |
3108 | | if ($type == '' and !empty($name)) { |
3109 | | $type = self::filenameToType($name); |
3110 | | } |
3111 | | |
3112 | | // Append to $attachment array |
3113 | | $this->attachment[] = array( |
3114 | | 0 => $string, |
3115 | | 1 => $name, |
3116 | | 2 => $name, |
3117 | | 3 => $encoding, |
3118 | | 4 => $type, |
3119 | | 5 => true, // isStringAttachment |
3120 | | 6 => $disposition, |
3121 | | 7 => $cid |
3122 | | ); |
3123 | | return true; |
3124 | | } |
3125 | | |
3126 | | /** |
3127 | | * Check if an inline attachment is present. |
3128 | | * @access public |
3129 | | * @return boolean |
3130 | | */ |
3131 | | public function inlineImageExists() |
3132 | | { |
3133 | | foreach ($this->attachment as $attachment) { |
3134 | | if ($attachment[6] == 'inline') { |
3135 | | return true; |
3136 | | } |
3137 | | } |
3138 | | return false; |
3139 | | } |
3140 | | |
3141 | | /** |
3142 | | * Check if an attachment (non-inline) is present. |
3143 | | * @return boolean |
3144 | | */ |
3145 | | public function attachmentExists() |
3146 | | { |
3147 | | foreach ($this->attachment as $attachment) { |
3148 | | if ($attachment[6] == 'attachment') { |
3149 | | return true; |
3150 | | } |
3151 | | } |
3152 | | return false; |
3153 | | } |
3154 | | |
3155 | | /** |
3156 | | * Check if this message has an alternative body set. |
3157 | | * @return boolean |
3158 | | */ |
3159 | | public function alternativeExists() |
3160 | | { |
3161 | | return !empty($this->AltBody); |
3162 | | } |
3163 | | |
3164 | | /** |
3165 | | * Clear queued addresses of given kind. |
3166 | | * @access protected |
3167 | | * @param string $kind 'to', 'cc', or 'bcc' |
3168 | | * @return void |
3169 | | */ |
3170 | | public function clearQueuedAddresses($kind) |
3171 | | { |
3172 | | $RecipientsQueue = $this->RecipientsQueue; |
3173 | | foreach ($RecipientsQueue as $address => $params) { |
3174 | | if ($params[0] == $kind) { |
3175 | | unset($this->RecipientsQueue[$address]); |
3176 | | } |
3177 | | } |
3178 | | } |
3179 | | |
3180 | | /** |
3181 | | * Clear all To recipients. |
3182 | | * @return void |
3183 | | */ |
3184 | | public function clearAddresses() |
3185 | | { |
3186 | | foreach ($this->to as $to) { |
3187 | | unset($this->all_recipients[strtolower($to[0])]); |
3188 | | } |
3189 | | $this->to = array(); |
3190 | | $this->clearQueuedAddresses('to'); |
3191 | | } |
3192 | | |
3193 | | /** |
3194 | | * Clear all CC recipients. |
3195 | | * @return void |
3196 | | */ |
3197 | | public function clearCCs() |
3198 | | { |
3199 | | foreach ($this->cc as $cc) { |
3200 | | unset($this->all_recipients[strtolower($cc[0])]); |
3201 | | } |
3202 | | $this->cc = array(); |
3203 | | $this->clearQueuedAddresses('cc'); |
3204 | | } |
3205 | | |
3206 | | /** |
3207 | | * Clear all BCC recipients. |
3208 | | * @return void |
3209 | | */ |
3210 | | public function clearBCCs() |
3211 | | { |
3212 | | foreach ($this->bcc as $bcc) { |
3213 | | unset($this->all_recipients[strtolower($bcc[0])]); |
3214 | | } |
3215 | | $this->bcc = array(); |
3216 | | $this->clearQueuedAddresses('bcc'); |
3217 | | } |
3218 | | |
3219 | | /** |
3220 | | * Clear all ReplyTo recipients. |
3221 | | * @return void |
3222 | | */ |
3223 | | public function clearReplyTos() |
3224 | | { |
3225 | | $this->ReplyTo = array(); |
3226 | | $this->ReplyToQueue = array(); |
3227 | | } |
3228 | | |
3229 | | /** |
3230 | | * Clear all recipient types. |
3231 | | * @return void |
3232 | | */ |
3233 | | public function clearAllRecipients() |
3234 | | { |
3235 | | $this->to = array(); |
3236 | | $this->cc = array(); |
3237 | | $this->bcc = array(); |
3238 | | $this->all_recipients = array(); |
3239 | | $this->RecipientsQueue = array(); |
3240 | | } |
3241 | | |
3242 | | /** |
3243 | | * Clear all filesystem, string, and binary attachments. |
3244 | | * @return void |
3245 | | */ |
3246 | | public function clearAttachments() |
3247 | | { |
3248 | | $this->attachment = array(); |
3249 | | } |
3250 | | |
3251 | | /** |
3252 | | * Clear all custom headers. |
3253 | | * @return void |
3254 | | */ |
3255 | | public function clearCustomHeaders() |
3256 | | { |
3257 | | $this->CustomHeader = array(); |
3258 | | } |
3259 | | |
3260 | | /** |
3261 | | * Add an error message to the error container. |
3262 | | * @access protected |
3263 | | * @param string $msg |
3264 | | * @return void |
3265 | | */ |
3266 | | protected function setError($msg) |
3267 | | { |
3268 | | $this->error_count++; |
3269 | | if ($this->Mailer == 'smtp' and !is_null($this->smtp)) { |
3270 | | $lasterror = $this->smtp->getError(); |
3271 | | if (!empty($lasterror['error'])) { |
3272 | | $msg .= $this->lang('smtp_error') . $lasterror['error']; |
3273 | | if (!empty($lasterror['detail'])) { |
3274 | | $msg .= ' Detail: '. $lasterror['detail']; |
3275 | | } |
3276 | | if (!empty($lasterror['smtp_code'])) { |
3277 | | $msg .= ' SMTP code: ' . $lasterror['smtp_code']; |
3278 | | } |
3279 | | if (!empty($lasterror['smtp_code_ex'])) { |
3280 | | $msg .= ' Additional SMTP info: ' . $lasterror['smtp_code_ex']; |
3281 | | } |
3282 | | } |
3283 | | } |
3284 | | $this->ErrorInfo = $msg; |
3285 | | } |
3286 | | |
3287 | | /** |
3288 | | * Return an RFC 822 formatted date. |
3289 | | * @access public |
3290 | | * @return string |
3291 | | * @static |
3292 | | */ |
3293 | | public static function rfcDate() |
3294 | | { |
3295 | | // Set the time zone to whatever the default is to avoid 500 errors |
3296 | | // Will default to UTC if it's not set properly in php.ini |
3297 | | date_default_timezone_set(@date_default_timezone_get()); |
3298 | | return date('D, j M Y H:i:s O'); |
3299 | | } |
3300 | | |
3301 | | /** |
3302 | | * Get the server hostname. |
3303 | | * Returns 'localhost.localdomain' if unknown. |
3304 | | * @access protected |
3305 | | * @return string |
3306 | | */ |
3307 | | protected function serverHostname() |
3308 | | { |
3309 | | $result = 'localhost.localdomain'; |
3310 | | if (!empty($this->Hostname)) { |
3311 | | $result = $this->Hostname; |
3312 | | } elseif (isset($_SERVER) and array_key_exists('SERVER_NAME', $_SERVER) and !empty($_SERVER['SERVER_NAME'])) { |
3313 | | $result = $_SERVER['SERVER_NAME']; |
3314 | | } elseif (function_exists('gethostname') && gethostname() !== false) { |
3315 | | $result = gethostname(); |
3316 | | } elseif (php_uname('n') !== false) { |
3317 | | $result = php_uname('n'); |
3318 | | } |
3319 | | return $result; |
3320 | | } |
3321 | | |
3322 | | /** |
3323 | | * Get an error message in the current language. |
3324 | | * @access protected |
3325 | | * @param string $key |
3326 | | * @return string |
3327 | | */ |
3328 | | protected function lang($key) |
3329 | | { |
3330 | | if (count($this->language) < 1) { |
3331 | | $this->setLanguage('en'); // set the default language |
3332 | | } |
3333 | | |
3334 | | if (array_key_exists($key, $this->language)) { |
3335 | | if ($key == 'smtp_connect_failed') { |
3336 | | //Include a link to troubleshooting docs on SMTP connection failure |
3337 | | //this is by far the biggest cause of support questions |
3338 | | //but it's usually not PHPMailer's fault. |
3339 | | return $this->language[$key] . ' https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting'; |
3340 | | } |
3341 | | return $this->language[$key]; |
3342 | | } else { |
3343 | | //Return the key as a fallback |
3344 | | return $key; |
3345 | | } |
3346 | | } |
3347 | | |
3348 | | /** |
3349 | | * Check if an error occurred. |
3350 | | * @access public |
3351 | | * @return boolean True if an error did occur. |
3352 | | */ |
3353 | | public function isError() |
3354 | | { |
3355 | | return ($this->error_count > 0); |
3356 | | } |
3357 | | |
3358 | | /** |
3359 | | * Ensure consistent line endings in a string. |
3360 | | * Changes every end of line from CRLF, CR or LF to $this->LE. |
3361 | | * @access public |
3362 | | * @param string $str String to fixEOL |
3363 | | * @return string |
3364 | | */ |
3365 | | public function fixEOL($str) |
3366 | | { |
3367 | | // Normalise to \n |
3368 | | $nstr = str_replace(array("\r\n", "\r"), "\n", $str); |
3369 | | // Now convert LE as needed |
3370 | | if ($this->LE !== "\n") { |
3371 | | $nstr = str_replace("\n", $this->LE, $nstr); |
3372 | | } |
3373 | | return $nstr; |
3374 | | } |
3375 | | |
3376 | | /** |
3377 | | * Add a custom header. |
3378 | | * $name value can be overloaded to contain |
3379 | | * both header name and value (name:value) |
3380 | | * @access public |
3381 | | * @param string $name Custom header name |
3382 | | * @param string $value Header value |
3383 | | * @return void |
3384 | | */ |
3385 | | public function addCustomHeader($name, $value = null) |
3386 | | { |
3387 | | if ($value === null) { |
3388 | | // Value passed in as name:value |
3389 | | $this->CustomHeader[] = explode(':', $name, 2); |
3390 | | } else { |
3391 | | $this->CustomHeader[] = array($name, $value); |
3392 | | } |
3393 | | } |
3394 | | |
3395 | | /** |
3396 | | * Returns all custom headers. |
3397 | | * @return array |
3398 | | */ |
3399 | | public function getCustomHeaders() |
3400 | | { |
3401 | | return $this->CustomHeader; |
3402 | | } |
3403 | | |
3404 | | /** |
3405 | | * Create a message body from an HTML string. |
3406 | | * Automatically inlines images and creates a plain-text version by converting the HTML, |
3407 | | * overwriting any existing values in Body and AltBody. |
3408 | | * Do not source $message content from user input! |
3409 | | * $basedir is prepended when handling relative URLs, e.g. <img src="/images/a.png"> and must not be empty |
3410 | | * will look for an image file in $basedir/images/a.png and convert it to inline. |
3411 | | * If you don't provide a $basedir, relative paths will be left untouched (and thus probably break in email) |
3412 | | * If you don't want to apply these transformations to your HTML, just set Body and AltBody directly. |
3413 | | * @access public |
3414 | | * @param string $message HTML message string |
3415 | | * @param string $basedir Absolute path to a base directory to prepend to relative paths to images |
3416 | | * @param boolean|callable $advanced Whether to use the internal HTML to text converter |
3417 | | * or your own custom converter @see PHPMailer::html2text() |
3418 | | * @return string $message The transformed message Body |
3419 | | */ |
3420 | | public function msgHTML($message, $basedir = '', $advanced = false) |
3421 | | { |
3422 | | preg_match_all('/(src|background)=["\'](.*)["\']/Ui', $message, $images); |
3423 | | if (array_key_exists(2, $images)) { |
3424 | | if (strlen($basedir) > 1 && substr($basedir, -1) != '/') { |
3425 | | // Ensure $basedir has a trailing / |
3426 | | $basedir .= '/'; |
3427 | | } |
3428 | | foreach ($images[2] as $imgindex => $url) { |
3429 | | // Convert data URIs into embedded images |
3430 | | if (preg_match('#^data:(image[^;,]*)(;base64)?,#', $url, $match)) { |
3431 | | $data = substr($url, strpos($url, ',')); |
3432 | | if ($match[2]) { |
3433 | | $data = base64_decode($data); |
3434 | | } else { |
3435 | | $data = rawurldecode($data); |
3436 | | } |
3437 | | $cid = md5($url) . '@phpmailer.0'; // RFC2392 S 2 |
3438 | | if ($this->addStringEmbeddedImage($data, $cid, 'embed' . $imgindex, 'base64', $match[1])) { |
3439 | | $message = str_replace( |
3440 | | $images[0][$imgindex], |
3441 | | $images[1][$imgindex] . '="cid:' . $cid . '"', |
3442 | | $message |
3443 | | ); |
3444 | | } |
3445 | | continue; |
3446 | | } |
3447 | | if ( |
3448 | | // Only process relative URLs if a basedir is provided (i.e. no absolute local paths) |
3449 | | !empty($basedir) |
3450 | | // Ignore URLs containing parent dir traversal (..) |
3451 | | && (strpos($url, '..') === false) |
3452 | | // Do not change urls that are already inline images |
3453 | | && substr($url, 0, 4) !== 'cid:' |
3454 | | // Do not change absolute URLs, including anonymous protocol |
3455 | | && !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url) |
3456 | | ) { |
3457 | | $filename = basename($url); |
3458 | | $directory = dirname($url); |
3459 | | if ($directory == '.') { |
3460 | | $directory = ''; |
3461 | | } |
3462 | | $cid = md5($url) . '@phpmailer.0'; // RFC2392 S 2 |
3463 | | if (strlen($directory) > 1 && substr($directory, -1) != '/') { |
3464 | | $directory .= '/'; |
3465 | | } |
3466 | | if ($this->addEmbeddedImage( |
3467 | | $basedir . $directory . $filename, |
3468 | | $cid, |
3469 | | $filename, |
3470 | | 'base64', |
3471 | | self::_mime_types((string)self::mb_pathinfo($filename, PATHINFO_EXTENSION)) |
3472 | | ) |
3473 | | ) { |
3474 | | $message = preg_replace( |
3475 | | '/' . $images[1][$imgindex] . '=["\']' . preg_quote($url, '/') . '["\']/Ui', |
3476 | | $images[1][$imgindex] . '="cid:' . $cid . '"', |
3477 | | $message |
3478 | | ); |
3479 | | } |
3480 | | } |
3481 | | } |
3482 | | } |
3483 | | $this->isHTML(true); |
3484 | | // Convert all message body line breaks to CRLF, makes quoted-printable encoding work much better |
3485 | | $this->Body = $this->normalizeBreaks($message); |
3486 | | $this->AltBody = $this->normalizeBreaks($this->html2text($message, $advanced)); |
3487 | | if (!$this->alternativeExists()) { |
3488 | | $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . |
3489 | | self::CRLF . self::CRLF; |
3490 | | } |
3491 | | return $this->Body; |
3492 | | } |
3493 | | |
3494 | | /** |
3495 | | * Convert an HTML string into plain text. |
3496 | | * This is used by msgHTML(). |
3497 | | * Note - older versions of this function used a bundled advanced converter |
3498 | | * which was been removed for license reasons in #232. |
3499 | | * Example usage: |
3500 | | * <code> |
3501 | | * // Use default conversion |
3502 | | * $plain = $mail->html2text($html); |
3503 | | * // Use your own custom converter |
3504 | | * $plain = $mail->html2text($html, function($html) { |
3505 | | * $converter = new MyHtml2text($html); |
3506 | | * return $converter->get_text(); |
3507 | | * }); |
3508 | | * </code> |
3509 | | * @param string $html The HTML text to convert |
3510 | | * @param boolean|callable $advanced Any boolean value to use the internal converter, |
3511 | | * or provide your own callable for custom conversion. |
3512 | | * @return string |
3513 | | */ |
3514 | | public function html2text($html, $advanced = false) |
3515 | | { |
3516 | | if (is_callable($advanced)) { |
3517 | | return call_user_func($advanced, $html); |
3518 | | } |
3519 | | return html_entity_decode( |
3520 | | trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/si', '', $html))), |
3521 | | ENT_QUOTES, |
3522 | | $this->CharSet |
3523 | | ); |
3524 | | } |
3525 | | |
3526 | | /** |
3527 | | * Get the MIME type for a file extension. |
3528 | | * @param string $ext File extension |
3529 | | * @access public |
3530 | | * @return string MIME type of file. |
3531 | | * @static |
3532 | | */ |
3533 | | public static function _mime_types($ext = '') |
3534 | | { |
3535 | | $mimes = array( |
3536 | | 'xl' => 'application/excel', |
3537 | | 'js' => 'application/javascript', |
3538 | | 'hqx' => 'application/mac-binhex40', |
3539 | | 'cpt' => 'application/mac-compactpro', |
3540 | | 'bin' => 'application/macbinary', |
3541 | | 'doc' => 'application/msword', |
3542 | | 'word' => 'application/msword', |
3543 | | 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
3544 | | 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', |
3545 | | 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', |
3546 | | 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
3547 | | 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
3548 | | 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
3549 | | 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
3550 | | 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', |
3551 | | 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', |
3552 | | 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', |
3553 | | 'class' => 'application/octet-stream', |
3554 | | 'dll' => 'application/octet-stream', |
3555 | | 'dms' => 'application/octet-stream', |
3556 | | 'exe' => 'application/octet-stream', |
3557 | | 'lha' => 'application/octet-stream', |
3558 | | 'lzh' => 'application/octet-stream', |
3559 | | 'psd' => 'application/octet-stream', |
3560 | | 'sea' => 'application/octet-stream', |
3561 | | 'so' => 'application/octet-stream', |
3562 | | 'oda' => 'application/oda', |
3563 | | 'pdf' => 'application/pdf', |
3564 | | 'ai' => 'application/postscript', |
3565 | | 'eps' => 'application/postscript', |
3566 | | 'ps' => 'application/postscript', |
3567 | | 'smi' => 'application/smil', |
3568 | | 'smil' => 'application/smil', |
3569 | | 'mif' => 'application/vnd.mif', |
3570 | | 'xls' => 'application/vnd.ms-excel', |
3571 | | 'ppt' => 'application/vnd.ms-powerpoint', |
3572 | | 'wbxml' => 'application/vnd.wap.wbxml', |
3573 | | 'wmlc' => 'application/vnd.wap.wmlc', |
3574 | | 'dcr' => 'application/x-director', |
3575 | | 'dir' => 'application/x-director', |
3576 | | 'dxr' => 'application/x-director', |
3577 | | 'dvi' => 'application/x-dvi', |
3578 | | 'gtar' => 'application/x-gtar', |
3579 | | 'php3' => 'application/x-httpd-php', |
3580 | | 'php4' => 'application/x-httpd-php', |
3581 | | 'php' => 'application/x-httpd-php', |
3582 | | 'phtml' => 'application/x-httpd-php', |
3583 | | 'phps' => 'application/x-httpd-php-source', |
3584 | | 'swf' => 'application/x-shockwave-flash', |
3585 | | 'sit' => 'application/x-stuffit', |
3586 | | 'tar' => 'application/x-tar', |
3587 | | 'tgz' => 'application/x-tar', |
3588 | | 'xht' => 'application/xhtml+xml', |
3589 | | 'xhtml' => 'application/xhtml+xml', |
3590 | | 'zip' => 'application/zip', |
3591 | | 'mid' => 'audio/midi', |
3592 | | 'midi' => 'audio/midi', |
3593 | | 'mp2' => 'audio/mpeg', |
3594 | | 'mp3' => 'audio/mpeg', |
3595 | | 'mpga' => 'audio/mpeg', |
3596 | | 'aif' => 'audio/x-aiff', |
3597 | | 'aifc' => 'audio/x-aiff', |
3598 | | 'aiff' => 'audio/x-aiff', |
3599 | | 'ram' => 'audio/x-pn-realaudio', |
3600 | | 'rm' => 'audio/x-pn-realaudio', |
3601 | | 'rpm' => 'audio/x-pn-realaudio-plugin', |
3602 | | 'ra' => 'audio/x-realaudio', |
3603 | | 'wav' => 'audio/x-wav', |
3604 | | 'bmp' => 'image/bmp', |
3605 | | 'gif' => 'image/gif', |
3606 | | 'jpeg' => 'image/jpeg', |
3607 | | 'jpe' => 'image/jpeg', |
3608 | | 'jpg' => 'image/jpeg', |
3609 | | 'png' => 'image/png', |
3610 | | 'tiff' => 'image/tiff', |
3611 | | 'tif' => 'image/tiff', |
3612 | | 'eml' => 'message/rfc822', |
3613 | | 'css' => 'text/css', |
3614 | | 'html' => 'text/html', |
3615 | | 'htm' => 'text/html', |
3616 | | 'shtml' => 'text/html', |
3617 | | 'log' => 'text/plain', |
3618 | | 'text' => 'text/plain', |
3619 | | 'txt' => 'text/plain', |
3620 | | 'rtx' => 'text/richtext', |
3621 | | 'rtf' => 'text/rtf', |
3622 | | 'vcf' => 'text/vcard', |
3623 | | 'vcard' => 'text/vcard', |
3624 | | 'xml' => 'text/xml', |
3625 | | 'xsl' => 'text/xml', |
3626 | | 'mpeg' => 'video/mpeg', |
3627 | | 'mpe' => 'video/mpeg', |
3628 | | 'mpg' => 'video/mpeg', |
3629 | | 'mov' => 'video/quicktime', |
3630 | | 'qt' => 'video/quicktime', |
3631 | | 'rv' => 'video/vnd.rn-realvideo', |
3632 | | 'avi' => 'video/x-msvideo', |
3633 | | 'movie' => 'video/x-sgi-movie' |
3634 | | ); |
3635 | | if (array_key_exists(strtolower($ext), $mimes)) { |
3636 | | return $mimes[strtolower($ext)]; |
3637 | | } |
3638 | | return 'application/octet-stream'; |
3639 | | } |
3640 | | |
3641 | | /** |
3642 | | * Map a file name to a MIME type. |
3643 | | * Defaults to 'application/octet-stream', i.e.. arbitrary binary data. |
3644 | | * @param string $filename A file name or full path, does not need to exist as a file |
3645 | | * @return string |
3646 | | * @static |
3647 | | */ |
3648 | | public static function filenameToType($filename) |
3649 | | { |
3650 | | // In case the path is a URL, strip any query string before getting extension |
3651 | | $qpos = strpos($filename, '?'); |
3652 | | if (false !== $qpos) { |
3653 | | $filename = substr($filename, 0, $qpos); |
3654 | | } |
3655 | | $pathinfo = self::mb_pathinfo($filename); |
3656 | | return self::_mime_types($pathinfo['extension']); |
3657 | | } |
3658 | | |
3659 | | /** |
3660 | | * Multi-byte-safe pathinfo replacement. |
3661 | | * Drop-in replacement for pathinfo(), but multibyte-safe, cross-platform-safe, old-version-safe. |
3662 | | * Works similarly to the one in PHP >= 5.2.0 |
3663 | | * @link http://www.php.net/manual/en/function.pathinfo.php#107461 |
3664 | | * @param string $path A filename or path, does not need to exist as a file |
3665 | | * @param integer|string $options Either a PATHINFO_* constant, |
3666 | | * or a string name to return only the specified piece, allows 'filename' to work on PHP < 5.2 |
3667 | | * @return string|array |
3668 | | * @static |
3669 | | */ |
3670 | | public static function mb_pathinfo($path, $options = null) |
3671 | | { |
3672 | | $ret = array('dirname' => '', 'basename' => '', 'extension' => '', 'filename' => ''); |
3673 | | $pathinfo = array(); |
3674 | | if (preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $pathinfo)) { |
3675 | | if (array_key_exists(1, $pathinfo)) { |
3676 | | $ret['dirname'] = $pathinfo[1]; |
3677 | | } |
3678 | | if (array_key_exists(2, $pathinfo)) { |
3679 | | $ret['basename'] = $pathinfo[2]; |
3680 | | } |
3681 | | if (array_key_exists(5, $pathinfo)) { |
3682 | | $ret['extension'] = $pathinfo[5]; |
3683 | | } |
3684 | | if (array_key_exists(3, $pathinfo)) { |
3685 | | $ret['filename'] = $pathinfo[3]; |
3686 | | } |
3687 | | } |
3688 | | switch ($options) { |
3689 | | case PATHINFO_DIRNAME: |
3690 | | case 'dirname': |
3691 | | return $ret['dirname']; |
3692 | | case PATHINFO_BASENAME: |
3693 | | case 'basename': |
3694 | | return $ret['basename']; |
3695 | | case PATHINFO_EXTENSION: |
3696 | | case 'extension': |
3697 | | return $ret['extension']; |
3698 | | case PATHINFO_FILENAME: |
3699 | | case 'filename': |
3700 | | return $ret['filename']; |
3701 | | default: |
3702 | | return $ret; |
3703 | | } |
3704 | | } |
3705 | | |
3706 | | /** |
3707 | | * Set or reset instance properties. |
3708 | | * You should avoid this function - it's more verbose, less efficient, more error-prone and |
3709 | | * harder to debug than setting properties directly. |
3710 | | * Usage Example: |
3711 | | * `$mail->set('SMTPSecure', 'tls');` |
3712 | | * is the same as: |
3713 | | * `$mail->SMTPSecure = 'tls';` |
3714 | | * @access public |
3715 | | * @param string $name The property name to set |
3716 | | * @param mixed $value The value to set the property to |
3717 | | * @return boolean |
3718 | | * @TODO Should this not be using the __set() magic function? |
3719 | | */ |
3720 | | public function set($name, $value = '') |
3721 | | { |
3722 | | if (property_exists($this, $name)) { |
3723 | | $this->$name = $value; |
3724 | | return true; |
3725 | | } else { |
3726 | | $this->setError($this->lang('variable_set') . $name); |
3727 | | return false; |
3728 | | } |
3729 | | } |
3730 | | |
3731 | | /** |
3732 | | * Strip newlines to prevent header injection. |
3733 | | * @access public |
3734 | | * @param string $str |
3735 | | * @return string |
3736 | | */ |
3737 | | public function secureHeader($str) |
3738 | | { |
3739 | | return trim(str_replace(array("\r", "\n"), '', $str)); |
3740 | | } |
3741 | | |
3742 | | /** |
3743 | | * Normalize line breaks in a string. |
3744 | | * Converts UNIX LF, Mac CR and Windows CRLF line breaks into a single line break format. |
3745 | | * Defaults to CRLF (for message bodies) and preserves consecutive breaks. |
3746 | | * @param string $text |
3747 | | * @param string $breaktype What kind of line break to use, defaults to CRLF |
3748 | | * @return string |
3749 | | * @access public |
3750 | | * @static |
3751 | | */ |
3752 | | public static function normalizeBreaks($text, $breaktype = "\r\n") |
3753 | | { |
3754 | | return preg_replace('/(\r\n|\r|\n)/ms', $breaktype, $text); |
3755 | | } |
3756 | | |
3757 | | /** |
3758 | | * Set the public and private key files and password for S/MIME signing. |
3759 | | * @access public |
3760 | | * @param string $cert_filename |
3761 | | * @param string $key_filename |
3762 | | * @param string $key_pass Password for private key |
3763 | | * @param string $extracerts_filename Optional path to chain certificate |
3764 | | */ |
3765 | | public function sign($cert_filename, $key_filename, $key_pass, $extracerts_filename = '') |
3766 | | { |
3767 | | $this->sign_cert_file = $cert_filename; |
3768 | | $this->sign_key_file = $key_filename; |
3769 | | $this->sign_key_pass = $key_pass; |
3770 | | $this->sign_extracerts_file = $extracerts_filename; |
3771 | | } |
3772 | | |
3773 | | /** |
3774 | | * Quoted-Printable-encode a DKIM header. |
3775 | | * @access public |
3776 | | * @param string $txt |
3777 | | * @return string |
3778 | | */ |
3779 | | public function DKIM_QP($txt) |
3780 | | { |
3781 | | $line = ''; |
3782 | | for ($i = 0; $i < strlen($txt); $i++) { |
3783 | | $ord = ord($txt[$i]); |
3784 | | if (((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E))) { |
3785 | | $line .= $txt[$i]; |
3786 | | } else { |
3787 | | $line .= '=' . sprintf('%02X', $ord); |
3788 | | } |
3789 | | } |
3790 | | return $line; |
3791 | | } |
3792 | | |
3793 | | /** |
3794 | | * Generate a DKIM signature. |
3795 | | * @access public |
3796 | | * @param string $signHeader |
3797 | | * @throws phpmailerException |
3798 | | * @return string The DKIM signature value |
3799 | | */ |
3800 | | public function DKIM_Sign($signHeader) |
3801 | | { |
3802 | | if (!defined('PKCS7_TEXT')) { |
3803 | | if ($this->exceptions) { |
3804 | | throw new phpmailerException($this->lang('extension_missing') . 'openssl'); |
3805 | | } |
3806 | | return ''; |
3807 | | } |
3808 | | $privKeyStr = !empty($this->DKIM_private_string) ? $this->DKIM_private_string : file_get_contents($this->DKIM_private); |
3809 | | if ('' != $this->DKIM_passphrase) { |
3810 | | $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase); |
3811 | | } else { |
3812 | | $privKey = openssl_pkey_get_private($privKeyStr); |
3813 | | } |
3814 | | //Workaround for missing digest algorithms in old PHP & OpenSSL versions |
3815 | | //@link http://stackoverflow.com/a/11117338/333340 |
3816 | | if (version_compare(PHP_VERSION, '5.3.0') >= 0 and |
3817 | | in_array('sha256WithRSAEncryption', openssl_get_md_methods(true))) { |
3818 | | if (openssl_sign($signHeader, $signature, $privKey, 'sha256WithRSAEncryption')) { |
3819 | | openssl_pkey_free($privKey); |
3820 | | return base64_encode($signature); |
3821 | | } |
3822 | | } else { |
3823 | | $pinfo = openssl_pkey_get_details($privKey); |
3824 | | $hash = hash('sha256', $signHeader); |
3825 | | //'Magic' constant for SHA256 from RFC3447 |
3826 | | //@link https://tools.ietf.org/html/rfc3447#page-43 |
3827 | | $t = '3031300d060960864801650304020105000420' . $hash; |
3828 | | $pslen = $pinfo['bits'] / 8 - (strlen($t) / 2 + 3); |
3829 | | $eb = pack('H*', '0001' . str_repeat('FF', $pslen) . '00' . $t); |
3830 | | |
3831 | | if (openssl_private_encrypt($eb, $signature, $privKey, OPENSSL_NO_PADDING)) { |
3832 | | openssl_pkey_free($privKey); |
3833 | | return base64_encode($signature); |
3834 | | } |
3835 | | } |
3836 | | openssl_pkey_free($privKey); |
3837 | | return ''; |
3838 | | } |
3839 | | |
3840 | | /** |
3841 | | * Generate a DKIM canonicalization header. |
3842 | | * @access public |
3843 | | * @param string $signHeader Header |
3844 | | * @return string |
3845 | | */ |
3846 | | public function DKIM_HeaderC($signHeader) |
3847 | | { |
3848 | | $signHeader = preg_replace('/\r\n\s+/', ' ', $signHeader); |
3849 | | $lines = explode("\r\n", $signHeader); |
3850 | | foreach ($lines as $key => $line) { |
3851 | | list($heading, $value) = explode(':', $line, 2); |
3852 | | $heading = strtolower($heading); |
3853 | | $value = preg_replace('/\s{2,}/', ' ', $value); // Compress useless spaces |
3854 | | $lines[$key] = $heading . ':' . trim($value); // Don't forget to remove WSP around the value |
3855 | | } |
3856 | | $signHeader = implode("\r\n", $lines); |
3857 | | return $signHeader; |
3858 | | } |
3859 | | |
3860 | | /** |
3861 | | * Generate a DKIM canonicalization body. |
3862 | | * @access public |
3863 | | * @param string $body Message Body |
3864 | | * @return string |
3865 | | */ |
3866 | | public function DKIM_BodyC($body) |
3867 | | { |
3868 | | if ($body == '') { |
3869 | | return "\r\n"; |
3870 | | } |
3871 | | // stabilize line endings |
3872 | | $body = str_replace("\r\n", "\n", $body); |
3873 | | $body = str_replace("\n", "\r\n", $body); |
3874 | | // END stabilize line endings |
3875 | | while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") { |
3876 | | $body = substr($body, 0, strlen($body) - 2); |
3877 | | } |
3878 | | return $body; |
3879 | | } |
3880 | | |
3881 | | /** |
3882 | | * Create the DKIM header and body in a new message header. |
3883 | | * @access public |
3884 | | * @param string $headers_line Header lines |
3885 | | * @param string $subject Subject |
3886 | | * @param string $body Body |
3887 | | * @return string |
3888 | | */ |
3889 | | public function DKIM_Add($headers_line, $subject, $body) |
3890 | | { |
3891 | | $DKIMsignatureType = 'rsa-sha256'; // Signature & hash algorithms |
3892 | | $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body |
3893 | | $DKIMquery = 'dns/txt'; // Query method |
3894 | | $DKIMtime = time(); // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone) |
3895 | | $subject_header = "Subject: $subject"; |
3896 | | $headers = explode($this->LE, $headers_line); |
3897 | | $from_header = ''; |
3898 | | $to_header = ''; |
3899 | | $date_header = ''; |
3900 | | $current = ''; |
3901 | | foreach ($headers as $header) { |
3902 | | if (strpos($header, 'From:') === 0) { |
3903 | | $from_header = $header; |
3904 | | $current = 'from_header'; |
3905 | | } elseif (strpos($header, 'To:') === 0) { |
3906 | | $to_header = $header; |
3907 | | $current = 'to_header'; |
3908 | | } elseif (strpos($header, 'Date:') === 0) { |
3909 | | $date_header = $header; |
3910 | | $current = 'date_header'; |
3911 | | } else { |
3912 | | if (!empty($$current) && strpos($header, ' =?') === 0) { |
3913 | | $$current .= $header; |
3914 | | } else { |
3915 | | $current = ''; |
3916 | | } |
3917 | | } |
3918 | | } |
3919 | | $from = str_replace('|', '=7C', $this->DKIM_QP($from_header)); |
3920 | | $to = str_replace('|', '=7C', $this->DKIM_QP($to_header)); |
3921 | | $date = str_replace('|', '=7C', $this->DKIM_QP($date_header)); |
3922 | | $subject = str_replace( |
3923 | | '|', |
3924 | | '=7C', |
3925 | | $this->DKIM_QP($subject_header) |
3926 | | ); // Copied header fields (dkim-quoted-printable) |
3927 | | $body = $this->DKIM_BodyC($body); |
3928 | | $DKIMlen = strlen($body); // Length of body |
3929 | | $DKIMb64 = base64_encode(pack('H*', hash('sha256', $body))); // Base64 of packed binary SHA-256 hash of body |
3930 | | if ('' == $this->DKIM_identity) { |
3931 | | $ident = ''; |
3932 | | } else { |
3933 | | $ident = ' i=' . $this->DKIM_identity . ';'; |
3934 | | } |
3935 | | $dkimhdrs = 'DKIM-Signature: v=1; a=' . |
3936 | | $DKIMsignatureType . '; q=' . |
3937 | | $DKIMquery . '; l=' . |
3938 | | $DKIMlen . '; s=' . |
3939 | | $this->DKIM_selector . |
3940 | | ";\r\n" . |
3941 | | "\tt=" . $DKIMtime . '; c=' . $DKIMcanonicalization . ";\r\n" . |
3942 | | "\th=From:To:Date:Subject;\r\n" . |
3943 | | "\td=" . $this->DKIM_domain . ';' . $ident . "\r\n" . |
3944 | | "\tz=$from\r\n" . |
3945 | | "\t|$to\r\n" . |
3946 | | "\t|$date\r\n" . |
3947 | | "\t|$subject;\r\n" . |
3948 | | "\tbh=" . $DKIMb64 . ";\r\n" . |
3949 | | "\tb="; |
3950 | | $toSign = $this->DKIM_HeaderC( |
3951 | | $from_header . "\r\n" . |
3952 | | $to_header . "\r\n" . |
3953 | | $date_header . "\r\n" . |
3954 | | $subject_header . "\r\n" . |
3955 | | $dkimhdrs |
3956 | | ); |
3957 | | $signed = $this->DKIM_Sign($toSign); |
3958 | | return $dkimhdrs . $signed . "\r\n"; |
3959 | | } |
3960 | | |
3961 | | /** |
3962 | | * Detect if a string contains a line longer than the maximum line length allowed. |
3963 | | * @param string $str |
3964 | | * @return boolean |
3965 | | * @static |
3966 | | */ |
3967 | | public static function hasLineLongerThanMax($str) |
3968 | | { |
3969 | | //+2 to include CRLF line break for a 1000 total |
3970 | | return (boolean)preg_match('/^(.{'.(self::MAX_LINE_LENGTH + 2).',})/m', $str); |
3971 | | } |
3972 | | |
3973 | | /** |
3974 | | * Allows for public read access to 'to' property. |
3975 | | * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
3976 | | * @access public |
3977 | | * @return array |
3978 | | */ |
3979 | | public function getToAddresses() |
3980 | | { |
3981 | | return $this->to; |
3982 | | } |
3983 | | |
3984 | | /** |
3985 | | * Allows for public read access to 'cc' property. |
3986 | | * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
3987 | | * @access public |
3988 | | * @return array |
3989 | | */ |
3990 | | public function getCcAddresses() |
3991 | | { |
3992 | | return $this->cc; |
3993 | | } |
3994 | | |
3995 | | /** |
3996 | | * Allows for public read access to 'bcc' property. |
3997 | | * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
3998 | | * @access public |
3999 | | * @return array |
4000 | | */ |
4001 | | public function getBccAddresses() |
4002 | | { |
4003 | | return $this->bcc; |
4004 | | } |
4005 | | |
4006 | | /** |
4007 | | * Allows for public read access to 'ReplyTo' property. |
4008 | | * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
4009 | | * @access public |
4010 | | * @return array |
4011 | | */ |
4012 | | public function getReplyToAddresses() |
4013 | | { |
4014 | | return $this->ReplyTo; |
4015 | | } |
4016 | | |
4017 | | /** |
4018 | | * Allows for public read access to 'all_recipients' property. |
4019 | | * @note: Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
4020 | | * @access public |
4021 | | * @return array |
4022 | | */ |
4023 | | public function getAllRecipientAddresses() |
4024 | | { |
4025 | | return $this->all_recipients; |
4026 | | } |
4027 | | |
4028 | | /** |
4029 | | * Perform a callback. |
4030 | | * @param boolean $isSent |
4031 | | * @param array $to |
4032 | | * @param array $cc |
4033 | | * @param array $bcc |
4034 | | * @param string $subject |
4035 | | * @param string $body |
4036 | | * @param string $from |
4037 | | */ |
4038 | | protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from) |
4039 | | { |
4040 | | if (!empty($this->action_function) && is_callable($this->action_function)) { |
4041 | | $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from); |
4042 | | call_user_func_array($this->action_function, $params); |
4043 | | } |
4044 | | } |
4045 | | } |
4046 | | |
4047 | | /** |
4048 | | * PHPMailer exception handler |
4049 | | * @package PHPMailer |
4050 | | */ |
4051 | | class phpmailerException extends Exception |
4052 | | { |
4053 | | /** |
4054 | | * Prettify error message output |
4055 | | * @return string |
4056 | | */ |
4057 | | public function errorMessage() |
4058 | | { |
4059 | | $errorMsg = '<strong>' . htmlspecialchars($this->getMessage()) . "</strong><br />\n"; |
4060 | | return $errorMsg; |
4061 | | } |
| 1342 | return (bool) preg_match( |
| 1343 | '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' . |
| 1344 | '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD', |
| 1345 | $address |
| 1346 | ); |
| 1347 | case 'php': |
| 1348 | default: |
| 1349 | return filter_var($address, FILTER_VALIDATE_EMAIL) !== false; |
| 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * Tells whether IDNs (Internationalized Domain Names) are supported or not. This requires the |
| 1355 | * `intl` and `mbstring` PHP extensions. |
| 1356 | * |
| 1357 | * @return bool `true` if required functions for IDN support are present |
| 1358 | */ |
| 1359 | public static function idnSupported() |
| 1360 | { |
| 1361 | return function_exists('idn_to_ascii') && function_exists('mb_convert_encoding'); |
| 1362 | } |
| 1363 | |
| 1364 | /** |
| 1365 | * Converts IDN in given email address to its ASCII form, also known as punycode, if possible. |
| 1366 | * Important: Address must be passed in same encoding as currently set in PHPMailer::$CharSet. |
| 1367 | * This function silently returns unmodified address if: |
| 1368 | * - No conversion is necessary (i.e. domain name is not an IDN, or is already in ASCII form) |
| 1369 | * - Conversion to punycode is impossible (e.g. required PHP functions are not available) |
| 1370 | * or fails for any reason (e.g. domain contains characters not allowed in an IDN). |
| 1371 | * |
| 1372 | * @see PHPMailer::$CharSet |
| 1373 | * |
| 1374 | * @param string $address The email address to convert |
| 1375 | * |
| 1376 | * @return string The encoded address in ASCII form |
| 1377 | */ |
| 1378 | public function punyencodeAddress($address) |
| 1379 | { |
| 1380 | // Verify we have required functions, CharSet, and at-sign. |
| 1381 | $pos = strrpos($address, '@'); |
| 1382 | if (!empty($this->CharSet) && |
| 1383 | false !== $pos && |
| 1384 | static::idnSupported() |
| 1385 | ) { |
| 1386 | $domain = substr($address, ++$pos); |
| 1387 | // Verify CharSet string is a valid one, and domain properly encoded in this CharSet. |
| 1388 | if ($this->has8bitChars($domain) && @mb_check_encoding($domain, $this->CharSet)) { |
| 1389 | $domain = mb_convert_encoding($domain, 'UTF-8', $this->CharSet); |
| 1390 | //Ignore IDE complaints about this line - method signature changed in PHP 5.4 |
| 1391 | $errorcode = 0; |
| 1392 | if (defined('INTL_IDNA_VARIANT_UTS46')) { |
| 1393 | $punycode = idn_to_ascii($domain, $errorcode, INTL_IDNA_VARIANT_UTS46); |
| 1394 | } elseif (defined('INTL_IDNA_VARIANT_2003')) { |
| 1395 | $punycode = idn_to_ascii($domain, $errorcode, INTL_IDNA_VARIANT_2003); |
| 1396 | } else { |
| 1397 | $punycode = idn_to_ascii($domain, $errorcode); |
| 1398 | } |
| 1399 | if (false !== $punycode) { |
| 1400 | return substr($address, 0, $pos) . $punycode; |
| 1401 | } |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | return $address; |
| 1406 | } |
| 1407 | |
| 1408 | /** |
| 1409 | * Create a message and send it. |
| 1410 | * Uses the sending method specified by $Mailer. |
| 1411 | * |
| 1412 | * @throws Exception |
| 1413 | * |
| 1414 | * @return bool false on error - See the ErrorInfo property for details of the error |
| 1415 | */ |
| 1416 | public function send() |
| 1417 | { |
| 1418 | try { |
| 1419 | if (!$this->preSend()) { |
| 1420 | return false; |
| 1421 | } |
| 1422 | |
| 1423 | return $this->postSend(); |
| 1424 | } catch (Exception $exc) { |
| 1425 | $this->mailHeader = ''; |
| 1426 | $this->setError($exc->getMessage()); |
| 1427 | if ($this->exceptions) { |
| 1428 | throw $exc; |
| 1429 | } |
| 1430 | |
| 1431 | return false; |
| 1432 | } |
| 1433 | } |
| 1434 | |
| 1435 | /** |
| 1436 | * Prepare a message for sending. |
| 1437 | * |
| 1438 | * @throws Exception |
| 1439 | * |
| 1440 | * @return bool |
| 1441 | */ |
| 1442 | public function preSend() |
| 1443 | { |
| 1444 | if ('smtp' === $this->Mailer |
| 1445 | || ('mail' === $this->Mailer && stripos(PHP_OS, 'WIN') === 0) |
| 1446 | ) { |
| 1447 | //SMTP mandates RFC-compliant line endings |
| 1448 | //and it's also used with mail() on Windows |
| 1449 | static::setLE("\r\n"); |
| 1450 | } else { |
| 1451 | //Maintain backward compatibility with legacy Linux command line mailers |
| 1452 | static::setLE(PHP_EOL); |
| 1453 | } |
| 1454 | //Check for buggy PHP versions that add a header with an incorrect line break |
| 1455 | if ('mail' === $this->Mailer |
| 1456 | && ((PHP_VERSION_ID >= 70000 && PHP_VERSION_ID < 70017) |
| 1457 | || (PHP_VERSION_ID >= 70100 && PHP_VERSION_ID < 70103)) |
| 1458 | && ini_get('mail.add_x_header') === '1' |
| 1459 | && stripos(PHP_OS, 'WIN') === 0 |
| 1460 | ) { |
| 1461 | trigger_error( |
| 1462 | 'Your version of PHP is affected by a bug that may result in corrupted messages.' . |
| 1463 | ' To fix it, switch to sending using SMTP, disable the mail.add_x_header option in' . |
| 1464 | ' your php.ini, switch to MacOS or Linux, or upgrade your PHP to version 7.0.17+ or 7.1.3+.', |
| 1465 | E_USER_WARNING |
| 1466 | ); |
| 1467 | } |
| 1468 | |
| 1469 | try { |
| 1470 | $this->error_count = 0; // Reset errors |
| 1471 | $this->mailHeader = ''; |
| 1472 | |
| 1473 | // Dequeue recipient and Reply-To addresses with IDN |
| 1474 | foreach (array_merge($this->RecipientsQueue, $this->ReplyToQueue) as $params) { |
| 1475 | $params[1] = $this->punyencodeAddress($params[1]); |
| 1476 | call_user_func_array([$this, 'addAnAddress'], $params); |
| 1477 | } |
| 1478 | if (count($this->to) + count($this->cc) + count($this->bcc) < 1) { |
| 1479 | throw new Exception($this->lang('provide_address'), self::STOP_CRITICAL); |
| 1480 | } |
| 1481 | |
| 1482 | // Validate From, Sender, and ConfirmReadingTo addresses |
| 1483 | foreach (['From', 'Sender', 'ConfirmReadingTo'] as $address_kind) { |
| 1484 | $this->$address_kind = trim($this->$address_kind); |
| 1485 | if (empty($this->$address_kind)) { |
| 1486 | continue; |
| 1487 | } |
| 1488 | $this->$address_kind = $this->punyencodeAddress($this->$address_kind); |
| 1489 | if (!static::validateAddress($this->$address_kind)) { |
| 1490 | $error_message = sprintf( |
| 1491 | '%s (%s): %s', |
| 1492 | $this->lang('invalid_address'), |
| 1493 | $address_kind, |
| 1494 | $this->$address_kind |
| 1495 | ); |
| 1496 | $this->setError($error_message); |
| 1497 | $this->edebug($error_message); |
| 1498 | if ($this->exceptions) { |
| 1499 | throw new Exception($error_message); |
| 1500 | } |
| 1501 | |
| 1502 | return false; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | // Set whether the message is multipart/alternative |
| 1507 | if ($this->alternativeExists()) { |
| 1508 | $this->ContentType = static::CONTENT_TYPE_MULTIPART_ALTERNATIVE; |
| 1509 | } |
| 1510 | |
| 1511 | $this->setMessageType(); |
| 1512 | // Refuse to send an empty message unless we are specifically allowing it |
| 1513 | if (!$this->AllowEmpty && empty($this->Body)) { |
| 1514 | throw new Exception($this->lang('empty_message'), self::STOP_CRITICAL); |
| 1515 | } |
| 1516 | |
| 1517 | //Trim subject consistently |
| 1518 | $this->Subject = trim($this->Subject); |
| 1519 | // Create body before headers in case body makes changes to headers (e.g. altering transfer encoding) |
| 1520 | $this->MIMEHeader = ''; |
| 1521 | $this->MIMEBody = $this->createBody(); |
| 1522 | // createBody may have added some headers, so retain them |
| 1523 | $tempheaders = $this->MIMEHeader; |
| 1524 | $this->MIMEHeader = $this->createHeader(); |
| 1525 | $this->MIMEHeader .= $tempheaders; |
| 1526 | |
| 1527 | // To capture the complete message when using mail(), create |
| 1528 | // an extra header list which createHeader() doesn't fold in |
| 1529 | if ('mail' === $this->Mailer) { |
| 1530 | if (count($this->to) > 0) { |
| 1531 | $this->mailHeader .= $this->addrAppend('To', $this->to); |
| 1532 | } else { |
| 1533 | $this->mailHeader .= $this->headerLine('To', 'undisclosed-recipients:;'); |
| 1534 | } |
| 1535 | $this->mailHeader .= $this->headerLine( |
| 1536 | 'Subject', |
| 1537 | $this->encodeHeader($this->secureHeader($this->Subject)) |
| 1538 | ); |
| 1539 | } |
| 1540 | |
| 1541 | // Sign with DKIM if enabled |
| 1542 | if (!empty($this->DKIM_domain) |
| 1543 | && !empty($this->DKIM_selector) |
| 1544 | && (!empty($this->DKIM_private_string) |
| 1545 | || (!empty($this->DKIM_private) |
| 1546 | && static::isPermittedPath($this->DKIM_private) |
| 1547 | && file_exists($this->DKIM_private) |
| 1548 | ) |
| 1549 | ) |
| 1550 | ) { |
| 1551 | $header_dkim = $this->DKIM_Add( |
| 1552 | $this->MIMEHeader . $this->mailHeader, |
| 1553 | $this->encodeHeader($this->secureHeader($this->Subject)), |
| 1554 | $this->MIMEBody |
| 1555 | ); |
| 1556 | $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . static::$LE . |
| 1557 | static::normalizeBreaks($header_dkim) . static::$LE; |
| 1558 | } |
| 1559 | |
| 1560 | return true; |
| 1561 | } catch (Exception $exc) { |
| 1562 | $this->setError($exc->getMessage()); |
| 1563 | if ($this->exceptions) { |
| 1564 | throw $exc; |
| 1565 | } |
| 1566 | |
| 1567 | return false; |
| 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | /** |
| 1572 | * Actually send a message via the selected mechanism. |
| 1573 | * |
| 1574 | * @throws Exception |
| 1575 | * |
| 1576 | * @return bool |
| 1577 | */ |
| 1578 | public function postSend() |
| 1579 | { |
| 1580 | try { |
| 1581 | // Choose the mailer and send through it |
| 1582 | switch ($this->Mailer) { |
| 1583 | case 'sendmail': |
| 1584 | case 'qmail': |
| 1585 | return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody); |
| 1586 | case 'smtp': |
| 1587 | return $this->smtpSend($this->MIMEHeader, $this->MIMEBody); |
| 1588 | case 'mail': |
| 1589 | return $this->mailSend($this->MIMEHeader, $this->MIMEBody); |
| 1590 | default: |
| 1591 | $sendMethod = $this->Mailer . 'Send'; |
| 1592 | if (method_exists($this, $sendMethod)) { |
| 1593 | return $this->$sendMethod($this->MIMEHeader, $this->MIMEBody); |
| 1594 | } |
| 1595 | |
| 1596 | return $this->mailSend($this->MIMEHeader, $this->MIMEBody); |
| 1597 | } |
| 1598 | } catch (Exception $exc) { |
| 1599 | $this->setError($exc->getMessage()); |
| 1600 | $this->edebug($exc->getMessage()); |
| 1601 | if ($this->exceptions) { |
| 1602 | throw $exc; |
| 1603 | } |
| 1604 | } |
| 1605 | |
| 1606 | return false; |
| 1607 | } |
| 1608 | |
| 1609 | /** |
| 1610 | * Send mail using the $Sendmail program. |
| 1611 | * |
| 1612 | * @see PHPMailer::$Sendmail |
| 1613 | * |
| 1614 | * @param string $header The message headers |
| 1615 | * @param string $body The message body |
| 1616 | * |
| 1617 | * @throws Exception |
| 1618 | * |
| 1619 | * @return bool |
| 1620 | */ |
| 1621 | protected function sendmailSend($header, $body) |
| 1622 | { |
| 1623 | $header = rtrim($header, "\r\n ") . static::$LE . static::$LE; |
| 1624 | |
| 1625 | // CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. |
| 1626 | if (!empty($this->Sender) && self::isShellSafe($this->Sender)) { |
| 1627 | if ('qmail' === $this->Mailer) { |
| 1628 | $sendmailFmt = '%s -f%s'; |
| 1629 | } else { |
| 1630 | $sendmailFmt = '%s -oi -f%s -t'; |
| 1631 | } |
| 1632 | } elseif ('qmail' === $this->Mailer) { |
| 1633 | $sendmailFmt = '%s'; |
| 1634 | } else { |
| 1635 | $sendmailFmt = '%s -oi -t'; |
| 1636 | } |
| 1637 | |
| 1638 | $sendmail = sprintf($sendmailFmt, escapeshellcmd($this->Sendmail), $this->Sender); |
| 1639 | |
| 1640 | if ($this->SingleTo) { |
| 1641 | foreach ($this->SingleToArray as $toAddr) { |
| 1642 | $mail = @popen($sendmail, 'w'); |
| 1643 | if (!$mail) { |
| 1644 | throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
| 1645 | } |
| 1646 | fwrite($mail, 'To: ' . $toAddr . "\n"); |
| 1647 | fwrite($mail, $header); |
| 1648 | fwrite($mail, $body); |
| 1649 | $result = pclose($mail); |
| 1650 | $this->doCallback( |
| 1651 | ($result === 0), |
| 1652 | [$toAddr], |
| 1653 | $this->cc, |
| 1654 | $this->bcc, |
| 1655 | $this->Subject, |
| 1656 | $body, |
| 1657 | $this->From, |
| 1658 | [] |
| 1659 | ); |
| 1660 | if (0 !== $result) { |
| 1661 | throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
| 1662 | } |
| 1663 | } |
| 1664 | } else { |
| 1665 | $mail = @popen($sendmail, 'w'); |
| 1666 | if (!$mail) { |
| 1667 | throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
| 1668 | } |
| 1669 | fwrite($mail, $header); |
| 1670 | fwrite($mail, $body); |
| 1671 | $result = pclose($mail); |
| 1672 | $this->doCallback( |
| 1673 | ($result === 0), |
| 1674 | $this->to, |
| 1675 | $this->cc, |
| 1676 | $this->bcc, |
| 1677 | $this->Subject, |
| 1678 | $body, |
| 1679 | $this->From, |
| 1680 | [] |
| 1681 | ); |
| 1682 | if (0 !== $result) { |
| 1683 | throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | return true; |
| 1688 | } |
| 1689 | |
| 1690 | /** |
| 1691 | * Fix CVE-2016-10033 and CVE-2016-10045 by disallowing potentially unsafe shell characters. |
| 1692 | * Note that escapeshellarg and escapeshellcmd are inadequate for our purposes, especially on Windows. |
| 1693 | * |
| 1694 | * @see https://github.com/PHPMailer/PHPMailer/issues/924 CVE-2016-10045 bug report |
| 1695 | * |
| 1696 | * @param string $string The string to be validated |
| 1697 | * |
| 1698 | * @return bool |
| 1699 | */ |
| 1700 | protected static function isShellSafe($string) |
| 1701 | { |
| 1702 | // Future-proof |
| 1703 | if (escapeshellcmd($string) !== $string |
| 1704 | || !in_array(escapeshellarg($string), ["'$string'", "\"$string\""]) |
| 1705 | ) { |
| 1706 | return false; |
| 1707 | } |
| 1708 | |
| 1709 | $length = strlen($string); |
| 1710 | |
| 1711 | for ($i = 0; $i < $length; ++$i) { |
| 1712 | $c = $string[$i]; |
| 1713 | |
| 1714 | // All other characters have a special meaning in at least one common shell, including = and +. |
| 1715 | // Full stop (.) has a special meaning in cmd.exe, but its impact should be negligible here. |
| 1716 | // Note that this does permit non-Latin alphanumeric characters based on the current locale. |
| 1717 | if (!ctype_alnum($c) && strpos('@_-.', $c) === false) { |
| 1718 | return false; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | return true; |
| 1723 | } |
| 1724 | |
| 1725 | /** |
| 1726 | * Check whether a file path is of a permitted type. |
| 1727 | * Used to reject URLs and phar files from functions that access local file paths, |
| 1728 | * such as addAttachment. |
| 1729 | * |
| 1730 | * @param string $path A relative or absolute path to a file |
| 1731 | * |
| 1732 | * @return bool |
| 1733 | */ |
| 1734 | protected static function isPermittedPath($path) |
| 1735 | { |
| 1736 | return !preg_match('#^[a-z]+://#i', $path); |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * Send mail using the PHP mail() function. |
| 1741 | * |
| 1742 | * @see http://www.php.net/manual/en/book.mail.php |
| 1743 | * |
| 1744 | * @param string $header The message headers |
| 1745 | * @param string $body The message body |
| 1746 | * |
| 1747 | * @throws Exception |
| 1748 | * |
| 1749 | * @return bool |
| 1750 | */ |
| 1751 | protected function mailSend($header, $body) |
| 1752 | { |
| 1753 | $header = rtrim($header, "\r\n ") . static::$LE . static::$LE; |
| 1754 | |
| 1755 | $toArr = []; |
| 1756 | foreach ($this->to as $toaddr) { |
| 1757 | $toArr[] = $this->addrFormat($toaddr); |
| 1758 | } |
| 1759 | $to = implode(', ', $toArr); |
| 1760 | |
| 1761 | $params = null; |
| 1762 | //This sets the SMTP envelope sender which gets turned into a return-path header by the receiver |
| 1763 | //A space after `-f` is optional, but there is a long history of its presence |
| 1764 | //causing problems, so we don't use one |
| 1765 | //Exim docs: http://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html |
| 1766 | //Sendmail docs: http://www.sendmail.org/~ca/email/man/sendmail.html |
| 1767 | //Qmail docs: http://www.qmail.org/man/man8/qmail-inject.html |
| 1768 | //Example problem: https://www.drupal.org/node/1057954 |
| 1769 | // CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. |
| 1770 | if (!empty($this->Sender) && static::validateAddress($this->Sender) && self::isShellSafe($this->Sender)) { |
| 1771 | $params = sprintf('-f%s', $this->Sender); |
| 1772 | } |
| 1773 | if (!empty($this->Sender) && static::validateAddress($this->Sender)) { |
| 1774 | $old_from = ini_get('sendmail_from'); |
| 1775 | ini_set('sendmail_from', $this->Sender); |
| 1776 | } |
| 1777 | $result = false; |
| 1778 | if ($this->SingleTo && count($toArr) > 1) { |
| 1779 | foreach ($toArr as $toAddr) { |
| 1780 | $result = $this->mailPassthru($toAddr, $this->Subject, $body, $header, $params); |
| 1781 | $this->doCallback($result, [$toAddr], $this->cc, $this->bcc, $this->Subject, $body, $this->From, []); |
| 1782 | } |
| 1783 | } else { |
| 1784 | $result = $this->mailPassthru($to, $this->Subject, $body, $header, $params); |
| 1785 | $this->doCallback($result, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From, []); |
| 1786 | } |
| 1787 | if (isset($old_from)) { |
| 1788 | ini_set('sendmail_from', $old_from); |
| 1789 | } |
| 1790 | if (!$result) { |
| 1791 | throw new Exception($this->lang('instantiate'), self::STOP_CRITICAL); |
| 1792 | } |
| 1793 | |
| 1794 | return true; |
| 1795 | } |
| 1796 | |
| 1797 | /** |
| 1798 | * Get an instance to use for SMTP operations. |
| 1799 | * Override this function to load your own SMTP implementation, |
| 1800 | * or set one with setSMTPInstance. |
| 1801 | * |
| 1802 | * @return SMTP |
| 1803 | */ |
| 1804 | public function getSMTPInstance() |
| 1805 | { |
| 1806 | if (!is_object($this->smtp)) { |
| 1807 | $this->smtp = new SMTP(); |
| 1808 | } |
| 1809 | |
| 1810 | return $this->smtp; |
| 1811 | } |
| 1812 | |
| 1813 | /** |
| 1814 | * Provide an instance to use for SMTP operations. |
| 1815 | * |
| 1816 | * @return SMTP |
| 1817 | */ |
| 1818 | public function setSMTPInstance(SMTP $smtp) |
| 1819 | { |
| 1820 | $this->smtp = $smtp; |
| 1821 | |
| 1822 | return $this->smtp; |
| 1823 | } |
| 1824 | |
| 1825 | /** |
| 1826 | * Send mail via SMTP. |
| 1827 | * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. |
| 1828 | * |
| 1829 | * @see PHPMailer::setSMTPInstance() to use a different class. |
| 1830 | * |
| 1831 | * @uses \PHPMailer\PHPMailer\SMTP |
| 1832 | * |
| 1833 | * @param string $header The message headers |
| 1834 | * @param string $body The message body |
| 1835 | * |
| 1836 | * @throws Exception |
| 1837 | * |
| 1838 | * @return bool |
| 1839 | */ |
| 1840 | protected function smtpSend($header, $body) |
| 1841 | { |
| 1842 | $header = rtrim($header, "\r\n ") . static::$LE . static::$LE; |
| 1843 | $bad_rcpt = []; |
| 1844 | if (!$this->smtpConnect($this->SMTPOptions)) { |
| 1845 | throw new Exception($this->lang('smtp_connect_failed'), self::STOP_CRITICAL); |
| 1846 | } |
| 1847 | //Sender already validated in preSend() |
| 1848 | if ('' === $this->Sender) { |
| 1849 | $smtp_from = $this->From; |
| 1850 | } else { |
| 1851 | $smtp_from = $this->Sender; |
| 1852 | } |
| 1853 | if (!$this->smtp->mail($smtp_from)) { |
| 1854 | $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError())); |
| 1855 | throw new Exception($this->ErrorInfo, self::STOP_CRITICAL); |
| 1856 | } |
| 1857 | |
| 1858 | $callbacks = []; |
| 1859 | // Attempt to send to all recipients |
| 1860 | foreach ([$this->to, $this->cc, $this->bcc] as $togroup) { |
| 1861 | foreach ($togroup as $to) { |
| 1862 | if (!$this->smtp->recipient($to[0], $this->dsn)) { |
| 1863 | $error = $this->smtp->getError(); |
| 1864 | $bad_rcpt[] = ['to' => $to[0], 'error' => $error['detail']]; |
| 1865 | $isSent = false; |
| 1866 | } else { |
| 1867 | $isSent = true; |
| 1868 | } |
| 1869 | |
| 1870 | $callbacks[] = ['issent'=>$isSent, 'to'=>$to[0]]; |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | // Only send the DATA command if we have viable recipients |
| 1875 | if ((count($this->all_recipients) > count($bad_rcpt)) && !$this->smtp->data($header . $body)) { |
| 1876 | throw new Exception($this->lang('data_not_accepted'), self::STOP_CRITICAL); |
| 1877 | } |
| 1878 | |
| 1879 | $smtp_transaction_id = $this->smtp->getLastTransactionID(); |
| 1880 | |
| 1881 | if ($this->SMTPKeepAlive) { |
| 1882 | $this->smtp->reset(); |
| 1883 | } else { |
| 1884 | $this->smtp->quit(); |
| 1885 | $this->smtp->close(); |
| 1886 | } |
| 1887 | |
| 1888 | foreach ($callbacks as $cb) { |
| 1889 | $this->doCallback( |
| 1890 | $cb['issent'], |
| 1891 | [$cb['to']], |
| 1892 | [], |
| 1893 | [], |
| 1894 | $this->Subject, |
| 1895 | $body, |
| 1896 | $this->From, |
| 1897 | ['smtp_transaction_id' => $smtp_transaction_id] |
| 1898 | ); |
| 1899 | } |
| 1900 | |
| 1901 | //Create error message for any bad addresses |
| 1902 | if (count($bad_rcpt) > 0) { |
| 1903 | $errstr = ''; |
| 1904 | foreach ($bad_rcpt as $bad) { |
| 1905 | $errstr .= $bad['to'] . ': ' . $bad['error']; |
| 1906 | } |
| 1907 | throw new Exception($this->lang('recipients_failed') . $errstr, self::STOP_CONTINUE); |
| 1908 | } |
| 1909 | |
| 1910 | return true; |
| 1911 | } |
| 1912 | |
| 1913 | /** |
| 1914 | * Initiate a connection to an SMTP server. |
| 1915 | * Returns false if the operation failed. |
| 1916 | * |
| 1917 | * @param array $options An array of options compatible with stream_context_create() |
| 1918 | * |
| 1919 | * @throws Exception |
| 1920 | * |
| 1921 | * @uses \PHPMailer\PHPMailer\SMTP |
| 1922 | * |
| 1923 | * @return bool |
| 1924 | */ |
| 1925 | public function smtpConnect($options = null) |
| 1926 | { |
| 1927 | if (null === $this->smtp) { |
| 1928 | $this->smtp = $this->getSMTPInstance(); |
| 1929 | } |
| 1930 | |
| 1931 | //If no options are provided, use whatever is set in the instance |
| 1932 | if (null === $options) { |
| 1933 | $options = $this->SMTPOptions; |
| 1934 | } |
| 1935 | |
| 1936 | // Already connected? |
| 1937 | if ($this->smtp->connected()) { |
| 1938 | return true; |
| 1939 | } |
| 1940 | |
| 1941 | $this->smtp->setTimeout($this->Timeout); |
| 1942 | $this->smtp->setDebugLevel($this->SMTPDebug); |
| 1943 | $this->smtp->setDebugOutput($this->Debugoutput); |
| 1944 | $this->smtp->setVerp($this->do_verp); |
| 1945 | $hosts = explode(';', $this->Host); |
| 1946 | $lastexception = null; |
| 1947 | |
| 1948 | foreach ($hosts as $hostentry) { |
| 1949 | $hostinfo = []; |
| 1950 | if (!preg_match( |
| 1951 | '/^(?:(ssl|tls):\/\/)?(.+?)(?::(\d+))?$/', |
| 1952 | trim($hostentry), |
| 1953 | $hostinfo |
| 1954 | )) { |
| 1955 | $this->edebug($this->lang('invalid_hostentry') . ' ' . trim($hostentry)); |
| 1956 | // Not a valid host entry |
| 1957 | continue; |
| 1958 | } |
| 1959 | // $hostinfo[1]: optional ssl or tls prefix |
| 1960 | // $hostinfo[2]: the hostname |
| 1961 | // $hostinfo[3]: optional port number |
| 1962 | // The host string prefix can temporarily override the current setting for SMTPSecure |
| 1963 | // If it's not specified, the default value is used |
| 1964 | |
| 1965 | //Check the host name is a valid name or IP address before trying to use it |
| 1966 | if (!static::isValidHost($hostinfo[2])) { |
| 1967 | $this->edebug($this->lang('invalid_host') . ' ' . $hostinfo[2]); |
| 1968 | continue; |
| 1969 | } |
| 1970 | $prefix = ''; |
| 1971 | $secure = $this->SMTPSecure; |
| 1972 | $tls = (static::ENCRYPTION_STARTTLS === $this->SMTPSecure); |
| 1973 | if ('ssl' === $hostinfo[1] || ('' === $hostinfo[1] && static::ENCRYPTION_SMTPS === $this->SMTPSecure)) { |
| 1974 | $prefix = 'ssl://'; |
| 1975 | $tls = false; // Can't have SSL and TLS at the same time |
| 1976 | $secure = static::ENCRYPTION_SMTPS; |
| 1977 | } elseif ('tls' === $hostinfo[1]) { |
| 1978 | $tls = true; |
| 1979 | // tls doesn't use a prefix |
| 1980 | $secure = static::ENCRYPTION_STARTTLS; |
| 1981 | } |
| 1982 | //Do we need the OpenSSL extension? |
| 1983 | $sslext = defined('OPENSSL_ALGO_SHA256'); |
| 1984 | if (static::ENCRYPTION_STARTTLS === $secure || static::ENCRYPTION_SMTPS === $secure) { |
| 1985 | //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled |
| 1986 | if (!$sslext) { |
| 1987 | throw new Exception($this->lang('extension_missing') . 'openssl', self::STOP_CRITICAL); |
| 1988 | } |
| 1989 | } |
| 1990 | $host = $hostinfo[2]; |
| 1991 | $port = $this->Port; |
| 1992 | if (array_key_exists(3, $hostinfo) && is_numeric($hostinfo[3]) && $hostinfo[3] > 0 && $hostinfo[3] < 65536) { |
| 1993 | $port = (int) $hostinfo[3]; |
| 1994 | } |
| 1995 | if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { |
| 1996 | try { |
| 1997 | if ($this->Helo) { |
| 1998 | $hello = $this->Helo; |
| 1999 | } else { |
| 2000 | $hello = $this->serverHostname(); |
| 2001 | } |
| 2002 | $this->smtp->hello($hello); |
| 2003 | //Automatically enable TLS encryption if: |
| 2004 | // * it's not disabled |
| 2005 | // * we have openssl extension |
| 2006 | // * we are not already using SSL |
| 2007 | // * the server offers STARTTLS |
| 2008 | if ($this->SMTPAutoTLS && $sslext && 'ssl' !== $secure && $this->smtp->getServerExt('STARTTLS')) { |
| 2009 | $tls = true; |
| 2010 | } |
| 2011 | if ($tls) { |
| 2012 | if (!$this->smtp->startTLS()) { |
| 2013 | throw new Exception($this->lang('connect_host')); |
| 2014 | } |
| 2015 | // We must resend EHLO after TLS negotiation |
| 2016 | $this->smtp->hello($hello); |
| 2017 | } |
| 2018 | if ($this->SMTPAuth && !$this->smtp->authenticate( |
| 2019 | $this->Username, |
| 2020 | $this->Password, |
| 2021 | $this->AuthType, |
| 2022 | $this->oauth |
| 2023 | )) { |
| 2024 | throw new Exception($this->lang('authenticate')); |
| 2025 | } |
| 2026 | |
| 2027 | return true; |
| 2028 | } catch (Exception $exc) { |
| 2029 | $lastexception = $exc; |
| 2030 | $this->edebug($exc->getMessage()); |
| 2031 | // We must have connected, but then failed TLS or Auth, so close connection nicely |
| 2032 | $this->smtp->quit(); |
| 2033 | } |
| 2034 | } |
| 2035 | } |
| 2036 | // If we get here, all connection attempts have failed, so close connection hard |
| 2037 | $this->smtp->close(); |
| 2038 | // As we've caught all exceptions, just report whatever the last one was |
| 2039 | if ($this->exceptions && null !== $lastexception) { |
| 2040 | throw $lastexception; |
| 2041 | } |
| 2042 | |
| 2043 | return false; |
| 2044 | } |
| 2045 | |
| 2046 | /** |
| 2047 | * Close the active SMTP session if one exists. |
| 2048 | */ |
| 2049 | public function smtpClose() |
| 2050 | { |
| 2051 | if ((null !== $this->smtp) && $this->smtp->connected()) { |
| 2052 | $this->smtp->quit(); |
| 2053 | $this->smtp->close(); |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | /** |
| 2058 | * Set the language for error messages. |
| 2059 | * Returns false if it cannot load the language file. |
| 2060 | * The default language is English. |
| 2061 | * |
| 2062 | * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr") |
| 2063 | * @param string $lang_path Path to the language file directory, with trailing separator (slash) |
| 2064 | * |
| 2065 | * @return bool |
| 2066 | */ |
| 2067 | public function setLanguage($langcode = 'en', $lang_path = '') |
| 2068 | { |
| 2069 | // Backwards compatibility for renamed language codes |
| 2070 | $renamed_langcodes = [ |
| 2071 | 'br' => 'pt_br', |
| 2072 | 'cz' => 'cs', |
| 2073 | 'dk' => 'da', |
| 2074 | 'no' => 'nb', |
| 2075 | 'se' => 'sv', |
| 2076 | 'rs' => 'sr', |
| 2077 | 'tg' => 'tl', |
| 2078 | ]; |
| 2079 | |
| 2080 | if (isset($renamed_langcodes[$langcode])) { |
| 2081 | $langcode = $renamed_langcodes[$langcode]; |
| 2082 | } |
| 2083 | |
| 2084 | // Define full set of translatable strings in English |
| 2085 | $PHPMAILER_LANG = [ |
| 2086 | 'authenticate' => 'SMTP Error: Could not authenticate.', |
| 2087 | 'connect_host' => 'SMTP Error: Could not connect to SMTP host.', |
| 2088 | 'data_not_accepted' => 'SMTP Error: data not accepted.', |
| 2089 | 'empty_message' => 'Message body empty', |
| 2090 | 'encoding' => 'Unknown encoding: ', |
| 2091 | 'execute' => 'Could not execute: ', |
| 2092 | 'file_access' => 'Could not access file: ', |
| 2093 | 'file_open' => 'File Error: Could not open file: ', |
| 2094 | 'from_failed' => 'The following From address failed: ', |
| 2095 | 'instantiate' => 'Could not instantiate mail function.', |
| 2096 | 'invalid_address' => 'Invalid address: ', |
| 2097 | 'invalid_hostentry' => 'Invalid hostentry: ', |
| 2098 | 'invalid_host' => 'Invalid host: ', |
| 2099 | 'mailer_not_supported' => ' mailer is not supported.', |
| 2100 | 'provide_address' => 'You must provide at least one recipient email address.', |
| 2101 | 'recipients_failed' => 'SMTP Error: The following recipients failed: ', |
| 2102 | 'signing' => 'Signing Error: ', |
| 2103 | 'smtp_connect_failed' => 'SMTP connect() failed.', |
| 2104 | 'smtp_error' => 'SMTP server error: ', |
| 2105 | 'variable_set' => 'Cannot set or reset variable: ', |
| 2106 | 'extension_missing' => 'Extension missing: ', |
| 2107 | ]; |
| 2108 | if (empty($lang_path)) { |
| 2109 | // Calculate an absolute path so it can work if CWD is not here |
| 2110 | $lang_path = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR; |
| 2111 | } |
| 2112 | //Validate $langcode |
| 2113 | if (!preg_match('/^[a-z]{2}(?:_[a-zA-Z]{2})?$/', $langcode)) { |
| 2114 | $langcode = 'en'; |
| 2115 | } |
| 2116 | $foundlang = true; |
| 2117 | $lang_file = $lang_path . 'phpmailer.lang-' . $langcode . '.php'; |
| 2118 | // There is no English translation file |
| 2119 | if ('en' !== $langcode) { |
| 2120 | // Make sure language file path is readable |
| 2121 | if (!static::isPermittedPath($lang_file) || !file_exists($lang_file)) { |
| 2122 | $foundlang = false; |
| 2123 | } else { |
| 2124 | // Overwrite language-specific strings. |
| 2125 | // This way we'll never have missing translation keys. |
| 2126 | $foundlang = include $lang_file; |
| 2127 | } |
| 2128 | } |
| 2129 | $this->language = $PHPMAILER_LANG; |
| 2130 | |
| 2131 | return (bool) $foundlang; // Returns false if language not found |
| 2132 | } |
| 2133 | |
| 2134 | /** |
| 2135 | * Get the array of strings for the current language. |
| 2136 | * |
| 2137 | * @return array |
| 2138 | */ |
| 2139 | public function getTranslations() |
| 2140 | { |
| 2141 | return $this->language; |
| 2142 | } |
| 2143 | |
| 2144 | /** |
| 2145 | * Create recipient headers. |
| 2146 | * |
| 2147 | * @param string $type |
| 2148 | * @param array $addr An array of recipients, |
| 2149 | * where each recipient is a 2-element indexed array with element 0 containing an address |
| 2150 | * and element 1 containing a name, like: |
| 2151 | * [['joe@example.com', 'Joe User'], ['zoe@example.com', 'Zoe User']] |
| 2152 | * |
| 2153 | * @return string |
| 2154 | */ |
| 2155 | public function addrAppend($type, $addr) |
| 2156 | { |
| 2157 | $addresses = []; |
| 2158 | foreach ($addr as $address) { |
| 2159 | $addresses[] = $this->addrFormat($address); |
| 2160 | } |
| 2161 | |
| 2162 | return $type . ': ' . implode(', ', $addresses) . static::$LE; |
| 2163 | } |
| 2164 | |
| 2165 | /** |
| 2166 | * Format an address for use in a message header. |
| 2167 | * |
| 2168 | * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name like |
| 2169 | * ['joe@example.com', 'Joe User'] |
| 2170 | * |
| 2171 | * @return string |
| 2172 | */ |
| 2173 | public function addrFormat($addr) |
| 2174 | { |
| 2175 | if (empty($addr[1])) { // No name provided |
| 2176 | return $this->secureHeader($addr[0]); |
| 2177 | } |
| 2178 | |
| 2179 | return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . |
| 2180 | ' <' . $this->secureHeader($addr[0]) . '>'; |
| 2181 | } |
| 2182 | |
| 2183 | /** |
| 2184 | * Word-wrap message. |
| 2185 | * For use with mailers that do not automatically perform wrapping |
| 2186 | * and for quoted-printable encoded messages. |
| 2187 | * Original written by philippe. |
| 2188 | * |
| 2189 | * @param string $message The message to wrap |
| 2190 | * @param int $length The line length to wrap to |
| 2191 | * @param bool $qp_mode Whether to run in Quoted-Printable mode |
| 2192 | * |
| 2193 | * @return string |
| 2194 | */ |
| 2195 | public function wrapText($message, $length, $qp_mode = false) |
| 2196 | { |
| 2197 | if ($qp_mode) { |
| 2198 | $soft_break = sprintf(' =%s', static::$LE); |
| 2199 | } else { |
| 2200 | $soft_break = static::$LE; |
| 2201 | } |
| 2202 | // If utf-8 encoding is used, we will need to make sure we don't |
| 2203 | // split multibyte characters when we wrap |
| 2204 | $is_utf8 = static::CHARSET_UTF8 === strtolower($this->CharSet); |
| 2205 | $lelen = strlen(static::$LE); |
| 2206 | $crlflen = strlen(static::$LE); |
| 2207 | |
| 2208 | $message = static::normalizeBreaks($message); |
| 2209 | //Remove a trailing line break |
| 2210 | if (substr($message, -$lelen) === static::$LE) { |
| 2211 | $message = substr($message, 0, -$lelen); |
| 2212 | } |
| 2213 | |
| 2214 | //Split message into lines |
| 2215 | $lines = explode(static::$LE, $message); |
| 2216 | //Message will be rebuilt in here |
| 2217 | $message = ''; |
| 2218 | foreach ($lines as $line) { |
| 2219 | $words = explode(' ', $line); |
| 2220 | $buf = ''; |
| 2221 | $firstword = true; |
| 2222 | foreach ($words as $word) { |
| 2223 | if ($qp_mode && (strlen($word) > $length)) { |
| 2224 | $space_left = $length - strlen($buf) - $crlflen; |
| 2225 | if (!$firstword) { |
| 2226 | if ($space_left > 20) { |
| 2227 | $len = $space_left; |
| 2228 | if ($is_utf8) { |
| 2229 | $len = $this->utf8CharBoundary($word, $len); |
| 2230 | } elseif ('=' === substr($word, $len - 1, 1)) { |
| 2231 | --$len; |
| 2232 | } elseif ('=' === substr($word, $len - 2, 1)) { |
| 2233 | $len -= 2; |
| 2234 | } |
| 2235 | $part = substr($word, 0, $len); |
| 2236 | $word = substr($word, $len); |
| 2237 | $buf .= ' ' . $part; |
| 2238 | $message .= $buf . sprintf('=%s', static::$LE); |
| 2239 | } else { |
| 2240 | $message .= $buf . $soft_break; |
| 2241 | } |
| 2242 | $buf = ''; |
| 2243 | } |
| 2244 | while ($word !== '') { |
| 2245 | if ($length <= 0) { |
| 2246 | break; |
| 2247 | } |
| 2248 | $len = $length; |
| 2249 | if ($is_utf8) { |
| 2250 | $len = $this->utf8CharBoundary($word, $len); |
| 2251 | } elseif ('=' === substr($word, $len - 1, 1)) { |
| 2252 | --$len; |
| 2253 | } elseif ('=' === substr($word, $len - 2, 1)) { |
| 2254 | $len -= 2; |
| 2255 | } |
| 2256 | $part = substr($word, 0, $len); |
| 2257 | $word = (string) substr($word, $len); |
| 2258 | |
| 2259 | if ($word !== '') { |
| 2260 | $message .= $part . sprintf('=%s', static::$LE); |
| 2261 | } else { |
| 2262 | $buf = $part; |
| 2263 | } |
| 2264 | } |
| 2265 | } else { |
| 2266 | $buf_o = $buf; |
| 2267 | if (!$firstword) { |
| 2268 | $buf .= ' '; |
| 2269 | } |
| 2270 | $buf .= $word; |
| 2271 | |
| 2272 | if ('' !== $buf_o && strlen($buf) > $length) { |
| 2273 | $message .= $buf_o . $soft_break; |
| 2274 | $buf = $word; |
| 2275 | } |
| 2276 | } |
| 2277 | $firstword = false; |
| 2278 | } |
| 2279 | $message .= $buf . static::$LE; |
| 2280 | } |
| 2281 | |
| 2282 | return $message; |
| 2283 | } |
| 2284 | |
| 2285 | /** |
| 2286 | * Find the last character boundary prior to $maxLength in a utf-8 |
| 2287 | * quoted-printable encoded string. |
| 2288 | * Original written by Colin Brown. |
| 2289 | * |
| 2290 | * @param string $encodedText utf-8 QP text |
| 2291 | * @param int $maxLength Find the last character boundary prior to this length |
| 2292 | * |
| 2293 | * @return int |
| 2294 | */ |
| 2295 | public function utf8CharBoundary($encodedText, $maxLength) |
| 2296 | { |
| 2297 | $foundSplitPos = false; |
| 2298 | $lookBack = 3; |
| 2299 | while (!$foundSplitPos) { |
| 2300 | $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack); |
| 2301 | $encodedCharPos = strpos($lastChunk, '='); |
| 2302 | if (false !== $encodedCharPos) { |
| 2303 | // Found start of encoded character byte within $lookBack block. |
| 2304 | // Check the encoded byte value (the 2 chars after the '=') |
| 2305 | $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2); |
| 2306 | $dec = hexdec($hex); |
| 2307 | if ($dec < 128) { |
| 2308 | // Single byte character. |
| 2309 | // If the encoded char was found at pos 0, it will fit |
| 2310 | // otherwise reduce maxLength to start of the encoded char |
| 2311 | if ($encodedCharPos > 0) { |
| 2312 | $maxLength -= $lookBack - $encodedCharPos; |
| 2313 | } |
| 2314 | $foundSplitPos = true; |
| 2315 | } elseif ($dec >= 192) { |
| 2316 | // First byte of a multi byte character |
| 2317 | // Reduce maxLength to split at start of character |
| 2318 | $maxLength -= $lookBack - $encodedCharPos; |
| 2319 | $foundSplitPos = true; |
| 2320 | } elseif ($dec < 192) { |
| 2321 | // Middle byte of a multi byte character, look further back |
| 2322 | $lookBack += 3; |
| 2323 | } |
| 2324 | } else { |
| 2325 | // No encoded character found |
| 2326 | $foundSplitPos = true; |
| 2327 | } |
| 2328 | } |
| 2329 | |
| 2330 | return $maxLength; |
| 2331 | } |
| 2332 | |
| 2333 | /** |
| 2334 | * Apply word wrapping to the message body. |
| 2335 | * Wraps the message body to the number of chars set in the WordWrap property. |
| 2336 | * You should only do this to plain-text bodies as wrapping HTML tags may break them. |
| 2337 | * This is called automatically by createBody(), so you don't need to call it yourself. |
| 2338 | */ |
| 2339 | public function setWordWrap() |
| 2340 | { |
| 2341 | if ($this->WordWrap < 1) { |
| 2342 | return; |
| 2343 | } |
| 2344 | |
| 2345 | switch ($this->message_type) { |
| 2346 | case 'alt': |
| 2347 | case 'alt_inline': |
| 2348 | case 'alt_attach': |
| 2349 | case 'alt_inline_attach': |
| 2350 | $this->AltBody = $this->wrapText($this->AltBody, $this->WordWrap); |
| 2351 | break; |
| 2352 | default: |
| 2353 | $this->Body = $this->wrapText($this->Body, $this->WordWrap); |
| 2354 | break; |
| 2355 | } |
| 2356 | } |
| 2357 | |
| 2358 | /** |
| 2359 | * Assemble message headers. |
| 2360 | * |
| 2361 | * @return string The assembled headers |
| 2362 | */ |
| 2363 | public function createHeader() |
| 2364 | { |
| 2365 | $result = ''; |
| 2366 | |
| 2367 | $result .= $this->headerLine('Date', '' === $this->MessageDate ? self::rfcDate() : $this->MessageDate); |
| 2368 | |
| 2369 | // To be created automatically by mail() |
| 2370 | if ($this->SingleTo) { |
| 2371 | if ('mail' !== $this->Mailer) { |
| 2372 | foreach ($this->to as $toaddr) { |
| 2373 | $this->SingleToArray[] = $this->addrFormat($toaddr); |
| 2374 | } |
| 2375 | } |
| 2376 | } elseif (count($this->to) > 0) { |
| 2377 | if ('mail' !== $this->Mailer) { |
| 2378 | $result .= $this->addrAppend('To', $this->to); |
| 2379 | } |
| 2380 | } elseif (count($this->cc) === 0) { |
| 2381 | $result .= $this->headerLine('To', 'undisclosed-recipients:;'); |
| 2382 | } |
| 2383 | |
| 2384 | $result .= $this->addrAppend('From', [[trim($this->From), $this->FromName]]); |
| 2385 | |
| 2386 | // sendmail and mail() extract Cc from the header before sending |
| 2387 | if (count($this->cc) > 0) { |
| 2388 | $result .= $this->addrAppend('Cc', $this->cc); |
| 2389 | } |
| 2390 | |
| 2391 | // sendmail and mail() extract Bcc from the header before sending |
| 2392 | if (( |
| 2393 | 'sendmail' === $this->Mailer || 'qmail' === $this->Mailer || 'mail' === $this->Mailer |
| 2394 | ) |
| 2395 | && count($this->bcc) > 0 |
| 2396 | ) { |
| 2397 | $result .= $this->addrAppend('Bcc', $this->bcc); |
| 2398 | } |
| 2399 | |
| 2400 | if (count($this->ReplyTo) > 0) { |
| 2401 | $result .= $this->addrAppend('Reply-To', $this->ReplyTo); |
| 2402 | } |
| 2403 | |
| 2404 | // mail() sets the subject itself |
| 2405 | if ('mail' !== $this->Mailer) { |
| 2406 | $result .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader($this->Subject))); |
| 2407 | } |
| 2408 | |
| 2409 | // Only allow a custom message ID if it conforms to RFC 5322 section 3.6.4 |
| 2410 | // https://tools.ietf.org/html/rfc5322#section-3.6.4 |
| 2411 | if ('' !== $this->MessageID && preg_match('/^<.*@.*>$/', $this->MessageID)) { |
| 2412 | $this->lastMessageID = $this->MessageID; |
| 2413 | } else { |
| 2414 | $this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->serverHostname()); |
| 2415 | } |
| 2416 | $result .= $this->headerLine('Message-ID', $this->lastMessageID); |
| 2417 | if (null !== $this->Priority) { |
| 2418 | $result .= $this->headerLine('X-Priority', $this->Priority); |
| 2419 | } |
| 2420 | if ('' === $this->XMailer) { |
| 2421 | $result .= $this->headerLine( |
| 2422 | 'X-Mailer', |
| 2423 | 'PHPMailer ' . self::VERSION . ' (https://github.com/PHPMailer/PHPMailer)' |
| 2424 | ); |
| 2425 | } else { |
| 2426 | $myXmailer = trim($this->XMailer); |
| 2427 | if ($myXmailer) { |
| 2428 | $result .= $this->headerLine('X-Mailer', $myXmailer); |
| 2429 | } |
| 2430 | } |
| 2431 | |
| 2432 | if ('' !== $this->ConfirmReadingTo) { |
| 2433 | $result .= $this->headerLine('Disposition-Notification-To', '<' . $this->ConfirmReadingTo . '>'); |
| 2434 | } |
| 2435 | |
| 2436 | // Add custom headers |
| 2437 | foreach ($this->CustomHeader as $header) { |
| 2438 | $result .= $this->headerLine( |
| 2439 | trim($header[0]), |
| 2440 | $this->encodeHeader(trim($header[1])) |
| 2441 | ); |
| 2442 | } |
| 2443 | if (!$this->sign_key_file) { |
| 2444 | $result .= $this->headerLine('MIME-Version', '1.0'); |
| 2445 | $result .= $this->getMailMIME(); |
| 2446 | } |
| 2447 | |
| 2448 | return $result; |
| 2449 | } |
| 2450 | |
| 2451 | /** |
| 2452 | * Get the message MIME type headers. |
| 2453 | * |
| 2454 | * @return string |
| 2455 | */ |
| 2456 | public function getMailMIME() |
| 2457 | { |
| 2458 | $result = ''; |
| 2459 | $ismultipart = true; |
| 2460 | switch ($this->message_type) { |
| 2461 | case 'inline': |
| 2462 | $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); |
| 2463 | $result .= $this->textLine(' boundary="' . $this->boundary[1] . '"'); |
| 2464 | break; |
| 2465 | case 'attach': |
| 2466 | case 'inline_attach': |
| 2467 | case 'alt_attach': |
| 2468 | case 'alt_inline_attach': |
| 2469 | $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_MIXED . ';'); |
| 2470 | $result .= $this->textLine(' boundary="' . $this->boundary[1] . '"'); |
| 2471 | break; |
| 2472 | case 'alt': |
| 2473 | case 'alt_inline': |
| 2474 | $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';'); |
| 2475 | $result .= $this->textLine(' boundary="' . $this->boundary[1] . '"'); |
| 2476 | break; |
| 2477 | default: |
| 2478 | // Catches case 'plain': and case '': |
| 2479 | $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet); |
| 2480 | $ismultipart = false; |
| 2481 | break; |
| 2482 | } |
| 2483 | // RFC1341 part 5 says 7bit is assumed if not specified |
| 2484 | if (static::ENCODING_7BIT !== $this->Encoding) { |
| 2485 | // RFC 2045 section 6.4 says multipart MIME parts may only use 7bit, 8bit or binary CTE |
| 2486 | if ($ismultipart) { |
| 2487 | if (static::ENCODING_8BIT === $this->Encoding) { |
| 2488 | $result .= $this->headerLine('Content-Transfer-Encoding', static::ENCODING_8BIT); |
| 2489 | } |
| 2490 | // The only remaining alternatives are quoted-printable and base64, which are both 7bit compatible |
| 2491 | } else { |
| 2492 | $result .= $this->headerLine('Content-Transfer-Encoding', $this->Encoding); |
| 2493 | } |
| 2494 | } |
| 2495 | |
| 2496 | if ('mail' !== $this->Mailer) { |
| 2497 | // $result .= static::$LE; |
| 2498 | } |
| 2499 | |
| 2500 | return $result; |
| 2501 | } |
| 2502 | |
| 2503 | /** |
| 2504 | * Returns the whole MIME message. |
| 2505 | * Includes complete headers and body. |
| 2506 | * Only valid post preSend(). |
| 2507 | * |
| 2508 | * @see PHPMailer::preSend() |
| 2509 | * |
| 2510 | * @return string |
| 2511 | */ |
| 2512 | public function getSentMIMEMessage() |
| 2513 | { |
| 2514 | return rtrim($this->MIMEHeader . $this->mailHeader, "\n\r") . static::$LE . static::$LE . $this->MIMEBody; |
| 2515 | } |
| 2516 | |
| 2517 | /** |
| 2518 | * Create a unique ID to use for boundaries. |
| 2519 | * |
| 2520 | * @return string |
| 2521 | */ |
| 2522 | protected function generateId() |
| 2523 | { |
| 2524 | $len = 32; //32 bytes = 256 bits |
| 2525 | $bytes = ''; |
| 2526 | if (function_exists('random_bytes')) { |
| 2527 | try { |
| 2528 | $bytes = random_bytes($len); |
| 2529 | } catch (\Exception $e) { |
| 2530 | //Do nothing |
| 2531 | } |
| 2532 | } elseif (function_exists('openssl_random_pseudo_bytes')) { |
| 2533 | /** @noinspection CryptographicallySecureRandomnessInspection */ |
| 2534 | $bytes = openssl_random_pseudo_bytes($len); |
| 2535 | } |
| 2536 | if ($bytes === '') { |
| 2537 | //We failed to produce a proper random string, so make do. |
| 2538 | //Use a hash to force the length to the same as the other methods |
| 2539 | $bytes = hash('sha256', uniqid((string) mt_rand(), true), true); |
| 2540 | } |
| 2541 | |
| 2542 | //We don't care about messing up base64 format here, just want a random string |
| 2543 | return str_replace(['=', '+', '/'], '', base64_encode(hash('sha256', $bytes, true))); |
| 2544 | } |
| 2545 | |
| 2546 | /** |
| 2547 | * Assemble the message body. |
| 2548 | * Returns an empty string on failure. |
| 2549 | * |
| 2550 | * @throws Exception |
| 2551 | * |
| 2552 | * @return string The assembled message body |
| 2553 | */ |
| 2554 | public function createBody() |
| 2555 | { |
| 2556 | $body = ''; |
| 2557 | //Create unique IDs and preset boundaries |
| 2558 | $this->uniqueid = $this->generateId(); |
| 2559 | $this->boundary[1] = 'b1_' . $this->uniqueid; |
| 2560 | $this->boundary[2] = 'b2_' . $this->uniqueid; |
| 2561 | $this->boundary[3] = 'b3_' . $this->uniqueid; |
| 2562 | |
| 2563 | if ($this->sign_key_file) { |
| 2564 | $body .= $this->getMailMIME() . static::$LE; |
| 2565 | } |
| 2566 | |
| 2567 | $this->setWordWrap(); |
| 2568 | |
| 2569 | $bodyEncoding = $this->Encoding; |
| 2570 | $bodyCharSet = $this->CharSet; |
| 2571 | //Can we do a 7-bit downgrade? |
| 2572 | if (static::ENCODING_8BIT === $bodyEncoding && !$this->has8bitChars($this->Body)) { |
| 2573 | $bodyEncoding = static::ENCODING_7BIT; |
| 2574 | //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit |
| 2575 | $bodyCharSet = static::CHARSET_ASCII; |
| 2576 | } |
| 2577 | //If lines are too long, and we're not already using an encoding that will shorten them, |
| 2578 | //change to quoted-printable transfer encoding for the body part only |
| 2579 | if (static::ENCODING_BASE64 !== $this->Encoding && static::hasLineLongerThanMax($this->Body)) { |
| 2580 | $bodyEncoding = static::ENCODING_QUOTED_PRINTABLE; |
| 2581 | } |
| 2582 | |
| 2583 | $altBodyEncoding = $this->Encoding; |
| 2584 | $altBodyCharSet = $this->CharSet; |
| 2585 | //Can we do a 7-bit downgrade? |
| 2586 | if (static::ENCODING_8BIT === $altBodyEncoding && !$this->has8bitChars($this->AltBody)) { |
| 2587 | $altBodyEncoding = static::ENCODING_7BIT; |
| 2588 | //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit |
| 2589 | $altBodyCharSet = static::CHARSET_ASCII; |
| 2590 | } |
| 2591 | //If lines are too long, and we're not already using an encoding that will shorten them, |
| 2592 | //change to quoted-printable transfer encoding for the alt body part only |
| 2593 | if (static::ENCODING_BASE64 !== $altBodyEncoding && static::hasLineLongerThanMax($this->AltBody)) { |
| 2594 | $altBodyEncoding = static::ENCODING_QUOTED_PRINTABLE; |
| 2595 | } |
| 2596 | //Use this as a preamble in all multipart message types |
| 2597 | $mimepre = 'This is a multi-part message in MIME format.' . static::$LE; |
| 2598 | switch ($this->message_type) { |
| 2599 | case 'inline': |
| 2600 | $body .= $mimepre; |
| 2601 | $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); |
| 2602 | $body .= $this->encodeString($this->Body, $bodyEncoding); |
| 2603 | $body .= static::$LE; |
| 2604 | $body .= $this->attachAll('inline', $this->boundary[1]); |
| 2605 | break; |
| 2606 | case 'attach': |
| 2607 | $body .= $mimepre; |
| 2608 | $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); |
| 2609 | $body .= $this->encodeString($this->Body, $bodyEncoding); |
| 2610 | $body .= static::$LE; |
| 2611 | $body .= $this->attachAll('attachment', $this->boundary[1]); |
| 2612 | break; |
| 2613 | case 'inline_attach': |
| 2614 | $body .= $mimepre; |
| 2615 | $body .= $this->textLine('--' . $this->boundary[1]); |
| 2616 | $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); |
| 2617 | $body .= $this->textLine(' boundary="' . $this->boundary[2] . '";'); |
| 2618 | $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"'); |
| 2619 | $body .= static::$LE; |
| 2620 | $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding); |
| 2621 | $body .= $this->encodeString($this->Body, $bodyEncoding); |
| 2622 | $body .= static::$LE; |
| 2623 | $body .= $this->attachAll('inline', $this->boundary[2]); |
| 2624 | $body .= static::$LE; |
| 2625 | $body .= $this->attachAll('attachment', $this->boundary[1]); |
| 2626 | break; |
| 2627 | case 'alt': |
| 2628 | $body .= $mimepre; |
| 2629 | $body .= $this->getBoundary( |
| 2630 | $this->boundary[1], |
| 2631 | $altBodyCharSet, |
| 2632 | static::CONTENT_TYPE_PLAINTEXT, |
| 2633 | $altBodyEncoding |
| 2634 | ); |
| 2635 | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
| 2636 | $body .= static::$LE; |
| 2637 | $body .= $this->getBoundary( |
| 2638 | $this->boundary[1], |
| 2639 | $bodyCharSet, |
| 2640 | static::CONTENT_TYPE_TEXT_HTML, |
| 2641 | $bodyEncoding |
| 2642 | ); |
| 2643 | $body .= $this->encodeString($this->Body, $bodyEncoding); |
| 2644 | $body .= static::$LE; |
| 2645 | if (!empty($this->Ical)) { |
| 2646 | $method = static::ICAL_METHOD_REQUEST; |
| 2647 | foreach (static::$IcalMethods as $imethod) { |
| 2648 | if (stripos($this->Ical, 'METHOD:' . $imethod) !== false) { |
| 2649 | $method = $imethod; |
| 2650 | break; |
| 2651 | } |
| 2652 | } |
| 2653 | $body .= $this->getBoundary( |
| 2654 | $this->boundary[1], |
| 2655 | '', |
| 2656 | static::CONTENT_TYPE_TEXT_CALENDAR . '; method=' . $method, |
| 2657 | '' |
| 2658 | ); |
| 2659 | $body .= $this->encodeString($this->Ical, $this->Encoding); |
| 2660 | $body .= static::$LE; |
| 2661 | } |
| 2662 | $body .= $this->endBoundary($this->boundary[1]); |
| 2663 | break; |
| 2664 | case 'alt_inline': |
| 2665 | $body .= $mimepre; |
| 2666 | $body .= $this->getBoundary( |
| 2667 | $this->boundary[1], |
| 2668 | $altBodyCharSet, |
| 2669 | static::CONTENT_TYPE_PLAINTEXT, |
| 2670 | $altBodyEncoding |
| 2671 | ); |
| 2672 | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
| 2673 | $body .= static::$LE; |
| 2674 | $body .= $this->textLine('--' . $this->boundary[1]); |
| 2675 | $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); |
| 2676 | $body .= $this->textLine(' boundary="' . $this->boundary[2] . '";'); |
| 2677 | $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"'); |
| 2678 | $body .= static::$LE; |
| 2679 | $body .= $this->getBoundary( |
| 2680 | $this->boundary[2], |
| 2681 | $bodyCharSet, |
| 2682 | static::CONTENT_TYPE_TEXT_HTML, |
| 2683 | $bodyEncoding |
| 2684 | ); |
| 2685 | $body .= $this->encodeString($this->Body, $bodyEncoding); |
| 2686 | $body .= static::$LE; |
| 2687 | $body .= $this->attachAll('inline', $this->boundary[2]); |
| 2688 | $body .= static::$LE; |
| 2689 | $body .= $this->endBoundary($this->boundary[1]); |
| 2690 | break; |
| 2691 | case 'alt_attach': |
| 2692 | $body .= $mimepre; |
| 2693 | $body .= $this->textLine('--' . $this->boundary[1]); |
| 2694 | $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';'); |
| 2695 | $body .= $this->textLine(' boundary="' . $this->boundary[2] . '"'); |
| 2696 | $body .= static::$LE; |
| 2697 | $body .= $this->getBoundary( |
| 2698 | $this->boundary[2], |
| 2699 | $altBodyCharSet, |
| 2700 | static::CONTENT_TYPE_PLAINTEXT, |
| 2701 | $altBodyEncoding |
| 2702 | ); |
| 2703 | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
| 2704 | $body .= static::$LE; |
| 2705 | $body .= $this->getBoundary( |
| 2706 | $this->boundary[2], |
| 2707 | $bodyCharSet, |
| 2708 | static::CONTENT_TYPE_TEXT_HTML, |
| 2709 | $bodyEncoding |
| 2710 | ); |
| 2711 | $body .= $this->encodeString($this->Body, $bodyEncoding); |
| 2712 | $body .= static::$LE; |
| 2713 | if (!empty($this->Ical)) { |
| 2714 | $method = static::ICAL_METHOD_REQUEST; |
| 2715 | foreach (static::$IcalMethods as $imethod) { |
| 2716 | if (stripos($this->Ical, 'METHOD:' . $imethod) !== false) { |
| 2717 | $method = $imethod; |
| 2718 | break; |
| 2719 | } |
| 2720 | } |
| 2721 | $body .= $this->getBoundary( |
| 2722 | $this->boundary[2], |
| 2723 | '', |
| 2724 | static::CONTENT_TYPE_TEXT_CALENDAR . '; method=' . $method, |
| 2725 | '' |
| 2726 | ); |
| 2727 | $body .= $this->encodeString($this->Ical, $this->Encoding); |
| 2728 | } |
| 2729 | $body .= $this->endBoundary($this->boundary[2]); |
| 2730 | $body .= static::$LE; |
| 2731 | $body .= $this->attachAll('attachment', $this->boundary[1]); |
| 2732 | break; |
| 2733 | case 'alt_inline_attach': |
| 2734 | $body .= $mimepre; |
| 2735 | $body .= $this->textLine('--' . $this->boundary[1]); |
| 2736 | $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';'); |
| 2737 | $body .= $this->textLine(' boundary="' . $this->boundary[2] . '"'); |
| 2738 | $body .= static::$LE; |
| 2739 | $body .= $this->getBoundary( |
| 2740 | $this->boundary[2], |
| 2741 | $altBodyCharSet, |
| 2742 | static::CONTENT_TYPE_PLAINTEXT, |
| 2743 | $altBodyEncoding |
| 2744 | ); |
| 2745 | $body .= $this->encodeString($this->AltBody, $altBodyEncoding); |
| 2746 | $body .= static::$LE; |
| 2747 | $body .= $this->textLine('--' . $this->boundary[2]); |
| 2748 | $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); |
| 2749 | $body .= $this->textLine(' boundary="' . $this->boundary[3] . '";'); |
| 2750 | $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"'); |
| 2751 | $body .= static::$LE; |
| 2752 | $body .= $this->getBoundary( |
| 2753 | $this->boundary[3], |
| 2754 | $bodyCharSet, |
| 2755 | static::CONTENT_TYPE_TEXT_HTML, |
| 2756 | $bodyEncoding |
| 2757 | ); |
| 2758 | $body .= $this->encodeString($this->Body, $bodyEncoding); |
| 2759 | $body .= static::$LE; |
| 2760 | $body .= $this->attachAll('inline', $this->boundary[3]); |
| 2761 | $body .= static::$LE; |
| 2762 | $body .= $this->endBoundary($this->boundary[2]); |
| 2763 | $body .= static::$LE; |
| 2764 | $body .= $this->attachAll('attachment', $this->boundary[1]); |
| 2765 | break; |
| 2766 | default: |
| 2767 | // Catch case 'plain' and case '', applies to simple `text/plain` and `text/html` body content types |
| 2768 | //Reset the `Encoding` property in case we changed it for line length reasons |
| 2769 | $this->Encoding = $bodyEncoding; |
| 2770 | $body .= $this->encodeString($this->Body, $this->Encoding); |
| 2771 | break; |
| 2772 | } |
| 2773 | |
| 2774 | if ($this->isError()) { |
| 2775 | $body = ''; |
| 2776 | if ($this->exceptions) { |
| 2777 | throw new Exception($this->lang('empty_message'), self::STOP_CRITICAL); |
| 2778 | } |
| 2779 | } elseif ($this->sign_key_file) { |
| 2780 | try { |
| 2781 | if (!defined('PKCS7_TEXT')) { |
| 2782 | throw new Exception($this->lang('extension_missing') . 'openssl'); |
| 2783 | } |
| 2784 | |
| 2785 | $file = tempnam(sys_get_temp_dir(), 'srcsign'); |
| 2786 | $signed = tempnam(sys_get_temp_dir(), 'mailsign'); |
| 2787 | file_put_contents($file, $body); |
| 2788 | |
| 2789 | //Workaround for PHP bug https://bugs.php.net/bug.php?id=69197 |
| 2790 | if (empty($this->sign_extracerts_file)) { |
| 2791 | $sign = @openssl_pkcs7_sign( |
| 2792 | $file, |
| 2793 | $signed, |
| 2794 | 'file://' . realpath($this->sign_cert_file), |
| 2795 | ['file://' . realpath($this->sign_key_file), $this->sign_key_pass], |
| 2796 | [] |
| 2797 | ); |
| 2798 | } else { |
| 2799 | $sign = @openssl_pkcs7_sign( |
| 2800 | $file, |
| 2801 | $signed, |
| 2802 | 'file://' . realpath($this->sign_cert_file), |
| 2803 | ['file://' . realpath($this->sign_key_file), $this->sign_key_pass], |
| 2804 | [], |
| 2805 | PKCS7_DETACHED, |
| 2806 | $this->sign_extracerts_file |
| 2807 | ); |
| 2808 | } |
| 2809 | |
| 2810 | @unlink($file); |
| 2811 | if ($sign) { |
| 2812 | $body = file_get_contents($signed); |
| 2813 | @unlink($signed); |
| 2814 | //The message returned by openssl contains both headers and body, so need to split them up |
| 2815 | $parts = explode("\n\n", $body, 2); |
| 2816 | $this->MIMEHeader .= $parts[0] . static::$LE . static::$LE; |
| 2817 | $body = $parts[1]; |
| 2818 | } else { |
| 2819 | @unlink($signed); |
| 2820 | throw new Exception($this->lang('signing') . openssl_error_string()); |
| 2821 | } |
| 2822 | } catch (Exception $exc) { |
| 2823 | $body = ''; |
| 2824 | if ($this->exceptions) { |
| 2825 | throw $exc; |
| 2826 | } |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | return $body; |
| 2831 | } |
| 2832 | |
| 2833 | /** |
| 2834 | * Return the start of a message boundary. |
| 2835 | * |
| 2836 | * @param string $boundary |
| 2837 | * @param string $charSet |
| 2838 | * @param string $contentType |
| 2839 | * @param string $encoding |
| 2840 | * |
| 2841 | * @return string |
| 2842 | */ |
| 2843 | protected function getBoundary($boundary, $charSet, $contentType, $encoding) |
| 2844 | { |
| 2845 | $result = ''; |
| 2846 | if ('' === $charSet) { |
| 2847 | $charSet = $this->CharSet; |
| 2848 | } |
| 2849 | if ('' === $contentType) { |
| 2850 | $contentType = $this->ContentType; |
| 2851 | } |
| 2852 | if ('' === $encoding) { |
| 2853 | $encoding = $this->Encoding; |
| 2854 | } |
| 2855 | $result .= $this->textLine('--' . $boundary); |
| 2856 | $result .= sprintf('Content-Type: %s; charset=%s', $contentType, $charSet); |
| 2857 | $result .= static::$LE; |
| 2858 | // RFC1341 part 5 says 7bit is assumed if not specified |
| 2859 | if (static::ENCODING_7BIT !== $encoding) { |
| 2860 | $result .= $this->headerLine('Content-Transfer-Encoding', $encoding); |
| 2861 | } |
| 2862 | $result .= static::$LE; |
| 2863 | |
| 2864 | return $result; |
| 2865 | } |
| 2866 | |
| 2867 | /** |
| 2868 | * Return the end of a message boundary. |
| 2869 | * |
| 2870 | * @param string $boundary |
| 2871 | * |
| 2872 | * @return string |
| 2873 | */ |
| 2874 | protected function endBoundary($boundary) |
| 2875 | { |
| 2876 | return static::$LE . '--' . $boundary . '--' . static::$LE; |
| 2877 | } |
| 2878 | |
| 2879 | /** |
| 2880 | * Set the message type. |
| 2881 | * PHPMailer only supports some preset message types, not arbitrary MIME structures. |
| 2882 | */ |
| 2883 | protected function setMessageType() |
| 2884 | { |
| 2885 | $type = []; |
| 2886 | if ($this->alternativeExists()) { |
| 2887 | $type[] = 'alt'; |
| 2888 | } |
| 2889 | if ($this->inlineImageExists()) { |
| 2890 | $type[] = 'inline'; |
| 2891 | } |
| 2892 | if ($this->attachmentExists()) { |
| 2893 | $type[] = 'attach'; |
| 2894 | } |
| 2895 | $this->message_type = implode('_', $type); |
| 2896 | if ('' === $this->message_type) { |
| 2897 | //The 'plain' message_type refers to the message having a single body element, not that it is plain-text |
| 2898 | $this->message_type = 'plain'; |
| 2899 | } |
| 2900 | } |
| 2901 | |
| 2902 | /** |
| 2903 | * Format a header line. |
| 2904 | * |
| 2905 | * @param string $name |
| 2906 | * @param string|int $value |
| 2907 | * |
| 2908 | * @return string |
| 2909 | */ |
| 2910 | public function headerLine($name, $value) |
| 2911 | { |
| 2912 | return $name . ': ' . $value . static::$LE; |
| 2913 | } |
| 2914 | |
| 2915 | /** |
| 2916 | * Return a formatted mail line. |
| 2917 | * |
| 2918 | * @param string $value |
| 2919 | * |
| 2920 | * @return string |
| 2921 | */ |
| 2922 | public function textLine($value) |
| 2923 | { |
| 2924 | return $value . static::$LE; |
| 2925 | } |
| 2926 | |
| 2927 | /** |
| 2928 | * Add an attachment from a path on the filesystem. |
| 2929 | * Never use a user-supplied path to a file! |
| 2930 | * Returns false if the file could not be found or read. |
| 2931 | * Explicitly *does not* support passing URLs; PHPMailer is not an HTTP client. |
| 2932 | * If you need to do that, fetch the resource yourself and pass it in via a local file or string. |
| 2933 | * |
| 2934 | * @param string $path Path to the attachment |
| 2935 | * @param string $name Overrides the attachment name |
| 2936 | * @param string $encoding File encoding (see $Encoding) |
| 2937 | * @param string $type File extension (MIME) type |
| 2938 | * @param string $disposition Disposition to use |
| 2939 | * |
| 2940 | * @throws Exception |
| 2941 | * |
| 2942 | * @return bool |
| 2943 | */ |
| 2944 | public function addAttachment( |
| 2945 | $path, |
| 2946 | $name = '', |
| 2947 | $encoding = self::ENCODING_BASE64, |
| 2948 | $type = '', |
| 2949 | $disposition = 'attachment' |
| 2950 | ) { |
| 2951 | try { |
| 2952 | if (!static::isPermittedPath($path) || !@is_file($path)) { |
| 2953 | throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE); |
| 2954 | } |
| 2955 | |
| 2956 | // If a MIME type is not specified, try to work it out from the file name |
| 2957 | if ('' === $type) { |
| 2958 | $type = static::filenameToType($path); |
| 2959 | } |
| 2960 | |
| 2961 | $filename = (string) static::mb_pathinfo($path, PATHINFO_BASENAME); |
| 2962 | if ('' === $name) { |
| 2963 | $name = $filename; |
| 2964 | } |
| 2965 | |
| 2966 | if (!$this->validateEncoding($encoding)) { |
| 2967 | throw new Exception($this->lang('encoding') . $encoding); |
| 2968 | } |
| 2969 | |
| 2970 | $this->attachment[] = [ |
| 2971 | 0 => $path, |
| 2972 | 1 => $filename, |
| 2973 | 2 => $name, |
| 2974 | 3 => $encoding, |
| 2975 | 4 => $type, |
| 2976 | 5 => false, // isStringAttachment |
| 2977 | 6 => $disposition, |
| 2978 | 7 => $name, |
| 2979 | ]; |
| 2980 | } catch (Exception $exc) { |
| 2981 | $this->setError($exc->getMessage()); |
| 2982 | $this->edebug($exc->getMessage()); |
| 2983 | if ($this->exceptions) { |
| 2984 | throw $exc; |
| 2985 | } |
| 2986 | |
| 2987 | return false; |
| 2988 | } |
| 2989 | |
| 2990 | return true; |
| 2991 | } |
| 2992 | |
| 2993 | /** |
| 2994 | * Return the array of attachments. |
| 2995 | * |
| 2996 | * @return array |
| 2997 | */ |
| 2998 | public function getAttachments() |
| 2999 | { |
| 3000 | return $this->attachment; |
| 3001 | } |
| 3002 | |
| 3003 | /** |
| 3004 | * Attach all file, string, and binary attachments to the message. |
| 3005 | * Returns an empty string on failure. |
| 3006 | * |
| 3007 | * @param string $disposition_type |
| 3008 | * @param string $boundary |
| 3009 | * |
| 3010 | * @throws Exception |
| 3011 | * |
| 3012 | * @return string |
| 3013 | */ |
| 3014 | protected function attachAll($disposition_type, $boundary) |
| 3015 | { |
| 3016 | // Return text of body |
| 3017 | $mime = []; |
| 3018 | $cidUniq = []; |
| 3019 | $incl = []; |
| 3020 | |
| 3021 | // Add all attachments |
| 3022 | foreach ($this->attachment as $attachment) { |
| 3023 | // Check if it is a valid disposition_filter |
| 3024 | if ($attachment[6] === $disposition_type) { |
| 3025 | // Check for string attachment |
| 3026 | $string = ''; |
| 3027 | $path = ''; |
| 3028 | $bString = $attachment[5]; |
| 3029 | if ($bString) { |
| 3030 | $string = $attachment[0]; |
| 3031 | } else { |
| 3032 | $path = $attachment[0]; |
| 3033 | } |
| 3034 | |
| 3035 | $inclhash = hash('sha256', serialize($attachment)); |
| 3036 | if (in_array($inclhash, $incl, true)) { |
| 3037 | continue; |
| 3038 | } |
| 3039 | $incl[] = $inclhash; |
| 3040 | $name = $attachment[2]; |
| 3041 | $encoding = $attachment[3]; |
| 3042 | $type = $attachment[4]; |
| 3043 | $disposition = $attachment[6]; |
| 3044 | $cid = $attachment[7]; |
| 3045 | if ('inline' === $disposition && array_key_exists($cid, $cidUniq)) { |
| 3046 | continue; |
| 3047 | } |
| 3048 | $cidUniq[$cid] = true; |
| 3049 | |
| 3050 | $mime[] = sprintf('--%s%s', $boundary, static::$LE); |
| 3051 | //Only include a filename property if we have one |
| 3052 | if (!empty($name)) { |
| 3053 | $mime[] = sprintf( |
| 3054 | 'Content-Type: %s; name="%s"%s', |
| 3055 | $type, |
| 3056 | $this->encodeHeader($this->secureHeader($name)), |
| 3057 | static::$LE |
| 3058 | ); |
| 3059 | } else { |
| 3060 | $mime[] = sprintf( |
| 3061 | 'Content-Type: %s%s', |
| 3062 | $type, |
| 3063 | static::$LE |
| 3064 | ); |
| 3065 | } |
| 3066 | // RFC1341 part 5 says 7bit is assumed if not specified |
| 3067 | if (static::ENCODING_7BIT !== $encoding) { |
| 3068 | $mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, static::$LE); |
| 3069 | } |
| 3070 | |
| 3071 | //Only set Content-IDs on inline attachments |
| 3072 | if ((string) $cid !== '' && $disposition === 'inline') { |
| 3073 | $mime[] = 'Content-ID: <' . $this->encodeHeader($this->secureHeader($cid)) . '>' . static::$LE; |
| 3074 | } |
| 3075 | |
| 3076 | // If a filename contains any of these chars, it should be quoted, |
| 3077 | // but not otherwise: RFC2183 & RFC2045 5.1 |
| 3078 | // Fixes a warning in IETF's msglint MIME checker |
| 3079 | // Allow for bypassing the Content-Disposition header totally |
| 3080 | if (!empty($disposition)) { |
| 3081 | $encoded_name = $this->encodeHeader($this->secureHeader($name)); |
| 3082 | if (preg_match('/[ ()<>@,;:"\/\[\]?=]/', $encoded_name)) { |
| 3083 | $mime[] = sprintf( |
| 3084 | 'Content-Disposition: %s; filename="%s"%s', |
| 3085 | $disposition, |
| 3086 | $encoded_name, |
| 3087 | static::$LE . static::$LE |
| 3088 | ); |
| 3089 | } elseif (!empty($encoded_name)) { |
| 3090 | $mime[] = sprintf( |
| 3091 | 'Content-Disposition: %s; filename=%s%s', |
| 3092 | $disposition, |
| 3093 | $encoded_name, |
| 3094 | static::$LE . static::$LE |
| 3095 | ); |
| 3096 | } else { |
| 3097 | $mime[] = sprintf( |
| 3098 | 'Content-Disposition: %s%s', |
| 3099 | $disposition, |
| 3100 | static::$LE . static::$LE |
| 3101 | ); |
| 3102 | } |
| 3103 | } else { |
| 3104 | $mime[] = static::$LE; |
| 3105 | } |
| 3106 | |
| 3107 | // Encode as string attachment |
| 3108 | if ($bString) { |
| 3109 | $mime[] = $this->encodeString($string, $encoding); |
| 3110 | } else { |
| 3111 | $mime[] = $this->encodeFile($path, $encoding); |
| 3112 | } |
| 3113 | if ($this->isError()) { |
| 3114 | return ''; |
| 3115 | } |
| 3116 | $mime[] = static::$LE; |
| 3117 | } |
| 3118 | } |
| 3119 | |
| 3120 | $mime[] = sprintf('--%s--%s', $boundary, static::$LE); |
| 3121 | |
| 3122 | return implode('', $mime); |
| 3123 | } |
| 3124 | |
| 3125 | /** |
| 3126 | * Encode a file attachment in requested format. |
| 3127 | * Returns an empty string on failure. |
| 3128 | * |
| 3129 | * @param string $path The full path to the file |
| 3130 | * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' |
| 3131 | * |
| 3132 | * @return string |
| 3133 | */ |
| 3134 | protected function encodeFile($path, $encoding = self::ENCODING_BASE64) |
| 3135 | { |
| 3136 | try { |
| 3137 | if (!static::isPermittedPath($path) || !file_exists($path)) { |
| 3138 | throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE); |
| 3139 | } |
| 3140 | $file_buffer = file_get_contents($path); |
| 3141 | if (false === $file_buffer) { |
| 3142 | throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE); |
| 3143 | } |
| 3144 | $file_buffer = $this->encodeString($file_buffer, $encoding); |
| 3145 | |
| 3146 | return $file_buffer; |
| 3147 | } catch (Exception $exc) { |
| 3148 | $this->setError($exc->getMessage()); |
| 3149 | |
| 3150 | return ''; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | /** |
| 3155 | * Encode a string in requested format. |
| 3156 | * Returns an empty string on failure. |
| 3157 | * |
| 3158 | * @param string $str The text to encode |
| 3159 | * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' |
| 3160 | * |
| 3161 | * @throws Exception |
| 3162 | * |
| 3163 | * @return string |
| 3164 | */ |
| 3165 | public function encodeString($str, $encoding = self::ENCODING_BASE64) |
| 3166 | { |
| 3167 | $encoded = ''; |
| 3168 | switch (strtolower($encoding)) { |
| 3169 | case static::ENCODING_BASE64: |
| 3170 | $encoded = chunk_split( |
| 3171 | base64_encode($str), |
| 3172 | static::STD_LINE_LENGTH, |
| 3173 | static::$LE |
| 3174 | ); |
| 3175 | break; |
| 3176 | case static::ENCODING_7BIT: |
| 3177 | case static::ENCODING_8BIT: |
| 3178 | $encoded = static::normalizeBreaks($str); |
| 3179 | // Make sure it ends with a line break |
| 3180 | if (substr($encoded, -(strlen(static::$LE))) !== static::$LE) { |
| 3181 | $encoded .= static::$LE; |
| 3182 | } |
| 3183 | break; |
| 3184 | case static::ENCODING_BINARY: |
| 3185 | $encoded = $str; |
| 3186 | break; |
| 3187 | case static::ENCODING_QUOTED_PRINTABLE: |
| 3188 | $encoded = $this->encodeQP($str); |
| 3189 | break; |
| 3190 | default: |
| 3191 | $this->setError($this->lang('encoding') . $encoding); |
| 3192 | if ($this->exceptions) { |
| 3193 | throw new Exception($this->lang('encoding') . $encoding); |
| 3194 | } |
| 3195 | break; |
| 3196 | } |
| 3197 | |
| 3198 | return $encoded; |
| 3199 | } |
| 3200 | |
| 3201 | /** |
| 3202 | * Encode a header value (not including its label) optimally. |
| 3203 | * Picks shortest of Q, B, or none. Result includes folding if needed. |
| 3204 | * See RFC822 definitions for phrase, comment and text positions. |
| 3205 | * |
| 3206 | * @param string $str The header value to encode |
| 3207 | * @param string $position What context the string will be used in |
| 3208 | * |
| 3209 | * @return string |
| 3210 | */ |
| 3211 | public function encodeHeader($str, $position = 'text') |
| 3212 | { |
| 3213 | $matchcount = 0; |
| 3214 | switch (strtolower($position)) { |
| 3215 | case 'phrase': |
| 3216 | if (!preg_match('/[\200-\377]/', $str)) { |
| 3217 | // Can't use addslashes as we don't know the value of magic_quotes_sybase |
| 3218 | $encoded = addcslashes($str, "\0..\37\177\\\""); |
| 3219 | if (($str === $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) { |
| 3220 | return $encoded; |
| 3221 | } |
| 3222 | |
| 3223 | return "\"$encoded\""; |
| 3224 | } |
| 3225 | $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches); |
| 3226 | break; |
| 3227 | /* @noinspection PhpMissingBreakStatementInspection */ |
| 3228 | case 'comment': |
| 3229 | $matchcount = preg_match_all('/[()"]/', $str, $matches); |
| 3230 | //fallthrough |
| 3231 | case 'text': |
| 3232 | default: |
| 3233 | $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); |
| 3234 | break; |
| 3235 | } |
| 3236 | |
| 3237 | if ($this->has8bitChars($str)) { |
| 3238 | $charset = $this->CharSet; |
| 3239 | } else { |
| 3240 | $charset = static::CHARSET_ASCII; |
| 3241 | } |
| 3242 | |
| 3243 | // Q/B encoding adds 8 chars and the charset ("` =?<charset>?[QB]?<content>?=`"). |
| 3244 | $overhead = 8 + strlen($charset); |
| 3245 | |
| 3246 | if ('mail' === $this->Mailer) { |
| 3247 | $maxlen = static::MAIL_MAX_LINE_LENGTH - $overhead; |
| 3248 | } else { |
| 3249 | $maxlen = static::MAX_LINE_LENGTH - $overhead; |
| 3250 | } |
| 3251 | |
| 3252 | // Select the encoding that produces the shortest output and/or prevents corruption. |
| 3253 | if ($matchcount > strlen($str) / 3) { |
| 3254 | // More than 1/3 of the content needs encoding, use B-encode. |
| 3255 | $encoding = 'B'; |
| 3256 | } elseif ($matchcount > 0) { |
| 3257 | // Less than 1/3 of the content needs encoding, use Q-encode. |
| 3258 | $encoding = 'Q'; |
| 3259 | } elseif (strlen($str) > $maxlen) { |
| 3260 | // No encoding needed, but value exceeds max line length, use Q-encode to prevent corruption. |
| 3261 | $encoding = 'Q'; |
| 3262 | } else { |
| 3263 | // No reformatting needed |
| 3264 | $encoding = false; |
| 3265 | } |
| 3266 | |
| 3267 | switch ($encoding) { |
| 3268 | case 'B': |
| 3269 | if ($this->hasMultiBytes($str)) { |
| 3270 | // Use a custom function which correctly encodes and wraps long |
| 3271 | // multibyte strings without breaking lines within a character |
| 3272 | $encoded = $this->base64EncodeWrapMB($str, "\n"); |
| 3273 | } else { |
| 3274 | $encoded = base64_encode($str); |
| 3275 | $maxlen -= $maxlen % 4; |
| 3276 | $encoded = trim(chunk_split($encoded, $maxlen, "\n")); |
| 3277 | } |
| 3278 | $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded); |
| 3279 | break; |
| 3280 | case 'Q': |
| 3281 | $encoded = $this->encodeQ($str, $position); |
| 3282 | $encoded = $this->wrapText($encoded, $maxlen, true); |
| 3283 | $encoded = str_replace('=' . static::$LE, "\n", trim($encoded)); |
| 3284 | $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded); |
| 3285 | break; |
| 3286 | default: |
| 3287 | return $str; |
| 3288 | } |
| 3289 | |
| 3290 | return trim(static::normalizeBreaks($encoded)); |
| 3291 | } |
| 3292 | |
| 3293 | /** |
| 3294 | * Check if a string contains multi-byte characters. |
| 3295 | * |
| 3296 | * @param string $str multi-byte text to wrap encode |
| 3297 | * |
| 3298 | * @return bool |
| 3299 | */ |
| 3300 | public function hasMultiBytes($str) |
| 3301 | { |
| 3302 | if (function_exists('mb_strlen')) { |
| 3303 | return strlen($str) > mb_strlen($str, $this->CharSet); |
| 3304 | } |
| 3305 | |
| 3306 | // Assume no multibytes (we can't handle without mbstring functions anyway) |
| 3307 | return false; |
| 3308 | } |
| 3309 | |
| 3310 | /** |
| 3311 | * Does a string contain any 8-bit chars (in any charset)? |
| 3312 | * |
| 3313 | * @param string $text |
| 3314 | * |
| 3315 | * @return bool |
| 3316 | */ |
| 3317 | public function has8bitChars($text) |
| 3318 | { |
| 3319 | return (bool) preg_match('/[\x80-\xFF]/', $text); |
| 3320 | } |
| 3321 | |
| 3322 | /** |
| 3323 | * Encode and wrap long multibyte strings for mail headers |
| 3324 | * without breaking lines within a character. |
| 3325 | * Adapted from a function by paravoid. |
| 3326 | * |
| 3327 | * @see http://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283 |
| 3328 | * |
| 3329 | * @param string $str multi-byte text to wrap encode |
| 3330 | * @param string $linebreak string to use as linefeed/end-of-line |
| 3331 | * |
| 3332 | * @return string |
| 3333 | */ |
| 3334 | public function base64EncodeWrapMB($str, $linebreak = null) |
| 3335 | { |
| 3336 | $start = '=?' . $this->CharSet . '?B?'; |
| 3337 | $end = '?='; |
| 3338 | $encoded = ''; |
| 3339 | if (null === $linebreak) { |
| 3340 | $linebreak = static::$LE; |
| 3341 | } |
| 3342 | |
| 3343 | $mb_length = mb_strlen($str, $this->CharSet); |
| 3344 | // Each line must have length <= 75, including $start and $end |
| 3345 | $length = 75 - strlen($start) - strlen($end); |
| 3346 | // Average multi-byte ratio |
| 3347 | $ratio = $mb_length / strlen($str); |
| 3348 | // Base64 has a 4:3 ratio |
| 3349 | $avgLength = floor($length * $ratio * .75); |
| 3350 | |
| 3351 | $offset = 0; |
| 3352 | for ($i = 0; $i < $mb_length; $i += $offset) { |
| 3353 | $lookBack = 0; |
| 3354 | do { |
| 3355 | $offset = $avgLength - $lookBack; |
| 3356 | $chunk = mb_substr($str, $i, $offset, $this->CharSet); |
| 3357 | $chunk = base64_encode($chunk); |
| 3358 | ++$lookBack; |
| 3359 | } while (strlen($chunk) > $length); |
| 3360 | $encoded .= $chunk . $linebreak; |
| 3361 | } |
| 3362 | |
| 3363 | // Chomp the last linefeed |
| 3364 | return substr($encoded, 0, -strlen($linebreak)); |
| 3365 | } |
| 3366 | |
| 3367 | /** |
| 3368 | * Encode a string in quoted-printable format. |
| 3369 | * According to RFC2045 section 6.7. |
| 3370 | * |
| 3371 | * @param string $string The text to encode |
| 3372 | * |
| 3373 | * @return string |
| 3374 | */ |
| 3375 | public function encodeQP($string) |
| 3376 | { |
| 3377 | return static::normalizeBreaks(quoted_printable_encode($string)); |
| 3378 | } |
| 3379 | |
| 3380 | /** |
| 3381 | * Encode a string using Q encoding. |
| 3382 | * |
| 3383 | * @see http://tools.ietf.org/html/rfc2047#section-4.2 |
| 3384 | * |
| 3385 | * @param string $str the text to encode |
| 3386 | * @param string $position Where the text is going to be used, see the RFC for what that means |
| 3387 | * |
| 3388 | * @return string |
| 3389 | */ |
| 3390 | public function encodeQ($str, $position = 'text') |
| 3391 | { |
| 3392 | // There should not be any EOL in the string |
| 3393 | $pattern = ''; |
| 3394 | $encoded = str_replace(["\r", "\n"], '', $str); |
| 3395 | switch (strtolower($position)) { |
| 3396 | case 'phrase': |
| 3397 | // RFC 2047 section 5.3 |
| 3398 | $pattern = '^A-Za-z0-9!*+\/ -'; |
| 3399 | break; |
| 3400 | /* |
| 3401 | * RFC 2047 section 5.2. |
| 3402 | * Build $pattern without including delimiters and [] |
| 3403 | */ |
| 3404 | /* @noinspection PhpMissingBreakStatementInspection */ |
| 3405 | case 'comment': |
| 3406 | $pattern = '\(\)"'; |
| 3407 | /* Intentional fall through */ |
| 3408 | case 'text': |
| 3409 | default: |
| 3410 | // RFC 2047 section 5.1 |
| 3411 | // Replace every high ascii, control, =, ? and _ characters |
| 3412 | $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern; |
| 3413 | break; |
| 3414 | } |
| 3415 | $matches = []; |
| 3416 | if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) { |
| 3417 | // If the string contains an '=', make sure it's the first thing we replace |
| 3418 | // so as to avoid double-encoding |
| 3419 | $eqkey = array_search('=', $matches[0], true); |
| 3420 | if (false !== $eqkey) { |
| 3421 | unset($matches[0][$eqkey]); |
| 3422 | array_unshift($matches[0], '='); |
| 3423 | } |
| 3424 | foreach (array_unique($matches[0]) as $char) { |
| 3425 | $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded); |
| 3426 | } |
| 3427 | } |
| 3428 | // Replace spaces with _ (more readable than =20) |
| 3429 | // RFC 2047 section 4.2(2) |
| 3430 | return str_replace(' ', '_', $encoded); |
| 3431 | } |
| 3432 | |
| 3433 | /** |
| 3434 | * Add a string or binary attachment (non-filesystem). |
| 3435 | * This method can be used to attach ascii or binary data, |
| 3436 | * such as a BLOB record from a database. |
| 3437 | * |
| 3438 | * @param string $string String attachment data |
| 3439 | * @param string $filename Name of the attachment |
| 3440 | * @param string $encoding File encoding (see $Encoding) |
| 3441 | * @param string $type File extension (MIME) type |
| 3442 | * @param string $disposition Disposition to use |
| 3443 | * |
| 3444 | * @throws Exception |
| 3445 | * |
| 3446 | * @return bool True on successfully adding an attachment |
| 3447 | */ |
| 3448 | public function addStringAttachment( |
| 3449 | $string, |
| 3450 | $filename, |
| 3451 | $encoding = self::ENCODING_BASE64, |
| 3452 | $type = '', |
| 3453 | $disposition = 'attachment' |
| 3454 | ) { |
| 3455 | try { |
| 3456 | // If a MIME type is not specified, try to work it out from the file name |
| 3457 | if ('' === $type) { |
| 3458 | $type = static::filenameToType($filename); |
| 3459 | } |
| 3460 | |
| 3461 | if (!$this->validateEncoding($encoding)) { |
| 3462 | throw new Exception($this->lang('encoding') . $encoding); |
| 3463 | } |
| 3464 | |
| 3465 | // Append to $attachment array |
| 3466 | $this->attachment[] = [ |
| 3467 | 0 => $string, |
| 3468 | 1 => $filename, |
| 3469 | 2 => static::mb_pathinfo($filename, PATHINFO_BASENAME), |
| 3470 | 3 => $encoding, |
| 3471 | 4 => $type, |
| 3472 | 5 => true, // isStringAttachment |
| 3473 | 6 => $disposition, |
| 3474 | 7 => 0, |
| 3475 | ]; |
| 3476 | } catch (Exception $exc) { |
| 3477 | $this->setError($exc->getMessage()); |
| 3478 | $this->edebug($exc->getMessage()); |
| 3479 | if ($this->exceptions) { |
| 3480 | throw $exc; |
| 3481 | } |
| 3482 | |
| 3483 | return false; |
| 3484 | } |
| 3485 | |
| 3486 | return true; |
| 3487 | } |
| 3488 | |
| 3489 | /** |
| 3490 | * Add an embedded (inline) attachment from a file. |
| 3491 | * This can include images, sounds, and just about any other document type. |
| 3492 | * These differ from 'regular' attachments in that they are intended to be |
| 3493 | * displayed inline with the message, not just attached for download. |
| 3494 | * This is used in HTML messages that embed the images |
| 3495 | * the HTML refers to using the $cid value. |
| 3496 | * Never use a user-supplied path to a file! |
| 3497 | * |
| 3498 | * @param string $path Path to the attachment |
| 3499 | * @param string $cid Content ID of the attachment; Use this to reference |
| 3500 | * the content when using an embedded image in HTML |
| 3501 | * @param string $name Overrides the attachment name |
| 3502 | * @param string $encoding File encoding (see $Encoding) |
| 3503 | * @param string $type File MIME type |
| 3504 | * @param string $disposition Disposition to use |
| 3505 | * |
| 3506 | * @throws Exception |
| 3507 | * |
| 3508 | * @return bool True on successfully adding an attachment |
| 3509 | */ |
| 3510 | public function addEmbeddedImage( |
| 3511 | $path, |
| 3512 | $cid, |
| 3513 | $name = '', |
| 3514 | $encoding = self::ENCODING_BASE64, |
| 3515 | $type = '', |
| 3516 | $disposition = 'inline' |
| 3517 | ) { |
| 3518 | try { |
| 3519 | if (!static::isPermittedPath($path) || !@is_file($path)) { |
| 3520 | throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE); |
| 3521 | } |
| 3522 | |
| 3523 | // If a MIME type is not specified, try to work it out from the file name |
| 3524 | if ('' === $type) { |
| 3525 | $type = static::filenameToType($path); |
| 3526 | } |
| 3527 | |
| 3528 | if (!$this->validateEncoding($encoding)) { |
| 3529 | throw new Exception($this->lang('encoding') . $encoding); |
| 3530 | } |
| 3531 | |
| 3532 | $filename = (string) static::mb_pathinfo($path, PATHINFO_BASENAME); |
| 3533 | if ('' === $name) { |
| 3534 | $name = $filename; |
| 3535 | } |
| 3536 | |
| 3537 | // Append to $attachment array |
| 3538 | $this->attachment[] = [ |
| 3539 | 0 => $path, |
| 3540 | 1 => $filename, |
| 3541 | 2 => $name, |
| 3542 | 3 => $encoding, |
| 3543 | 4 => $type, |
| 3544 | 5 => false, // isStringAttachment |
| 3545 | 6 => $disposition, |
| 3546 | 7 => $cid, |
| 3547 | ]; |
| 3548 | } catch (Exception $exc) { |
| 3549 | $this->setError($exc->getMessage()); |
| 3550 | $this->edebug($exc->getMessage()); |
| 3551 | if ($this->exceptions) { |
| 3552 | throw $exc; |
| 3553 | } |
| 3554 | |
| 3555 | return false; |
| 3556 | } |
| 3557 | |
| 3558 | return true; |
| 3559 | } |
| 3560 | |
| 3561 | /** |
| 3562 | * Add an embedded stringified attachment. |
| 3563 | * This can include images, sounds, and just about any other document type. |
| 3564 | * If your filename doesn't contain an extension, be sure to set the $type to an appropriate MIME type. |
| 3565 | * |
| 3566 | * @param string $string The attachment binary data |
| 3567 | * @param string $cid Content ID of the attachment; Use this to reference |
| 3568 | * the content when using an embedded image in HTML |
| 3569 | * @param string $name A filename for the attachment. If this contains an extension, |
| 3570 | * PHPMailer will attempt to set a MIME type for the attachment. |
| 3571 | * For example 'file.jpg' would get an 'image/jpeg' MIME type. |
| 3572 | * @param string $encoding File encoding (see $Encoding), defaults to 'base64' |
| 3573 | * @param string $type MIME type - will be used in preference to any automatically derived type |
| 3574 | * @param string $disposition Disposition to use |
| 3575 | * |
| 3576 | * @throws Exception |
| 3577 | * |
| 3578 | * @return bool True on successfully adding an attachment |
| 3579 | */ |
| 3580 | public function addStringEmbeddedImage( |
| 3581 | $string, |
| 3582 | $cid, |
| 3583 | $name = '', |
| 3584 | $encoding = self::ENCODING_BASE64, |
| 3585 | $type = '', |
| 3586 | $disposition = 'inline' |
| 3587 | ) { |
| 3588 | try { |
| 3589 | // If a MIME type is not specified, try to work it out from the name |
| 3590 | if ('' === $type && !empty($name)) { |
| 3591 | $type = static::filenameToType($name); |
| 3592 | } |
| 3593 | |
| 3594 | if (!$this->validateEncoding($encoding)) { |
| 3595 | throw new Exception($this->lang('encoding') . $encoding); |
| 3596 | } |
| 3597 | |
| 3598 | // Append to $attachment array |
| 3599 | $this->attachment[] = [ |
| 3600 | 0 => $string, |
| 3601 | 1 => $name, |
| 3602 | 2 => $name, |
| 3603 | 3 => $encoding, |
| 3604 | 4 => $type, |
| 3605 | 5 => true, // isStringAttachment |
| 3606 | 6 => $disposition, |
| 3607 | 7 => $cid, |
| 3608 | ]; |
| 3609 | } catch (Exception $exc) { |
| 3610 | $this->setError($exc->getMessage()); |
| 3611 | $this->edebug($exc->getMessage()); |
| 3612 | if ($this->exceptions) { |
| 3613 | throw $exc; |
| 3614 | } |
| 3615 | |
| 3616 | return false; |
| 3617 | } |
| 3618 | |
| 3619 | return true; |
| 3620 | } |
| 3621 | |
| 3622 | /** |
| 3623 | * Validate encodings. |
| 3624 | * |
| 3625 | * @param string $encoding |
| 3626 | * |
| 3627 | * @return bool |
| 3628 | */ |
| 3629 | protected function validateEncoding($encoding) |
| 3630 | { |
| 3631 | return in_array( |
| 3632 | $encoding, |
| 3633 | [ |
| 3634 | self::ENCODING_7BIT, |
| 3635 | self::ENCODING_QUOTED_PRINTABLE, |
| 3636 | self::ENCODING_BASE64, |
| 3637 | self::ENCODING_8BIT, |
| 3638 | self::ENCODING_BINARY, |
| 3639 | ], |
| 3640 | true |
| 3641 | ); |
| 3642 | } |
| 3643 | |
| 3644 | /** |
| 3645 | * Check if an embedded attachment is present with this cid. |
| 3646 | * |
| 3647 | * @param string $cid |
| 3648 | * |
| 3649 | * @return bool |
| 3650 | */ |
| 3651 | protected function cidExists($cid) |
| 3652 | { |
| 3653 | foreach ($this->attachment as $attachment) { |
| 3654 | if ('inline' === $attachment[6] && $cid === $attachment[7]) { |
| 3655 | return true; |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | return false; |
| 3660 | } |
| 3661 | |
| 3662 | /** |
| 3663 | * Check if an inline attachment is present. |
| 3664 | * |
| 3665 | * @return bool |
| 3666 | */ |
| 3667 | public function inlineImageExists() |
| 3668 | { |
| 3669 | foreach ($this->attachment as $attachment) { |
| 3670 | if ('inline' === $attachment[6]) { |
| 3671 | return true; |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | return false; |
| 3676 | } |
| 3677 | |
| 3678 | /** |
| 3679 | * Check if an attachment (non-inline) is present. |
| 3680 | * |
| 3681 | * @return bool |
| 3682 | */ |
| 3683 | public function attachmentExists() |
| 3684 | { |
| 3685 | foreach ($this->attachment as $attachment) { |
| 3686 | if ('attachment' === $attachment[6]) { |
| 3687 | return true; |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | return false; |
| 3692 | } |
| 3693 | |
| 3694 | /** |
| 3695 | * Check if this message has an alternative body set. |
| 3696 | * |
| 3697 | * @return bool |
| 3698 | */ |
| 3699 | public function alternativeExists() |
| 3700 | { |
| 3701 | return !empty($this->AltBody); |
| 3702 | } |
| 3703 | |
| 3704 | /** |
| 3705 | * Clear queued addresses of given kind. |
| 3706 | * |
| 3707 | * @param string $kind 'to', 'cc', or 'bcc' |
| 3708 | */ |
| 3709 | public function clearQueuedAddresses($kind) |
| 3710 | { |
| 3711 | $this->RecipientsQueue = array_filter( |
| 3712 | $this->RecipientsQueue, |
| 3713 | static function ($params) use ($kind) { |
| 3714 | return $params[0] !== $kind; |
| 3715 | } |
| 3716 | ); |
| 3717 | } |
| 3718 | |
| 3719 | /** |
| 3720 | * Clear all To recipients. |
| 3721 | */ |
| 3722 | public function clearAddresses() |
| 3723 | { |
| 3724 | foreach ($this->to as $to) { |
| 3725 | unset($this->all_recipients[strtolower($to[0])]); |
| 3726 | } |
| 3727 | $this->to = []; |
| 3728 | $this->clearQueuedAddresses('to'); |
| 3729 | } |
| 3730 | |
| 3731 | /** |
| 3732 | * Clear all CC recipients. |
| 3733 | */ |
| 3734 | public function clearCCs() |
| 3735 | { |
| 3736 | foreach ($this->cc as $cc) { |
| 3737 | unset($this->all_recipients[strtolower($cc[0])]); |
| 3738 | } |
| 3739 | $this->cc = []; |
| 3740 | $this->clearQueuedAddresses('cc'); |
| 3741 | } |
| 3742 | |
| 3743 | /** |
| 3744 | * Clear all BCC recipients. |
| 3745 | */ |
| 3746 | public function clearBCCs() |
| 3747 | { |
| 3748 | foreach ($this->bcc as $bcc) { |
| 3749 | unset($this->all_recipients[strtolower($bcc[0])]); |
| 3750 | } |
| 3751 | $this->bcc = []; |
| 3752 | $this->clearQueuedAddresses('bcc'); |
| 3753 | } |
| 3754 | |
| 3755 | /** |
| 3756 | * Clear all ReplyTo recipients. |
| 3757 | */ |
| 3758 | public function clearReplyTos() |
| 3759 | { |
| 3760 | $this->ReplyTo = []; |
| 3761 | $this->ReplyToQueue = []; |
| 3762 | } |
| 3763 | |
| 3764 | /** |
| 3765 | * Clear all recipient types. |
| 3766 | */ |
| 3767 | public function clearAllRecipients() |
| 3768 | { |
| 3769 | $this->to = []; |
| 3770 | $this->cc = []; |
| 3771 | $this->bcc = []; |
| 3772 | $this->all_recipients = []; |
| 3773 | $this->RecipientsQueue = []; |
| 3774 | } |
| 3775 | |
| 3776 | /** |
| 3777 | * Clear all filesystem, string, and binary attachments. |
| 3778 | */ |
| 3779 | public function clearAttachments() |
| 3780 | { |
| 3781 | $this->attachment = []; |
| 3782 | } |
| 3783 | |
| 3784 | /** |
| 3785 | * Clear all custom headers. |
| 3786 | */ |
| 3787 | public function clearCustomHeaders() |
| 3788 | { |
| 3789 | $this->CustomHeader = []; |
| 3790 | } |
| 3791 | |
| 3792 | /** |
| 3793 | * Add an error message to the error container. |
| 3794 | * |
| 3795 | * @param string $msg |
| 3796 | */ |
| 3797 | protected function setError($msg) |
| 3798 | { |
| 3799 | ++$this->error_count; |
| 3800 | if ('smtp' === $this->Mailer && null !== $this->smtp) { |
| 3801 | $lasterror = $this->smtp->getError(); |
| 3802 | if (!empty($lasterror['error'])) { |
| 3803 | $msg .= $this->lang('smtp_error') . $lasterror['error']; |
| 3804 | if (!empty($lasterror['detail'])) { |
| 3805 | $msg .= ' Detail: ' . $lasterror['detail']; |
| 3806 | } |
| 3807 | if (!empty($lasterror['smtp_code'])) { |
| 3808 | $msg .= ' SMTP code: ' . $lasterror['smtp_code']; |
| 3809 | } |
| 3810 | if (!empty($lasterror['smtp_code_ex'])) { |
| 3811 | $msg .= ' Additional SMTP info: ' . $lasterror['smtp_code_ex']; |
| 3812 | } |
| 3813 | } |
| 3814 | } |
| 3815 | $this->ErrorInfo = $msg; |
| 3816 | } |
| 3817 | |
| 3818 | /** |
| 3819 | * Return an RFC 822 formatted date. |
| 3820 | * |
| 3821 | * @return string |
| 3822 | */ |
| 3823 | public static function rfcDate() |
| 3824 | { |
| 3825 | // Set the time zone to whatever the default is to avoid 500 errors |
| 3826 | // Will default to UTC if it's not set properly in php.ini |
| 3827 | date_default_timezone_set(@date_default_timezone_get()); |
| 3828 | |
| 3829 | return date('D, j M Y H:i:s O'); |
| 3830 | } |
| 3831 | |
| 3832 | /** |
| 3833 | * Get the server hostname. |
| 3834 | * Returns 'localhost.localdomain' if unknown. |
| 3835 | * |
| 3836 | * @return string |
| 3837 | */ |
| 3838 | protected function serverHostname() |
| 3839 | { |
| 3840 | $result = ''; |
| 3841 | if (!empty($this->Hostname)) { |
| 3842 | $result = $this->Hostname; |
| 3843 | } elseif (isset($_SERVER) && array_key_exists('SERVER_NAME', $_SERVER)) { |
| 3844 | $result = $_SERVER['SERVER_NAME']; |
| 3845 | } elseif (function_exists('gethostname') && gethostname() !== false) { |
| 3846 | $result = gethostname(); |
| 3847 | } elseif (php_uname('n') !== false) { |
| 3848 | $result = php_uname('n'); |
| 3849 | } |
| 3850 | if (!static::isValidHost($result)) { |
| 3851 | return 'localhost.localdomain'; |
| 3852 | } |
| 3853 | |
| 3854 | return $result; |
| 3855 | } |
| 3856 | |
| 3857 | /** |
| 3858 | * Validate whether a string contains a valid value to use as a hostname or IP address. |
| 3859 | * IPv6 addresses must include [], e.g. `[::1]`, not just `::1`. |
| 3860 | * |
| 3861 | * @param string $host The host name or IP address to check |
| 3862 | * |
| 3863 | * @return bool |
| 3864 | */ |
| 3865 | public static function isValidHost($host) |
| 3866 | { |
| 3867 | //Simple syntax limits |
| 3868 | if (empty($host) |
| 3869 | || !is_string($host) |
| 3870 | || strlen($host) > 256 |
| 3871 | || !preg_match('/^([a-zA-Z\d.-]*|\[[a-fA-F\d:]+])$/', $host) |
| 3872 | ) { |
| 3873 | return false; |
| 3874 | } |
| 3875 | //Looks like a bracketed IPv6 address |
| 3876 | if (strlen($host) > 2 && substr($host, 0, 1) === '[' && substr($host, -1, 1) === ']') { |
| 3877 | return filter_var(substr($host, 1, -1), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false; |
| 3878 | } |
| 3879 | //If removing all the dots results in a numeric string, it must be an IPv4 address. |
| 3880 | //Need to check this first because otherwise things like `999.0.0.0` are considered valid host names |
| 3881 | if (is_numeric(str_replace('.', '', $host))) { |
| 3882 | //Is it a valid IPv4 address? |
| 3883 | return filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false; |
| 3884 | } |
| 3885 | if (filter_var('http://' . $host, FILTER_VALIDATE_URL) !== false) { |
| 3886 | //Is it a syntactically valid hostname? |
| 3887 | return true; |
| 3888 | } |
| 3889 | |
| 3890 | return false; |
| 3891 | } |
| 3892 | |
| 3893 | /** |
| 3894 | * Get an error message in the current language. |
| 3895 | * |
| 3896 | * @param string $key |
| 3897 | * |
| 3898 | * @return string |
| 3899 | */ |
| 3900 | protected function lang($key) |
| 3901 | { |
| 3902 | if (count($this->language) < 1) { |
| 3903 | $this->setLanguage(); // set the default language |
| 3904 | } |
| 3905 | |
| 3906 | if (array_key_exists($key, $this->language)) { |
| 3907 | if ('smtp_connect_failed' === $key) { |
| 3908 | //Include a link to troubleshooting docs on SMTP connection failure |
| 3909 | //this is by far the biggest cause of support questions |
| 3910 | //but it's usually not PHPMailer's fault. |
| 3911 | return $this->language[$key] . ' https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting'; |
| 3912 | } |
| 3913 | |
| 3914 | return $this->language[$key]; |
| 3915 | } |
| 3916 | |
| 3917 | //Return the key as a fallback |
| 3918 | return $key; |
| 3919 | } |
| 3920 | |
| 3921 | /** |
| 3922 | * Check if an error occurred. |
| 3923 | * |
| 3924 | * @return bool True if an error did occur |
| 3925 | */ |
| 3926 | public function isError() |
| 3927 | { |
| 3928 | return $this->error_count > 0; |
| 3929 | } |
| 3930 | |
| 3931 | /** |
| 3932 | * Add a custom header. |
| 3933 | * $name value can be overloaded to contain |
| 3934 | * both header name and value (name:value). |
| 3935 | * |
| 3936 | * @param string $name Custom header name |
| 3937 | * @param string|null $value Header value |
| 3938 | */ |
| 3939 | public function addCustomHeader($name, $value = null) |
| 3940 | { |
| 3941 | if (null === $value) { |
| 3942 | // Value passed in as name:value |
| 3943 | $this->CustomHeader[] = explode(':', $name, 2); |
| 3944 | } else { |
| 3945 | $this->CustomHeader[] = [$name, $value]; |
| 3946 | } |
| 3947 | } |
| 3948 | |
| 3949 | /** |
| 3950 | * Returns all custom headers. |
| 3951 | * |
| 3952 | * @return array |
| 3953 | */ |
| 3954 | public function getCustomHeaders() |
| 3955 | { |
| 3956 | return $this->CustomHeader; |
| 3957 | } |
| 3958 | |
| 3959 | /** |
| 3960 | * Create a message body from an HTML string. |
| 3961 | * Automatically inlines images and creates a plain-text version by converting the HTML, |
| 3962 | * overwriting any existing values in Body and AltBody. |
| 3963 | * Do not source $message content from user input! |
| 3964 | * $basedir is prepended when handling relative URLs, e.g. <img src="/images/a.png"> and must not be empty |
| 3965 | * will look for an image file in $basedir/images/a.png and convert it to inline. |
| 3966 | * If you don't provide a $basedir, relative paths will be left untouched (and thus probably break in email) |
| 3967 | * Converts data-uri images into embedded attachments. |
| 3968 | * If you don't want to apply these transformations to your HTML, just set Body and AltBody directly. |
| 3969 | * |
| 3970 | * @param string $message HTML message string |
| 3971 | * @param string $basedir Absolute path to a base directory to prepend to relative paths to images |
| 3972 | * @param bool|callable $advanced Whether to use the internal HTML to text converter |
| 3973 | * or your own custom converter @return string $message The transformed message Body |
| 3974 | * |
| 3975 | * @throws Exception |
| 3976 | * |
| 3977 | * @see PHPMailer::html2text() |
| 3978 | */ |
| 3979 | public function msgHTML($message, $basedir = '', $advanced = false) |
| 3980 | { |
| 3981 | preg_match_all('/(?<!-)(src|background)=["\'](.*)["\']/Ui', $message, $images); |
| 3982 | if (array_key_exists(2, $images)) { |
| 3983 | if (strlen($basedir) > 1 && '/' !== substr($basedir, -1)) { |
| 3984 | // Ensure $basedir has a trailing / |
| 3985 | $basedir .= '/'; |
| 3986 | } |
| 3987 | foreach ($images[2] as $imgindex => $url) { |
| 3988 | // Convert data URIs into embedded images |
| 3989 | //e.g. "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" |
| 3990 | if (preg_match('#^data:(image/(?:jpe?g|gif|png));?(base64)?,(.+)#', $url, $match)) { |
| 3991 | if (count($match) === 4 && static::ENCODING_BASE64 === $match[2]) { |
| 3992 | $data = base64_decode($match[3]); |
| 3993 | } elseif ('' === $match[2]) { |
| 3994 | $data = rawurldecode($match[3]); |
| 3995 | } else { |
| 3996 | //Not recognised so leave it alone |
| 3997 | continue; |
| 3998 | } |
| 3999 | //Hash the decoded data, not the URL, so that the same data-URI image used in multiple places |
| 4000 | //will only be embedded once, even if it used a different encoding |
| 4001 | $cid = substr(hash('sha256', $data), 0, 32) . '@phpmailer.0'; // RFC2392 S 2 |
| 4002 | |
| 4003 | if (!$this->cidExists($cid)) { |
| 4004 | $this->addStringEmbeddedImage( |
| 4005 | $data, |
| 4006 | $cid, |
| 4007 | 'embed' . $imgindex, |
| 4008 | static::ENCODING_BASE64, |
| 4009 | $match[1] |
| 4010 | ); |
| 4011 | } |
| 4012 | $message = str_replace( |
| 4013 | $images[0][$imgindex], |
| 4014 | $images[1][$imgindex] . '="cid:' . $cid . '"', |
| 4015 | $message |
| 4016 | ); |
| 4017 | continue; |
| 4018 | } |
| 4019 | if (// Only process relative URLs if a basedir is provided (i.e. no absolute local paths) |
| 4020 | !empty($basedir) |
| 4021 | // Ignore URLs containing parent dir traversal (..) |
| 4022 | && (strpos($url, '..') === false) |
| 4023 | // Do not change urls that are already inline images |
| 4024 | && 0 !== strpos($url, 'cid:') |
| 4025 | // Do not change absolute URLs, including anonymous protocol |
| 4026 | && !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url) |
| 4027 | ) { |
| 4028 | $filename = static::mb_pathinfo($url, PATHINFO_BASENAME); |
| 4029 | $directory = dirname($url); |
| 4030 | if ('.' === $directory) { |
| 4031 | $directory = ''; |
| 4032 | } |
| 4033 | // RFC2392 S 2 |
| 4034 | $cid = substr(hash('sha256', $url), 0, 32) . '@phpmailer.0'; |
| 4035 | if (strlen($basedir) > 1 && '/' !== substr($basedir, -1)) { |
| 4036 | $basedir .= '/'; |
| 4037 | } |
| 4038 | if (strlen($directory) > 1 && '/' !== substr($directory, -1)) { |
| 4039 | $directory .= '/'; |
| 4040 | } |
| 4041 | if ($this->addEmbeddedImage( |
| 4042 | $basedir . $directory . $filename, |
| 4043 | $cid, |
| 4044 | $filename, |
| 4045 | static::ENCODING_BASE64, |
| 4046 | static::_mime_types((string) static::mb_pathinfo($filename, PATHINFO_EXTENSION)) |
| 4047 | ) |
| 4048 | ) { |
| 4049 | $message = preg_replace( |
| 4050 | '/' . $images[1][$imgindex] . '=["\']' . preg_quote($url, '/') . '["\']/Ui', |
| 4051 | $images[1][$imgindex] . '="cid:' . $cid . '"', |
| 4052 | $message |
| 4053 | ); |
| 4054 | } |
| 4055 | } |
| 4056 | } |
| 4057 | } |
| 4058 | $this->isHTML(); |
| 4059 | // Convert all message body line breaks to LE, makes quoted-printable encoding work much better |
| 4060 | $this->Body = static::normalizeBreaks($message); |
| 4061 | $this->AltBody = static::normalizeBreaks($this->html2text($message, $advanced)); |
| 4062 | if (!$this->alternativeExists()) { |
| 4063 | $this->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.' |
| 4064 | . static::$LE; |
| 4065 | } |
| 4066 | |
| 4067 | return $this->Body; |
| 4068 | } |
| 4069 | |
| 4070 | /** |
| 4071 | * Convert an HTML string into plain text. |
| 4072 | * This is used by msgHTML(). |
| 4073 | * Note - older versions of this function used a bundled advanced converter |
| 4074 | * which was removed for license reasons in #232. |
| 4075 | * Example usage: |
| 4076 | * |
| 4077 | * ```php |
| 4078 | * // Use default conversion |
| 4079 | * $plain = $mail->html2text($html); |
| 4080 | * // Use your own custom converter |
| 4081 | * $plain = $mail->html2text($html, function($html) { |
| 4082 | * $converter = new MyHtml2text($html); |
| 4083 | * return $converter->get_text(); |
| 4084 | * }); |
| 4085 | * ``` |
| 4086 | * |
| 4087 | * @param string $html The HTML text to convert |
| 4088 | * @param bool|callable $advanced Any boolean value to use the internal converter, |
| 4089 | * or provide your own callable for custom conversion |
| 4090 | * |
| 4091 | * @return string |
| 4092 | */ |
| 4093 | public function html2text($html, $advanced = false) |
| 4094 | { |
| 4095 | if (is_callable($advanced)) { |
| 4096 | return $advanced($html); |
| 4097 | } |
| 4098 | |
| 4099 | return html_entity_decode( |
| 4100 | trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/si', '', $html))), |
| 4101 | ENT_QUOTES, |
| 4102 | $this->CharSet |
| 4103 | ); |
| 4104 | } |
| 4105 | |
| 4106 | /** |
| 4107 | * Get the MIME type for a file extension. |
| 4108 | * |
| 4109 | * @param string $ext File extension |
| 4110 | * |
| 4111 | * @return string MIME type of file |
| 4112 | */ |
| 4113 | public static function _mime_types($ext = '') |
| 4114 | { |
| 4115 | $mimes = [ |
| 4116 | 'xl' => 'application/excel', |
| 4117 | 'js' => 'application/javascript', |
| 4118 | 'hqx' => 'application/mac-binhex40', |
| 4119 | 'cpt' => 'application/mac-compactpro', |
| 4120 | 'bin' => 'application/macbinary', |
| 4121 | 'doc' => 'application/msword', |
| 4122 | 'word' => 'application/msword', |
| 4123 | 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', |
| 4124 | 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', |
| 4125 | 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', |
| 4126 | 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', |
| 4127 | 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', |
| 4128 | 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', |
| 4129 | 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', |
| 4130 | 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', |
| 4131 | 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', |
| 4132 | 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', |
| 4133 | 'class' => 'application/octet-stream', |
| 4134 | 'dll' => 'application/octet-stream', |
| 4135 | 'dms' => 'application/octet-stream', |
| 4136 | 'exe' => 'application/octet-stream', |
| 4137 | 'lha' => 'application/octet-stream', |
| 4138 | 'lzh' => 'application/octet-stream', |
| 4139 | 'psd' => 'application/octet-stream', |
| 4140 | 'sea' => 'application/octet-stream', |
| 4141 | 'so' => 'application/octet-stream', |
| 4142 | 'oda' => 'application/oda', |
| 4143 | 'pdf' => 'application/pdf', |
| 4144 | 'ai' => 'application/postscript', |
| 4145 | 'eps' => 'application/postscript', |
| 4146 | 'ps' => 'application/postscript', |
| 4147 | 'smi' => 'application/smil', |
| 4148 | 'smil' => 'application/smil', |
| 4149 | 'mif' => 'application/vnd.mif', |
| 4150 | 'xls' => 'application/vnd.ms-excel', |
| 4151 | 'ppt' => 'application/vnd.ms-powerpoint', |
| 4152 | 'wbxml' => 'application/vnd.wap.wbxml', |
| 4153 | 'wmlc' => 'application/vnd.wap.wmlc', |
| 4154 | 'dcr' => 'application/x-director', |
| 4155 | 'dir' => 'application/x-director', |
| 4156 | 'dxr' => 'application/x-director', |
| 4157 | 'dvi' => 'application/x-dvi', |
| 4158 | 'gtar' => 'application/x-gtar', |
| 4159 | 'php3' => 'application/x-httpd-php', |
| 4160 | 'php4' => 'application/x-httpd-php', |
| 4161 | 'php' => 'application/x-httpd-php', |
| 4162 | 'phtml' => 'application/x-httpd-php', |
| 4163 | 'phps' => 'application/x-httpd-php-source', |
| 4164 | 'swf' => 'application/x-shockwave-flash', |
| 4165 | 'sit' => 'application/x-stuffit', |
| 4166 | 'tar' => 'application/x-tar', |
| 4167 | 'tgz' => 'application/x-tar', |
| 4168 | 'xht' => 'application/xhtml+xml', |
| 4169 | 'xhtml' => 'application/xhtml+xml', |
| 4170 | 'zip' => 'application/zip', |
| 4171 | 'mid' => 'audio/midi', |
| 4172 | 'midi' => 'audio/midi', |
| 4173 | 'mp2' => 'audio/mpeg', |
| 4174 | 'mp3' => 'audio/mpeg', |
| 4175 | 'm4a' => 'audio/mp4', |
| 4176 | 'mpga' => 'audio/mpeg', |
| 4177 | 'aif' => 'audio/x-aiff', |
| 4178 | 'aifc' => 'audio/x-aiff', |
| 4179 | 'aiff' => 'audio/x-aiff', |
| 4180 | 'ram' => 'audio/x-pn-realaudio', |
| 4181 | 'rm' => 'audio/x-pn-realaudio', |
| 4182 | 'rpm' => 'audio/x-pn-realaudio-plugin', |
| 4183 | 'ra' => 'audio/x-realaudio', |
| 4184 | 'wav' => 'audio/x-wav', |
| 4185 | 'mka' => 'audio/x-matroska', |
| 4186 | 'bmp' => 'image/bmp', |
| 4187 | 'gif' => 'image/gif', |
| 4188 | 'jpeg' => 'image/jpeg', |
| 4189 | 'jpe' => 'image/jpeg', |
| 4190 | 'jpg' => 'image/jpeg', |
| 4191 | 'png' => 'image/png', |
| 4192 | 'tiff' => 'image/tiff', |
| 4193 | 'tif' => 'image/tiff', |
| 4194 | 'webp' => 'image/webp', |
| 4195 | 'heif' => 'image/heif', |
| 4196 | 'heifs' => 'image/heif-sequence', |
| 4197 | 'heic' => 'image/heic', |
| 4198 | 'heics' => 'image/heic-sequence', |
| 4199 | 'eml' => 'message/rfc822', |
| 4200 | 'css' => 'text/css', |
| 4201 | 'html' => 'text/html', |
| 4202 | 'htm' => 'text/html', |
| 4203 | 'shtml' => 'text/html', |
| 4204 | 'log' => 'text/plain', |
| 4205 | 'text' => 'text/plain', |
| 4206 | 'txt' => 'text/plain', |
| 4207 | 'rtx' => 'text/richtext', |
| 4208 | 'rtf' => 'text/rtf', |
| 4209 | 'vcf' => 'text/vcard', |
| 4210 | 'vcard' => 'text/vcard', |
| 4211 | 'ics' => 'text/calendar', |
| 4212 | 'xml' => 'text/xml', |
| 4213 | 'xsl' => 'text/xml', |
| 4214 | 'wmv' => 'video/x-ms-wmv', |
| 4215 | 'mpeg' => 'video/mpeg', |
| 4216 | 'mpe' => 'video/mpeg', |
| 4217 | 'mpg' => 'video/mpeg', |
| 4218 | 'mp4' => 'video/mp4', |
| 4219 | 'm4v' => 'video/mp4', |
| 4220 | 'mov' => 'video/quicktime', |
| 4221 | 'qt' => 'video/quicktime', |
| 4222 | 'rv' => 'video/vnd.rn-realvideo', |
| 4223 | 'avi' => 'video/x-msvideo', |
| 4224 | 'movie' => 'video/x-sgi-movie', |
| 4225 | 'webm' => 'video/webm', |
| 4226 | 'mkv' => 'video/x-matroska', |
| 4227 | ]; |
| 4228 | $ext = strtolower($ext); |
| 4229 | if (array_key_exists($ext, $mimes)) { |
| 4230 | return $mimes[$ext]; |
| 4231 | } |
| 4232 | |
| 4233 | return 'application/octet-stream'; |
| 4234 | } |
| 4235 | |
| 4236 | /** |
| 4237 | * Map a file name to a MIME type. |
| 4238 | * Defaults to 'application/octet-stream', i.e.. arbitrary binary data. |
| 4239 | * |
| 4240 | * @param string $filename A file name or full path, does not need to exist as a file |
| 4241 | * |
| 4242 | * @return string |
| 4243 | */ |
| 4244 | public static function filenameToType($filename) |
| 4245 | { |
| 4246 | // In case the path is a URL, strip any query string before getting extension |
| 4247 | $qpos = strpos($filename, '?'); |
| 4248 | if (false !== $qpos) { |
| 4249 | $filename = substr($filename, 0, $qpos); |
| 4250 | } |
| 4251 | $ext = static::mb_pathinfo($filename, PATHINFO_EXTENSION); |
| 4252 | |
| 4253 | return static::_mime_types($ext); |
| 4254 | } |
| 4255 | |
| 4256 | /** |
| 4257 | * Multi-byte-safe pathinfo replacement. |
| 4258 | * Drop-in replacement for pathinfo(), but multibyte- and cross-platform-safe. |
| 4259 | * |
| 4260 | * @see http://www.php.net/manual/en/function.pathinfo.php#107461 |
| 4261 | * |
| 4262 | * @param string $path A filename or path, does not need to exist as a file |
| 4263 | * @param int|string $options Either a PATHINFO_* constant, |
| 4264 | * or a string name to return only the specified piece |
| 4265 | * |
| 4266 | * @return string|array |
| 4267 | */ |
| 4268 | public static function mb_pathinfo($path, $options = null) |
| 4269 | { |
| 4270 | $ret = ['dirname' => '', 'basename' => '', 'extension' => '', 'filename' => '']; |
| 4271 | $pathinfo = []; |
| 4272 | if (preg_match('#^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^.\\\\/]+?)|))[\\\\/.]*$#m', $path, $pathinfo)) { |
| 4273 | if (array_key_exists(1, $pathinfo)) { |
| 4274 | $ret['dirname'] = $pathinfo[1]; |
| 4275 | } |
| 4276 | if (array_key_exists(2, $pathinfo)) { |
| 4277 | $ret['basename'] = $pathinfo[2]; |
| 4278 | } |
| 4279 | if (array_key_exists(5, $pathinfo)) { |
| 4280 | $ret['extension'] = $pathinfo[5]; |
| 4281 | } |
| 4282 | if (array_key_exists(3, $pathinfo)) { |
| 4283 | $ret['filename'] = $pathinfo[3]; |
| 4284 | } |
| 4285 | } |
| 4286 | switch ($options) { |
| 4287 | case PATHINFO_DIRNAME: |
| 4288 | case 'dirname': |
| 4289 | return $ret['dirname']; |
| 4290 | case PATHINFO_BASENAME: |
| 4291 | case 'basename': |
| 4292 | return $ret['basename']; |
| 4293 | case PATHINFO_EXTENSION: |
| 4294 | case 'extension': |
| 4295 | return $ret['extension']; |
| 4296 | case PATHINFO_FILENAME: |
| 4297 | case 'filename': |
| 4298 | return $ret['filename']; |
| 4299 | default: |
| 4300 | return $ret; |
| 4301 | } |
| 4302 | } |
| 4303 | |
| 4304 | /** |
| 4305 | * Set or reset instance properties. |
| 4306 | * You should avoid this function - it's more verbose, less efficient, more error-prone and |
| 4307 | * harder to debug than setting properties directly. |
| 4308 | * Usage Example: |
| 4309 | * `$mail->set('SMTPSecure', static::ENCRYPTION_STARTTLS);` |
| 4310 | * is the same as: |
| 4311 | * `$mail->SMTPSecure = static::ENCRYPTION_STARTTLS;`. |
| 4312 | * |
| 4313 | * @param string $name The property name to set |
| 4314 | * @param mixed $value The value to set the property to |
| 4315 | * |
| 4316 | * @return bool |
| 4317 | */ |
| 4318 | public function set($name, $value = '') |
| 4319 | { |
| 4320 | if (property_exists($this, $name)) { |
| 4321 | $this->$name = $value; |
| 4322 | |
| 4323 | return true; |
| 4324 | } |
| 4325 | $this->setError($this->lang('variable_set') . $name); |
| 4326 | |
| 4327 | return false; |
| 4328 | } |
| 4329 | |
| 4330 | /** |
| 4331 | * Strip newlines to prevent header injection. |
| 4332 | * |
| 4333 | * @param string $str |
| 4334 | * |
| 4335 | * @return string |
| 4336 | */ |
| 4337 | public function secureHeader($str) |
| 4338 | { |
| 4339 | return trim(str_replace(["\r", "\n"], '', $str)); |
| 4340 | } |
| 4341 | |
| 4342 | /** |
| 4343 | * Normalize line breaks in a string. |
| 4344 | * Converts UNIX LF, Mac CR and Windows CRLF line breaks into a single line break format. |
| 4345 | * Defaults to CRLF (for message bodies) and preserves consecutive breaks. |
| 4346 | * |
| 4347 | * @param string $text |
| 4348 | * @param string $breaktype What kind of line break to use; defaults to static::$LE |
| 4349 | * |
| 4350 | * @return string |
| 4351 | */ |
| 4352 | public static function normalizeBreaks($text, $breaktype = null) |
| 4353 | { |
| 4354 | if (null === $breaktype) { |
| 4355 | $breaktype = static::$LE; |
| 4356 | } |
| 4357 | // Normalise to \n |
| 4358 | $text = str_replace(["\r\n", "\r"], "\n", $text); |
| 4359 | // Now convert LE as needed |
| 4360 | if ("\n" !== $breaktype) { |
| 4361 | $text = str_replace("\n", $breaktype, $text); |
| 4362 | } |
| 4363 | |
| 4364 | return $text; |
| 4365 | } |
| 4366 | |
| 4367 | /** |
| 4368 | * Return the current line break format string. |
| 4369 | * |
| 4370 | * @return string |
| 4371 | */ |
| 4372 | public static function getLE() |
| 4373 | { |
| 4374 | return static::$LE; |
| 4375 | } |
| 4376 | |
| 4377 | /** |
| 4378 | * Set the line break format string, e.g. "\r\n". |
| 4379 | * |
| 4380 | * @param string $le |
| 4381 | */ |
| 4382 | protected static function setLE($le) |
| 4383 | { |
| 4384 | static::$LE = $le; |
| 4385 | } |
| 4386 | |
| 4387 | /** |
| 4388 | * Set the public and private key files and password for S/MIME signing. |
| 4389 | * |
| 4390 | * @param string $cert_filename |
| 4391 | * @param string $key_filename |
| 4392 | * @param string $key_pass Password for private key |
| 4393 | * @param string $extracerts_filename Optional path to chain certificate |
| 4394 | */ |
| 4395 | public function sign($cert_filename, $key_filename, $key_pass, $extracerts_filename = '') |
| 4396 | { |
| 4397 | $this->sign_cert_file = $cert_filename; |
| 4398 | $this->sign_key_file = $key_filename; |
| 4399 | $this->sign_key_pass = $key_pass; |
| 4400 | $this->sign_extracerts_file = $extracerts_filename; |
| 4401 | } |
| 4402 | |
| 4403 | /** |
| 4404 | * Quoted-Printable-encode a DKIM header. |
| 4405 | * |
| 4406 | * @param string $txt |
| 4407 | * |
| 4408 | * @return string |
| 4409 | */ |
| 4410 | public function DKIM_QP($txt) |
| 4411 | { |
| 4412 | $line = ''; |
| 4413 | $len = strlen($txt); |
| 4414 | for ($i = 0; $i < $len; ++$i) { |
| 4415 | $ord = ord($txt[$i]); |
| 4416 | if (((0x21 <= $ord) && ($ord <= 0x3A)) || $ord === 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E))) { |
| 4417 | $line .= $txt[$i]; |
| 4418 | } else { |
| 4419 | $line .= '=' . sprintf('%02X', $ord); |
| 4420 | } |
| 4421 | } |
| 4422 | |
| 4423 | return $line; |
| 4424 | } |
| 4425 | |
| 4426 | /** |
| 4427 | * Generate a DKIM signature. |
| 4428 | * |
| 4429 | * @param string $signHeader |
| 4430 | * |
| 4431 | * @throws Exception |
| 4432 | * |
| 4433 | * @return string The DKIM signature value |
| 4434 | */ |
| 4435 | public function DKIM_Sign($signHeader) |
| 4436 | { |
| 4437 | if (!defined('PKCS7_TEXT')) { |
| 4438 | if ($this->exceptions) { |
| 4439 | throw new Exception($this->lang('extension_missing') . 'openssl'); |
| 4440 | } |
| 4441 | |
| 4442 | return ''; |
| 4443 | } |
| 4444 | $privKeyStr = !empty($this->DKIM_private_string) ? |
| 4445 | $this->DKIM_private_string : |
| 4446 | file_get_contents($this->DKIM_private); |
| 4447 | if ('' !== $this->DKIM_passphrase) { |
| 4448 | $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase); |
| 4449 | } else { |
| 4450 | $privKey = openssl_pkey_get_private($privKeyStr); |
| 4451 | } |
| 4452 | if (openssl_sign($signHeader, $signature, $privKey, 'sha256WithRSAEncryption')) { |
| 4453 | openssl_pkey_free($privKey); |
| 4454 | |
| 4455 | return base64_encode($signature); |
| 4456 | } |
| 4457 | openssl_pkey_free($privKey); |
| 4458 | |
| 4459 | return ''; |
| 4460 | } |
| 4461 | |
| 4462 | /** |
| 4463 | * Generate a DKIM canonicalization header. |
| 4464 | * Uses the 'relaxed' algorithm from RFC6376 section 3.4.2. |
| 4465 | * Canonicalized headers should *always* use CRLF, regardless of mailer setting. |
| 4466 | * |
| 4467 | * @see https://tools.ietf.org/html/rfc6376#section-3.4.2 |
| 4468 | * |
| 4469 | * @param string $signHeader Header |
| 4470 | * |
| 4471 | * @return string |
| 4472 | */ |
| 4473 | public function DKIM_HeaderC($signHeader) |
| 4474 | { |
| 4475 | //Note PCRE \s is too broad a definition of whitespace; RFC5322 defines it as `[ \t]` |
| 4476 | //@see https://tools.ietf.org/html/rfc5322#section-2.2 |
| 4477 | //That means this may break if you do something daft like put vertical tabs in your headers. |
| 4478 | //Unfold header lines |
| 4479 | $signHeader = preg_replace('/\r\n[ \t]+/', ' ', $signHeader); |
| 4480 | //Break headers out into an array |
| 4481 | $lines = explode("\r\n", $signHeader); |
| 4482 | foreach ($lines as $key => $line) { |
| 4483 | //If the header is missing a :, skip it as it's invalid |
| 4484 | //This is likely to happen because the explode() above will also split |
| 4485 | //on the trailing LE, leaving an empty line |
| 4486 | if (strpos($line, ':') === false) { |
| 4487 | continue; |
| 4488 | } |
| 4489 | list($heading, $value) = explode(':', $line, 2); |
| 4490 | //Lower-case header name |
| 4491 | $heading = strtolower($heading); |
| 4492 | //Collapse white space within the value, also convert WSP to space |
| 4493 | $value = preg_replace('/[ \t]+/', ' ', $value); |
| 4494 | //RFC6376 is slightly unclear here - it says to delete space at the *end* of each value |
| 4495 | //But then says to delete space before and after the colon. |
| 4496 | //Net result is the same as trimming both ends of the value. |
| 4497 | //By elimination, the same applies to the field name |
| 4498 | $lines[$key] = trim($heading, " \t") . ':' . trim($value, " \t"); |
| 4499 | } |
| 4500 | |
| 4501 | return implode("\r\n", $lines); |
| 4502 | } |
| 4503 | |
| 4504 | /** |
| 4505 | * Generate a DKIM canonicalization body. |
| 4506 | * Uses the 'simple' algorithm from RFC6376 section 3.4.3. |
| 4507 | * Canonicalized bodies should *always* use CRLF, regardless of mailer setting. |
| 4508 | * |
| 4509 | * @see https://tools.ietf.org/html/rfc6376#section-3.4.3 |
| 4510 | * |
| 4511 | * @param string $body Message Body |
| 4512 | * |
| 4513 | * @return string |
| 4514 | */ |
| 4515 | public function DKIM_BodyC($body) |
| 4516 | { |
| 4517 | if (empty($body)) { |
| 4518 | return "\r\n"; |
| 4519 | } |
| 4520 | // Normalize line endings to CRLF |
| 4521 | $body = static::normalizeBreaks($body, "\r\n"); |
| 4522 | |
| 4523 | //Reduce multiple trailing line breaks to a single one |
| 4524 | return rtrim($body, "\r\n") . "\r\n"; |
| 4525 | } |
| 4526 | |
| 4527 | /** |
| 4528 | * Create the DKIM header and body in a new message header. |
| 4529 | * |
| 4530 | * @param string $headers_line Header lines |
| 4531 | * @param string $subject Subject |
| 4532 | * @param string $body Body |
| 4533 | * |
| 4534 | * @throws Exception |
| 4535 | * |
| 4536 | * @return string |
| 4537 | */ |
| 4538 | public function DKIM_Add($headers_line, $subject, $body) |
| 4539 | { |
| 4540 | $DKIMsignatureType = 'rsa-sha256'; // Signature & hash algorithms |
| 4541 | $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization methods of header & body |
| 4542 | $DKIMquery = 'dns/txt'; // Query method |
| 4543 | $DKIMtime = time(); |
| 4544 | //Always sign these headers without being asked |
| 4545 | $autoSignHeaders = [ |
| 4546 | 'From', |
| 4547 | 'To', |
| 4548 | 'CC', |
| 4549 | 'Date', |
| 4550 | 'Subject', |
| 4551 | 'Reply-To', |
| 4552 | 'Message-ID', |
| 4553 | 'Content-Type', |
| 4554 | 'Mime-Version', |
| 4555 | 'X-Mailer', |
| 4556 | ]; |
| 4557 | if (stripos($headers_line, 'Subject') === false) { |
| 4558 | $headers_line .= 'Subject: ' . $subject . static::$LE; |
| 4559 | } |
| 4560 | $headerLines = explode(static::$LE, $headers_line); |
| 4561 | $currentHeaderLabel = ''; |
| 4562 | $currentHeaderValue = ''; |
| 4563 | $parsedHeaders = []; |
| 4564 | $headerLineIndex = 0; |
| 4565 | $headerLineCount = count($headerLines); |
| 4566 | foreach ($headerLines as $headerLine) { |
| 4567 | $matches = []; |
| 4568 | if (preg_match('/^([^ \t]*?)(?::[ \t]*)(.*)$/', $headerLine, $matches)) { |
| 4569 | if ($currentHeaderLabel !== '') { |
| 4570 | //We were previously in another header; This is the start of a new header, so save the previous one |
| 4571 | $parsedHeaders[] = ['label' => $currentHeaderLabel, 'value' => $currentHeaderValue]; |
| 4572 | } |
| 4573 | $currentHeaderLabel = $matches[1]; |
| 4574 | $currentHeaderValue = $matches[2]; |
| 4575 | } elseif (preg_match('/^[ \t]+(.*)$/', $headerLine, $matches)) { |
| 4576 | //This is a folded continuation of the current header, so unfold it |
| 4577 | $currentHeaderValue .= ' ' . $matches[1]; |
| 4578 | } |
| 4579 | ++$headerLineIndex; |
| 4580 | if ($headerLineIndex >= $headerLineCount) { |
| 4581 | //This was the last line, so finish off this header |
| 4582 | $parsedHeaders[] = ['label' => $currentHeaderLabel, 'value' => $currentHeaderValue]; |
| 4583 | } |
| 4584 | } |
| 4585 | $copiedHeaders = []; |
| 4586 | $headersToSignKeys = []; |
| 4587 | $headersToSign = []; |
| 4588 | foreach ($parsedHeaders as $header) { |
| 4589 | //Is this header one that must be included in the DKIM signature? |
| 4590 | if (in_array($header['label'], $autoSignHeaders, true)) { |
| 4591 | $headersToSignKeys[] = $header['label']; |
| 4592 | $headersToSign[] = $header['label'] . ': ' . $header['value']; |
| 4593 | if ($this->DKIM_copyHeaderFields) { |
| 4594 | $copiedHeaders[] = $header['label'] . ':' . //Note no space after this, as per RFC |
| 4595 | str_replace('|', '=7C', $this->DKIM_QP($header['value'])); |
| 4596 | } |
| 4597 | continue; |
| 4598 | } |
| 4599 | //Is this an extra custom header we've been asked to sign? |
| 4600 | if (in_array($header['label'], $this->DKIM_extraHeaders, true)) { |
| 4601 | //Find its value in custom headers |
| 4602 | foreach ($this->CustomHeader as $customHeader) { |
| 4603 | if ($customHeader[0] === $header['label']) { |
| 4604 | $headersToSignKeys[] = $header['label']; |
| 4605 | $headersToSign[] = $header['label'] . ': ' . $header['value']; |
| 4606 | if ($this->DKIM_copyHeaderFields) { |
| 4607 | $copiedHeaders[] = $header['label'] . ':' . //Note no space after this, as per RFC |
| 4608 | str_replace('|', '=7C', $this->DKIM_QP($header['value'])); |
| 4609 | } |
| 4610 | //Skip straight to the next header |
| 4611 | continue 2; |
| 4612 | } |
| 4613 | } |
| 4614 | } |
| 4615 | } |
| 4616 | $copiedHeaderFields = ''; |
| 4617 | if ($this->DKIM_copyHeaderFields && count($copiedHeaders) > 0) { |
| 4618 | //Assemble a DKIM 'z' tag |
| 4619 | $copiedHeaderFields = ' z='; |
| 4620 | $first = true; |
| 4621 | foreach ($copiedHeaders as $copiedHeader) { |
| 4622 | if (!$first) { |
| 4623 | $copiedHeaderFields .= static::$LE . ' |'; |
| 4624 | } |
| 4625 | //Fold long values |
| 4626 | if (strlen($copiedHeader) > self::STD_LINE_LENGTH - 3) { |
| 4627 | $copiedHeaderFields .= substr( |
| 4628 | chunk_split($copiedHeader, self::STD_LINE_LENGTH - 3, static::$LE . ' '), |
| 4629 | 0, |
| 4630 | -strlen(static::$LE . ' ') |
| 4631 | ); |
| 4632 | } else { |
| 4633 | $copiedHeaderFields .= $copiedHeader; |
| 4634 | } |
| 4635 | $first = false; |
| 4636 | } |
| 4637 | $copiedHeaderFields .= ';' . static::$LE; |
| 4638 | } |
| 4639 | $headerKeys = ' h=' . implode(':', $headersToSignKeys) . ';' . static::$LE; |
| 4640 | $headerValues = implode(static::$LE, $headersToSign); |
| 4641 | $body = $this->DKIM_BodyC($body); |
| 4642 | $DKIMlen = strlen($body); // Length of body |
| 4643 | $DKIMb64 = base64_encode(pack('H*', hash('sha256', $body))); // Base64 of packed binary SHA-256 hash of body |
| 4644 | $ident = ''; |
| 4645 | if ('' !== $this->DKIM_identity) { |
| 4646 | $ident = ' i=' . $this->DKIM_identity . ';' . static::$LE; |
| 4647 | } |
| 4648 | //The DKIM-Signature header is included in the signature *except for* the value of the `b` tag |
| 4649 | //which is appended after calculating the signature |
| 4650 | //https://tools.ietf.org/html/rfc6376#section-3.5 |
| 4651 | $dkimSignatureHeader = 'DKIM-Signature: v=1;' . |
| 4652 | ' d=' . $this->DKIM_domain . ';' . |
| 4653 | ' s=' . $this->DKIM_selector . ';' . static::$LE . |
| 4654 | ' a=' . $DKIMsignatureType . ';' . |
| 4655 | ' q=' . $DKIMquery . ';' . |
| 4656 | ' l=' . $DKIMlen . ';' . |
| 4657 | ' t=' . $DKIMtime . ';' . |
| 4658 | ' c=' . $DKIMcanonicalization . ';' . static::$LE . |
| 4659 | $headerKeys . |
| 4660 | $ident . |
| 4661 | $copiedHeaderFields . |
| 4662 | ' bh=' . $DKIMb64 . ';' . static::$LE . |
| 4663 | ' b='; |
| 4664 | //Canonicalize the set of headers |
| 4665 | $canonicalizedHeaders = $this->DKIM_HeaderC( |
| 4666 | $headerValues . static::$LE . $dkimSignatureHeader |
| 4667 | ); |
| 4668 | $signature = $this->DKIM_Sign($canonicalizedHeaders); |
| 4669 | $signature = trim(chunk_split($signature, self::STD_LINE_LENGTH - 3, static::$LE . ' ')); |
| 4670 | |
| 4671 | return static::normalizeBreaks($dkimSignatureHeader . $signature) . static::$LE; |
| 4672 | } |
| 4673 | |
| 4674 | /** |
| 4675 | * Detect if a string contains a line longer than the maximum line length |
| 4676 | * allowed by RFC 2822 section 2.1.1. |
| 4677 | * |
| 4678 | * @param string $str |
| 4679 | * |
| 4680 | * @return bool |
| 4681 | */ |
| 4682 | public static function hasLineLongerThanMax($str) |
| 4683 | { |
| 4684 | return (bool) preg_match('/^(.{' . (self::MAX_LINE_LENGTH + strlen(static::$LE)) . ',})/m', $str); |
| 4685 | } |
| 4686 | |
| 4687 | /** |
| 4688 | * Allows for public read access to 'to' property. |
| 4689 | * Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
| 4690 | * |
| 4691 | * @return array |
| 4692 | */ |
| 4693 | public function getToAddresses() |
| 4694 | { |
| 4695 | return $this->to; |
| 4696 | } |
| 4697 | |
| 4698 | /** |
| 4699 | * Allows for public read access to 'cc' property. |
| 4700 | * Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
| 4701 | * |
| 4702 | * @return array |
| 4703 | */ |
| 4704 | public function getCcAddresses() |
| 4705 | { |
| 4706 | return $this->cc; |
| 4707 | } |
| 4708 | |
| 4709 | /** |
| 4710 | * Allows for public read access to 'bcc' property. |
| 4711 | * Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
| 4712 | * |
| 4713 | * @return array |
| 4714 | */ |
| 4715 | public function getBccAddresses() |
| 4716 | { |
| 4717 | return $this->bcc; |
| 4718 | } |
| 4719 | |
| 4720 | /** |
| 4721 | * Allows for public read access to 'ReplyTo' property. |
| 4722 | * Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
| 4723 | * |
| 4724 | * @return array |
| 4725 | */ |
| 4726 | public function getReplyToAddresses() |
| 4727 | { |
| 4728 | return $this->ReplyTo; |
| 4729 | } |
| 4730 | |
| 4731 | /** |
| 4732 | * Allows for public read access to 'all_recipients' property. |
| 4733 | * Before the send() call, queued addresses (i.e. with IDN) are not yet included. |
| 4734 | * |
| 4735 | * @return array |
| 4736 | */ |
| 4737 | public function getAllRecipientAddresses() |
| 4738 | { |
| 4739 | return $this->all_recipients; |
| 4740 | } |
| 4741 | |
| 4742 | /** |
| 4743 | * Perform a callback. |
| 4744 | * |
| 4745 | * @param bool $isSent |
| 4746 | * @param array $to |
| 4747 | * @param array $cc |
| 4748 | * @param array $bcc |
| 4749 | * @param string $subject |
| 4750 | * @param string $body |
| 4751 | * @param string $from |
| 4752 | * @param array $extra |
| 4753 | */ |
| 4754 | protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from, $extra) |
| 4755 | { |
| 4756 | if (!empty($this->action_function) && is_callable($this->action_function)) { |
| 4757 | call_user_func($this->action_function, $isSent, $to, $cc, $bcc, $subject, $body, $from, $extra); |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | /** |
| 4762 | * Get the OAuth instance. |
| 4763 | * |
| 4764 | * @return OAuth |
| 4765 | */ |
| 4766 | public function getOAuth() |
| 4767 | { |
| 4768 | return $this->oauth; |
| 4769 | } |
| 4770 | |
| 4771 | /** |
| 4772 | * Set an OAuth instance. |
| 4773 | */ |
| 4774 | public function setOAuth(OAuth $oauth) |
| 4775 | { |
| 4776 | $this->oauth = $oauth; |
| 4777 | } |