Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [Versions 402 and 403]

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