[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * PHPMailer - PHP email creation and transport class. 5 * PHP Version 5.5. 6 * 7 * @see https://github.com/PHPMailer/PHPMailer/ The PHPMailer GitHub project 8 * 9 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk> 10 * @author Jim Jagielski (jimjag) <jimjag@gmail.com> 11 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net> 12 * @author Brent R. Matzelle (original founder) 13 * @copyright 2012 - 2020 Marcus Bointon 14 * @copyright 2010 - 2012 Jim Jagielski 15 * @copyright 2004 - 2009 Andy Prevost 16 * @license https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html GNU Lesser General Public License 17 * @note This program is distributed in the hope that it will be useful - WITHOUT 18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 19 * FITNESS FOR A PARTICULAR PURPOSE. 20 */ 21 22 namespace PHPMailer\PHPMailer; 23 24 /** 25 * PHPMailer - PHP email creation and transport class. 26 * 27 * @author Marcus Bointon (Synchro/coolbru) <phpmailer@synchromedia.co.uk> 28 * @author Jim Jagielski (jimjag) <jimjag@gmail.com> 29 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net> 30 * @author Brent R. Matzelle (original founder) 31 */ 32 class PHPMailer 33 { 34 const CHARSET_ASCII = 'us-ascii'; 35 const CHARSET_ISO88591 = 'iso-8859-1'; 36 const CHARSET_UTF8 = 'utf-8'; 37 38 const CONTENT_TYPE_PLAINTEXT = 'text/plain'; 39 const CONTENT_TYPE_TEXT_CALENDAR = 'text/calendar'; 40 const CONTENT_TYPE_TEXT_HTML = 'text/html'; 41 const CONTENT_TYPE_MULTIPART_ALTERNATIVE = 'multipart/alternative'; 42 const CONTENT_TYPE_MULTIPART_MIXED = 'multipart/mixed'; 43 const CONTENT_TYPE_MULTIPART_RELATED = 'multipart/related'; 44 45 const ENCODING_7BIT = '7bit'; 46 const ENCODING_8BIT = '8bit'; 47 const ENCODING_BASE64 = 'base64'; 48 const ENCODING_BINARY = 'binary'; 49 const ENCODING_QUOTED_PRINTABLE = 'quoted-printable'; 50 51 const ENCRYPTION_STARTTLS = 'tls'; 52 const ENCRYPTION_SMTPS = 'ssl'; 53 54 const ICAL_METHOD_REQUEST = 'REQUEST'; 55 const ICAL_METHOD_PUBLISH = 'PUBLISH'; 56 const ICAL_METHOD_REPLY = 'REPLY'; 57 const ICAL_METHOD_ADD = 'ADD'; 58 const ICAL_METHOD_CANCEL = 'CANCEL'; 59 const ICAL_METHOD_REFRESH = 'REFRESH'; 60 const ICAL_METHOD_COUNTER = 'COUNTER'; 61 const ICAL_METHOD_DECLINECOUNTER = 'DECLINECOUNTER'; 62 63 /** 64 * Email priority. 65 * Options: null (default), 1 = High, 3 = Normal, 5 = low. 66 * When null, the header is not set at all. 67 * 68 * @var int|null 69 */ 70 public $Priority; 71 72 /** 73 * The character set of the message. 74 * 75 * @var string 76 */ 77 public $CharSet = self::CHARSET_ISO88591; 78 79 /** 80 * The MIME Content-type of the message. 81 * 82 * @var string 83 */ 84 public $ContentType = self::CONTENT_TYPE_PLAINTEXT; 85 86 /** 87 * The message encoding. 88 * Options: "8bit", "7bit", "binary", "base64", and "quoted-printable". 89 * 90 * @var string 91 */ 92 public $Encoding = self::ENCODING_8BIT; 93 94 /** 95 * Holds the most recent mailer error message. 96 * 97 * @var string 98 */ 99 public $ErrorInfo = ''; 100 101 /** 102 * The From email address for the message. 103 * 104 * @var string 105 */ 106 public $From = ''; 107 108 /** 109 * The From name of the message. 110 * 111 * @var string 112 */ 113 public $FromName = ''; 114 115 /** 116 * The envelope sender of the message. 117 * This will usually be turned into a Return-Path header by the receiver, 118 * and is the address that bounces will be sent to. 119 * If not empty, will be passed via `-f` to sendmail or as the 'MAIL FROM' value over SMTP. 120 * 121 * @var string 122 */ 123 public $Sender = ''; 124 125 /** 126 * The Subject of the message. 127 * 128 * @var string 129 */ 130 public $Subject = ''; 131 132 /** 133 * An HTML or plain text message body. 134 * If HTML then call isHTML(true). 135 * 136 * @var string 137 */ 138 public $Body = ''; 139 140 /** 141 * The plain-text message body. 142 * This body can be read by mail clients that do not have HTML email 143 * capability such as mutt & Eudora. 144 * Clients that can read HTML will view the normal Body. 145 * 146 * @var string 147 */ 148 public $AltBody = ''; 149 150 /** 151 * An iCal message part body. 152 * Only supported in simple alt or alt_inline message types 153 * To generate iCal event structures, use classes like EasyPeasyICS or iCalcreator. 154 * 155 * @see https://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://www.rfc-editor.org/rfc/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 authentication type. Options are CRAM-MD5, LOGIN, PLAIN, XOAUTH2. 353 * If not specified, the first one from that list that the server supports will be selected. 354 * 355 * @var string 356 */ 357 public $AuthType = ''; 358 359 /** 360 * SMTP SMTPXClient command attributes 361 * 362 * @var array 363 */ 364 protected $SMTPXClient = []; 365 366 /** 367 * An implementation of the PHPMailer OAuthTokenProvider interface. 368 * 369 * @var OAuthTokenProvider 370 */ 371 protected $oauth; 372 373 /** 374 * The SMTP server timeout in seconds. 375 * Default of 5 minutes (300sec) is from RFC2821 section 4.5.3.2. 376 * 377 * @var int 378 */ 379 public $Timeout = 300; 380 381 /** 382 * Comma separated list of DSN notifications 383 * 'NEVER' under no circumstances a DSN must be returned to the sender. 384 * If you use NEVER all other notifications will be ignored. 385 * 'SUCCESS' will notify you when your mail has arrived at its destination. 386 * 'FAILURE' will arrive if an error occurred during delivery. 387 * 'DELAY' will notify you if there is an unusual delay in delivery, but the actual 388 * delivery's outcome (success or failure) is not yet decided. 389 * 390 * @see https://www.rfc-editor.org/rfc/rfc3461.html#section-4.1 for more information about NOTIFY 391 */ 392 public $dsn = ''; 393 394 /** 395 * SMTP class debug output mode. 396 * Debug output level. 397 * Options: 398 * @see SMTP::DEBUG_OFF: No output 399 * @see SMTP::DEBUG_CLIENT: Client messages 400 * @see SMTP::DEBUG_SERVER: Client and server messages 401 * @see SMTP::DEBUG_CONNECTION: As SERVER plus connection status 402 * @see SMTP::DEBUG_LOWLEVEL: Noisy, low-level data output, rarely needed 403 * 404 * @see SMTP::$do_debug 405 * 406 * @var int 407 */ 408 public $SMTPDebug = 0; 409 410 /** 411 * How to handle debug output. 412 * Options: 413 * * `echo` Output plain-text as-is, appropriate for CLI 414 * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output 415 * * `error_log` Output to error log as configured in php.ini 416 * By default PHPMailer will use `echo` if run from a `cli` or `cli-server` SAPI, `html` otherwise. 417 * Alternatively, you can provide a callable expecting two params: a message string and the debug level: 418 * 419 * ```php 420 * $mail->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; 421 * ``` 422 * 423 * Alternatively, you can pass in an instance of a PSR-3 compatible logger, though only `debug` 424 * level output is used: 425 * 426 * ```php 427 * $mail->Debugoutput = new myPsr3Logger; 428 * ``` 429 * 430 * @see SMTP::$Debugoutput 431 * 432 * @var string|callable|\Psr\Log\LoggerInterface 433 */ 434 public $Debugoutput = 'echo'; 435 436 /** 437 * Whether to keep the SMTP connection open after each message. 438 * If this is set to true then the connection will remain open after a send, 439 * and closing the connection will require an explicit call to smtpClose(). 440 * It's a good idea to use this if you are sending multiple messages as it reduces overhead. 441 * See the mailing list example for how to use it. 442 * 443 * @var bool 444 */ 445 public $SMTPKeepAlive = false; 446 447 /** 448 * Whether to split multiple to addresses into multiple messages 449 * or send them all in one message. 450 * Only supported in `mail` and `sendmail` transports, not in SMTP. 451 * 452 * @var bool 453 * 454 * @deprecated 6.0.0 PHPMailer isn't a mailing list manager! 455 */ 456 public $SingleTo = false; 457 458 /** 459 * Storage for addresses when SingleTo is enabled. 460 * 461 * @var array 462 */ 463 protected $SingleToArray = []; 464 465 /** 466 * Whether to generate VERP addresses on send. 467 * Only applicable when sending via SMTP. 468 * 469 * @see https://en.wikipedia.org/wiki/Variable_envelope_return_path 470 * @see https://www.postfix.org/VERP_README.html Postfix VERP info 471 * 472 * @var bool 473 */ 474 public $do_verp = false; 475 476 /** 477 * Whether to allow sending messages with an empty body. 478 * 479 * @var bool 480 */ 481 public $AllowEmpty = false; 482 483 /** 484 * DKIM selector. 485 * 486 * @var string 487 */ 488 public $DKIM_selector = ''; 489 490 /** 491 * DKIM Identity. 492 * Usually the email address used as the source of the email. 493 * 494 * @var string 495 */ 496 public $DKIM_identity = ''; 497 498 /** 499 * DKIM passphrase. 500 * Used if your key is encrypted. 501 * 502 * @var string 503 */ 504 public $DKIM_passphrase = ''; 505 506 /** 507 * DKIM signing domain name. 508 * 509 * @example 'example.com' 510 * 511 * @var string 512 */ 513 public $DKIM_domain = ''; 514 515 /** 516 * DKIM Copy header field values for diagnostic use. 517 * 518 * @var bool 519 */ 520 public $DKIM_copyHeaderFields = true; 521 522 /** 523 * DKIM Extra signing headers. 524 * 525 * @example ['List-Unsubscribe', 'List-Help'] 526 * 527 * @var array 528 */ 529 public $DKIM_extraHeaders = []; 530 531 /** 532 * DKIM private key file path. 533 * 534 * @var string 535 */ 536 public $DKIM_private = ''; 537 538 /** 539 * DKIM private key string. 540 * 541 * If set, takes precedence over `$DKIM_private`. 542 * 543 * @var string 544 */ 545 public $DKIM_private_string = ''; 546 547 /** 548 * Callback Action function name. 549 * 550 * The function that handles the result of the send email action. 551 * It is called out by send() for each email sent. 552 * 553 * Value can be any php callable: https://www.php.net/is_callable 554 * 555 * Parameters: 556 * bool $result result of the send action 557 * array $to email addresses of the recipients 558 * array $cc cc email addresses 559 * array $bcc bcc email addresses 560 * string $subject the subject 561 * string $body the email body 562 * string $from email address of sender 563 * string $extra extra information of possible use 564 * "smtp_transaction_id' => last smtp transaction id 565 * 566 * @var string 567 */ 568 public $action_function = ''; 569 570 /** 571 * What to put in the X-Mailer header. 572 * Options: An empty string for PHPMailer default, whitespace/null for none, or a string to use. 573 * 574 * @var string|null 575 */ 576 public $XMailer = ''; 577 578 /** 579 * Which validator to use by default when validating email addresses. 580 * May be a callable to inject your own validator, but there are several built-in validators. 581 * The default validator uses PHP's FILTER_VALIDATE_EMAIL filter_var option. 582 * 583 * If CharSet is UTF8, the validator is left at the default value, 584 * and you send to addresses that use non-ASCII local parts, then 585 * PHPMailer automatically changes to the 'eai' validator. 586 * 587 * @see PHPMailer::validateAddress() 588 * 589 * @var string|callable 590 */ 591 public static $validator = 'php'; 592 593 /** 594 * An instance of the SMTP sender class. 595 * 596 * @var SMTP 597 */ 598 protected $smtp; 599 600 /** 601 * The array of 'to' names and addresses. 602 * 603 * @var array 604 */ 605 protected $to = []; 606 607 /** 608 * The array of 'cc' names and addresses. 609 * 610 * @var array 611 */ 612 protected $cc = []; 613 614 /** 615 * The array of 'bcc' names and addresses. 616 * 617 * @var array 618 */ 619 protected $bcc = []; 620 621 /** 622 * The array of reply-to names and addresses. 623 * 624 * @var array 625 */ 626 protected $ReplyTo = []; 627 628 /** 629 * An array of all kinds of addresses. 630 * Includes all of $to, $cc, $bcc. 631 * 632 * @see PHPMailer::$to 633 * @see PHPMailer::$cc 634 * @see PHPMailer::$bcc 635 * 636 * @var array 637 */ 638 protected $all_recipients = []; 639 640 /** 641 * An array of names and addresses queued for validation. 642 * In send(), valid and non duplicate entries are moved to $all_recipients 643 * and one of $to, $cc, or $bcc. 644 * This array is used only for addresses with IDN. 645 * 646 * @see PHPMailer::$to 647 * @see PHPMailer::$cc 648 * @see PHPMailer::$bcc 649 * @see PHPMailer::$all_recipients 650 * 651 * @var array 652 */ 653 protected $RecipientsQueue = []; 654 655 /** 656 * An array of reply-to names and addresses queued for validation. 657 * In send(), valid and non duplicate entries are moved to $ReplyTo. 658 * This array is used only for addresses with IDN. 659 * 660 * @see PHPMailer::$ReplyTo 661 * 662 * @var array 663 */ 664 protected $ReplyToQueue = []; 665 666 /** 667 * Whether the need for SMTPUTF8 has been detected. Set by 668 * preSend() if necessary. 669 * 670 * @var bool 671 */ 672 public $UseSMTPUTF8 = false; 673 674 /** 675 * The array of attachments. 676 * 677 * @var array 678 */ 679 protected $attachment = []; 680 681 /** 682 * The array of custom headers. 683 * 684 * @var array 685 */ 686 protected $CustomHeader = []; 687 688 /** 689 * The most recent Message-ID (including angular brackets). 690 * 691 * @var string 692 */ 693 protected $lastMessageID = ''; 694 695 /** 696 * The message's MIME type. 697 * 698 * @var string 699 */ 700 protected $message_type = ''; 701 702 /** 703 * The array of MIME boundary strings. 704 * 705 * @var array 706 */ 707 protected $boundary = []; 708 709 /** 710 * The array of available text strings for the current language. 711 * 712 * @var array 713 */ 714 protected $language = []; 715 716 /** 717 * The number of errors encountered. 718 * 719 * @var int 720 */ 721 protected $error_count = 0; 722 723 /** 724 * The S/MIME certificate file path. 725 * 726 * @var string 727 */ 728 protected $sign_cert_file = ''; 729 730 /** 731 * The S/MIME key file path. 732 * 733 * @var string 734 */ 735 protected $sign_key_file = ''; 736 737 /** 738 * The optional S/MIME extra certificates ("CA Chain") file path. 739 * 740 * @var string 741 */ 742 protected $sign_extracerts_file = ''; 743 744 /** 745 * The S/MIME password for the key. 746 * Used only if the key is encrypted. 747 * 748 * @var string 749 */ 750 protected $sign_key_pass = ''; 751 752 /** 753 * Whether to throw exceptions for errors. 754 * 755 * @var bool 756 */ 757 protected $exceptions = false; 758 759 /** 760 * Unique ID used for message ID and boundaries. 761 * 762 * @var string 763 */ 764 protected $uniqueid = ''; 765 766 /** 767 * The PHPMailer Version number. 768 * 769 * @var string 770 */ 771 const VERSION = '6.10.0'; 772 773 /** 774 * Error severity: message only, continue processing. 775 * 776 * @var int 777 */ 778 const STOP_MESSAGE = 0; 779 780 /** 781 * Error severity: message, likely ok to continue processing. 782 * 783 * @var int 784 */ 785 const STOP_CONTINUE = 1; 786 787 /** 788 * Error severity: message, plus full stop, critical error reached. 789 * 790 * @var int 791 */ 792 const STOP_CRITICAL = 2; 793 794 /** 795 * The SMTP standard CRLF line break. 796 * If you want to change line break format, change static::$LE, not this. 797 */ 798 const CRLF = "\r\n"; 799 800 /** 801 * "Folding White Space" a white space string used for line folding. 802 */ 803 const FWS = ' '; 804 805 /** 806 * SMTP RFC standard line ending; Carriage Return, Line Feed. 807 * 808 * @var string 809 */ 810 protected static $LE = self::CRLF; 811 812 /** 813 * The maximum line length supported by mail(). 814 * 815 * Background: mail() will sometimes corrupt messages 816 * with headers longer than 65 chars, see #818. 817 * 818 * @var int 819 */ 820 const MAIL_MAX_LINE_LENGTH = 63; 821 822 /** 823 * The maximum line length allowed by RFC 2822 section 2.1.1. 824 * 825 * @var int 826 */ 827 const MAX_LINE_LENGTH = 998; 828 829 /** 830 * The lower maximum line length allowed by RFC 2822 section 2.1.1. 831 * This length does NOT include the line break 832 * 76 means that lines will be 77 or 78 chars depending on whether 833 * the line break format is LF or CRLF; both are valid. 834 * 835 * @var int 836 */ 837 const STD_LINE_LENGTH = 76; 838 839 /** 840 * Constructor. 841 * 842 * @param bool $exceptions Should we throw external exceptions? 843 */ 844 public function __construct($exceptions = null) 845 { 846 if (null !== $exceptions) { 847 $this->exceptions = (bool) $exceptions; 848 } 849 //Pick an appropriate debug output format automatically 850 $this->Debugoutput = (strpos(PHP_SAPI, 'cli') !== false ? 'echo' : 'html'); 851 } 852 853 /** 854 * Destructor. 855 */ 856 public function __destruct() 857 { 858 //Close any open SMTP connection nicely 859 $this->smtpClose(); 860 } 861 862 /** 863 * Call mail() in a safe_mode-aware fashion. 864 * Also, unless sendmail_path points to sendmail (or something that 865 * claims to be sendmail), don't pass params (not a perfect fix, 866 * but it will do). 867 * 868 * @param string $to To 869 * @param string $subject Subject 870 * @param string $body Message Body 871 * @param string $header Additional Header(s) 872 * @param string|null $params Params 873 * 874 * @return bool 875 */ 876 private function mailPassthru($to, $subject, $body, $header, $params) 877 { 878 //Check overloading of mail function to avoid double-encoding 879 if ((int)ini_get('mbstring.func_overload') & 1) { // phpcs:ignore PHPCompatibility.IniDirectives.RemovedIniDirectives.mbstring_func_overloadDeprecated 880 $subject = $this->secureHeader($subject); 881 } else { 882 $subject = $this->encodeHeader($this->secureHeader($subject)); 883 } 884 //Calling mail() with null params breaks 885 $this->edebug('Sending with mail()'); 886 $this->edebug('Sendmail path: ' . ini_get('sendmail_path')); 887 $this->edebug("Envelope sender: {$this->Sender}"); 888 $this->edebug("To: {$to}"); 889 $this->edebug("Subject: {$subject}"); 890 $this->edebug("Headers: {$header}"); 891 if (!$this->UseSendmailOptions || null === $params) { 892 $result = @mail($to, $subject, $body, $header); 893 } else { 894 $this->edebug("Additional params: {$params}"); 895 $result = @mail($to, $subject, $body, $header, $params); 896 } 897 $this->edebug('Result: ' . ($result ? 'true' : 'false')); 898 return $result; 899 } 900 901 /** 902 * Output debugging info via a user-defined method. 903 * Only generates output if debug output is enabled. 904 * 905 * @see PHPMailer::$Debugoutput 906 * @see PHPMailer::$SMTPDebug 907 * 908 * @param string $str 909 */ 910 protected function edebug($str) 911 { 912 if ($this->SMTPDebug <= 0) { 913 return; 914 } 915 //Is this a PSR-3 logger? 916 if ($this->Debugoutput instanceof \Psr\Log\LoggerInterface) { 917 $this->Debugoutput->debug(rtrim($str, "\r\n")); 918 919 return; 920 } 921 //Avoid clash with built-in function names 922 if (is_callable($this->Debugoutput) && !in_array($this->Debugoutput, ['error_log', 'html', 'echo'])) { 923 call_user_func($this->Debugoutput, $str, $this->SMTPDebug); 924 925 return; 926 } 927 switch ($this->Debugoutput) { 928 case 'error_log': 929 //Don't output, just log 930 /** @noinspection ForgottenDebugOutputInspection */ 931 error_log($str); 932 break; 933 case 'html': 934 //Cleans up output a bit for a better looking, HTML-safe output 935 echo htmlentities( 936 preg_replace('/[\r\n]+/', '', $str), 937 ENT_QUOTES, 938 'UTF-8' 939 ), "<br>\n"; 940 break; 941 case 'echo': 942 default: 943 //Normalize line breaks 944 $str = preg_replace('/\r\n|\r/m', "\n", $str); 945 echo gmdate('Y-m-d H:i:s'), 946 "\t", 947 //Trim trailing space 948 trim( 949 //Indent for readability, except for trailing break 950 str_replace( 951 "\n", 952 "\n \t ", 953 trim($str) 954 ) 955 ), 956 "\n"; 957 } 958 } 959 960 /** 961 * Sets message type to HTML or plain. 962 * 963 * @param bool $isHtml True for HTML mode 964 */ 965 public function isHTML($isHtml = true) 966 { 967 if ($isHtml) { 968 $this->ContentType = static::CONTENT_TYPE_TEXT_HTML; 969 } else { 970 $this->ContentType = static::CONTENT_TYPE_PLAINTEXT; 971 } 972 } 973 974 /** 975 * Send messages using SMTP. 976 */ 977 public function isSMTP() 978 { 979 $this->Mailer = 'smtp'; 980 } 981 982 /** 983 * Send messages using PHP's mail() function. 984 */ 985 public function isMail() 986 { 987 $this->Mailer = 'mail'; 988 } 989 990 /** 991 * Send messages using $Sendmail. 992 */ 993 public function isSendmail() 994 { 995 $ini_sendmail_path = ini_get('sendmail_path'); 996 997 if (false === stripos($ini_sendmail_path, 'sendmail')) { 998 $this->Sendmail = '/usr/sbin/sendmail'; 999 } else { 1000 $this->Sendmail = $ini_sendmail_path; 1001 } 1002 $this->Mailer = 'sendmail'; 1003 } 1004 1005 /** 1006 * Send messages using qmail. 1007 */ 1008 public function isQmail() 1009 { 1010 $ini_sendmail_path = ini_get('sendmail_path'); 1011 1012 if (false === stripos($ini_sendmail_path, 'qmail')) { 1013 $this->Sendmail = '/var/qmail/bin/qmail-inject'; 1014 } else { 1015 $this->Sendmail = $ini_sendmail_path; 1016 } 1017 $this->Mailer = 'qmail'; 1018 } 1019 1020 /** 1021 * Add a "To" address. 1022 * 1023 * @param string $address The email address to send to 1024 * @param string $name 1025 * 1026 * @throws Exception 1027 * 1028 * @return bool true on success, false if address already used or invalid in some way 1029 */ 1030 public function addAddress($address, $name = '') 1031 { 1032 return $this->addOrEnqueueAnAddress('to', $address, $name); 1033 } 1034 1035 /** 1036 * Add a "CC" address. 1037 * 1038 * @param string $address The email address to send to 1039 * @param string $name 1040 * 1041 * @throws Exception 1042 * 1043 * @return bool true on success, false if address already used or invalid in some way 1044 */ 1045 public function addCC($address, $name = '') 1046 { 1047 return $this->addOrEnqueueAnAddress('cc', $address, $name); 1048 } 1049 1050 /** 1051 * Add a "BCC" address. 1052 * 1053 * @param string $address The email address to send to 1054 * @param string $name 1055 * 1056 * @throws Exception 1057 * 1058 * @return bool true on success, false if address already used or invalid in some way 1059 */ 1060 public function addBCC($address, $name = '') 1061 { 1062 return $this->addOrEnqueueAnAddress('bcc', $address, $name); 1063 } 1064 1065 /** 1066 * Add a "Reply-To" address. 1067 * 1068 * @param string $address The email address to reply to 1069 * @param string $name 1070 * 1071 * @throws Exception 1072 * 1073 * @return bool true on success, false if address already used or invalid in some way 1074 */ 1075 public function addReplyTo($address, $name = '') 1076 { 1077 return $this->addOrEnqueueAnAddress('Reply-To', $address, $name); 1078 } 1079 1080 /** 1081 * Add an address to one of the recipient arrays or to the ReplyTo array. Because PHPMailer 1082 * can't validate addresses with an IDN without knowing the PHPMailer::$CharSet (that can still 1083 * be modified after calling this function), addition of such addresses is delayed until send(). 1084 * Addresses that have been added already return false, but do not throw exceptions. 1085 * 1086 * @param string $kind One of 'to', 'cc', 'bcc', or 'Reply-To' 1087 * @param string $address The email address 1088 * @param string $name An optional username associated with the address 1089 * 1090 * @throws Exception 1091 * 1092 * @return bool true on success, false if address already used or invalid in some way 1093 */ 1094 protected function addOrEnqueueAnAddress($kind, $address, $name) 1095 { 1096 $pos = false; 1097 if ($address !== null) { 1098 $address = trim($address); 1099 $pos = strrpos($address, '@'); 1100 } 1101 if (false === $pos) { 1102 //At-sign is missing. 1103 $error_message = sprintf( 1104 '%s (%s): %s', 1105 $this->lang('invalid_address'), 1106 $kind, 1107 $address 1108 ); 1109 $this->setError($error_message); 1110 $this->edebug($error_message); 1111 if ($this->exceptions) { 1112 throw new Exception($error_message); 1113 } 1114 1115 return false; 1116 } 1117 if ($name !== null && is_string($name)) { 1118 $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim 1119 } else { 1120 $name = ''; 1121 } 1122 $params = [$kind, $address, $name]; 1123 //Enqueue addresses with IDN until we know the PHPMailer::$CharSet. 1124 //Domain is assumed to be whatever is after the last @ symbol in the address 1125 if ($this->has8bitChars(substr($address, ++$pos))) { 1126 if (static::idnSupported()) { 1127 if ('Reply-To' !== $kind) { 1128 if (!array_key_exists($address, $this->RecipientsQueue)) { 1129 $this->RecipientsQueue[$address] = $params; 1130 1131 return true; 1132 } 1133 } elseif (!array_key_exists($address, $this->ReplyToQueue)) { 1134 $this->ReplyToQueue[$address] = $params; 1135 1136 return true; 1137 } 1138 } 1139 //We have an 8-bit domain, but we are missing the necessary extensions to support it 1140 //Or we are already sending to this address 1141 return false; 1142 } 1143 1144 //Immediately add standard addresses without IDN. 1145 return call_user_func_array([$this, 'addAnAddress'], $params); 1146 } 1147 1148 /** 1149 * Set the boundaries to use for delimiting MIME parts. 1150 * If you override this, ensure you set all 3 boundaries to unique values. 1151 * The default boundaries include a "=_" sequence which cannot occur in quoted-printable bodies, 1152 * as suggested by https://www.rfc-editor.org/rfc/rfc2045#section-6.7 1153 * 1154 * @return void 1155 */ 1156 public function setBoundaries() 1157 { 1158 $this->uniqueid = $this->generateId(); 1159 $this->boundary[1] = 'b1=_' . $this->uniqueid; 1160 $this->boundary[2] = 'b2=_' . $this->uniqueid; 1161 $this->boundary[3] = 'b3=_' . $this->uniqueid; 1162 } 1163 1164 /** 1165 * Add an address to one of the recipient arrays or to the ReplyTo array. 1166 * Addresses that have been added already return false, but do not throw exceptions. 1167 * 1168 * @param string $kind One of 'to', 'cc', 'bcc', or 'ReplyTo' 1169 * @param string $address The email address to send, resp. to reply to 1170 * @param string $name 1171 * 1172 * @throws Exception 1173 * 1174 * @return bool true on success, false if address already used or invalid in some way 1175 */ 1176 protected function addAnAddress($kind, $address, $name = '') 1177 { 1178 if ( 1179 self::$validator === 'php' && 1180 ((bool) preg_match('/[\x80-\xFF]/', $address)) 1181 ) { 1182 //The caller has not altered the validator and is sending to an address 1183 //with UTF-8, so assume that they want UTF-8 support instead of failing 1184 $this->CharSet = self::CHARSET_UTF8; 1185 self::$validator = 'eai'; 1186 } 1187 if (!in_array($kind, ['to', 'cc', 'bcc', 'Reply-To'])) { 1188 $error_message = sprintf( 1189 '%s: %s', 1190 $this->lang('Invalid recipient kind'), 1191 $kind 1192 ); 1193 $this->setError($error_message); 1194 $this->edebug($error_message); 1195 if ($this->exceptions) { 1196 throw new Exception($error_message); 1197 } 1198 1199 return false; 1200 } 1201 if (!static::validateAddress($address)) { 1202 $error_message = sprintf( 1203 '%s (%s): %s', 1204 $this->lang('invalid_address'), 1205 $kind, 1206 $address 1207 ); 1208 $this->setError($error_message); 1209 $this->edebug($error_message); 1210 if ($this->exceptions) { 1211 throw new Exception($error_message); 1212 } 1213 1214 return false; 1215 } 1216 if ('Reply-To' !== $kind) { 1217 if (!array_key_exists(strtolower($address), $this->all_recipients)) { 1218 $this->{$kind}[] = [$address, $name]; 1219 $this->all_recipients[strtolower($address)] = true; 1220 1221 return true; 1222 } 1223 } elseif (!array_key_exists(strtolower($address), $this->ReplyTo)) { 1224 $this->ReplyTo[strtolower($address)] = [$address, $name]; 1225 1226 return true; 1227 } 1228 1229 return false; 1230 } 1231 1232 /** 1233 * Parse and validate a string containing one or more RFC822-style comma-separated email addresses 1234 * of the form "display name <address>" into an array of name/address pairs. 1235 * Uses the imap_rfc822_parse_adrlist function if the IMAP extension is available. 1236 * Note that quotes in the name part are removed. 1237 * 1238 * @see https://www.andrew.cmu.edu/user/agreen1/testing/mrbs/web/Mail/RFC822.php A more careful implementation 1239 * 1240 * @param string $addrstr The address list string 1241 * @param bool $useimap Whether to use the IMAP extension to parse the list 1242 * @param string $charset The charset to use when decoding the address list string. 1243 * 1244 * @return array 1245 */ 1246 public static function parseAddresses($addrstr, $useimap = true, $charset = self::CHARSET_ISO88591) 1247 { 1248 $addresses = []; 1249 if ($useimap && function_exists('imap_rfc822_parse_adrlist')) { 1250 //Use this built-in parser if it's available 1251 $list = imap_rfc822_parse_adrlist($addrstr, ''); 1252 // Clear any potential IMAP errors to get rid of notices being thrown at end of script. 1253 imap_errors(); 1254 foreach ($list as $address) { 1255 if ( 1256 '.SYNTAX-ERROR.' !== $address->host && 1257 static::validateAddress($address->mailbox . '@' . $address->host) 1258 ) { 1259 //Decode the name part if it's present and encoded 1260 if ( 1261 property_exists($address, 'personal') && 1262 //Check for a Mbstring constant rather than using extension_loaded, which is sometimes disabled 1263 defined('MB_CASE_UPPER') && 1264 preg_match('/^=\?.*\?=$/s', $address->personal) 1265 ) { 1266 $origCharset = mb_internal_encoding(); 1267 mb_internal_encoding($charset); 1268 //Undo any RFC2047-encoded spaces-as-underscores 1269 $address->personal = str_replace('_', '=20', $address->personal); 1270 //Decode the name 1271 $address->personal = mb_decode_mimeheader($address->personal); 1272 mb_internal_encoding($origCharset); 1273 } 1274 1275 $addresses[] = [ 1276 'name' => (property_exists($address, 'personal') ? $address->personal : ''), 1277 'address' => $address->mailbox . '@' . $address->host, 1278 ]; 1279 } 1280 } 1281 } else { 1282 //Use this simpler parser 1283 $list = explode(',', $addrstr); 1284 foreach ($list as $address) { 1285 $address = trim($address); 1286 //Is there a separate name part? 1287 if (strpos($address, '<') === false) { 1288 //No separate name, just use the whole thing 1289 if (static::validateAddress($address)) { 1290 $addresses[] = [ 1291 'name' => '', 1292 'address' => $address, 1293 ]; 1294 } 1295 } else { 1296 list($name, $email) = explode('<', $address); 1297 $email = trim(str_replace('>', '', $email)); 1298 $name = trim($name); 1299 if (static::validateAddress($email)) { 1300 //Check for a Mbstring constant rather than using extension_loaded, which is sometimes disabled 1301 //If this name is encoded, decode it 1302 if (defined('MB_CASE_UPPER') && preg_match('/^=\?.*\?=$/s', $name)) { 1303 $origCharset = mb_internal_encoding(); 1304 mb_internal_encoding($charset); 1305 //Undo any RFC2047-encoded spaces-as-underscores 1306 $name = str_replace('_', '=20', $name); 1307 //Decode the name 1308 $name = mb_decode_mimeheader($name); 1309 mb_internal_encoding($origCharset); 1310 } 1311 $addresses[] = [ 1312 //Remove any surrounding quotes and spaces from the name 1313 'name' => trim($name, '\'" '), 1314 'address' => $email, 1315 ]; 1316 } 1317 } 1318 } 1319 } 1320 1321 return $addresses; 1322 } 1323 1324 /** 1325 * Set the From and FromName properties. 1326 * 1327 * @param string $address 1328 * @param string $name 1329 * @param bool $auto Whether to also set the Sender address, defaults to true 1330 * 1331 * @throws Exception 1332 * 1333 * @return bool 1334 */ 1335 public function setFrom($address, $name = '', $auto = true) 1336 { 1337 $address = trim((string)$address); 1338 $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim 1339 //Don't validate now addresses with IDN. Will be done in send(). 1340 $pos = strrpos($address, '@'); 1341 if ( 1342 (false === $pos) 1343 || ((!$this->has8bitChars(substr($address, ++$pos)) || !static::idnSupported()) 1344 && !static::validateAddress($address)) 1345 ) { 1346 $error_message = sprintf( 1347 '%s (From): %s', 1348 $this->lang('invalid_address'), 1349 $address 1350 ); 1351 $this->setError($error_message); 1352 $this->edebug($error_message); 1353 if ($this->exceptions) { 1354 throw new Exception($error_message); 1355 } 1356 1357 return false; 1358 } 1359 $this->From = $address; 1360 $this->FromName = $name; 1361 if ($auto && empty($this->Sender)) { 1362 $this->Sender = $address; 1363 } 1364 1365 return true; 1366 } 1367 1368 /** 1369 * Return the Message-ID header of the last email. 1370 * Technically this is the value from the last time the headers were created, 1371 * but it's also the message ID of the last sent message except in 1372 * pathological cases. 1373 * 1374 * @return string 1375 */ 1376 public function getLastMessageID() 1377 { 1378 return $this->lastMessageID; 1379 } 1380 1381 /** 1382 * Check that a string looks like an email address. 1383 * Validation patterns supported: 1384 * * `auto` Pick best pattern automatically; 1385 * * `pcre8` Use the squiloople.com pattern, requires PCRE > 8.0; 1386 * * `pcre` Use old PCRE implementation; 1387 * * `php` Use PHP built-in FILTER_VALIDATE_EMAIL; 1388 * * `html5` Use the pattern given by the HTML5 spec for 'email' type form input elements. 1389 * * `eai` Use a pattern similar to the HTML5 spec for 'email' and to firefox, extended to support EAI (RFC6530). 1390 * * `noregex` Don't use a regex: super fast, really dumb. 1391 * Alternatively you may pass in a callable to inject your own validator, for example: 1392 * 1393 * ```php 1394 * PHPMailer::validateAddress('user@example.com', function($address) { 1395 * return (strpos($address, '@') !== false); 1396 * }); 1397 * ``` 1398 * 1399 * You can also set the PHPMailer::$validator static to a callable, allowing built-in methods to use your validator. 1400 * 1401 * @param string $address The email address to check 1402 * @param string|callable $patternselect Which pattern to use 1403 * 1404 * @return bool 1405 */ 1406 public static function validateAddress($address, $patternselect = null) 1407 { 1408 if (null === $patternselect) { 1409 $patternselect = static::$validator; 1410 } 1411 //Don't allow strings as callables, see SECURITY.md and CVE-2021-3603 1412 if (is_callable($patternselect) && !is_string($patternselect)) { 1413 return call_user_func($patternselect, $address); 1414 } 1415 //Reject line breaks in addresses; it's valid RFC5322, but not RFC5321 1416 if (strpos($address, "\n") !== false || strpos($address, "\r") !== false) { 1417 return false; 1418 } 1419 switch ($patternselect) { 1420 case 'pcre': //Kept for BC 1421 case 'pcre8': 1422 /* 1423 * A more complex and more permissive version of the RFC5322 regex on which FILTER_VALIDATE_EMAIL 1424 * is based. 1425 * In addition to the addresses allowed by filter_var, also permits: 1426 * * dotless domains: `a@b` 1427 * * comments: `1234 @ local(blah) .machine .example` 1428 * * quoted elements: `'"test blah"@example.org'` 1429 * * numeric TLDs: `a@b.123` 1430 * * unbracketed IPv4 literals: `a@192.168.0.1` 1431 * * IPv6 literals: 'first.last@[IPv6:a1::]' 1432 * Not all of these will necessarily work for sending! 1433 * 1434 * @copyright 2009-2010 Michael Rushton 1435 * Feel free to use and redistribute this code. But please keep this copyright notice. 1436 */ 1437 return (bool) preg_match( 1438 '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' . 1439 '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' . 1440 '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' . 1441 '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' . 1442 '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' . 1443 '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' . 1444 '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' . 1445 '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' . 1446 '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD', 1447 $address 1448 ); 1449 case 'html5': 1450 /* 1451 * This is the pattern used in the HTML5 spec for validation of 'email' type form input elements. 1452 * 1453 * @see https://html.spec.whatwg.org/#e-mail-state-(type=email) 1454 */ 1455 return (bool) preg_match( 1456 '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}' . 1457 '[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/sD', 1458 $address 1459 ); 1460 case 'eai': 1461 /* 1462 * This is the pattern used in the HTML5 spec for validation of 'email' type 1463 * form input elements (as above), modified to accept Unicode email addresses. 1464 * This is also more lenient than Firefox' html5 spec, in order to make the regex faster. 1465 * 'eai' is an acronym for Email Address Internationalization. 1466 * This validator is selected automatically if you attempt to use recipient addresses 1467 * that contain Unicode characters in the local part. 1468 * 1469 * @see https://html.spec.whatwg.org/#e-mail-state-(type=email) 1470 * @see https://en.wikipedia.org/wiki/International_email 1471 */ 1472 return (bool) preg_match( 1473 '/^[-\p{L}\p{N}\p{M}.!#$%&\'*+\/=?^_`{|}~]+@[\p{L}\p{N}\p{M}](?:[\p{L}\p{N}\p{M}-]{0,61}' . 1474 '[\p{L}\p{N}\p{M}])?(?:\.[\p{L}\p{N}\p{M}]' . 1475 '(?:[-\p{L}\p{N}\p{M}]{0,61}[\p{L}\p{N}\p{M}])?)*$/usD', 1476 $address 1477 ); 1478 case 'php': 1479 default: 1480 return filter_var($address, FILTER_VALIDATE_EMAIL) !== false; 1481 } 1482 } 1483 1484 /** 1485 * Tells whether IDNs (Internationalized Domain Names) are supported or not. This requires the 1486 * `intl` and `mbstring` PHP extensions. 1487 * 1488 * @return bool `true` if required functions for IDN support are present 1489 */ 1490 public static function idnSupported() 1491 { 1492 return function_exists('idn_to_ascii') && function_exists('mb_convert_encoding'); 1493 } 1494 1495 /** 1496 * Converts IDN in given email address to its ASCII form, also known as punycode, if possible. 1497 * Important: Address must be passed in same encoding as currently set in PHPMailer::$CharSet. 1498 * This function silently returns unmodified address if: 1499 * - No conversion is necessary (i.e. domain name is not an IDN, or is already in ASCII form) 1500 * - Conversion to punycode is impossible (e.g. required PHP functions are not available) 1501 * or fails for any reason (e.g. domain contains characters not allowed in an IDN). 1502 * 1503 * @see PHPMailer::$CharSet 1504 * 1505 * @param string $address The email address to convert 1506 * 1507 * @return string The encoded address in ASCII form 1508 */ 1509 public function punyencodeAddress($address) 1510 { 1511 //Verify we have required functions, CharSet, and at-sign. 1512 $pos = strrpos($address, '@'); 1513 if ( 1514 !empty($this->CharSet) && 1515 false !== $pos && 1516 static::idnSupported() 1517 ) { 1518 $domain = substr($address, ++$pos); 1519 //Verify CharSet string is a valid one, and domain properly encoded in this CharSet. 1520 if ($this->has8bitChars($domain) && @mb_check_encoding($domain, $this->CharSet)) { 1521 //Convert the domain from whatever charset it's in to UTF-8 1522 $domain = mb_convert_encoding($domain, self::CHARSET_UTF8, $this->CharSet); 1523 //Ignore IDE complaints about this line - method signature changed in PHP 5.4 1524 $errorcode = 0; 1525 if (defined('INTL_IDNA_VARIANT_UTS46')) { 1526 //Use the current punycode standard (appeared in PHP 7.2) 1527 $punycode = idn_to_ascii( 1528 $domain, 1529 \IDNA_DEFAULT | \IDNA_USE_STD3_RULES | \IDNA_CHECK_BIDI | 1530 \IDNA_CHECK_CONTEXTJ | \IDNA_NONTRANSITIONAL_TO_ASCII, 1531 \INTL_IDNA_VARIANT_UTS46 1532 ); 1533 } elseif (defined('INTL_IDNA_VARIANT_2003')) { 1534 //Fall back to this old, deprecated/removed encoding 1535 // phpcs:ignore PHPCompatibility.Constants.RemovedConstants.intl_idna_variant_2003Deprecated 1536 $punycode = idn_to_ascii($domain, $errorcode, \INTL_IDNA_VARIANT_2003); 1537 } else { 1538 //Fall back to a default we don't know about 1539 // phpcs:ignore PHPCompatibility.ParameterValues.NewIDNVariantDefault.NotSet 1540 $punycode = idn_to_ascii($domain, $errorcode); 1541 } 1542 if (false !== $punycode) { 1543 return substr($address, 0, $pos) . $punycode; 1544 } 1545 } 1546 } 1547 1548 return $address; 1549 } 1550 1551 /** 1552 * Create a message and send it. 1553 * Uses the sending method specified by $Mailer. 1554 * 1555 * @throws Exception 1556 * 1557 * @return bool false on error - See the ErrorInfo property for details of the error 1558 */ 1559 public function send() 1560 { 1561 try { 1562 if (!$this->preSend()) { 1563 return false; 1564 } 1565 1566 return $this->postSend(); 1567 } catch (Exception $exc) { 1568 $this->mailHeader = ''; 1569 $this->setError($exc->getMessage()); 1570 if ($this->exceptions) { 1571 throw $exc; 1572 } 1573 1574 return false; 1575 } 1576 } 1577 1578 /** 1579 * Prepare a message for sending. 1580 * 1581 * @throws Exception 1582 * 1583 * @return bool 1584 */ 1585 public function preSend() 1586 { 1587 if ( 1588 'smtp' === $this->Mailer 1589 || ('mail' === $this->Mailer && (\PHP_VERSION_ID >= 80000 || stripos(PHP_OS, 'WIN') === 0)) 1590 ) { 1591 //SMTP mandates RFC-compliant line endings 1592 //and it's also used with mail() on Windows 1593 static::setLE(self::CRLF); 1594 } else { 1595 //Maintain backward compatibility with legacy Linux command line mailers 1596 static::setLE(PHP_EOL); 1597 } 1598 //Check for buggy PHP versions that add a header with an incorrect line break 1599 if ( 1600 'mail' === $this->Mailer 1601 && ((\PHP_VERSION_ID >= 70000 && \PHP_VERSION_ID < 70017) 1602 || (\PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70103)) 1603 && ini_get('mail.add_x_header') === '1' 1604 && stripos(PHP_OS, 'WIN') === 0 1605 ) { 1606 trigger_error($this->lang('buggy_php'), E_USER_WARNING); 1607 } 1608 1609 try { 1610 $this->error_count = 0; //Reset errors 1611 $this->mailHeader = ''; 1612 1613 //The code below tries to support full use of Unicode, 1614 //while remaining compatible with legacy SMTP servers to 1615 //the greatest degree possible: If the message uses 1616 //Unicode in the local parts of any addresses, it is sent 1617 //using SMTPUTF8. If not, it it sent using 1618 //punycode-encoded domains and plain SMTP. 1619 if ( 1620 static::CHARSET_UTF8 === strtolower($this->CharSet) && 1621 ($this->anyAddressHasUnicodeLocalPart($this->RecipientsQueue) || 1622 $this->anyAddressHasUnicodeLocalPart(array_keys($this->all_recipients)) || 1623 $this->anyAddressHasUnicodeLocalPart($this->ReplyToQueue) || 1624 $this->addressHasUnicodeLocalPart($this->From)) 1625 ) { 1626 $this->UseSMTPUTF8 = true; 1627 } 1628 //Dequeue recipient and Reply-To addresses with IDN 1629 foreach (array_merge($this->RecipientsQueue, $this->ReplyToQueue) as $params) { 1630 if (!$this->UseSMTPUTF8) { 1631 $params[1] = $this->punyencodeAddress($params[1]); 1632 } 1633 call_user_func_array([$this, 'addAnAddress'], $params); 1634 } 1635 if (count($this->to) + count($this->cc) + count($this->bcc) < 1) { 1636 throw new Exception($this->lang('provide_address'), self::STOP_CRITICAL); 1637 } 1638 1639 //Validate From, Sender, and ConfirmReadingTo addresses 1640 foreach (['From', 'Sender', 'ConfirmReadingTo'] as $address_kind) { 1641 if ($this->{$address_kind} === null) { 1642 $this->{$address_kind} = ''; 1643 continue; 1644 } 1645 $this->{$address_kind} = trim($this->{$address_kind}); 1646 if (empty($this->{$address_kind})) { 1647 continue; 1648 } 1649 $this->{$address_kind} = $this->punyencodeAddress($this->{$address_kind}); 1650 if (!static::validateAddress($this->{$address_kind})) { 1651 $error_message = sprintf( 1652 '%s (%s): %s', 1653 $this->lang('invalid_address'), 1654 $address_kind, 1655 $this->{$address_kind} 1656 ); 1657 $this->setError($error_message); 1658 $this->edebug($error_message); 1659 if ($this->exceptions) { 1660 throw new Exception($error_message); 1661 } 1662 1663 return false; 1664 } 1665 } 1666 1667 //Set whether the message is multipart/alternative 1668 if ($this->alternativeExists()) { 1669 $this->ContentType = static::CONTENT_TYPE_MULTIPART_ALTERNATIVE; 1670 } 1671 1672 $this->setMessageType(); 1673 //Refuse to send an empty message unless we are specifically allowing it 1674 if (!$this->AllowEmpty && empty($this->Body)) { 1675 throw new Exception($this->lang('empty_message'), self::STOP_CRITICAL); 1676 } 1677 1678 //Trim subject consistently 1679 $this->Subject = trim($this->Subject); 1680 //Create body before headers in case body makes changes to headers (e.g. altering transfer encoding) 1681 $this->MIMEHeader = ''; 1682 $this->MIMEBody = $this->createBody(); 1683 //createBody may have added some headers, so retain them 1684 $tempheaders = $this->MIMEHeader; 1685 $this->MIMEHeader = $this->createHeader(); 1686 $this->MIMEHeader .= $tempheaders; 1687 1688 //To capture the complete message when using mail(), create 1689 //an extra header list which createHeader() doesn't fold in 1690 if ('mail' === $this->Mailer) { 1691 if (count($this->to) > 0) { 1692 $this->mailHeader .= $this->addrAppend('To', $this->to); 1693 } else { 1694 $this->mailHeader .= $this->headerLine('To', 'undisclosed-recipients:;'); 1695 } 1696 $this->mailHeader .= $this->headerLine( 1697 'Subject', 1698 $this->encodeHeader($this->secureHeader($this->Subject)) 1699 ); 1700 } 1701 1702 //Sign with DKIM if enabled 1703 if ( 1704 !empty($this->DKIM_domain) 1705 && !empty($this->DKIM_selector) 1706 && (!empty($this->DKIM_private_string) 1707 || (!empty($this->DKIM_private) 1708 && static::isPermittedPath($this->DKIM_private) 1709 && file_exists($this->DKIM_private) 1710 ) 1711 ) 1712 ) { 1713 $header_dkim = $this->DKIM_Add( 1714 $this->MIMEHeader . $this->mailHeader, 1715 $this->encodeHeader($this->secureHeader($this->Subject)), 1716 $this->MIMEBody 1717 ); 1718 $this->MIMEHeader = static::stripTrailingWSP($this->MIMEHeader) . static::$LE . 1719 static::normalizeBreaks($header_dkim) . static::$LE; 1720 } 1721 1722 return true; 1723 } catch (Exception $exc) { 1724 $this->setError($exc->getMessage()); 1725 if ($this->exceptions) { 1726 throw $exc; 1727 } 1728 1729 return false; 1730 } 1731 } 1732 1733 /** 1734 * Actually send a message via the selected mechanism. 1735 * 1736 * @throws Exception 1737 * 1738 * @return bool 1739 */ 1740 public function postSend() 1741 { 1742 try { 1743 //Choose the mailer and send through it 1744 switch ($this->Mailer) { 1745 case 'sendmail': 1746 case 'qmail': 1747 return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody); 1748 case 'smtp': 1749 return $this->smtpSend($this->MIMEHeader, $this->MIMEBody); 1750 case 'mail': 1751 return $this->mailSend($this->MIMEHeader, $this->MIMEBody); 1752 default: 1753 $sendMethod = $this->Mailer . 'Send'; 1754 if (method_exists($this, $sendMethod)) { 1755 return $this->{$sendMethod}($this->MIMEHeader, $this->MIMEBody); 1756 } 1757 1758 return $this->mailSend($this->MIMEHeader, $this->MIMEBody); 1759 } 1760 } catch (Exception $exc) { 1761 $this->setError($exc->getMessage()); 1762 $this->edebug($exc->getMessage()); 1763 if ($this->Mailer === 'smtp' && $this->SMTPKeepAlive == true && $this->smtp->connected()) { 1764 $this->smtp->reset(); 1765 } 1766 if ($this->exceptions) { 1767 throw $exc; 1768 } 1769 } 1770 1771 return false; 1772 } 1773 1774 /** 1775 * Send mail using the $Sendmail program. 1776 * 1777 * @see PHPMailer::$Sendmail 1778 * 1779 * @param string $header The message headers 1780 * @param string $body The message body 1781 * 1782 * @throws Exception 1783 * 1784 * @return bool 1785 */ 1786 protected function sendmailSend($header, $body) 1787 { 1788 if ($this->Mailer === 'qmail') { 1789 $this->edebug('Sending with qmail'); 1790 } else { 1791 $this->edebug('Sending with sendmail'); 1792 } 1793 $header = static::stripTrailingWSP($header) . static::$LE . static::$LE; 1794 //This sets the SMTP envelope sender which gets turned into a return-path header by the receiver 1795 //A space after `-f` is optional, but there is a long history of its presence 1796 //causing problems, so we don't use one 1797 //Exim docs: https://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html 1798 //Sendmail docs: https://www.sendmail.org/~ca/email/man/sendmail.html 1799 //Example problem: https://www.drupal.org/node/1057954 1800 1801 //PHP 5.6 workaround 1802 $sendmail_from_value = ini_get('sendmail_from'); 1803 if (empty($this->Sender) && !empty($sendmail_from_value)) { 1804 //PHP config has a sender address we can use 1805 $this->Sender = ini_get('sendmail_from'); 1806 } 1807 //CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. 1808 if (!empty($this->Sender) && static::validateAddress($this->Sender) && self::isShellSafe($this->Sender)) { 1809 if ($this->Mailer === 'qmail') { 1810 $sendmailFmt = '%s -f%s'; 1811 } else { 1812 $sendmailFmt = '%s -oi -f%s -t'; 1813 } 1814 } else { 1815 //allow sendmail to choose a default envelope sender. It may 1816 //seem preferable to force it to use the From header as with 1817 //SMTP, but that introduces new problems (see 1818 //<https://github.com/PHPMailer/PHPMailer/issues/2298>), and 1819 //it has historically worked this way. 1820 $sendmailFmt = '%s -oi -t'; 1821 } 1822 1823 $sendmail = sprintf($sendmailFmt, escapeshellcmd($this->Sendmail), $this->Sender); 1824 $this->edebug('Sendmail path: ' . $this->Sendmail); 1825 $this->edebug('Sendmail command: ' . $sendmail); 1826 $this->edebug('Envelope sender: ' . $this->Sender); 1827 $this->edebug("Headers: {$header}"); 1828 1829 if ($this->SingleTo) { 1830 foreach ($this->SingleToArray as $toAddr) { 1831 $mail = @popen($sendmail, 'w'); 1832 if (!$mail) { 1833 throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 1834 } 1835 $this->edebug("To: {$toAddr}"); 1836 fwrite($mail, 'To: ' . $toAddr . "\n"); 1837 fwrite($mail, $header); 1838 fwrite($mail, $body); 1839 $result = pclose($mail); 1840 $addrinfo = static::parseAddresses($toAddr, true, $this->CharSet); 1841 $this->doCallback( 1842 ($result === 0), 1843 [[$addrinfo['address'], $addrinfo['name']]], 1844 $this->cc, 1845 $this->bcc, 1846 $this->Subject, 1847 $body, 1848 $this->From, 1849 [] 1850 ); 1851 $this->edebug("Result: " . ($result === 0 ? 'true' : 'false')); 1852 if (0 !== $result) { 1853 throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 1854 } 1855 } 1856 } else { 1857 $mail = @popen($sendmail, 'w'); 1858 if (!$mail) { 1859 throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 1860 } 1861 fwrite($mail, $header); 1862 fwrite($mail, $body); 1863 $result = pclose($mail); 1864 $this->doCallback( 1865 ($result === 0), 1866 $this->to, 1867 $this->cc, 1868 $this->bcc, 1869 $this->Subject, 1870 $body, 1871 $this->From, 1872 [] 1873 ); 1874 $this->edebug("Result: " . ($result === 0 ? 'true' : 'false')); 1875 if (0 !== $result) { 1876 throw new Exception($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL); 1877 } 1878 } 1879 1880 return true; 1881 } 1882 1883 /** 1884 * Fix CVE-2016-10033 and CVE-2016-10045 by disallowing potentially unsafe shell characters. 1885 * Note that escapeshellarg and escapeshellcmd are inadequate for our purposes, especially on Windows. 1886 * 1887 * @see https://github.com/PHPMailer/PHPMailer/issues/924 CVE-2016-10045 bug report 1888 * 1889 * @param string $string The string to be validated 1890 * 1891 * @return bool 1892 */ 1893 protected static function isShellSafe($string) 1894 { 1895 //It's not possible to use shell commands safely (which includes the mail() function) without escapeshellarg, 1896 //but some hosting providers disable it, creating a security problem that we don't want to have to deal with, 1897 //so we don't. 1898 if (!function_exists('escapeshellarg') || !function_exists('escapeshellcmd')) { 1899 return false; 1900 } 1901 1902 if ( 1903 escapeshellcmd($string) !== $string 1904 || !in_array(escapeshellarg($string), ["'$string'", "\"$string\""]) 1905 ) { 1906 return false; 1907 } 1908 1909 $length = strlen($string); 1910 1911 for ($i = 0; $i < $length; ++$i) { 1912 $c = $string[$i]; 1913 1914 //All other characters have a special meaning in at least one common shell, including = and +. 1915 //Full stop (.) has a special meaning in cmd.exe, but its impact should be negligible here. 1916 //Note that this does permit non-Latin alphanumeric characters based on the current locale. 1917 if (!ctype_alnum($c) && strpos('@_-.', $c) === false) { 1918 return false; 1919 } 1920 } 1921 1922 return true; 1923 } 1924 1925 /** 1926 * Check whether a file path is of a permitted type. 1927 * Used to reject URLs and phar files from functions that access local file paths, 1928 * such as addAttachment. 1929 * 1930 * @param string $path A relative or absolute path to a file 1931 * 1932 * @return bool 1933 */ 1934 protected static function isPermittedPath($path) 1935 { 1936 //Matches scheme definition from https://www.rfc-editor.org/rfc/rfc3986#section-3.1 1937 return !preg_match('#^[a-z][a-z\d+.-]*://#i', $path); 1938 } 1939 1940 /** 1941 * Check whether a file path is safe, accessible, and readable. 1942 * 1943 * @param string $path A relative or absolute path to a file 1944 * 1945 * @return bool 1946 */ 1947 protected static function fileIsAccessible($path) 1948 { 1949 if (!static::isPermittedPath($path)) { 1950 return false; 1951 } 1952 $readable = is_file($path); 1953 //If not a UNC path (expected to start with \\), check read permission, see #2069 1954 if (strpos($path, '\\\\') !== 0) { 1955 $readable = $readable && is_readable($path); 1956 } 1957 return $readable; 1958 } 1959 1960 /** 1961 * Send mail using the PHP mail() function. 1962 * 1963 * @see https://www.php.net/manual/en/book.mail.php 1964 * 1965 * @param string $header The message headers 1966 * @param string $body The message body 1967 * 1968 * @throws Exception 1969 * 1970 * @return bool 1971 */ 1972 protected function mailSend($header, $body) 1973 { 1974 $header = static::stripTrailingWSP($header) . static::$LE . static::$LE; 1975 1976 $toArr = []; 1977 foreach ($this->to as $toaddr) { 1978 $toArr[] = $this->addrFormat($toaddr); 1979 } 1980 $to = trim(implode(', ', $toArr)); 1981 1982 //If there are no To-addresses (e.g. when sending only to BCC-addresses) 1983 //the following should be added to get a correct DKIM-signature. 1984 //Compare with $this->preSend() 1985 if ($to === '') { 1986 $to = 'undisclosed-recipients:;'; 1987 } 1988 1989 $params = null; 1990 //This sets the SMTP envelope sender which gets turned into a return-path header by the receiver 1991 //A space after `-f` is optional, but there is a long history of its presence 1992 //causing problems, so we don't use one 1993 //Exim docs: https://www.exim.org/exim-html-current/doc/html/spec_html/ch-the_exim_command_line.html 1994 //Sendmail docs: https://www.sendmail.org/~ca/email/man/sendmail.html 1995 //Example problem: https://www.drupal.org/node/1057954 1996 //CVE-2016-10033, CVE-2016-10045: Don't pass -f if characters will be escaped. 1997 1998 //PHP 5.6 workaround 1999 $sendmail_from_value = ini_get('sendmail_from'); 2000 if (empty($this->Sender) && !empty($sendmail_from_value)) { 2001 //PHP config has a sender address we can use 2002 $this->Sender = ini_get('sendmail_from'); 2003 } 2004 if (!empty($this->Sender) && static::validateAddress($this->Sender)) { 2005 if (self::isShellSafe($this->Sender)) { 2006 $params = sprintf('-f%s', $this->Sender); 2007 } 2008 $old_from = ini_get('sendmail_from'); 2009 ini_set('sendmail_from', $this->Sender); 2010 } 2011 $result = false; 2012 if ($this->SingleTo && count($toArr) > 1) { 2013 foreach ($toArr as $toAddr) { 2014 $result = $this->mailPassthru($toAddr, $this->Subject, $body, $header, $params); 2015 $addrinfo = static::parseAddresses($toAddr, true, $this->CharSet); 2016 $this->doCallback( 2017 $result, 2018 [[$addrinfo['address'], $addrinfo['name']]], 2019 $this->cc, 2020 $this->bcc, 2021 $this->Subject, 2022 $body, 2023 $this->From, 2024 [] 2025 ); 2026 } 2027 } else { 2028 $result = $this->mailPassthru($to, $this->Subject, $body, $header, $params); 2029 $this->doCallback($result, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From, []); 2030 } 2031 if (isset($old_from)) { 2032 ini_set('sendmail_from', $old_from); 2033 } 2034 if (!$result) { 2035 throw new Exception($this->lang('instantiate'), self::STOP_CRITICAL); 2036 } 2037 2038 return true; 2039 } 2040 2041 /** 2042 * Get an instance to use for SMTP operations. 2043 * Override this function to load your own SMTP implementation, 2044 * or set one with setSMTPInstance. 2045 * 2046 * @return SMTP 2047 */ 2048 public function getSMTPInstance() 2049 { 2050 if (!is_object($this->smtp)) { 2051 $this->smtp = new SMTP(); 2052 } 2053 2054 return $this->smtp; 2055 } 2056 2057 /** 2058 * Provide an instance to use for SMTP operations. 2059 * 2060 * @return SMTP 2061 */ 2062 public function setSMTPInstance(SMTP $smtp) 2063 { 2064 $this->smtp = $smtp; 2065 2066 return $this->smtp; 2067 } 2068 2069 /** 2070 * Provide SMTP XCLIENT attributes 2071 * 2072 * @param string $name Attribute name 2073 * @param ?string $value Attribute value 2074 * 2075 * @return bool 2076 */ 2077 public function setSMTPXclientAttribute($name, $value) 2078 { 2079 if (!in_array($name, SMTP::$xclient_allowed_attributes)) { 2080 return false; 2081 } 2082 if (isset($this->SMTPXClient[$name]) && $value === null) { 2083 unset($this->SMTPXClient[$name]); 2084 } elseif ($value !== null) { 2085 $this->SMTPXClient[$name] = $value; 2086 } 2087 2088 return true; 2089 } 2090 2091 /** 2092 * Get SMTP XCLIENT attributes 2093 * 2094 * @return array 2095 */ 2096 public function getSMTPXclientAttributes() 2097 { 2098 return $this->SMTPXClient; 2099 } 2100 2101 /** 2102 * Send mail via SMTP. 2103 * Returns false if there is a bad MAIL FROM, RCPT, or DATA input. 2104 * 2105 * @see PHPMailer::setSMTPInstance() to use a different class. 2106 * 2107 * @uses \PHPMailer\PHPMailer\SMTP 2108 * 2109 * @param string $header The message headers 2110 * @param string $body The message body 2111 * 2112 * @throws Exception 2113 * 2114 * @return bool 2115 */ 2116 protected function smtpSend($header, $body) 2117 { 2118 $header = static::stripTrailingWSP($header) . static::$LE . static::$LE; 2119 $bad_rcpt = []; 2120 if (!$this->smtpConnect($this->SMTPOptions)) { 2121 throw new Exception($this->lang('smtp_connect_failed'), self::STOP_CRITICAL); 2122 } 2123 //If we have recipient addresses that need Unicode support, 2124 //but the server doesn't support it, stop here 2125 if ($this->UseSMTPUTF8 && !$this->smtp->getServerExt('SMTPUTF8')) { 2126 throw new Exception($this->lang('no_smtputf8'), self::STOP_CRITICAL); 2127 } 2128 //Sender already validated in preSend() 2129 if ('' === $this->Sender) { 2130 $smtp_from = $this->From; 2131 } else { 2132 $smtp_from = $this->Sender; 2133 } 2134 if (count($this->SMTPXClient)) { 2135 $this->smtp->xclient($this->SMTPXClient); 2136 } 2137 if (!$this->smtp->mail($smtp_from)) { 2138 $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError())); 2139 throw new Exception($this->ErrorInfo, self::STOP_CRITICAL); 2140 } 2141 2142 $callbacks = []; 2143 //Attempt to send to all recipients 2144 foreach ([$this->to, $this->cc, $this->bcc] as $togroup) { 2145 foreach ($togroup as $to) { 2146 if (!$this->smtp->recipient($to[0], $this->dsn)) { 2147 $error = $this->smtp->getError(); 2148 $bad_rcpt[] = ['to' => $to[0], 'error' => $error['detail']]; 2149 $isSent = false; 2150 } else { 2151 $isSent = true; 2152 } 2153 2154 $callbacks[] = ['issent' => $isSent, 'to' => $to[0], 'name' => $to[1]]; 2155 } 2156 } 2157 2158 //Only send the DATA command if we have viable recipients 2159 if ((count($this->all_recipients) > count($bad_rcpt)) && !$this->smtp->data($header . $body)) { 2160 throw new Exception($this->lang('data_not_accepted'), self::STOP_CRITICAL); 2161 } 2162 2163 $smtp_transaction_id = $this->smtp->getLastTransactionID(); 2164 2165 if ($this->SMTPKeepAlive) { 2166 $this->smtp->reset(); 2167 } else { 2168 $this->smtp->quit(); 2169 $this->smtp->close(); 2170 } 2171 2172 foreach ($callbacks as $cb) { 2173 $this->doCallback( 2174 $cb['issent'], 2175 [[$cb['to'], $cb['name']]], 2176 [], 2177 [], 2178 $this->Subject, 2179 $body, 2180 $this->From, 2181 ['smtp_transaction_id' => $smtp_transaction_id] 2182 ); 2183 } 2184 2185 //Create error message for any bad addresses 2186 if (count($bad_rcpt) > 0) { 2187 $errstr = ''; 2188 foreach ($bad_rcpt as $bad) { 2189 $errstr .= $bad['to'] . ': ' . $bad['error']; 2190 } 2191 throw new Exception($this->lang('recipients_failed') . $errstr, self::STOP_CONTINUE); 2192 } 2193 2194 return true; 2195 } 2196 2197 /** 2198 * Initiate a connection to an SMTP server. 2199 * Returns false if the operation failed. 2200 * 2201 * @param array $options An array of options compatible with stream_context_create() 2202 * 2203 * @throws Exception 2204 * 2205 * @uses \PHPMailer\PHPMailer\SMTP 2206 * 2207 * @return bool 2208 */ 2209 public function smtpConnect($options = null) 2210 { 2211 if (null === $this->smtp) { 2212 $this->smtp = $this->getSMTPInstance(); 2213 } 2214 2215 //If no options are provided, use whatever is set in the instance 2216 if (null === $options) { 2217 $options = $this->SMTPOptions; 2218 } 2219 2220 //Already connected? 2221 if ($this->smtp->connected()) { 2222 return true; 2223 } 2224 2225 $this->smtp->setTimeout($this->Timeout); 2226 $this->smtp->setDebugLevel($this->SMTPDebug); 2227 $this->smtp->setDebugOutput($this->Debugoutput); 2228 $this->smtp->setVerp($this->do_verp); 2229 $this->smtp->setSMTPUTF8($this->UseSMTPUTF8); 2230 if ($this->Host === null) { 2231 $this->Host = 'localhost'; 2232 } 2233 $hosts = explode(';', $this->Host); 2234 $lastexception = null; 2235 2236 foreach ($hosts as $hostentry) { 2237 $hostinfo = []; 2238 if ( 2239 !preg_match( 2240 '/^(?:(ssl|tls):\/\/)?(.+?)(?::(\d+))?$/', 2241 trim($hostentry), 2242 $hostinfo 2243 ) 2244 ) { 2245 $this->edebug($this->lang('invalid_hostentry') . ' ' . trim($hostentry)); 2246 //Not a valid host entry 2247 continue; 2248 } 2249 //$hostinfo[1]: optional ssl or tls prefix 2250 //$hostinfo[2]: the hostname 2251 //$hostinfo[3]: optional port number 2252 //The host string prefix can temporarily override the current setting for SMTPSecure 2253 //If it's not specified, the default value is used 2254 2255 //Check the host name is a valid name or IP address before trying to use it 2256 if (!static::isValidHost($hostinfo[2])) { 2257 $this->edebug($this->lang('invalid_host') . ' ' . $hostinfo[2]); 2258 continue; 2259 } 2260 $prefix = ''; 2261 $secure = $this->SMTPSecure; 2262 $tls = (static::ENCRYPTION_STARTTLS === $this->SMTPSecure); 2263 if ('ssl' === $hostinfo[1] || ('' === $hostinfo[1] && static::ENCRYPTION_SMTPS === $this->SMTPSecure)) { 2264 $prefix = 'ssl://'; 2265 $tls = false; //Can't have SSL and TLS at the same time 2266 $secure = static::ENCRYPTION_SMTPS; 2267 } elseif ('tls' === $hostinfo[1]) { 2268 $tls = true; 2269 //TLS doesn't use a prefix 2270 $secure = static::ENCRYPTION_STARTTLS; 2271 } 2272 //Do we need the OpenSSL extension? 2273 $sslext = defined('OPENSSL_ALGO_SHA256'); 2274 if (static::ENCRYPTION_STARTTLS === $secure || static::ENCRYPTION_SMTPS === $secure) { 2275 //Check for an OpenSSL constant rather than using extension_loaded, which is sometimes disabled 2276 if (!$sslext) { 2277 throw new Exception($this->lang('extension_missing') . 'openssl', self::STOP_CRITICAL); 2278 } 2279 } 2280 $host = $hostinfo[2]; 2281 $port = $this->Port; 2282 if ( 2283 array_key_exists(3, $hostinfo) && 2284 is_numeric($hostinfo[3]) && 2285 $hostinfo[3] > 0 && 2286 $hostinfo[3] < 65536 2287 ) { 2288 $port = (int) $hostinfo[3]; 2289 } 2290 if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) { 2291 try { 2292 if ($this->Helo) { 2293 $hello = $this->Helo; 2294 } else { 2295 $hello = $this->serverHostname(); 2296 } 2297 $this->smtp->hello($hello); 2298 //Automatically enable TLS encryption if: 2299 //* it's not disabled 2300 //* we are not connecting to localhost 2301 //* we have openssl extension 2302 //* we are not already using SSL 2303 //* the server offers STARTTLS 2304 if ( 2305 $this->SMTPAutoTLS && 2306 $this->Host !== 'localhost' && 2307 $sslext && 2308 $secure !== 'ssl' && 2309 $this->smtp->getServerExt('STARTTLS') 2310 ) { 2311 $tls = true; 2312 } 2313 if ($tls) { 2314 if (!$this->smtp->startTLS()) { 2315 $message = $this->getSmtpErrorMessage('connect_host'); 2316 throw new Exception($message); 2317 } 2318 //We must resend EHLO after TLS negotiation 2319 $this->smtp->hello($hello); 2320 } 2321 if ( 2322 $this->SMTPAuth && !$this->smtp->authenticate( 2323 $this->Username, 2324 $this->Password, 2325 $this->AuthType, 2326 $this->oauth 2327 ) 2328 ) { 2329 throw new Exception($this->lang('authenticate')); 2330 } 2331 2332 return true; 2333 } catch (Exception $exc) { 2334 $lastexception = $exc; 2335 $this->edebug($exc->getMessage()); 2336 //We must have connected, but then failed TLS or Auth, so close connection nicely 2337 $this->smtp->quit(); 2338 } 2339 } 2340 } 2341 //If we get here, all connection attempts have failed, so close connection hard 2342 $this->smtp->close(); 2343 //As we've caught all exceptions, just report whatever the last one was 2344 if ($this->exceptions && null !== $lastexception) { 2345 throw $lastexception; 2346 } 2347 if ($this->exceptions) { 2348 // no exception was thrown, likely $this->smtp->connect() failed 2349 $message = $this->getSmtpErrorMessage('connect_host'); 2350 throw new Exception($message); 2351 } 2352 2353 return false; 2354 } 2355 2356 /** 2357 * Close the active SMTP session if one exists. 2358 */ 2359 public function smtpClose() 2360 { 2361 if ((null !== $this->smtp) && $this->smtp->connected()) { 2362 $this->smtp->quit(); 2363 $this->smtp->close(); 2364 } 2365 } 2366 2367 /** 2368 * Set the language for error messages. 2369 * The default language is English. 2370 * 2371 * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr") 2372 * Optionally, the language code can be enhanced with a 4-character 2373 * script annotation and/or a 2-character country annotation. 2374 * @param string $lang_path Path to the language file directory, with trailing separator (slash) 2375 * Do not set this from user input! 2376 * 2377 * @return bool Returns true if the requested language was loaded, false otherwise. 2378 */ 2379 public function setLanguage($langcode = 'en', $lang_path = '') 2380 { 2381 //Backwards compatibility for renamed language codes 2382 $renamed_langcodes = [ 2383 'br' => 'pt_br', 2384 'cz' => 'cs', 2385 'dk' => 'da', 2386 'no' => 'nb', 2387 'se' => 'sv', 2388 'rs' => 'sr', 2389 'tg' => 'tl', 2390 'am' => 'hy', 2391 ]; 2392 2393 if (array_key_exists($langcode, $renamed_langcodes)) { 2394 $langcode = $renamed_langcodes[$langcode]; 2395 } 2396 2397 //Define full set of translatable strings in English 2398 $PHPMAILER_LANG = [ 2399 'authenticate' => 'SMTP Error: Could not authenticate.', 2400 'buggy_php' => 'Your version of PHP is affected by a bug that may result in corrupted messages.' . 2401 ' To fix it, switch to sending using SMTP, disable the mail.add_x_header option in' . 2402 ' your php.ini, switch to MacOS or Linux, or upgrade your PHP to version 7.0.17+ or 7.1.3+.', 2403 'connect_host' => 'SMTP Error: Could not connect to SMTP host.', 2404 'data_not_accepted' => 'SMTP Error: data not accepted.', 2405 'empty_message' => 'Message body empty', 2406 'encoding' => 'Unknown encoding: ', 2407 'execute' => 'Could not execute: ', 2408 'extension_missing' => 'Extension missing: ', 2409 'file_access' => 'Could not access file: ', 2410 'file_open' => 'File Error: Could not open file: ', 2411 'from_failed' => 'The following From address failed: ', 2412 'instantiate' => 'Could not instantiate mail function.', 2413 'invalid_address' => 'Invalid address: ', 2414 'invalid_header' => 'Invalid header name or value', 2415 'invalid_hostentry' => 'Invalid hostentry: ', 2416 'invalid_host' => 'Invalid host: ', 2417 'mailer_not_supported' => ' mailer is not supported.', 2418 'provide_address' => 'You must provide at least one recipient email address.', 2419 'recipients_failed' => 'SMTP Error: The following recipients failed: ', 2420 'signing' => 'Signing Error: ', 2421 'smtp_code' => 'SMTP code: ', 2422 'smtp_code_ex' => 'Additional SMTP info: ', 2423 'smtp_connect_failed' => 'SMTP connect() failed.', 2424 'smtp_detail' => 'Detail: ', 2425 'smtp_error' => 'SMTP server error: ', 2426 'variable_set' => 'Cannot set or reset variable: ', 2427 'no_smtputf8' => 'Server does not support SMTPUTF8 needed to send to Unicode addresses', 2428 ]; 2429 if (empty($lang_path)) { 2430 //Calculate an absolute path so it can work if CWD is not here 2431 $lang_path = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR; 2432 } 2433 2434 //Validate $langcode 2435 $foundlang = true; 2436 $langcode = strtolower($langcode); 2437 if ( 2438 !preg_match('/^(?P<lang>[a-z]{2})(?P<script>_[a-z]{4})?(?P<country>_[a-z]{2})?$/', $langcode, $matches) 2439 && $langcode !== 'en' 2440 ) { 2441 $foundlang = false; 2442 $langcode = 'en'; 2443 } 2444 2445 //There is no English translation file 2446 if ('en' !== $langcode) { 2447 $langcodes = []; 2448 if (!empty($matches['script']) && !empty($matches['country'])) { 2449 $langcodes[] = $matches['lang'] . $matches['script'] . $matches['country']; 2450 } 2451 if (!empty($matches['country'])) { 2452 $langcodes[] = $matches['lang'] . $matches['country']; 2453 } 2454 if (!empty($matches['script'])) { 2455 $langcodes[] = $matches['lang'] . $matches['script']; 2456 } 2457 $langcodes[] = $matches['lang']; 2458 2459 //Try and find a readable language file for the requested language. 2460 $foundFile = false; 2461 foreach ($langcodes as $code) { 2462 $lang_file = $lang_path . 'phpmailer.lang-' . $code . '.php'; 2463 if (static::fileIsAccessible($lang_file)) { 2464 $foundFile = true; 2465 break; 2466 } 2467 } 2468 2469 if ($foundFile === false) { 2470 $foundlang = false; 2471 } else { 2472 $lines = file($lang_file); 2473 foreach ($lines as $line) { 2474 //Translation file lines look like this: 2475 //$PHPMAILER_LANG['authenticate'] = 'SMTP-Fehler: Authentifizierung fehlgeschlagen.'; 2476 //These files are parsed as text and not PHP so as to avoid the possibility of code injection 2477 //See https://blog.stevenlevithan.com/archives/match-quoted-string 2478 $matches = []; 2479 if ( 2480 preg_match( 2481 '/^\$PHPMAILER_LANG\[\'([a-z\d_]+)\'\]\s*=\s*(["\'])(.+)*?\2;/', 2482 $line, 2483 $matches 2484 ) && 2485 //Ignore unknown translation keys 2486 array_key_exists($matches[1], $PHPMAILER_LANG) 2487 ) { 2488 //Overwrite language-specific strings so we'll never have missing translation keys. 2489 $PHPMAILER_LANG[$matches[1]] = (string)$matches[3]; 2490 } 2491 } 2492 } 2493 } 2494 $this->language = $PHPMAILER_LANG; 2495 2496 return $foundlang; //Returns false if language not found 2497 } 2498 2499 /** 2500 * Get the array of strings for the current language. 2501 * 2502 * @return array 2503 */ 2504 public function getTranslations() 2505 { 2506 if (empty($this->language)) { 2507 $this->setLanguage(); // Set the default language. 2508 } 2509 2510 return $this->language; 2511 } 2512 2513 /** 2514 * Create recipient headers. 2515 * 2516 * @param string $type 2517 * @param array $addr An array of recipients, 2518 * where each recipient is a 2-element indexed array with element 0 containing an address 2519 * and element 1 containing a name, like: 2520 * [['joe@example.com', 'Joe User'], ['zoe@example.com', 'Zoe User']] 2521 * 2522 * @return string 2523 */ 2524 public function addrAppend($type, $addr) 2525 { 2526 $addresses = []; 2527 foreach ($addr as $address) { 2528 $addresses[] = $this->addrFormat($address); 2529 } 2530 2531 return $type . ': ' . implode(', ', $addresses) . static::$LE; 2532 } 2533 2534 /** 2535 * Format an address for use in a message header. 2536 * 2537 * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name like 2538 * ['joe@example.com', 'Joe User'] 2539 * 2540 * @return string 2541 */ 2542 public function addrFormat($addr) 2543 { 2544 if (!isset($addr[1]) || ($addr[1] === '')) { //No name provided 2545 return $this->secureHeader($addr[0]); 2546 } 2547 2548 return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . 2549 ' <' . $this->secureHeader($addr[0]) . '>'; 2550 } 2551 2552 /** 2553 * Word-wrap message. 2554 * For use with mailers that do not automatically perform wrapping 2555 * and for quoted-printable encoded messages. 2556 * Original written by philippe. 2557 * 2558 * @param string $message The message to wrap 2559 * @param int $length The line length to wrap to 2560 * @param bool $qp_mode Whether to run in Quoted-Printable mode 2561 * 2562 * @return string 2563 */ 2564 public function wrapText($message, $length, $qp_mode = false) 2565 { 2566 if ($qp_mode) { 2567 $soft_break = sprintf(' =%s', static::$LE); 2568 } else { 2569 $soft_break = static::$LE; 2570 } 2571 //If utf-8 encoding is used, we will need to make sure we don't 2572 //split multibyte characters when we wrap 2573 $is_utf8 = static::CHARSET_UTF8 === strtolower($this->CharSet); 2574 $lelen = strlen(static::$LE); 2575 $crlflen = strlen(static::$LE); 2576 2577 $message = static::normalizeBreaks($message); 2578 //Remove a trailing line break 2579 if (substr($message, -$lelen) === static::$LE) { 2580 $message = substr($message, 0, -$lelen); 2581 } 2582 2583 //Split message into lines 2584 $lines = explode(static::$LE, $message); 2585 //Message will be rebuilt in here 2586 $message = ''; 2587 foreach ($lines as $line) { 2588 $words = explode(' ', $line); 2589 $buf = ''; 2590 $firstword = true; 2591 foreach ($words as $word) { 2592 if ($qp_mode && (strlen($word) > $length)) { 2593 $space_left = $length - strlen($buf) - $crlflen; 2594 if (!$firstword) { 2595 if ($space_left > 20) { 2596 $len = $space_left; 2597 if ($is_utf8) { 2598 $len = $this->utf8CharBoundary($word, $len); 2599 } elseif ('=' === substr($word, $len - 1, 1)) { 2600 --$len; 2601 } elseif ('=' === substr($word, $len - 2, 1)) { 2602 $len -= 2; 2603 } 2604 $part = substr($word, 0, $len); 2605 $word = substr($word, $len); 2606 $buf .= ' ' . $part; 2607 $message .= $buf . sprintf('=%s', static::$LE); 2608 } else { 2609 $message .= $buf . $soft_break; 2610 } 2611 $buf = ''; 2612 } 2613 while ($word !== '') { 2614 if ($length <= 0) { 2615 break; 2616 } 2617 $len = $length; 2618 if ($is_utf8) { 2619 $len = $this->utf8CharBoundary($word, $len); 2620 } elseif ('=' === substr($word, $len - 1, 1)) { 2621 --$len; 2622 } elseif ('=' === substr($word, $len - 2, 1)) { 2623 $len -= 2; 2624 } 2625 $part = substr($word, 0, $len); 2626 $word = (string) substr($word, $len); 2627 2628 if ($word !== '') { 2629 $message .= $part . sprintf('=%s', static::$LE); 2630 } else { 2631 $buf = $part; 2632 } 2633 } 2634 } else { 2635 $buf_o = $buf; 2636 if (!$firstword) { 2637 $buf .= ' '; 2638 } 2639 $buf .= $word; 2640 2641 if ('' !== $buf_o && strlen($buf) > $length) { 2642 $message .= $buf_o . $soft_break; 2643 $buf = $word; 2644 } 2645 } 2646 $firstword = false; 2647 } 2648 $message .= $buf . static::$LE; 2649 } 2650 2651 return $message; 2652 } 2653 2654 /** 2655 * Find the last character boundary prior to $maxLength in a utf-8 2656 * quoted-printable encoded string. 2657 * Original written by Colin Brown. 2658 * 2659 * @param string $encodedText utf-8 QP text 2660 * @param int $maxLength Find the last character boundary prior to this length 2661 * 2662 * @return int 2663 */ 2664 public function utf8CharBoundary($encodedText, $maxLength) 2665 { 2666 $foundSplitPos = false; 2667 $lookBack = 3; 2668 while (!$foundSplitPos) { 2669 $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack); 2670 $encodedCharPos = strpos($lastChunk, '='); 2671 if (false !== $encodedCharPos) { 2672 //Found start of encoded character byte within $lookBack block. 2673 //Check the encoded byte value (the 2 chars after the '=') 2674 $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2); 2675 $dec = hexdec($hex); 2676 if ($dec < 128) { 2677 //Single byte character. 2678 //If the encoded char was found at pos 0, it will fit 2679 //otherwise reduce maxLength to start of the encoded char 2680 if ($encodedCharPos > 0) { 2681 $maxLength -= $lookBack - $encodedCharPos; 2682 } 2683 $foundSplitPos = true; 2684 } elseif ($dec >= 192) { 2685 //First byte of a multi byte character 2686 //Reduce maxLength to split at start of character 2687 $maxLength -= $lookBack - $encodedCharPos; 2688 $foundSplitPos = true; 2689 } elseif ($dec < 192) { 2690 //Middle byte of a multi byte character, look further back 2691 $lookBack += 3; 2692 } 2693 } else { 2694 //No encoded character found 2695 $foundSplitPos = true; 2696 } 2697 } 2698 2699 return $maxLength; 2700 } 2701 2702 /** 2703 * Apply word wrapping to the message body. 2704 * Wraps the message body to the number of chars set in the WordWrap property. 2705 * You should only do this to plain-text bodies as wrapping HTML tags may break them. 2706 * This is called automatically by createBody(), so you don't need to call it yourself. 2707 */ 2708 public function setWordWrap() 2709 { 2710 if ($this->WordWrap < 1) { 2711 return; 2712 } 2713 2714 switch ($this->message_type) { 2715 case 'alt': 2716 case 'alt_inline': 2717 case 'alt_attach': 2718 case 'alt_inline_attach': 2719 $this->AltBody = $this->wrapText($this->AltBody, $this->WordWrap); 2720 break; 2721 default: 2722 $this->Body = $this->wrapText($this->Body, $this->WordWrap); 2723 break; 2724 } 2725 } 2726 2727 /** 2728 * Assemble message headers. 2729 * 2730 * @return string The assembled headers 2731 */ 2732 public function createHeader() 2733 { 2734 $result = ''; 2735 2736 $result .= $this->headerLine('Date', '' === $this->MessageDate ? self::rfcDate() : $this->MessageDate); 2737 2738 //The To header is created automatically by mail(), so needs to be omitted here 2739 if ('mail' !== $this->Mailer) { 2740 if ($this->SingleTo) { 2741 foreach ($this->to as $toaddr) { 2742 $this->SingleToArray[] = $this->addrFormat($toaddr); 2743 } 2744 } elseif (count($this->to) > 0) { 2745 $result .= $this->addrAppend('To', $this->to); 2746 } elseif (count($this->cc) === 0) { 2747 $result .= $this->headerLine('To', 'undisclosed-recipients:;'); 2748 } 2749 } 2750 $result .= $this->addrAppend('From', [[trim($this->From), $this->FromName]]); 2751 2752 //sendmail and mail() extract Cc from the header before sending 2753 if (count($this->cc) > 0) { 2754 $result .= $this->addrAppend('Cc', $this->cc); 2755 } 2756 2757 //sendmail and mail() extract Bcc from the header before sending 2758 if ( 2759 ( 2760 'sendmail' === $this->Mailer || 'qmail' === $this->Mailer || 'mail' === $this->Mailer 2761 ) 2762 && count($this->bcc) > 0 2763 ) { 2764 $result .= $this->addrAppend('Bcc', $this->bcc); 2765 } 2766 2767 if (count($this->ReplyTo) > 0) { 2768 $result .= $this->addrAppend('Reply-To', $this->ReplyTo); 2769 } 2770 2771 //mail() sets the subject itself 2772 if ('mail' !== $this->Mailer) { 2773 $result .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader($this->Subject))); 2774 } 2775 2776 //Only allow a custom message ID if it conforms to RFC 5322 section 3.6.4 2777 //https://www.rfc-editor.org/rfc/rfc5322#section-3.6.4 2778 if ( 2779 '' !== $this->MessageID && 2780 preg_match( 2781 '/^<((([a-z\d!#$%&\'*+\/=?^_`{|}~-]+(\.[a-z\d!#$%&\'*+\/=?^_`{|}~-]+)*)' . 2782 '|("(([\x01-\x08\x0B\x0C\x0E-\x1F\x7F]|[\x21\x23-\x5B\x5D-\x7E])' . 2783 '|(\\[\x01-\x09\x0B\x0C\x0E-\x7F]))*"))@(([a-z\d!#$%&\'*+\/=?^_`{|}~-]+' . 2784 '(\.[a-z\d!#$%&\'*+\/=?^_`{|}~-]+)*)|(\[(([\x01-\x08\x0B\x0C\x0E-\x1F\x7F]' . 2785 '|[\x21-\x5A\x5E-\x7E])|(\\[\x01-\x09\x0B\x0C\x0E-\x7F]))*\])))>$/Di', 2786 $this->MessageID 2787 ) 2788 ) { 2789 $this->lastMessageID = $this->MessageID; 2790 } else { 2791 $this->lastMessageID = sprintf('<%s@%s>', $this->uniqueid, $this->serverHostname()); 2792 } 2793 $result .= $this->headerLine('Message-ID', $this->lastMessageID); 2794 if (null !== $this->Priority) { 2795 $result .= $this->headerLine('X-Priority', $this->Priority); 2796 } 2797 if ('' === $this->XMailer) { 2798 //Empty string for default X-Mailer header 2799 $result .= $this->headerLine( 2800 'X-Mailer', 2801 'PHPMailer ' . self::VERSION . ' (https://github.com/PHPMailer/PHPMailer)' 2802 ); 2803 } elseif (is_string($this->XMailer) && trim($this->XMailer) !== '') { 2804 //Some string 2805 $result .= $this->headerLine('X-Mailer', trim($this->XMailer)); 2806 } //Other values result in no X-Mailer header 2807 2808 if ('' !== $this->ConfirmReadingTo) { 2809 $result .= $this->headerLine('Disposition-Notification-To', '<' . $this->ConfirmReadingTo . '>'); 2810 } 2811 2812 //Add custom headers 2813 foreach ($this->CustomHeader as $header) { 2814 $result .= $this->headerLine( 2815 trim($header[0]), 2816 $this->encodeHeader(trim($header[1])) 2817 ); 2818 } 2819 if (!$this->sign_key_file) { 2820 $result .= $this->headerLine('MIME-Version', '1.0'); 2821 $result .= $this->getMailMIME(); 2822 } 2823 2824 return $result; 2825 } 2826 2827 /** 2828 * Get the message MIME type headers. 2829 * 2830 * @return string 2831 */ 2832 public function getMailMIME() 2833 { 2834 $result = ''; 2835 $ismultipart = true; 2836 switch ($this->message_type) { 2837 case 'inline': 2838 $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); 2839 $result .= $this->textLine(' boundary="' . $this->boundary[1] . '"'); 2840 break; 2841 case 'attach': 2842 case 'inline_attach': 2843 case 'alt_attach': 2844 case 'alt_inline_attach': 2845 $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_MIXED . ';'); 2846 $result .= $this->textLine(' boundary="' . $this->boundary[1] . '"'); 2847 break; 2848 case 'alt': 2849 case 'alt_inline': 2850 $result .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';'); 2851 $result .= $this->textLine(' boundary="' . $this->boundary[1] . '"'); 2852 break; 2853 default: 2854 //Catches case 'plain': and case '': 2855 $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet); 2856 $ismultipart = false; 2857 break; 2858 } 2859 //RFC1341 part 5 says 7bit is assumed if not specified 2860 if (static::ENCODING_7BIT !== $this->Encoding) { 2861 //RFC 2045 section 6.4 says multipart MIME parts may only use 7bit, 8bit or binary CTE 2862 if ($ismultipart) { 2863 if (static::ENCODING_8BIT === $this->Encoding) { 2864 $result .= $this->headerLine('Content-Transfer-Encoding', static::ENCODING_8BIT); 2865 } 2866 //The only remaining alternatives are quoted-printable and base64, which are both 7bit compatible 2867 } else { 2868 $result .= $this->headerLine('Content-Transfer-Encoding', $this->Encoding); 2869 } 2870 } 2871 2872 return $result; 2873 } 2874 2875 /** 2876 * Returns the whole MIME message. 2877 * Includes complete headers and body. 2878 * Only valid post preSend(). 2879 * 2880 * @see PHPMailer::preSend() 2881 * 2882 * @return string 2883 */ 2884 public function getSentMIMEMessage() 2885 { 2886 return static::stripTrailingWSP($this->MIMEHeader . $this->mailHeader) . 2887 static::$LE . static::$LE . $this->MIMEBody; 2888 } 2889 2890 /** 2891 * Create a unique ID to use for boundaries. 2892 * 2893 * @return string 2894 */ 2895 protected function generateId() 2896 { 2897 $len = 32; //32 bytes = 256 bits 2898 $bytes = ''; 2899 if (function_exists('random_bytes')) { 2900 try { 2901 $bytes = random_bytes($len); 2902 } catch (\Exception $e) { 2903 //Do nothing 2904 } 2905 } elseif (function_exists('openssl_random_pseudo_bytes')) { 2906 /** @noinspection CryptographicallySecureRandomnessInspection */ 2907 $bytes = openssl_random_pseudo_bytes($len); 2908 } 2909 if ($bytes === '') { 2910 //We failed to produce a proper random string, so make do. 2911 //Use a hash to force the length to the same as the other methods 2912 $bytes = hash('sha256', uniqid((string) mt_rand(), true), true); 2913 } 2914 2915 //We don't care about messing up base64 format here, just want a random string 2916 return str_replace(['=', '+', '/'], '', base64_encode(hash('sha256', $bytes, true))); 2917 } 2918 2919 /** 2920 * Assemble the message body. 2921 * Returns an empty string on failure. 2922 * 2923 * @throws Exception 2924 * 2925 * @return string The assembled message body 2926 */ 2927 public function createBody() 2928 { 2929 $body = ''; 2930 //Create unique IDs and preset boundaries 2931 $this->setBoundaries(); 2932 2933 if ($this->sign_key_file) { 2934 $body .= $this->getMailMIME() . static::$LE; 2935 } 2936 2937 $this->setWordWrap(); 2938 2939 $bodyEncoding = $this->Encoding; 2940 $bodyCharSet = $this->CharSet; 2941 //Can we do a 7-bit downgrade? 2942 if ($this->UseSMTPUTF8) { 2943 $bodyEncoding = static::ENCODING_8BIT; 2944 } elseif (static::ENCODING_8BIT === $bodyEncoding && !$this->has8bitChars($this->Body)) { 2945 $bodyEncoding = static::ENCODING_7BIT; 2946 //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit 2947 $bodyCharSet = static::CHARSET_ASCII; 2948 } 2949 //If lines are too long, and we're not already using an encoding that will shorten them, 2950 //change to quoted-printable transfer encoding for the body part only 2951 if (static::ENCODING_BASE64 !== $this->Encoding && static::hasLineLongerThanMax($this->Body)) { 2952 $bodyEncoding = static::ENCODING_QUOTED_PRINTABLE; 2953 } 2954 2955 $altBodyEncoding = $this->Encoding; 2956 $altBodyCharSet = $this->CharSet; 2957 //Can we do a 7-bit downgrade? 2958 if (static::ENCODING_8BIT === $altBodyEncoding && !$this->has8bitChars($this->AltBody)) { 2959 $altBodyEncoding = static::ENCODING_7BIT; 2960 //All ISO 8859, Windows codepage and UTF-8 charsets are ascii compatible up to 7-bit 2961 $altBodyCharSet = static::CHARSET_ASCII; 2962 } 2963 //If lines are too long, and we're not already using an encoding that will shorten them, 2964 //change to quoted-printable transfer encoding for the alt body part only 2965 if (static::ENCODING_BASE64 !== $altBodyEncoding && static::hasLineLongerThanMax($this->AltBody)) { 2966 $altBodyEncoding = static::ENCODING_QUOTED_PRINTABLE; 2967 } 2968 //Use this as a preamble in all multipart message types 2969 $mimepre = ''; 2970 switch ($this->message_type) { 2971 case 'inline': 2972 $body .= $mimepre; 2973 $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); 2974 $body .= $this->encodeString($this->Body, $bodyEncoding); 2975 $body .= static::$LE; 2976 $body .= $this->attachAll('inline', $this->boundary[1]); 2977 break; 2978 case 'attach': 2979 $body .= $mimepre; 2980 $body .= $this->getBoundary($this->boundary[1], $bodyCharSet, '', $bodyEncoding); 2981 $body .= $this->encodeString($this->Body, $bodyEncoding); 2982 $body .= static::$LE; 2983 $body .= $this->attachAll('attachment', $this->boundary[1]); 2984 break; 2985 case 'inline_attach': 2986 $body .= $mimepre; 2987 $body .= $this->textLine('--' . $this->boundary[1]); 2988 $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); 2989 $body .= $this->textLine(' boundary="' . $this->boundary[2] . '";'); 2990 $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"'); 2991 $body .= static::$LE; 2992 $body .= $this->getBoundary($this->boundary[2], $bodyCharSet, '', $bodyEncoding); 2993 $body .= $this->encodeString($this->Body, $bodyEncoding); 2994 $body .= static::$LE; 2995 $body .= $this->attachAll('inline', $this->boundary[2]); 2996 $body .= static::$LE; 2997 $body .= $this->attachAll('attachment', $this->boundary[1]); 2998 break; 2999 case 'alt': 3000 $body .= $mimepre; 3001 $body .= $this->getBoundary( 3002 $this->boundary[1], 3003 $altBodyCharSet, 3004 static::CONTENT_TYPE_PLAINTEXT, 3005 $altBodyEncoding 3006 ); 3007 $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 3008 $body .= static::$LE; 3009 $body .= $this->getBoundary( 3010 $this->boundary[1], 3011 $bodyCharSet, 3012 static::CONTENT_TYPE_TEXT_HTML, 3013 $bodyEncoding 3014 ); 3015 $body .= $this->encodeString($this->Body, $bodyEncoding); 3016 $body .= static::$LE; 3017 if (!empty($this->Ical)) { 3018 $method = static::ICAL_METHOD_REQUEST; 3019 foreach (static::$IcalMethods as $imethod) { 3020 if (stripos($this->Ical, 'METHOD:' . $imethod) !== false) { 3021 $method = $imethod; 3022 break; 3023 } 3024 } 3025 $body .= $this->getBoundary( 3026 $this->boundary[1], 3027 '', 3028 static::CONTENT_TYPE_TEXT_CALENDAR . '; method=' . $method, 3029 '' 3030 ); 3031 $body .= $this->encodeString($this->Ical, $this->Encoding); 3032 $body .= static::$LE; 3033 } 3034 $body .= $this->endBoundary($this->boundary[1]); 3035 break; 3036 case 'alt_inline': 3037 $body .= $mimepre; 3038 $body .= $this->getBoundary( 3039 $this->boundary[1], 3040 $altBodyCharSet, 3041 static::CONTENT_TYPE_PLAINTEXT, 3042 $altBodyEncoding 3043 ); 3044 $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 3045 $body .= static::$LE; 3046 $body .= $this->textLine('--' . $this->boundary[1]); 3047 $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); 3048 $body .= $this->textLine(' boundary="' . $this->boundary[2] . '";'); 3049 $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"'); 3050 $body .= static::$LE; 3051 $body .= $this->getBoundary( 3052 $this->boundary[2], 3053 $bodyCharSet, 3054 static::CONTENT_TYPE_TEXT_HTML, 3055 $bodyEncoding 3056 ); 3057 $body .= $this->encodeString($this->Body, $bodyEncoding); 3058 $body .= static::$LE; 3059 $body .= $this->attachAll('inline', $this->boundary[2]); 3060 $body .= static::$LE; 3061 $body .= $this->endBoundary($this->boundary[1]); 3062 break; 3063 case 'alt_attach': 3064 $body .= $mimepre; 3065 $body .= $this->textLine('--' . $this->boundary[1]); 3066 $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';'); 3067 $body .= $this->textLine(' boundary="' . $this->boundary[2] . '"'); 3068 $body .= static::$LE; 3069 $body .= $this->getBoundary( 3070 $this->boundary[2], 3071 $altBodyCharSet, 3072 static::CONTENT_TYPE_PLAINTEXT, 3073 $altBodyEncoding 3074 ); 3075 $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 3076 $body .= static::$LE; 3077 $body .= $this->getBoundary( 3078 $this->boundary[2], 3079 $bodyCharSet, 3080 static::CONTENT_TYPE_TEXT_HTML, 3081 $bodyEncoding 3082 ); 3083 $body .= $this->encodeString($this->Body, $bodyEncoding); 3084 $body .= static::$LE; 3085 if (!empty($this->Ical)) { 3086 $method = static::ICAL_METHOD_REQUEST; 3087 foreach (static::$IcalMethods as $imethod) { 3088 if (stripos($this->Ical, 'METHOD:' . $imethod) !== false) { 3089 $method = $imethod; 3090 break; 3091 } 3092 } 3093 $body .= $this->getBoundary( 3094 $this->boundary[2], 3095 '', 3096 static::CONTENT_TYPE_TEXT_CALENDAR . '; method=' . $method, 3097 '' 3098 ); 3099 $body .= $this->encodeString($this->Ical, $this->Encoding); 3100 } 3101 $body .= $this->endBoundary($this->boundary[2]); 3102 $body .= static::$LE; 3103 $body .= $this->attachAll('attachment', $this->boundary[1]); 3104 break; 3105 case 'alt_inline_attach': 3106 $body .= $mimepre; 3107 $body .= $this->textLine('--' . $this->boundary[1]); 3108 $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_ALTERNATIVE . ';'); 3109 $body .= $this->textLine(' boundary="' . $this->boundary[2] . '"'); 3110 $body .= static::$LE; 3111 $body .= $this->getBoundary( 3112 $this->boundary[2], 3113 $altBodyCharSet, 3114 static::CONTENT_TYPE_PLAINTEXT, 3115 $altBodyEncoding 3116 ); 3117 $body .= $this->encodeString($this->AltBody, $altBodyEncoding); 3118 $body .= static::$LE; 3119 $body .= $this->textLine('--' . $this->boundary[2]); 3120 $body .= $this->headerLine('Content-Type', static::CONTENT_TYPE_MULTIPART_RELATED . ';'); 3121 $body .= $this->textLine(' boundary="' . $this->boundary[3] . '";'); 3122 $body .= $this->textLine(' type="' . static::CONTENT_TYPE_TEXT_HTML . '"'); 3123 $body .= static::$LE; 3124 $body .= $this->getBoundary( 3125 $this->boundary[3], 3126 $bodyCharSet, 3127 static::CONTENT_TYPE_TEXT_HTML, 3128 $bodyEncoding 3129 ); 3130 $body .= $this->encodeString($this->Body, $bodyEncoding); 3131 $body .= static::$LE; 3132 $body .= $this->attachAll('inline', $this->boundary[3]); 3133 $body .= static::$LE; 3134 $body .= $this->endBoundary($this->boundary[2]); 3135 $body .= static::$LE; 3136 $body .= $this->attachAll('attachment', $this->boundary[1]); 3137 break; 3138 default: 3139 //Catch case 'plain' and case '', applies to simple `text/plain` and `text/html` body content types 3140 //Reset the `Encoding` property in case we changed it for line length reasons 3141 $this->Encoding = $bodyEncoding; 3142 $body .= $this->encodeString($this->Body, $this->Encoding); 3143 break; 3144 } 3145 3146 if ($this->isError()) { 3147 $body = ''; 3148 if ($this->exceptions) { 3149 throw new Exception($this->lang('empty_message'), self::STOP_CRITICAL); 3150 } 3151 } elseif ($this->sign_key_file) { 3152 try { 3153 if (!defined('PKCS7_TEXT')) { 3154 throw new Exception($this->lang('extension_missing') . 'openssl'); 3155 } 3156 3157 $file = tempnam(sys_get_temp_dir(), 'srcsign'); 3158 $signed = tempnam(sys_get_temp_dir(), 'mailsign'); 3159 file_put_contents($file, $body); 3160 3161 //Workaround for PHP bug https://bugs.php.net/bug.php?id=69197 3162 if (empty($this->sign_extracerts_file)) { 3163 $sign = @openssl_pkcs7_sign( 3164 $file, 3165 $signed, 3166 'file://' . realpath($this->sign_cert_file), 3167 ['file://' . realpath($this->sign_key_file), $this->sign_key_pass], 3168 [] 3169 ); 3170 } else { 3171 $sign = @openssl_pkcs7_sign( 3172 $file, 3173 $signed, 3174 'file://' . realpath($this->sign_cert_file), 3175 ['file://' . realpath($this->sign_key_file), $this->sign_key_pass], 3176 [], 3177 PKCS7_DETACHED, 3178 $this->sign_extracerts_file 3179 ); 3180 } 3181 3182 @unlink($file); 3183 if ($sign) { 3184 $body = file_get_contents($signed); 3185 @unlink($signed); 3186 //The message returned by openssl contains both headers and body, so need to split them up 3187 $parts = explode("\n\n", $body, 2); 3188 $this->MIMEHeader .= $parts[0] . static::$LE . static::$LE; 3189 $body = $parts[1]; 3190 } else { 3191 @unlink($signed); 3192 throw new Exception($this->lang('signing') . openssl_error_string()); 3193 } 3194 } catch (Exception $exc) { 3195 $body = ''; 3196 if ($this->exceptions) { 3197 throw $exc; 3198 } 3199 } 3200 } 3201 3202 return $body; 3203 } 3204 3205 /** 3206 * Get the boundaries that this message will use 3207 * @return array 3208 */ 3209 public function getBoundaries() 3210 { 3211 if (empty($this->boundary)) { 3212 $this->setBoundaries(); 3213 } 3214 return $this->boundary; 3215 } 3216 3217 /** 3218 * Return the start of a message boundary. 3219 * 3220 * @param string $boundary 3221 * @param string $charSet 3222 * @param string $contentType 3223 * @param string $encoding 3224 * 3225 * @return string 3226 */ 3227 protected function getBoundary($boundary, $charSet, $contentType, $encoding) 3228 { 3229 $result = ''; 3230 if ('' === $charSet) { 3231 $charSet = $this->CharSet; 3232 } 3233 if ('' === $contentType) { 3234 $contentType = $this->ContentType; 3235 } 3236 if ('' === $encoding) { 3237 $encoding = $this->Encoding; 3238 } 3239 $result .= $this->textLine('--' . $boundary); 3240 $result .= sprintf('Content-Type: %s; charset=%s', $contentType, $charSet); 3241 $result .= static::$LE; 3242 //RFC1341 part 5 says 7bit is assumed if not specified 3243 if (static::ENCODING_7BIT !== $encoding) { 3244 $result .= $this->headerLine('Content-Transfer-Encoding', $encoding); 3245 } 3246 $result .= static::$LE; 3247 3248 return $result; 3249 } 3250 3251 /** 3252 * Return the end of a message boundary. 3253 * 3254 * @param string $boundary 3255 * 3256 * @return string 3257 */ 3258 protected function endBoundary($boundary) 3259 { 3260 return static::$LE . '--' . $boundary . '--' . static::$LE; 3261 } 3262 3263 /** 3264 * Set the message type. 3265 * PHPMailer only supports some preset message types, not arbitrary MIME structures. 3266 */ 3267 protected function setMessageType() 3268 { 3269 $type = []; 3270 if ($this->alternativeExists()) { 3271 $type[] = 'alt'; 3272 } 3273 if ($this->inlineImageExists()) { 3274 $type[] = 'inline'; 3275 } 3276 if ($this->attachmentExists()) { 3277 $type[] = 'attach'; 3278 } 3279 $this->message_type = implode('_', $type); 3280 if ('' === $this->message_type) { 3281 //The 'plain' message_type refers to the message having a single body element, not that it is plain-text 3282 $this->message_type = 'plain'; 3283 } 3284 } 3285 3286 /** 3287 * Format a header line. 3288 * 3289 * @param string $name 3290 * @param string|int $value 3291 * 3292 * @return string 3293 */ 3294 public function headerLine($name, $value) 3295 { 3296 return $name . ': ' . $value . static::$LE; 3297 } 3298 3299 /** 3300 * Return a formatted mail line. 3301 * 3302 * @param string $value 3303 * 3304 * @return string 3305 */ 3306 public function textLine($value) 3307 { 3308 return $value . static::$LE; 3309 } 3310 3311 /** 3312 * Add an attachment from a path on the filesystem. 3313 * Never use a user-supplied path to a file! 3314 * Returns false if the file could not be found or read. 3315 * Explicitly *does not* support passing URLs; PHPMailer is not an HTTP client. 3316 * If you need to do that, fetch the resource yourself and pass it in via a local file or string. 3317 * 3318 * @param string $path Path to the attachment 3319 * @param string $name Overrides the attachment name 3320 * @param string $encoding File encoding (see $Encoding) 3321 * @param string $type MIME type, e.g. `image/jpeg`; determined automatically from $path if not specified 3322 * @param string $disposition Disposition to use 3323 * 3324 * @throws Exception 3325 * 3326 * @return bool 3327 */ 3328 public function addAttachment( 3329 $path, 3330 $name = '', 3331 $encoding = self::ENCODING_BASE64, 3332 $type = '', 3333 $disposition = 'attachment' 3334 ) { 3335 try { 3336 if (!static::fileIsAccessible($path)) { 3337 throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE); 3338 } 3339 3340 //If a MIME type is not specified, try to work it out from the file name 3341 if ('' === $type) { 3342 $type = static::filenameToType($path); 3343 } 3344 3345 $filename = (string) static::mb_pathinfo($path, PATHINFO_BASENAME); 3346 if ('' === $name) { 3347 $name = $filename; 3348 } 3349 if (!$this->validateEncoding($encoding)) { 3350 throw new Exception($this->lang('encoding') . $encoding); 3351 } 3352 3353 $this->attachment[] = [ 3354 0 => $path, 3355 1 => $filename, 3356 2 => $name, 3357 3 => $encoding, 3358 4 => $type, 3359 5 => false, //isStringAttachment 3360 6 => $disposition, 3361 7 => $name, 3362 ]; 3363 } catch (Exception $exc) { 3364 $this->setError($exc->getMessage()); 3365 $this->edebug($exc->getMessage()); 3366 if ($this->exceptions) { 3367 throw $exc; 3368 } 3369 3370 return false; 3371 } 3372 3373 return true; 3374 } 3375 3376 /** 3377 * Return the array of attachments. 3378 * 3379 * @return array 3380 */ 3381 public function getAttachments() 3382 { 3383 return $this->attachment; 3384 } 3385 3386 /** 3387 * Attach all file, string, and binary attachments to the message. 3388 * Returns an empty string on failure. 3389 * 3390 * @param string $disposition_type 3391 * @param string $boundary 3392 * 3393 * @throws Exception 3394 * 3395 * @return string 3396 */ 3397 protected function attachAll($disposition_type, $boundary) 3398 { 3399 //Return text of body 3400 $mime = []; 3401 $cidUniq = []; 3402 $incl = []; 3403 3404 //Add all attachments 3405 foreach ($this->attachment as $attachment) { 3406 //Check if it is a valid disposition_filter 3407 if ($attachment[6] === $disposition_type) { 3408 //Check for string attachment 3409 $string = ''; 3410 $path = ''; 3411 $bString = $attachment[5]; 3412 if ($bString) { 3413 $string = $attachment[0]; 3414 } else { 3415 $path = $attachment[0]; 3416 } 3417 3418 $inclhash = hash('sha256', serialize($attachment)); 3419 if (in_array($inclhash, $incl, true)) { 3420 continue; 3421 } 3422 $incl[] = $inclhash; 3423 $name = $attachment[2]; 3424 $encoding = $attachment[3]; 3425 $type = $attachment[4]; 3426 $disposition = $attachment[6]; 3427 $cid = $attachment[7]; 3428 if ('inline' === $disposition && array_key_exists($cid, $cidUniq)) { 3429 continue; 3430 } 3431 $cidUniq[$cid] = true; 3432 3433 $mime[] = sprintf('--%s%s', $boundary, static::$LE); 3434 //Only include a filename property if we have one 3435 if (!empty($name)) { 3436 $mime[] = sprintf( 3437 'Content-Type: %s; name=%s%s', 3438 $type, 3439 static::quotedString($this->encodeHeader($this->secureHeader($name))), 3440 static::$LE 3441 ); 3442 } else { 3443 $mime[] = sprintf( 3444 'Content-Type: %s%s', 3445 $type, 3446 static::$LE 3447 ); 3448 } 3449 //RFC1341 part 5 says 7bit is assumed if not specified 3450 if (static::ENCODING_7BIT !== $encoding) { 3451 $mime[] = sprintf('Content-Transfer-Encoding: %s%s', $encoding, static::$LE); 3452 } 3453 3454 //Only set Content-IDs on inline attachments 3455 if ((string) $cid !== '' && $disposition === 'inline') { 3456 $mime[] = 'Content-ID: <' . $this->encodeHeader($this->secureHeader($cid)) . '>' . static::$LE; 3457 } 3458 3459 //Allow for bypassing the Content-Disposition header 3460 if (!empty($disposition)) { 3461 $encoded_name = $this->encodeHeader($this->secureHeader($name)); 3462 if (!empty($encoded_name)) { 3463 $mime[] = sprintf( 3464 'Content-Disposition: %s; filename=%s%s', 3465 $disposition, 3466 static::quotedString($encoded_name), 3467 static::$LE . static::$LE 3468 ); 3469 } else { 3470 $mime[] = sprintf( 3471 'Content-Disposition: %s%s', 3472 $disposition, 3473 static::$LE . static::$LE 3474 ); 3475 } 3476 } else { 3477 $mime[] = static::$LE; 3478 } 3479 3480 //Encode as string attachment 3481 if ($bString) { 3482 $mime[] = $this->encodeString($string, $encoding); 3483 } else { 3484 $mime[] = $this->encodeFile($path, $encoding); 3485 } 3486 if ($this->isError()) { 3487 return ''; 3488 } 3489 $mime[] = static::$LE; 3490 } 3491 } 3492 3493 $mime[] = sprintf('--%s--%s', $boundary, static::$LE); 3494 3495 return implode('', $mime); 3496 } 3497 3498 /** 3499 * Encode a file attachment in requested format. 3500 * Returns an empty string on failure. 3501 * 3502 * @param string $path The full path to the file 3503 * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' 3504 * 3505 * @return string 3506 */ 3507 protected function encodeFile($path, $encoding = self::ENCODING_BASE64) 3508 { 3509 try { 3510 if (!static::fileIsAccessible($path)) { 3511 throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE); 3512 } 3513 $file_buffer = file_get_contents($path); 3514 if (false === $file_buffer) { 3515 throw new Exception($this->lang('file_open') . $path, self::STOP_CONTINUE); 3516 } 3517 $file_buffer = $this->encodeString($file_buffer, $encoding); 3518 3519 return $file_buffer; 3520 } catch (Exception $exc) { 3521 $this->setError($exc->getMessage()); 3522 $this->edebug($exc->getMessage()); 3523 if ($this->exceptions) { 3524 throw $exc; 3525 } 3526 3527 return ''; 3528 } 3529 } 3530 3531 /** 3532 * Encode a string in requested format. 3533 * Returns an empty string on failure. 3534 * 3535 * @param string $str The text to encode 3536 * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable' 3537 * 3538 * @throws Exception 3539 * 3540 * @return string 3541 */ 3542 public function encodeString($str, $encoding = self::ENCODING_BASE64) 3543 { 3544 $encoded = ''; 3545 switch (strtolower($encoding)) { 3546 case static::ENCODING_BASE64: 3547 $encoded = chunk_split( 3548 base64_encode($str), 3549 static::STD_LINE_LENGTH, 3550 static::$LE 3551 ); 3552 break; 3553 case static::ENCODING_7BIT: 3554 case static::ENCODING_8BIT: 3555 $encoded = static::normalizeBreaks($str); 3556 //Make sure it ends with a line break 3557 if (substr($encoded, -(strlen(static::$LE))) !== static::$LE) { 3558 $encoded .= static::$LE; 3559 } 3560 break; 3561 case static::ENCODING_BINARY: 3562 $encoded = $str; 3563 break; 3564 case static::ENCODING_QUOTED_PRINTABLE: 3565 $encoded = $this->encodeQP($str); 3566 break; 3567 default: 3568 $this->setError($this->lang('encoding') . $encoding); 3569 if ($this->exceptions) { 3570 throw new Exception($this->lang('encoding') . $encoding); 3571 } 3572 break; 3573 } 3574 3575 return $encoded; 3576 } 3577 3578 /** 3579 * Encode a header value (not including its label) optimally. 3580 * Picks shortest of Q, B, or none. Result includes folding if needed. 3581 * See RFC822 definitions for phrase, comment and text positions, 3582 * and RFC2047 for inline encodings. 3583 * 3584 * @param string $str The header value to encode 3585 * @param string $position What context the string will be used in 3586 * 3587 * @return string 3588 */ 3589 public function encodeHeader($str, $position = 'text') 3590 { 3591 $position = strtolower($position); 3592 if ($this->UseSMTPUTF8 && !("comment" === $position)) { 3593 return trim(static::normalizeBreaks($str)); 3594 } 3595 3596 $matchcount = 0; 3597 switch (strtolower($position)) { 3598 case 'phrase': 3599 if (!preg_match('/[\200-\377]/', $str)) { 3600 //Can't use addslashes as we don't know the value of magic_quotes_sybase 3601 $encoded = addcslashes($str, "\0..\37\177\\\""); 3602 if (($str === $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) { 3603 return $encoded; 3604 } 3605 3606 return "\"$encoded\""; 3607 } 3608 $matchcount = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches); 3609 break; 3610 /* @noinspection PhpMissingBreakStatementInspection */ 3611 case 'comment': 3612 $matchcount = preg_match_all('/[()"]/', $str, $matches); 3613 //fallthrough 3614 case 'text': 3615 default: 3616 $matchcount += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches); 3617 break; 3618 } 3619 3620 if ($this->has8bitChars($str)) { 3621 $charset = $this->CharSet; 3622 } else { 3623 $charset = static::CHARSET_ASCII; 3624 } 3625 3626 //Q/B encoding adds 8 chars and the charset ("` =?<charset>?[QB]?<content>?=`"). 3627 $overhead = 8 + strlen($charset); 3628 3629 if ('mail' === $this->Mailer) { 3630 $maxlen = static::MAIL_MAX_LINE_LENGTH - $overhead; 3631 } else { 3632 $maxlen = static::MAX_LINE_LENGTH - $overhead; 3633 } 3634 3635 //Select the encoding that produces the shortest output and/or prevents corruption. 3636 if ($matchcount > strlen($str) / 3) { 3637 //More than 1/3 of the content needs encoding, use B-encode. 3638 $encoding = 'B'; 3639 } elseif ($matchcount > 0) { 3640 //Less than 1/3 of the content needs encoding, use Q-encode. 3641 $encoding = 'Q'; 3642 } elseif (strlen($str) > $maxlen) { 3643 //No encoding needed, but value exceeds max line length, use Q-encode to prevent corruption. 3644 $encoding = 'Q'; 3645 } else { 3646 //No reformatting needed 3647 $encoding = false; 3648 } 3649 3650 switch ($encoding) { 3651 case 'B': 3652 if ($this->hasMultiBytes($str)) { 3653 //Use a custom function which correctly encodes and wraps long 3654 //multibyte strings without breaking lines within a character 3655 $encoded = $this->base64EncodeWrapMB($str, "\n"); 3656 } else { 3657 $encoded = base64_encode($str); 3658 $maxlen -= $maxlen % 4; 3659 $encoded = trim(chunk_split($encoded, $maxlen, "\n")); 3660 } 3661 $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded); 3662 break; 3663 case 'Q': 3664 $encoded = $this->encodeQ($str, $position); 3665 $encoded = $this->wrapText($encoded, $maxlen, true); 3666 $encoded = str_replace('=' . static::$LE, "\n", trim($encoded)); 3667 $encoded = preg_replace('/^(.*)$/m', ' =?' . $charset . "?$encoding?\\1?=", $encoded); 3668 break; 3669 default: 3670 return $str; 3671 } 3672 3673 return trim(static::normalizeBreaks($encoded)); 3674 } 3675 3676 /** 3677 * Check if a string contains multi-byte characters. 3678 * 3679 * @param string $str multi-byte text to wrap encode 3680 * 3681 * @return bool 3682 */ 3683 public function hasMultiBytes($str) 3684 { 3685 if (function_exists('mb_strlen')) { 3686 return strlen($str) > mb_strlen($str, $this->CharSet); 3687 } 3688 3689 //Assume no multibytes (we can't handle without mbstring functions anyway) 3690 return false; 3691 } 3692 3693 /** 3694 * Does a string contain any 8-bit chars (in any charset)? 3695 * 3696 * @param string $text 3697 * 3698 * @return bool 3699 */ 3700 public function has8bitChars($text) 3701 { 3702 return (bool) preg_match('/[\x80-\xFF]/', $text); 3703 } 3704 3705 /** 3706 * Encode and wrap long multibyte strings for mail headers 3707 * without breaking lines within a character. 3708 * Adapted from a function by paravoid. 3709 * 3710 * @see https://www.php.net/manual/en/function.mb-encode-mimeheader.php#60283 3711 * 3712 * @param string $str multi-byte text to wrap encode 3713 * @param string $linebreak string to use as linefeed/end-of-line 3714 * 3715 * @return string 3716 */ 3717 public function base64EncodeWrapMB($str, $linebreak = null) 3718 { 3719 $start = '=?' . $this->CharSet . '?B?'; 3720 $end = '?='; 3721 $encoded = ''; 3722 if (null === $linebreak) { 3723 $linebreak = static::$LE; 3724 } 3725 3726 $mb_length = mb_strlen($str, $this->CharSet); 3727 //Each line must have length <= 75, including $start and $end 3728 $length = 75 - strlen($start) - strlen($end); 3729 //Average multi-byte ratio 3730 $ratio = $mb_length / strlen($str); 3731 //Base64 has a 4:3 ratio 3732 $avgLength = floor($length * $ratio * .75); 3733 3734 $offset = 0; 3735 for ($i = 0; $i < $mb_length; $i += $offset) { 3736 $lookBack = 0; 3737 do { 3738 $offset = $avgLength - $lookBack; 3739 $chunk = mb_substr($str, $i, $offset, $this->CharSet); 3740 $chunk = base64_encode($chunk); 3741 ++$lookBack; 3742 } while (strlen($chunk) > $length); 3743 $encoded .= $chunk . $linebreak; 3744 } 3745 3746 //Chomp the last linefeed 3747 return substr($encoded, 0, -strlen($linebreak)); 3748 } 3749 3750 /** 3751 * Encode a string in quoted-printable format. 3752 * According to RFC2045 section 6.7. 3753 * 3754 * @param string $string The text to encode 3755 * 3756 * @return string 3757 */ 3758 public function encodeQP($string) 3759 { 3760 return static::normalizeBreaks(quoted_printable_encode($string)); 3761 } 3762 3763 /** 3764 * Encode a string using Q encoding. 3765 * 3766 * @see https://www.rfc-editor.org/rfc/rfc2047#section-4.2 3767 * 3768 * @param string $str the text to encode 3769 * @param string $position Where the text is going to be used, see the RFC for what that means 3770 * 3771 * @return string 3772 */ 3773 public function encodeQ($str, $position = 'text') 3774 { 3775 //There should not be any EOL in the string 3776 $pattern = ''; 3777 $encoded = str_replace(["\r", "\n"], '', $str); 3778 switch (strtolower($position)) { 3779 case 'phrase': 3780 //RFC 2047 section 5.3 3781 $pattern = '^A-Za-z0-9!*+\/ -'; 3782 break; 3783 /* 3784 * RFC 2047 section 5.2. 3785 * Build $pattern without including delimiters and [] 3786 */ 3787 /* @noinspection PhpMissingBreakStatementInspection */ 3788 case 'comment': 3789 $pattern = '\(\)"'; 3790 /* Intentional fall through */ 3791 case 'text': 3792 default: 3793 //RFC 2047 section 5.1 3794 //Replace every high ascii, control, =, ? and _ characters 3795 $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern; 3796 break; 3797 } 3798 $matches = []; 3799 if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) { 3800 //If the string contains an '=', make sure it's the first thing we replace 3801 //so as to avoid double-encoding 3802 $eqkey = array_search('=', $matches[0], true); 3803 if (false !== $eqkey) { 3804 unset($matches[0][$eqkey]); 3805 array_unshift($matches[0], '='); 3806 } 3807 foreach (array_unique($matches[0]) as $char) { 3808 $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded); 3809 } 3810 } 3811 //Replace spaces with _ (more readable than =20) 3812 //RFC 2047 section 4.2(2) 3813 return str_replace(' ', '_', $encoded); 3814 } 3815 3816 /** 3817 * Add a string or binary attachment (non-filesystem). 3818 * This method can be used to attach ascii or binary data, 3819 * such as a BLOB record from a database. 3820 * 3821 * @param string $string String attachment data 3822 * @param string $filename Name of the attachment 3823 * @param string $encoding File encoding (see $Encoding) 3824 * @param string $type File extension (MIME) type 3825 * @param string $disposition Disposition to use 3826 * 3827 * @throws Exception 3828 * 3829 * @return bool True on successfully adding an attachment 3830 */ 3831 public function addStringAttachment( 3832 $string, 3833 $filename, 3834 $encoding = self::ENCODING_BASE64, 3835 $type = '', 3836 $disposition = 'attachment' 3837 ) { 3838 try { 3839 //If a MIME type is not specified, try to work it out from the file name 3840 if ('' === $type) { 3841 $type = static::filenameToType($filename); 3842 } 3843 3844 if (!$this->validateEncoding($encoding)) { 3845 throw new Exception($this->lang('encoding') . $encoding); 3846 } 3847 3848 //Append to $attachment array 3849 $this->attachment[] = [ 3850 0 => $string, 3851 1 => $filename, 3852 2 => static::mb_pathinfo($filename, PATHINFO_BASENAME), 3853 3 => $encoding, 3854 4 => $type, 3855 5 => true, //isStringAttachment 3856 6 => $disposition, 3857 7 => 0, 3858 ]; 3859 } catch (Exception $exc) { 3860 $this->setError($exc->getMessage()); 3861 $this->edebug($exc->getMessage()); 3862 if ($this->exceptions) { 3863 throw $exc; 3864 } 3865 3866 return false; 3867 } 3868 3869 return true; 3870 } 3871 3872 /** 3873 * Add an embedded (inline) attachment from a file. 3874 * This can include images, sounds, and just about any other document type. 3875 * These differ from 'regular' attachments in that they are intended to be 3876 * displayed inline with the message, not just attached for download. 3877 * This is used in HTML messages that embed the images 3878 * the HTML refers to using the `$cid` value in `img` tags, for example `<img src="cid:mylogo">`. 3879 * Never use a user-supplied path to a file! 3880 * 3881 * @param string $path Path to the attachment 3882 * @param string $cid Content ID of the attachment; Use this to reference 3883 * the content when using an embedded image in HTML 3884 * @param string $name Overrides the attachment filename 3885 * @param string $encoding File encoding (see $Encoding) defaults to `base64` 3886 * @param string $type File MIME type (by default mapped from the `$path` filename's extension) 3887 * @param string $disposition Disposition to use: `inline` (default) or `attachment` 3888 * (unlikely you want this – {@see `addAttachment()`} instead) 3889 * 3890 * @return bool True on successfully adding an attachment 3891 * @throws Exception 3892 * 3893 */ 3894 public function addEmbeddedImage( 3895 $path, 3896 $cid, 3897 $name = '', 3898 $encoding = self::ENCODING_BASE64, 3899 $type = '', 3900 $disposition = 'inline' 3901 ) { 3902 try { 3903 if (!static::fileIsAccessible($path)) { 3904 throw new Exception($this->lang('file_access') . $path, self::STOP_CONTINUE); 3905 } 3906 3907 //If a MIME type is not specified, try to work it out from the file name 3908 if ('' === $type) { 3909 $type = static::filenameToType($path); 3910 } 3911 3912 if (!$this->validateEncoding($encoding)) { 3913 throw new Exception($this->lang('encoding') . $encoding); 3914 } 3915 3916 $filename = (string) static::mb_pathinfo($path, PATHINFO_BASENAME); 3917 if ('' === $name) { 3918 $name = $filename; 3919 } 3920 3921 //Append to $attachment array 3922 $this->attachment[] = [ 3923 0 => $path, 3924 1 => $filename, 3925 2 => $name, 3926 3 => $encoding, 3927 4 => $type, 3928 5 => false, //isStringAttachment 3929 6 => $disposition, 3930 7 => $cid, 3931 ]; 3932 } catch (Exception $exc) { 3933 $this->setError($exc->getMessage()); 3934 $this->edebug($exc->getMessage()); 3935 if ($this->exceptions) { 3936 throw $exc; 3937 } 3938 3939 return false; 3940 } 3941 3942 return true; 3943 } 3944 3945 /** 3946 * Add an embedded stringified attachment. 3947 * This can include images, sounds, and just about any other document type. 3948 * If your filename doesn't contain an extension, be sure to set the $type to an appropriate MIME type. 3949 * 3950 * @param string $string The attachment binary data 3951 * @param string $cid Content ID of the attachment; Use this to reference 3952 * the content when using an embedded image in HTML 3953 * @param string $name A filename for the attachment. If this contains an extension, 3954 * PHPMailer will attempt to set a MIME type for the attachment. 3955 * For example 'file.jpg' would get an 'image/jpeg' MIME type. 3956 * @param string $encoding File encoding (see $Encoding), defaults to 'base64' 3957 * @param string $type MIME type - will be used in preference to any automatically derived type 3958 * @param string $disposition Disposition to use 3959 * 3960 * @throws Exception 3961 * 3962 * @return bool True on successfully adding an attachment 3963 */ 3964 public function addStringEmbeddedImage( 3965 $string, 3966 $cid, 3967 $name = '', 3968 $encoding = self::ENCODING_BASE64, 3969 $type = '', 3970 $disposition = 'inline' 3971 ) { 3972 try { 3973 //If a MIME type is not specified, try to work it out from the name 3974 if ('' === $type && !empty($name)) { 3975 $type = static::filenameToType($name); 3976 } 3977 3978 if (!$this->validateEncoding($encoding)) { 3979 throw new Exception($this->lang('encoding') . $encoding); 3980 } 3981 3982 //Append to $attachment array 3983 $this->attachment[] = [ 3984 0 => $string, 3985 1 => $name, 3986 2 => $name, 3987 3 => $encoding, 3988 4 => $type, 3989 5 => true, //isStringAttachment 3990 6 => $disposition, 3991 7 => $cid, 3992 ]; 3993 } catch (Exception $exc) { 3994 $this->setError($exc->getMessage()); 3995 $this->edebug($exc->getMessage()); 3996 if ($this->exceptions) { 3997 throw $exc; 3998 } 3999 4000 return false; 4001 } 4002 4003 return true; 4004 } 4005 4006 /** 4007 * Validate encodings. 4008 * 4009 * @param string $encoding 4010 * 4011 * @return bool 4012 */ 4013 protected function validateEncoding($encoding) 4014 { 4015 return in_array( 4016 $encoding, 4017 [ 4018 self::ENCODING_7BIT, 4019 self::ENCODING_QUOTED_PRINTABLE, 4020 self::ENCODING_BASE64, 4021 self::ENCODING_8BIT, 4022 self::ENCODING_BINARY, 4023 ], 4024 true 4025 ); 4026 } 4027 4028 /** 4029 * Check if an embedded attachment is present with this cid. 4030 * 4031 * @param string $cid 4032 * 4033 * @return bool 4034 */ 4035 protected function cidExists($cid) 4036 { 4037 foreach ($this->attachment as $attachment) { 4038 if ('inline' === $attachment[6] && $cid === $attachment[7]) { 4039 return true; 4040 } 4041 } 4042 4043 return false; 4044 } 4045 4046 /** 4047 * Check if an inline attachment is present. 4048 * 4049 * @return bool 4050 */ 4051 public function inlineImageExists() 4052 { 4053 foreach ($this->attachment as $attachment) { 4054 if ('inline' === $attachment[6]) { 4055 return true; 4056 } 4057 } 4058 4059 return false; 4060 } 4061 4062 /** 4063 * Check if an attachment (non-inline) is present. 4064 * 4065 * @return bool 4066 */ 4067 public function attachmentExists() 4068 { 4069 foreach ($this->attachment as $attachment) { 4070 if ('attachment' === $attachment[6]) { 4071 return true; 4072 } 4073 } 4074 4075 return false; 4076 } 4077 4078 /** 4079 * Check if this message has an alternative body set. 4080 * 4081 * @return bool 4082 */ 4083 public function alternativeExists() 4084 { 4085 return !empty($this->AltBody); 4086 } 4087 4088 /** 4089 * Clear queued addresses of given kind. 4090 * 4091 * @param string $kind 'to', 'cc', or 'bcc' 4092 */ 4093 public function clearQueuedAddresses($kind) 4094 { 4095 $this->RecipientsQueue = array_filter( 4096 $this->RecipientsQueue, 4097 static function ($params) use ($kind) { 4098 return $params[0] !== $kind; 4099 } 4100 ); 4101 } 4102 4103 /** 4104 * Clear all To recipients. 4105 */ 4106 public function clearAddresses() 4107 { 4108 foreach ($this->to as $to) { 4109 unset($this->all_recipients[strtolower($to[0])]); 4110 } 4111 $this->to = []; 4112 $this->clearQueuedAddresses('to'); 4113 } 4114 4115 /** 4116 * Clear all CC recipients. 4117 */ 4118 public function clearCCs() 4119 { 4120 foreach ($this->cc as $cc) { 4121 unset($this->all_recipients[strtolower($cc[0])]); 4122 } 4123 $this->cc = []; 4124 $this->clearQueuedAddresses('cc'); 4125 } 4126 4127 /** 4128 * Clear all BCC recipients. 4129 */ 4130 public function clearBCCs() 4131 { 4132 foreach ($this->bcc as $bcc) { 4133 unset($this->all_recipients[strtolower($bcc[0])]); 4134 } 4135 $this->bcc = []; 4136 $this->clearQueuedAddresses('bcc'); 4137 } 4138 4139 /** 4140 * Clear all ReplyTo recipients. 4141 */ 4142 public function clearReplyTos() 4143 { 4144 $this->ReplyTo = []; 4145 $this->ReplyToQueue = []; 4146 } 4147 4148 /** 4149 * Clear all recipient types. 4150 */ 4151 public function clearAllRecipients() 4152 { 4153 $this->to = []; 4154 $this->cc = []; 4155 $this->bcc = []; 4156 $this->all_recipients = []; 4157 $this->RecipientsQueue = []; 4158 } 4159 4160 /** 4161 * Clear all filesystem, string, and binary attachments. 4162 */ 4163 public function clearAttachments() 4164 { 4165 $this->attachment = []; 4166 } 4167 4168 /** 4169 * Clear all custom headers. 4170 */ 4171 public function clearCustomHeaders() 4172 { 4173 $this->CustomHeader = []; 4174 } 4175 4176 /** 4177 * Clear a specific custom header by name or name and value. 4178 * $name value can be overloaded to contain 4179 * both header name and value (name:value). 4180 * 4181 * @param string $name Custom header name 4182 * @param string|null $value Header value 4183 * 4184 * @return bool True if a header was replaced successfully 4185 */ 4186 public function clearCustomHeader($name, $value = null) 4187 { 4188 if (null === $value && strpos($name, ':') !== false) { 4189 //Value passed in as name:value 4190 list($name, $value) = explode(':', $name, 2); 4191 } 4192 $name = trim($name); 4193 $value = (null === $value) ? null : trim($value); 4194 4195 foreach ($this->CustomHeader as $k => $pair) { 4196 if ($pair[0] == $name) { 4197 // We remove the header if the value is not provided or it matches. 4198 if (null === $value || $pair[1] == $value) { 4199 unset($this->CustomHeader[$k]); 4200 } 4201 } 4202 } 4203 4204 return true; 4205 } 4206 4207 /** 4208 * Replace a custom header. 4209 * $name value can be overloaded to contain 4210 * both header name and value (name:value). 4211 * 4212 * @param string $name Custom header name 4213 * @param string|null $value Header value 4214 * 4215 * @return bool True if a header was replaced successfully 4216 * @throws Exception 4217 */ 4218 public function replaceCustomHeader($name, $value = null) 4219 { 4220 if (null === $value && strpos($name, ':') !== false) { 4221 //Value passed in as name:value 4222 list($name, $value) = explode(':', $name, 2); 4223 } 4224 $name = trim($name); 4225 $value = (null === $value) ? '' : trim($value); 4226 4227 $replaced = false; 4228 foreach ($this->CustomHeader as $k => $pair) { 4229 if ($pair[0] == $name) { 4230 if ($replaced) { 4231 unset($this->CustomHeader[$k]); 4232 continue; 4233 } 4234 if (strpbrk($name . $value, "\r\n") !== false) { 4235 if ($this->exceptions) { 4236 throw new Exception($this->lang('invalid_header')); 4237 } 4238 4239 return false; 4240 } 4241 $this->CustomHeader[$k] = [$name, $value]; 4242 $replaced = true; 4243 } 4244 } 4245 4246 return true; 4247 } 4248 4249 /** 4250 * Add an error message to the error container. 4251 * 4252 * @param string $msg 4253 */ 4254 protected function setError($msg) 4255 { 4256 ++$this->error_count; 4257 if ('smtp' === $this->Mailer && null !== $this->smtp) { 4258 $lasterror = $this->smtp->getError(); 4259 if (!empty($lasterror['error'])) { 4260 $msg .= ' ' . $this->lang('smtp_error') . $lasterror['error']; 4261 if (!empty($lasterror['detail'])) { 4262 $msg .= ' ' . $this->lang('smtp_detail') . $lasterror['detail']; 4263 } 4264 if (!empty($lasterror['smtp_code'])) { 4265 $msg .= ' ' . $this->lang('smtp_code') . $lasterror['smtp_code']; 4266 } 4267 if (!empty($lasterror['smtp_code_ex'])) { 4268 $msg .= ' ' . $this->lang('smtp_code_ex') . $lasterror['smtp_code_ex']; 4269 } 4270 } 4271 } 4272 $this->ErrorInfo = $msg; 4273 } 4274 4275 /** 4276 * Return an RFC 822 formatted date. 4277 * 4278 * @return string 4279 */ 4280 public static function rfcDate() 4281 { 4282 //Set the time zone to whatever the default is to avoid 500 errors 4283 //Will default to UTC if it's not set properly in php.ini 4284 date_default_timezone_set(@date_default_timezone_get()); 4285 4286 return date('D, j M Y H:i:s O'); 4287 } 4288 4289 /** 4290 * Get the server hostname. 4291 * Returns 'localhost.localdomain' if unknown. 4292 * 4293 * @return string 4294 */ 4295 protected function serverHostname() 4296 { 4297 $result = ''; 4298 if (!empty($this->Hostname)) { 4299 $result = $this->Hostname; 4300 } elseif (isset($_SERVER) && array_key_exists('SERVER_NAME', $_SERVER)) { 4301 $result = $_SERVER['SERVER_NAME']; 4302 } elseif (function_exists('gethostname') && gethostname() !== false) { 4303 $result = gethostname(); 4304 } elseif (php_uname('n') !== '') { 4305 $result = php_uname('n'); 4306 } 4307 if (!static::isValidHost($result)) { 4308 return 'localhost.localdomain'; 4309 } 4310 4311 return $result; 4312 } 4313 4314 /** 4315 * Validate whether a string contains a valid value to use as a hostname or IP address. 4316 * IPv6 addresses must include [], e.g. `[::1]`, not just `::1`. 4317 * 4318 * @param string $host The host name or IP address to check 4319 * 4320 * @return bool 4321 */ 4322 public static function isValidHost($host) 4323 { 4324 //Simple syntax limits 4325 if ( 4326 empty($host) 4327 || !is_string($host) 4328 || strlen($host) > 256 4329 || !preg_match('/^([a-z\d.-]*|\[[a-f\d:]+\])$/i', $host) 4330 ) { 4331 return false; 4332 } 4333 //Looks like a bracketed IPv6 address 4334 if (strlen($host) > 2 && substr($host, 0, 1) === '[' && substr($host, -1, 1) === ']') { 4335 return filter_var(substr($host, 1, -1), FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false; 4336 } 4337 //If removing all the dots results in a numeric string, it must be an IPv4 address. 4338 //Need to check this first because otherwise things like `999.0.0.0` are considered valid host names 4339 if (is_numeric(str_replace('.', '', $host))) { 4340 //Is it a valid IPv4 address? 4341 return filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false; 4342 } 4343 //Is it a syntactically valid hostname (when embedded in a URL)? 4344 return filter_var('https://' . $host, FILTER_VALIDATE_URL) !== false; 4345 } 4346 4347 /** 4348 * Check whether the supplied address uses Unicode in the local part. 4349 * 4350 * @return bool 4351 */ 4352 protected function addressHasUnicodeLocalPart($address) 4353 { 4354 return (bool) preg_match('/[\x80-\xFF].*@/', $address); 4355 } 4356 4357 /** 4358 * Check whether any of the supplied addresses use Unicode in the local part. 4359 * 4360 * @return bool 4361 */ 4362 protected function anyAddressHasUnicodeLocalPart($addresses) 4363 { 4364 foreach ($addresses as $address) { 4365 if (is_array($address)) { 4366 $address = $address[0]; 4367 } 4368 if ($this->addressHasUnicodeLocalPart($address)) { 4369 return true; 4370 } 4371 } 4372 return false; 4373 } 4374 4375 /** 4376 * Check whether the message requires SMTPUTF8 based on what's known so far. 4377 * 4378 * @return bool 4379 */ 4380 public function needsSMTPUTF8() 4381 { 4382 return $this->UseSMTPUTF8; 4383 } 4384 4385 4386 /** 4387 * Get an error message in the current language. 4388 * 4389 * @param string $key 4390 * 4391 * @return string 4392 */ 4393 protected function lang($key) 4394 { 4395 if (count($this->language) < 1) { 4396 $this->setLanguage(); //Set the default language 4397 } 4398 4399 if (array_key_exists($key, $this->language)) { 4400 if ('smtp_connect_failed' === $key) { 4401 //Include a link to troubleshooting docs on SMTP connection failure. 4402 //This is by far the biggest cause of support questions 4403 //but it's usually not PHPMailer's fault. 4404 return $this->language[$key] . ' https://github.com/PHPMailer/PHPMailer/wiki/Troubleshooting'; 4405 } 4406 4407 return $this->language[$key]; 4408 } 4409 4410 //Return the key as a fallback 4411 return $key; 4412 } 4413 4414 /** 4415 * Build an error message starting with a generic one and adding details if possible. 4416 * 4417 * @param string $base_key 4418 * @return string 4419 */ 4420 private function getSmtpErrorMessage($base_key) 4421 { 4422 $message = $this->lang($base_key); 4423 $error = $this->smtp->getError(); 4424 if (!empty($error['error'])) { 4425 $message .= ' ' . $error['error']; 4426 if (!empty($error['detail'])) { 4427 $message .= ' ' . $error['detail']; 4428 } 4429 } 4430 4431 return $message; 4432 } 4433 4434 /** 4435 * Check if an error occurred. 4436 * 4437 * @return bool True if an error did occur 4438 */ 4439 public function isError() 4440 { 4441 return $this->error_count > 0; 4442 } 4443 4444 /** 4445 * Add a custom header. 4446 * $name value can be overloaded to contain 4447 * both header name and value (name:value). 4448 * 4449 * @param string $name Custom header name 4450 * @param string|null $value Header value 4451 * 4452 * @return bool True if a header was set successfully 4453 * @throws Exception 4454 */ 4455 public function addCustomHeader($name, $value = null) 4456 { 4457 if (null === $value && strpos($name, ':') !== false) { 4458 //Value passed in as name:value 4459 list($name, $value) = explode(':', $name, 2); 4460 } 4461 $name = trim($name); 4462 $value = (null === $value) ? '' : trim($value); 4463 //Ensure name is not empty, and that neither name nor value contain line breaks 4464 if (empty($name) || strpbrk($name . $value, "\r\n") !== false) { 4465 if ($this->exceptions) { 4466 throw new Exception($this->lang('invalid_header')); 4467 } 4468 4469 return false; 4470 } 4471 $this->CustomHeader[] = [$name, $value]; 4472 4473 return true; 4474 } 4475 4476 /** 4477 * Returns all custom headers. 4478 * 4479 * @return array 4480 */ 4481 public function getCustomHeaders() 4482 { 4483 return $this->CustomHeader; 4484 } 4485 4486 /** 4487 * Create a message body from an HTML string. 4488 * Automatically inlines images and creates a plain-text version by converting the HTML, 4489 * overwriting any existing values in Body and AltBody. 4490 * Do not source $message content from user input! 4491 * $basedir is prepended when handling relative URLs, e.g. <img src="/images/a.png"> and must not be empty 4492 * will look for an image file in $basedir/images/a.png and convert it to inline. 4493 * If you don't provide a $basedir, relative paths will be left untouched (and thus probably break in email) 4494 * Converts data-uri images into embedded attachments. 4495 * If you don't want to apply these transformations to your HTML, just set Body and AltBody directly. 4496 * 4497 * @param string $message HTML message string 4498 * @param string $basedir Absolute path to a base directory to prepend to relative paths to images 4499 * @param bool|callable $advanced Whether to use the internal HTML to text converter 4500 * or your own custom converter 4501 * @return string The transformed message body 4502 * 4503 * @throws Exception 4504 * 4505 * @see PHPMailer::html2text() 4506 */ 4507 public function msgHTML($message, $basedir = '', $advanced = false) 4508 { 4509 preg_match_all('/(?<!-)(src|background)=["\'](.*)["\']/Ui', $message, $images); 4510 if (array_key_exists(2, $images)) { 4511 if (strlen($basedir) > 1 && '/' !== substr($basedir, -1)) { 4512 //Ensure $basedir has a trailing / 4513 $basedir .= '/'; 4514 } 4515 foreach ($images[2] as $imgindex => $url) { 4516 //Convert data URIs into embedded images 4517 //e.g. "data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" 4518 $match = []; 4519 if (preg_match('#^data:(image/(?:jpe?g|gif|png));?(base64)?,(.+)#', $url, $match)) { 4520 if (count($match) === 4 && static::ENCODING_BASE64 === $match[2]) { 4521 $data = base64_decode($match[3]); 4522 } elseif ('' === $match[2]) { 4523 $data = rawurldecode($match[3]); 4524 } else { 4525 //Not recognised so leave it alone 4526 continue; 4527 } 4528 //Hash the decoded data, not the URL, so that the same data-URI image used in multiple places 4529 //will only be embedded once, even if it used a different encoding 4530 $cid = substr(hash('sha256', $data), 0, 32) . '@phpmailer.0'; //RFC2392 S 2 4531 4532 if (!$this->cidExists($cid)) { 4533 $this->addStringEmbeddedImage( 4534 $data, 4535 $cid, 4536 'embed' . $imgindex, 4537 static::ENCODING_BASE64, 4538 $match[1] 4539 ); 4540 } 4541 $message = str_replace( 4542 $images[0][$imgindex], 4543 $images[1][$imgindex] . '="cid:' . $cid . '"', 4544 $message 4545 ); 4546 continue; 4547 } 4548 if ( 4549 //Only process relative URLs if a basedir is provided (i.e. no absolute local paths) 4550 !empty($basedir) 4551 //Ignore URLs containing parent dir traversal (..) 4552 && (strpos($url, '..') === false) 4553 //Do not change urls that are already inline images 4554 && 0 !== strpos($url, 'cid:') 4555 //Do not change absolute URLs, including anonymous protocol 4556 && !preg_match('#^[a-z][a-z0-9+.-]*:?//#i', $url) 4557 ) { 4558 $filename = static::mb_pathinfo($url, PATHINFO_BASENAME); 4559 $directory = dirname($url); 4560 if ('.' === $directory) { 4561 $directory = ''; 4562 } 4563 //RFC2392 S 2 4564 $cid = substr(hash('sha256', $url), 0, 32) . '@phpmailer.0'; 4565 if (strlen($basedir) > 1 && '/' !== substr($basedir, -1)) { 4566 $basedir .= '/'; 4567 } 4568 if (strlen($directory) > 1 && '/' !== substr($directory, -1)) { 4569 $directory .= '/'; 4570 } 4571 if ( 4572 $this->addEmbeddedImage( 4573 $basedir . $directory . $filename, 4574 $cid, 4575 $filename, 4576 static::ENCODING_BASE64, 4577 static::_mime_types((string) static::mb_pathinfo($filename, PATHINFO_EXTENSION)) 4578 ) 4579 ) { 4580 $message = preg_replace( 4581 '/' . $images[1][$imgindex] . '=["\']' . preg_quote($url, '/') . '["\']/Ui', 4582 $images[1][$imgindex] . '="cid:' . $cid . '"', 4583 $message 4584 ); 4585 } 4586 } 4587 } 4588 } 4589 $this->isHTML(); 4590 //Convert all message body line breaks to LE, makes quoted-printable encoding work much better 4591 $this->Body = static::normalizeBreaks($message); 4592 $this->AltBody = static::normalizeBreaks($this->html2text($message, $advanced)); 4593 if (!$this->alternativeExists()) { 4594 $this->AltBody = 'This is an HTML-only message. To view it, activate HTML in your email application.' 4595 . static::$LE; 4596 } 4597 4598 return $this->Body; 4599 } 4600 4601 /** 4602 * Convert an HTML string into plain text. 4603 * This is used by msgHTML(). 4604 * Note - older versions of this function used a bundled advanced converter 4605 * which was removed for license reasons in #232. 4606 * Example usage: 4607 * 4608 * ```php 4609 * //Use default conversion 4610 * $plain = $mail->html2text($html); 4611 * //Use your own custom converter 4612 * $plain = $mail->html2text($html, function($html) { 4613 * $converter = new MyHtml2text($html); 4614 * return $converter->get_text(); 4615 * }); 4616 * ``` 4617 * 4618 * @param string $html The HTML text to convert 4619 * @param bool|callable $advanced Any boolean value to use the internal converter, 4620 * or provide your own callable for custom conversion. 4621 * *Never* pass user-supplied data into this parameter 4622 * 4623 * @return string 4624 */ 4625 public function html2text($html, $advanced = false) 4626 { 4627 if (is_callable($advanced)) { 4628 return call_user_func($advanced, $html); 4629 } 4630 4631 return html_entity_decode( 4632 trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/si', '', $html))), 4633 ENT_QUOTES, 4634 $this->CharSet 4635 ); 4636 } 4637 4638 /** 4639 * Get the MIME type for a file extension. 4640 * 4641 * @param string $ext File extension 4642 * 4643 * @return string MIME type of file 4644 */ 4645 public static function _mime_types($ext = '') 4646 { 4647 $mimes = [ 4648 'xl' => 'application/excel', 4649 'js' => 'application/javascript', 4650 'hqx' => 'application/mac-binhex40', 4651 'cpt' => 'application/mac-compactpro', 4652 'bin' => 'application/macbinary', 4653 'doc' => 'application/msword', 4654 'word' => 'application/msword', 4655 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 4656 'xltx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.template', 4657 'potx' => 'application/vnd.openxmlformats-officedocument.presentationml.template', 4658 'ppsx' => 'application/vnd.openxmlformats-officedocument.presentationml.slideshow', 4659 'pptx' => 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 4660 'sldx' => 'application/vnd.openxmlformats-officedocument.presentationml.slide', 4661 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 4662 'dotx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.template', 4663 'xlam' => 'application/vnd.ms-excel.addin.macroEnabled.12', 4664 'xlsb' => 'application/vnd.ms-excel.sheet.binary.macroEnabled.12', 4665 'class' => 'application/octet-stream', 4666 'dll' => 'application/octet-stream', 4667 'dms' => 'application/octet-stream', 4668 'exe' => 'application/octet-stream', 4669 'lha' => 'application/octet-stream', 4670 'lzh' => 'application/octet-stream', 4671 'psd' => 'application/octet-stream', 4672 'sea' => 'application/octet-stream', 4673 'so' => 'application/octet-stream', 4674 'oda' => 'application/oda', 4675 'pdf' => 'application/pdf', 4676 'ai' => 'application/postscript', 4677 'eps' => 'application/postscript', 4678 'ps' => 'application/postscript', 4679 'smi' => 'application/smil', 4680 'smil' => 'application/smil', 4681 'mif' => 'application/vnd.mif', 4682 'xls' => 'application/vnd.ms-excel', 4683 'ppt' => 'application/vnd.ms-powerpoint', 4684 'wbxml' => 'application/vnd.wap.wbxml', 4685 'wmlc' => 'application/vnd.wap.wmlc', 4686 'dcr' => 'application/x-director', 4687 'dir' => 'application/x-director', 4688 'dxr' => 'application/x-director', 4689 'dvi' => 'application/x-dvi', 4690 'gtar' => 'application/x-gtar', 4691 'php3' => 'application/x-httpd-php', 4692 'php4' => 'application/x-httpd-php', 4693 'php' => 'application/x-httpd-php', 4694 'phtml' => 'application/x-httpd-php', 4695 'phps' => 'application/x-httpd-php-source', 4696 'swf' => 'application/x-shockwave-flash', 4697 'sit' => 'application/x-stuffit', 4698 'tar' => 'application/x-tar', 4699 'tgz' => 'application/x-tar', 4700 'xht' => 'application/xhtml+xml', 4701 'xhtml' => 'application/xhtml+xml', 4702 'zip' => 'application/zip', 4703 'mid' => 'audio/midi', 4704 'midi' => 'audio/midi', 4705 'mp2' => 'audio/mpeg', 4706 'mp3' => 'audio/mpeg', 4707 'm4a' => 'audio/mp4', 4708 'mpga' => 'audio/mpeg', 4709 'aif' => 'audio/x-aiff', 4710 'aifc' => 'audio/x-aiff', 4711 'aiff' => 'audio/x-aiff', 4712 'ram' => 'audio/x-pn-realaudio', 4713 'rm' => 'audio/x-pn-realaudio', 4714 'rpm' => 'audio/x-pn-realaudio-plugin', 4715 'ra' => 'audio/x-realaudio', 4716 'wav' => 'audio/x-wav', 4717 'mka' => 'audio/x-matroska', 4718 'bmp' => 'image/bmp', 4719 'gif' => 'image/gif', 4720 'jpeg' => 'image/jpeg', 4721 'jpe' => 'image/jpeg', 4722 'jpg' => 'image/jpeg', 4723 'png' => 'image/png', 4724 'tiff' => 'image/tiff', 4725 'tif' => 'image/tiff', 4726 'webp' => 'image/webp', 4727 'avif' => 'image/avif', 4728 'heif' => 'image/heif', 4729 'heifs' => 'image/heif-sequence', 4730 'heic' => 'image/heic', 4731 'heics' => 'image/heic-sequence', 4732 'eml' => 'message/rfc822', 4733 'css' => 'text/css', 4734 'html' => 'text/html', 4735 'htm' => 'text/html', 4736 'shtml' => 'text/html', 4737 'log' => 'text/plain', 4738 'text' => 'text/plain', 4739 'txt' => 'text/plain', 4740 'rtx' => 'text/richtext', 4741 'rtf' => 'text/rtf', 4742 'vcf' => 'text/vcard', 4743 'vcard' => 'text/vcard', 4744 'ics' => 'text/calendar', 4745 'xml' => 'text/xml', 4746 'xsl' => 'text/xml', 4747 'csv' => 'text/csv', 4748 'wmv' => 'video/x-ms-wmv', 4749 'mpeg' => 'video/mpeg', 4750 'mpe' => 'video/mpeg', 4751 'mpg' => 'video/mpeg', 4752 'mp4' => 'video/mp4', 4753 'm4v' => 'video/mp4', 4754 'mov' => 'video/quicktime', 4755 'qt' => 'video/quicktime', 4756 'rv' => 'video/vnd.rn-realvideo', 4757 'avi' => 'video/x-msvideo', 4758 'movie' => 'video/x-sgi-movie', 4759 'webm' => 'video/webm', 4760 'mkv' => 'video/x-matroska', 4761 ]; 4762 $ext = strtolower($ext); 4763 if (array_key_exists($ext, $mimes)) { 4764 return $mimes[$ext]; 4765 } 4766 4767 return 'application/octet-stream'; 4768 } 4769 4770 /** 4771 * Map a file name to a MIME type. 4772 * Defaults to 'application/octet-stream', i.e.. arbitrary binary data. 4773 * 4774 * @param string $filename A file name or full path, does not need to exist as a file 4775 * 4776 * @return string 4777 */ 4778 public static function filenameToType($filename) 4779 { 4780 //In case the path is a URL, strip any query string before getting extension 4781 $qpos = strpos($filename, '?'); 4782 if (false !== $qpos) { 4783 $filename = substr($filename, 0, $qpos); 4784 } 4785 $ext = static::mb_pathinfo($filename, PATHINFO_EXTENSION); 4786 4787 return static::_mime_types($ext); 4788 } 4789 4790 /** 4791 * Multi-byte-safe pathinfo replacement. 4792 * Drop-in replacement for pathinfo(), but multibyte- and cross-platform-safe. 4793 * 4794 * @see https://www.php.net/manual/en/function.pathinfo.php#107461 4795 * 4796 * @param string $path A filename or path, does not need to exist as a file 4797 * @param int|string $options Either a PATHINFO_* constant, 4798 * or a string name to return only the specified piece 4799 * 4800 * @return string|array 4801 */ 4802 public static function mb_pathinfo($path, $options = null) 4803 { 4804 $ret = ['dirname' => '', 'basename' => '', 'extension' => '', 'filename' => '']; 4805 $pathinfo = []; 4806 if (preg_match('#^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^.\\\\/]+?)|))[\\\\/.]*$#m', $path, $pathinfo)) { 4807 if (array_key_exists(1, $pathinfo)) { 4808 $ret['dirname'] = $pathinfo[1]; 4809 } 4810 if (array_key_exists(2, $pathinfo)) { 4811 $ret['basename'] = $pathinfo[2]; 4812 } 4813 if (array_key_exists(5, $pathinfo)) { 4814 $ret['extension'] = $pathinfo[5]; 4815 } 4816 if (array_key_exists(3, $pathinfo)) { 4817 $ret['filename'] = $pathinfo[3]; 4818 } 4819 } 4820 switch ($options) { 4821 case PATHINFO_DIRNAME: 4822 case 'dirname': 4823 return $ret['dirname']; 4824 case PATHINFO_BASENAME: 4825 case 'basename': 4826 return $ret['basename']; 4827 case PATHINFO_EXTENSION: 4828 case 'extension': 4829 return $ret['extension']; 4830 case PATHINFO_FILENAME: 4831 case 'filename': 4832 return $ret['filename']; 4833 default: 4834 return $ret; 4835 } 4836 } 4837 4838 /** 4839 * Set or reset instance properties. 4840 * You should avoid this function - it's more verbose, less efficient, more error-prone and 4841 * harder to debug than setting properties directly. 4842 * Usage Example: 4843 * `$mail->set('SMTPSecure', static::ENCRYPTION_STARTTLS);` 4844 * is the same as: 4845 * `$mail->SMTPSecure = static::ENCRYPTION_STARTTLS;`. 4846 * 4847 * @param string $name The property name to set 4848 * @param mixed $value The value to set the property to 4849 * 4850 * @return bool 4851 */ 4852 public function set($name, $value = '') 4853 { 4854 if (property_exists($this, $name)) { 4855 $this->{$name} = $value; 4856 4857 return true; 4858 } 4859 $this->setError($this->lang('variable_set') . $name); 4860 4861 return false; 4862 } 4863 4864 /** 4865 * Strip newlines to prevent header injection. 4866 * 4867 * @param string $str 4868 * 4869 * @return string 4870 */ 4871 public function secureHeader($str) 4872 { 4873 return trim(str_replace(["\r", "\n"], '', $str)); 4874 } 4875 4876 /** 4877 * Normalize line breaks in a string. 4878 * Converts UNIX LF, Mac CR and Windows CRLF line breaks into a single line break format. 4879 * Defaults to CRLF (for message bodies) and preserves consecutive breaks. 4880 * 4881 * @param string $text 4882 * @param string $breaktype What kind of line break to use; defaults to static::$LE 4883 * 4884 * @return string 4885 */ 4886 public static function normalizeBreaks($text, $breaktype = null) 4887 { 4888 if (null === $breaktype) { 4889 $breaktype = static::$LE; 4890 } 4891 //Normalise to \n 4892 $text = str_replace([self::CRLF, "\r"], "\n", $text); 4893 //Now convert LE as needed 4894 if ("\n" !== $breaktype) { 4895 $text = str_replace("\n", $breaktype, $text); 4896 } 4897 4898 return $text; 4899 } 4900 4901 /** 4902 * Remove trailing whitespace from a string. 4903 * 4904 * @param string $text 4905 * 4906 * @return string The text to remove whitespace from 4907 */ 4908 public static function stripTrailingWSP($text) 4909 { 4910 return rtrim($text, " \r\n\t"); 4911 } 4912 4913 /** 4914 * Strip trailing line breaks from a string. 4915 * 4916 * @param string $text 4917 * 4918 * @return string The text to remove breaks from 4919 */ 4920 public static function stripTrailingBreaks($text) 4921 { 4922 return rtrim($text, "\r\n"); 4923 } 4924 4925 /** 4926 * Return the current line break format string. 4927 * 4928 * @return string 4929 */ 4930 public static function getLE() 4931 { 4932 return static::$LE; 4933 } 4934 4935 /** 4936 * Set the line break format string, e.g. "\r\n". 4937 * 4938 * @param string $le 4939 */ 4940 protected static function setLE($le) 4941 { 4942 static::$LE = $le; 4943 } 4944 4945 /** 4946 * Set the public and private key files and password for S/MIME signing. 4947 * 4948 * @param string $cert_filename 4949 * @param string $key_filename 4950 * @param string $key_pass Password for private key 4951 * @param string $extracerts_filename Optional path to chain certificate 4952 */ 4953 public function sign($cert_filename, $key_filename, $key_pass, $extracerts_filename = '') 4954 { 4955 $this->sign_cert_file = $cert_filename; 4956 $this->sign_key_file = $key_filename; 4957 $this->sign_key_pass = $key_pass; 4958 $this->sign_extracerts_file = $extracerts_filename; 4959 } 4960 4961 /** 4962 * Quoted-Printable-encode a DKIM header. 4963 * 4964 * @param string $txt 4965 * 4966 * @return string 4967 */ 4968 public function DKIM_QP($txt) 4969 { 4970 $line = ''; 4971 $len = strlen($txt); 4972 for ($i = 0; $i < $len; ++$i) { 4973 $ord = ord($txt[$i]); 4974 if (((0x21 <= $ord) && ($ord <= 0x3A)) || $ord === 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E))) { 4975 $line .= $txt[$i]; 4976 } else { 4977 $line .= '=' . sprintf('%02X', $ord); 4978 } 4979 } 4980 4981 return $line; 4982 } 4983 4984 /** 4985 * Generate a DKIM signature. 4986 * 4987 * @param string $signHeader 4988 * 4989 * @throws Exception 4990 * 4991 * @return string The DKIM signature value 4992 */ 4993 public function DKIM_Sign($signHeader) 4994 { 4995 if (!defined('PKCS7_TEXT')) { 4996 if ($this->exceptions) { 4997 throw new Exception($this->lang('extension_missing') . 'openssl'); 4998 } 4999 5000 return ''; 5001 } 5002 $privKeyStr = !empty($this->DKIM_private_string) ? 5003 $this->DKIM_private_string : 5004 file_get_contents($this->DKIM_private); 5005 if ('' !== $this->DKIM_passphrase) { 5006 $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase); 5007 } else { 5008 $privKey = openssl_pkey_get_private($privKeyStr); 5009 } 5010 if (openssl_sign($signHeader, $signature, $privKey, 'sha256WithRSAEncryption')) { 5011 if (\PHP_MAJOR_VERSION < 8) { 5012 openssl_pkey_free($privKey); 5013 } 5014 5015 return base64_encode($signature); 5016 } 5017 if (\PHP_MAJOR_VERSION < 8) { 5018 openssl_pkey_free($privKey); 5019 } 5020 5021 return ''; 5022 } 5023 5024 /** 5025 * Generate a DKIM canonicalization header. 5026 * Uses the 'relaxed' algorithm from RFC6376 section 3.4.2. 5027 * Canonicalized headers should *always* use CRLF, regardless of mailer setting. 5028 * 5029 * @see https://www.rfc-editor.org/rfc/rfc6376#section-3.4.2 5030 * 5031 * @param string $signHeader Header 5032 * 5033 * @return string 5034 */ 5035 public function DKIM_HeaderC($signHeader) 5036 { 5037 //Normalize breaks to CRLF (regardless of the mailer) 5038 $signHeader = static::normalizeBreaks($signHeader, self::CRLF); 5039 //Unfold header lines 5040 //Note PCRE \s is too broad a definition of whitespace; RFC5322 defines it as `[ \t]` 5041 //@see https://www.rfc-editor.org/rfc/rfc5322#section-2.2 5042 //That means this may break if you do something daft like put vertical tabs in your headers. 5043 $signHeader = preg_replace('/\r\n[ \t]+/', ' ', $signHeader); 5044 //Break headers out into an array 5045 $lines = explode(self::CRLF, $signHeader); 5046 foreach ($lines as $key => $line) { 5047 //If the header is missing a :, skip it as it's invalid 5048 //This is likely to happen because the explode() above will also split 5049 //on the trailing LE, leaving an empty line 5050 if (strpos($line, ':') === false) { 5051 continue; 5052 } 5053 list($heading, $value) = explode(':', $line, 2); 5054 //Lower-case header name 5055 $heading = strtolower($heading); 5056 //Collapse white space within the value, also convert WSP to space 5057 $value = preg_replace('/[ \t]+/', ' ', $value); 5058 //RFC6376 is slightly unclear here - it says to delete space at the *end* of each value 5059 //But then says to delete space before and after the colon. 5060 //Net result is the same as trimming both ends of the value. 5061 //By elimination, the same applies to the field name 5062 $lines[$key] = trim($heading, " \t") . ':' . trim($value, " \t"); 5063 } 5064 5065 return implode(self::CRLF, $lines); 5066 } 5067 5068 /** 5069 * Generate a DKIM canonicalization body. 5070 * Uses the 'simple' algorithm from RFC6376 section 3.4.3. 5071 * Canonicalized bodies should *always* use CRLF, regardless of mailer setting. 5072 * 5073 * @see https://www.rfc-editor.org/rfc/rfc6376#section-3.4.3 5074 * 5075 * @param string $body Message Body 5076 * 5077 * @return string 5078 */ 5079 public function DKIM_BodyC($body) 5080 { 5081 if (empty($body)) { 5082 return self::CRLF; 5083 } 5084 //Normalize line endings to CRLF 5085 $body = static::normalizeBreaks($body, self::CRLF); 5086 5087 //Reduce multiple trailing line breaks to a single one 5088 return static::stripTrailingBreaks($body) . self::CRLF; 5089 } 5090 5091 /** 5092 * Create the DKIM header and body in a new message header. 5093 * 5094 * @param string $headers_line Header lines 5095 * @param string $subject Subject 5096 * @param string $body Body 5097 * 5098 * @throws Exception 5099 * 5100 * @return string 5101 */ 5102 public function DKIM_Add($headers_line, $subject, $body) 5103 { 5104 $DKIMsignatureType = 'rsa-sha256'; //Signature & hash algorithms 5105 $DKIMcanonicalization = 'relaxed/simple'; //Canonicalization methods of header & body 5106 $DKIMquery = 'dns/txt'; //Query method 5107 $DKIMtime = time(); 5108 //Always sign these headers without being asked 5109 //Recommended list from https://www.rfc-editor.org/rfc/rfc6376#section-5.4.1 5110 $autoSignHeaders = [ 5111 'from', 5112 'to', 5113 'cc', 5114 'date', 5115 'subject', 5116 'reply-to', 5117 'message-id', 5118 'content-type', 5119 'mime-version', 5120 'x-mailer', 5121 ]; 5122 if (stripos($headers_line, 'Subject') === false) { 5123 $headers_line .= 'Subject: ' . $subject . static::$LE; 5124 } 5125 $headerLines = explode(static::$LE, $headers_line); 5126 $currentHeaderLabel = ''; 5127 $currentHeaderValue = ''; 5128 $parsedHeaders = []; 5129 $headerLineIndex = 0; 5130 $headerLineCount = count($headerLines); 5131 foreach ($headerLines as $headerLine) { 5132 $matches = []; 5133 if (preg_match('/^([^ \t]*?)(?::[ \t]*)(.*)$/', $headerLine, $matches)) { 5134 if ($currentHeaderLabel !== '') { 5135 //We were previously in another header; This is the start of a new header, so save the previous one 5136 $parsedHeaders[] = ['label' => $currentHeaderLabel, 'value' => $currentHeaderValue]; 5137 } 5138 $currentHeaderLabel = $matches[1]; 5139 $currentHeaderValue = $matches[2]; 5140 } elseif (preg_match('/^[ \t]+(.*)$/', $headerLine, $matches)) { 5141 //This is a folded continuation of the current header, so unfold it 5142 $currentHeaderValue .= ' ' . $matches[1]; 5143 } 5144 ++$headerLineIndex; 5145 if ($headerLineIndex >= $headerLineCount) { 5146 //This was the last line, so finish off this header 5147 $parsedHeaders[] = ['label' => $currentHeaderLabel, 'value' => $currentHeaderValue]; 5148 } 5149 } 5150 $copiedHeaders = []; 5151 $headersToSignKeys = []; 5152 $headersToSign = []; 5153 foreach ($parsedHeaders as $header) { 5154 //Is this header one that must be included in the DKIM signature? 5155 if (in_array(strtolower($header['label']), $autoSignHeaders, true)) { 5156 $headersToSignKeys[] = $header['label']; 5157 $headersToSign[] = $header['label'] . ': ' . $header['value']; 5158 if ($this->DKIM_copyHeaderFields) { 5159 $copiedHeaders[] = $header['label'] . ':' . //Note no space after this, as per RFC 5160 str_replace('|', '=7C', $this->DKIM_QP($header['value'])); 5161 } 5162 continue; 5163 } 5164 //Is this an extra custom header we've been asked to sign? 5165 if (in_array($header['label'], $this->DKIM_extraHeaders, true)) { 5166 //Find its value in custom headers 5167 foreach ($this->CustomHeader as $customHeader) { 5168 if ($customHeader[0] === $header['label']) { 5169 $headersToSignKeys[] = $header['label']; 5170 $headersToSign[] = $header['label'] . ': ' . $header['value']; 5171 if ($this->DKIM_copyHeaderFields) { 5172 $copiedHeaders[] = $header['label'] . ':' . //Note no space after this, as per RFC 5173 str_replace('|', '=7C', $this->DKIM_QP($header['value'])); 5174 } 5175 //Skip straight to the next header 5176 continue 2; 5177 } 5178 } 5179 } 5180 } 5181 $copiedHeaderFields = ''; 5182 if ($this->DKIM_copyHeaderFields && count($copiedHeaders) > 0) { 5183 //Assemble a DKIM 'z' tag 5184 $copiedHeaderFields = ' z='; 5185 $first = true; 5186 foreach ($copiedHeaders as $copiedHeader) { 5187 if (!$first) { 5188 $copiedHeaderFields .= static::$LE . ' |'; 5189 } 5190 //Fold long values 5191 if (strlen($copiedHeader) > self::STD_LINE_LENGTH - 3) { 5192 $copiedHeaderFields .= substr( 5193 chunk_split($copiedHeader, self::STD_LINE_LENGTH - 3, static::$LE . self::FWS), 5194 0, 5195 -strlen(static::$LE . self::FWS) 5196 ); 5197 } else { 5198 $copiedHeaderFields .= $copiedHeader; 5199 } 5200 $first = false; 5201 } 5202 $copiedHeaderFields .= ';' . static::$LE; 5203 } 5204 $headerKeys = ' h=' . implode(':', $headersToSignKeys) . ';' . static::$LE; 5205 $headerValues = implode(static::$LE, $headersToSign); 5206 $body = $this->DKIM_BodyC($body); 5207 //Base64 of packed binary SHA-256 hash of body 5208 $DKIMb64 = base64_encode(pack('H*', hash('sha256', $body))); 5209 $ident = ''; 5210 if ('' !== $this->DKIM_identity) { 5211 $ident = ' i=' . $this->DKIM_identity . ';' . static::$LE; 5212 } 5213 //The DKIM-Signature header is included in the signature *except for* the value of the `b` tag 5214 //which is appended after calculating the signature 5215 //https://www.rfc-editor.org/rfc/rfc6376#section-3.5 5216 $dkimSignatureHeader = 'DKIM-Signature: v=1;' . 5217 ' d=' . $this->DKIM_domain . ';' . 5218 ' s=' . $this->DKIM_selector . ';' . static::$LE . 5219 ' a=' . $DKIMsignatureType . ';' . 5220 ' q=' . $DKIMquery . ';' . 5221 ' t=' . $DKIMtime . ';' . 5222 ' c=' . $DKIMcanonicalization . ';' . static::$LE . 5223 $headerKeys . 5224 $ident . 5225 $copiedHeaderFields . 5226 ' bh=' . $DKIMb64 . ';' . static::$LE . 5227 ' b='; 5228 //Canonicalize the set of headers 5229 $canonicalizedHeaders = $this->DKIM_HeaderC( 5230 $headerValues . static::$LE . $dkimSignatureHeader 5231 ); 5232 $signature = $this->DKIM_Sign($canonicalizedHeaders); 5233 $signature = trim(chunk_split($signature, self::STD_LINE_LENGTH - 3, static::$LE . self::FWS)); 5234 5235 return static::normalizeBreaks($dkimSignatureHeader . $signature); 5236 } 5237 5238 /** 5239 * Detect if a string contains a line longer than the maximum line length 5240 * allowed by RFC 2822 section 2.1.1. 5241 * 5242 * @param string $str 5243 * 5244 * @return bool 5245 */ 5246 public static function hasLineLongerThanMax($str) 5247 { 5248 return (bool) preg_match('/^(.{' . (self::MAX_LINE_LENGTH + strlen(static::$LE)) . ',})/m', $str); 5249 } 5250 5251 /** 5252 * If a string contains any "special" characters, double-quote the name, 5253 * and escape any double quotes with a backslash. 5254 * 5255 * @param string $str 5256 * 5257 * @return string 5258 * 5259 * @see RFC822 3.4.1 5260 */ 5261 public static function quotedString($str) 5262 { 5263 if (preg_match('/[ ()<>@,;:"\/\[\]?=]/', $str)) { 5264 //If the string contains any of these chars, it must be double-quoted 5265 //and any double quotes must be escaped with a backslash 5266 return '"' . str_replace('"', '\\"', $str) . '"'; 5267 } 5268 5269 //Return the string untouched, it doesn't need quoting 5270 return $str; 5271 } 5272 5273 /** 5274 * Allows for public read access to 'to' property. 5275 * Before the send() call, queued addresses (i.e. with IDN) are not yet included. 5276 * 5277 * @return array 5278 */ 5279 public function getToAddresses() 5280 { 5281 return $this->to; 5282 } 5283 5284 /** 5285 * Allows for public read access to 'cc' property. 5286 * Before the send() call, queued addresses (i.e. with IDN) are not yet included. 5287 * 5288 * @return array 5289 */ 5290 public function getCcAddresses() 5291 { 5292 return $this->cc; 5293 } 5294 5295 /** 5296 * Allows for public read access to 'bcc' property. 5297 * Before the send() call, queued addresses (i.e. with IDN) are not yet included. 5298 * 5299 * @return array 5300 */ 5301 public function getBccAddresses() 5302 { 5303 return $this->bcc; 5304 } 5305 5306 /** 5307 * Allows for public read access to 'ReplyTo' property. 5308 * Before the send() call, queued addresses (i.e. with IDN) are not yet included. 5309 * 5310 * @return array 5311 */ 5312 public function getReplyToAddresses() 5313 { 5314 return $this->ReplyTo; 5315 } 5316 5317 /** 5318 * Allows for public read access to 'all_recipients' property. 5319 * Before the send() call, queued addresses (i.e. with IDN) are not yet included. 5320 * 5321 * @return array 5322 */ 5323 public function getAllRecipientAddresses() 5324 { 5325 return $this->all_recipients; 5326 } 5327 5328 /** 5329 * Perform a callback. 5330 * 5331 * @param bool $isSent 5332 * @param array $to 5333 * @param array $cc 5334 * @param array $bcc 5335 * @param string $subject 5336 * @param string $body 5337 * @param string $from 5338 * @param array $extra 5339 */ 5340 protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from, $extra) 5341 { 5342 if (!empty($this->action_function) && is_callable($this->action_function)) { 5343 call_user_func($this->action_function, $isSent, $to, $cc, $bcc, $subject, $body, $from, $extra); 5344 } 5345 } 5346 5347 /** 5348 * Get the OAuthTokenProvider instance. 5349 * 5350 * @return OAuthTokenProvider 5351 */ 5352 public function getOAuth() 5353 { 5354 return $this->oauth; 5355 } 5356 5357 /** 5358 * Set an OAuthTokenProvider instance. 5359 */ 5360 public function setOAuth(OAuthTokenProvider $oauth) 5361 { 5362 $this->oauth = $oauth; 5363 } 5364 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Tue Aug 19 08:20:01 2025 | Cross-referenced by PHPXref |