PageRenderTime 47ms CodeModel.GetById 4ms app.highlight 31ms RepoModel.GetById 0ms app.codeStats 1ms

/applicationv2/libraries/v2/phpmailer/class.phpmailer.php

https://bitbucket.org/cschuette/grand-prix-tickets-gmbh-staging
PHP | 3391 lines | 2092 code | 216 blank | 1083 comment | 316 complexity | 65776ff39c853ac7bc0e80eb02cc0abf MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/**
   3 * PHPMailer - PHP email creation and transport class.
   4 * PHP Version 5.0.0
   5 * Version 5.2.7
   6 * @package PHPMailer
   7 * @link https://github.com/PHPMailer/PHPMailer/
   8 * @author Marcus Bointon (coolbru) <phpmailer@synchromedia.co.uk>
   9 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  10 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  11 * @author Brent R. Matzelle (original founder)
  12 * @copyright 2013 Marcus Bointon
  13 * @copyright 2010 - 2012 Jim Jagielski
  14 * @copyright 2004 - 2009 Andy Prevost
  15 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  16 * @note This program is distributed in the hope that it will be useful - WITHOUT
  17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  18 * FITNESS FOR A PARTICULAR PURPOSE.
  19 */
  20
  21if (version_compare(PHP_VERSION, '5.0.0', '<')) {
  22    exit("Sorry, PHPMailer will only run on PHP version 5 or greater!\n");
  23}
  24
  25/**
  26 * PHPMailer - PHP email creation and transport class.
  27 * PHP Version 5.0.0
  28 * @package PHPMailer
  29 * @author Marcus Bointon (coolbru) <phpmailer@synchromedia.co.uk>
  30 * @author Jim Jagielski (jimjag) <jimjag@gmail.com>
  31 * @author Andy Prevost (codeworxtech) <codeworxtech@users.sourceforge.net>
  32 * @author Brent R. Matzelle (original founder)
  33 * @copyright 2013 Marcus Bointon
  34 * @copyright 2010 - 2012 Jim Jagielski
  35 * @copyright 2004 - 2009 Andy Prevost
  36 */
  37class PHPMailer
  38{
  39    /**
  40     * The PHPMailer Version number.
  41     * @type string
  42     */
  43    public $Version = '5.2.7';
  44
  45    /**
  46     * Email priority.
  47     * Options: 1 = High, 3 = Normal, 5 = low.
  48     * @type int
  49     */
  50    public $Priority = 3;
  51
  52    /**
  53     * The character set of the message.
  54     * @type string
  55     */
  56    public $CharSet = 'utf-8';
  57
  58    /**
  59     * The MIME Content-type of the message.
  60     * @type string
  61     */
  62    public $ContentType = 'text/plain';
  63
  64    /**
  65     * The message encoding.
  66     * Options: "8bit", "7bit", "binary", "base64", and "quoted-printable".
  67     * @type string
  68     */
  69    public $Encoding = '8bit';
  70
  71    /**
  72     * Holds the most recent mailer error message.
  73     * @type string
  74     */
  75    public $ErrorInfo = '';
  76
  77    /**
  78     * The From email address for the message.
  79     * @type string
  80     */
  81    public $From = 'root@localhost';
  82
  83    /**
  84     * The From name of the message.
  85     * @type string
  86     */
  87    public $FromName = 'Root User';
  88
  89    /**
  90     * The Sender email (Return-Path) of the message.
  91     * If not empty, will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  92     * @type string
  93     */
  94    public $Sender = '';
  95
  96    /**
  97     * The Return-Path of the message.
  98     * If empty, it will be set to either From or Sender.
  99     * @type string
 100     */
 101    public $ReturnPath = '';
 102
 103    /**
 104     * The Subject of the message.
 105     * @type string
 106     */
 107    public $Subject = '';
 108
 109    /**
 110     * An HTML or plain text message body.
 111     * If HTML then call isHTML(true).
 112     * @type string
 113     */
 114    public $Body = '';
 115
 116    /**
 117     * The plain-text message body.
 118     * This body can be read by mail clients that do not have HTML email
 119     * capability such as mutt & Eudora.
 120     * Clients that can read HTML will view the normal Body.
 121     * @type string
 122     */
 123    public $AltBody = '';
 124
 125    /**
 126     * An iCal message part body.
 127     * Only supported in simple alt or alt_inline message types
 128     * To generate iCal events, use the bundled extras/EasyPeasyICS.php class or iCalcreator
 129     * @link http://sprain.ch/blog/downloads/php-class-easypeasyics-create-ical-files-with-php/
 130     * @link http://kigkonsult.se/iCalcreator/
 131     * @type string
 132     */
 133    public $Ical = '';
 134
 135    /**
 136     * The complete compiled MIME message body.
 137     * @access protected
 138     * @type string
 139     */
 140    protected $MIMEBody = '';
 141
 142    /**
 143     * The complete compiled MIME message headers.
 144     * @type string
 145     * @access protected
 146     */
 147    protected $MIMEHeader = '';
 148
 149    /**
 150     * Extra headers that createHeader() doesn't fold in.
 151     * @type string
 152     * @access protected
 153     */
 154    protected $mailHeader = '';
 155
 156    /**
 157     * Word-wrap the message body to this number of chars.
 158     * @type int
 159     */
 160    public $WordWrap = 0;
 161
 162    /**
 163     * Which method to use to send mail.
 164     * Options: "mail", "sendmail", or "smtp".
 165     * @type string
 166     */
 167    public $Mailer = 'mail';
 168
 169    /**
 170     * The path to the sendmail program.
 171     * @type string
 172     */
 173    public $Sendmail = '/usr/sbin/sendmail';
 174
 175    /**
 176     * Whether mail() uses a fully sendmail-compatible MTA.
 177     * One which supports sendmail's "-oi -f" options.
 178     * @type bool
 179     */
 180    public $UseSendmailOptions = true;
 181
 182    /**
 183     * Path to PHPMailer plugins.
 184     * Useful if the SMTP class is not in the PHP include path.
 185     * @type string
 186     * @deprecated Should not be needed now there is an autoloader.
 187     */
 188    public $PluginDir = '';
 189
 190    /**
 191     * The email address that a reading confirmation should be sent to.
 192     * @type string
 193     */
 194    public $ConfirmReadingTo = '';
 195
 196    /**
 197     * The hostname to use in Message-Id and Received headers
 198     * and as default HELO string.
 199     * If empty, the value returned
 200     * by SERVER_NAME is used or 'localhost.localdomain'.
 201     * @type string
 202     */
 203    public $Hostname = '';
 204
 205    /**
 206     * An ID to be used in the Message-Id header.
 207     * If empty, a unique id will be generated.
 208     * @type string
 209     */
 210    public $MessageID = '';
 211
 212    /**
 213     * The message Date to be used in the Date header.
 214     * If empty, the current date will be added.
 215     * @type string
 216     */
 217    public $MessageDate = '';
 218
 219    /**
 220     * SMTP hosts.
 221     * Either a single hostname or multiple semicolon-delimited hostnames.
 222     * You can also specify a different port
 223     * for each host by using this format: [hostname:port]
 224     * (e.g. "smtp1.example.com:25;smtp2.example.com").
 225     * Hosts will be tried in order.
 226     * @type string
 227     */
 228    public $Host = 'localhost';
 229
 230    /**
 231     * The default SMTP server port.
 232     * @type int
 233     * @Todo Why is this needed when the SMTP class takes care of it?
 234     */
 235    public $Port = 25;
 236
 237    /**
 238     * The SMTP HELO of the message.
 239     * Default is $Hostname.
 240     * @type string
 241     * @see PHPMailer::$Hostname
 242     */
 243    public $Helo = '';
 244
 245    /**
 246     * The secure connection prefix.
 247     * Options: "", "ssl" or "tls"
 248     * @type string
 249     */
 250    public $SMTPSecure = '';
 251
 252    /**
 253     * Whether to use SMTP authentication.
 254     * Uses the Username and Password properties.
 255     * @type bool
 256     * @see PHPMailer::$Username
 257     * @see PHPMailer::$Password
 258     */
 259    public $SMTPAuth = false;
 260
 261    /**
 262     * SMTP username.
 263     * @type string
 264     */
 265    public $Username = '';
 266
 267    /**
 268     * SMTP password.
 269     * @type string
 270     */
 271    public $Password = '';
 272
 273    /**
 274     * SMTP auth type.
 275     * Options are LOGIN (default), PLAIN, NTLM, CRAM-MD5
 276     * @type string
 277     */
 278    public $AuthType = '';
 279
 280    /**
 281     * SMTP realm.
 282     * Used for NTLM auth
 283     * @type string
 284     */
 285    public $Realm = '';
 286
 287    /**
 288     * SMTP workstation.
 289     * Used for NTLM auth
 290     * @type string
 291     */
 292    public $Workstation = '';
 293
 294    /**
 295     * The SMTP server timeout in seconds.
 296     * @type int
 297     */
 298    public $Timeout = 10;
 299
 300    /**
 301     * SMTP class debug output mode.
 302     * Options: 0 = off, 1 = commands, 2 = commands and data
 303     * @type int
 304     * @see SMTP::$do_debug
 305     */
 306    public $SMTPDebug = 0;
 307
 308    /**
 309     * The function/method to use for debugging output.
 310     * Options: "echo" or "error_log"
 311     * @type string
 312     * @see SMTP::$Debugoutput
 313     */
 314    public $Debugoutput = "echo";
 315
 316    /**
 317     * Whether to keep SMTP connection open after each message.
 318     * If this is set to true then to close the connection
 319     * requires an explicit call to smtpClose().
 320     * @type bool
 321     */
 322    public $SMTPKeepAlive = false;
 323
 324    /**
 325     * Whether to split multiple to addresses into multiple messages
 326     * or send them all in one message.
 327     * @type bool
 328     */
 329    public $SingleTo = false;
 330
 331    /**
 332     * Storage for addresses when SingleTo is enabled.
 333     * @type array
 334     * @todo This should really not be public
 335     */
 336    public $SingleToArray = array();
 337
 338    /**
 339     * Whether to generate VERP addresses on send.
 340     * Only applicable when sending via SMTP.
 341     * @link http://en.wikipedia.org/wiki/Variable_envelope_return_path
 342     * @type bool
 343     */
 344    public $do_verp = false;
 345
 346    /**
 347     * Whether to allow sending messages with an empty body.
 348     * @type bool
 349     */
 350    public $AllowEmpty = false;
 351
 352    /**
 353     * The default line ending.
 354     * @note The default remains "\n". We force CRLF where we know
 355     *        it must be used via self::CRLF.
 356     * @type string
 357     */
 358    public $LE = "\n";
 359
 360    /**
 361     * DKIM selector.
 362     * @type string
 363     */
 364    public $DKIM_selector = '';
 365
 366    /**
 367     * DKIM Identity.
 368     * Usually the email address used as the source of the email
 369     * @type string
 370     */
 371    public $DKIM_identity = '';
 372
 373    /**
 374     * DKIM passphrase.
 375     * Used if your key is encrypted.
 376     * @type string
 377     */
 378    public $DKIM_passphrase = '';
 379
 380    /**
 381     * DKIM signing domain name.
 382     * @example 'example.com'
 383     * @type string
 384     */
 385    public $DKIM_domain = '';
 386
 387    /**
 388     * DKIM private key file path.
 389     * @type string
 390     */
 391    public $DKIM_private = '';
 392
 393    /**
 394     * Callback Action function name.
 395     *
 396     * The function that handles the result of the send email action.
 397     * It is called out by send() for each email sent.
 398     *
 399     * Value can be any php callable: http://www.php.net/is_callable
 400     *
 401     * Parameters:
 402     *   bool    $result        result of the send action
 403     *   string  $to            email address of the recipient
 404     *   string  $cc            cc email addresses
 405     *   string  $bcc           bcc email addresses
 406     *   string  $subject       the subject
 407     *   string  $body          the email body
 408     *   string  $from          email address of sender
 409     * @type string
 410     */
 411    public $action_function = '';
 412
 413    /**
 414     * What to use in the X-Mailer header.
 415     * Options: null for default, whitespace for none, or a string to use
 416     * @type string
 417     */
 418    public $XMailer = '';
 419
 420    /**
 421     * An instance of the SMTP sender class.
 422     * @type SMTP
 423     * @access protected
 424     */
 425    protected $smtp = null;
 426
 427    /**
 428     * The array of 'to' addresses.
 429     * @type array
 430     * @access protected
 431     */
 432    protected $to = array();
 433
 434    /**
 435     * The array of 'cc' addresses.
 436     * @type array
 437     * @access protected
 438     */
 439    protected $cc = array();
 440
 441    /**
 442     * The array of 'bcc' addresses.
 443     * @type array
 444     * @access protected
 445     */
 446    protected $bcc = array();
 447
 448    /**
 449     * The array of reply-to names and addresses.
 450     * @type array
 451     * @access protected
 452     */
 453    protected $ReplyTo = array();
 454
 455    /**
 456     * An array of all kinds of addresses.
 457     * Includes all of $to, $cc, $bcc, $replyto
 458     * @type array
 459     * @access protected
 460     */
 461    protected $all_recipients = array();
 462
 463    /**
 464     * The array of attachments.
 465     * @type array
 466     * @access protected
 467     */
 468    protected $attachment = array();
 469
 470    /**
 471     * The array of custom headers.
 472     * @type array
 473     * @access protected
 474     */
 475    protected $CustomHeader = array();
 476
 477    /**
 478     * The most recent Message-ID (including angular brackets).
 479     * @type string
 480     * @access protected
 481     */
 482    protected $lastMessageID = '';
 483
 484    /**
 485     * The message's MIME type.
 486     * @type string
 487     * @access protected
 488     */
 489    protected $message_type = '';
 490
 491    /**
 492     * The array of MIME boundary strings.
 493     * @type array
 494     * @access protected
 495     */
 496    protected $boundary = array();
 497
 498    /**
 499     * The array of available languages.
 500     * @type array
 501     * @access protected
 502     */
 503    protected $language = array();
 504
 505    /**
 506     * The number of errors encountered.
 507     * @type integer
 508     * @access protected
 509     */
 510    protected $error_count = 0;
 511
 512    /**
 513     * The S/MIME certificate file path.
 514     * @type string
 515     * @access protected
 516     */
 517    protected $sign_cert_file = '';
 518
 519    /**
 520     * The S/MIME key file path.
 521     * @type string
 522     * @access protected
 523     */
 524    protected $sign_key_file = '';
 525
 526    /**
 527     * The S/MIME password for the key.
 528     * Used only if the key is encrypted.
 529     * @type string
 530     * @access protected
 531     */
 532    protected $sign_key_pass = '';
 533
 534    /**
 535     * Whether to throw exceptions for errors.
 536     * @type bool
 537     * @access protected
 538     */
 539    protected $exceptions = false;
 540
 541    /**
 542     * Error severity: message only, continue processing
 543     */
 544    const STOP_MESSAGE = 0;
 545
 546    /**
 547     * Error severity: message, likely ok to continue processing
 548     */
 549    const STOP_CONTINUE = 1;
 550
 551    /**
 552     * Error severity: message, plus full stop, critical error reached
 553     */
 554    const STOP_CRITICAL = 2;
 555
 556    /**
 557     * SMTP RFC standard line ending
 558     */
 559    const CRLF = "\r\n";
 560
 561    /**
 562     * Constructor
 563     * @param bool $exceptions Should we throw external exceptions?
 564     */
 565    public function __construct($exceptions = false)
 566    {
 567        $this->exceptions = ($exceptions == true);
 568        //Make sure our autoloader is loaded
 569        if (version_compare(PHP_VERSION, '5.1.2', '>=')) {
 570            $al = spl_autoload_functions();
 571            if ($al === false or !in_array('PHPMailerAutoload', $al)) {
 572                require 'PHPMailerAutoload.php';
 573            }
 574        }
 575    }
 576
 577    /**
 578     * Destructor.
 579     */
 580    public function __destruct()
 581    {
 582        if ($this->Mailer == 'smtp') { //close any open SMTP connection nicely
 583            $this->smtpClose();
 584        }
 585    }
 586
 587    /**
 588     * Call mail() in a safe_mode-aware fashion.
 589     * Also, unless sendmail_path points to sendmail (or something that
 590     * claims to be sendmail), don't pass params (not a perfect fix,
 591     * but it will do)
 592     * @param string $to To
 593     * @param string $subject Subject
 594     * @param string $body Message Body
 595     * @param string $header Additional Header(s)
 596     * @param string $params Params
 597     * @access private
 598     * @return bool
 599     */
 600    private function mailPassthru($to, $subject, $body, $header, $params)
 601    {
 602        //Check overloading of mail function to avoid double-encoding
 603        if (ini_get('mbstring.func_overload') & 1) {
 604            $subject = $this->secureHeader($subject);
 605        } else {
 606            $subject = $this->encodeHeader($this->secureHeader($subject));
 607        }
 608        if (ini_get('safe_mode') || !($this->UseSendmailOptions)) {
 609            $rt = @mail($to, $subject, $body, $header);
 610        } else {
 611            $rt = @mail($to, $subject, $body, $header, $params);
 612        }
 613        return $rt;
 614    }
 615
 616    /**
 617     * Output debugging info via user-defined method.
 618     * Only if debug output is enabled.
 619     * @see PHPMailer::$Debugoutput
 620     * @see PHPMailer::$SMTPDebug
 621     * @param string $str
 622     */
 623    protected function edebug($str)
 624    {
 625        if (!$this->SMTPDebug) {
 626            return;
 627        }
 628        switch ($this->Debugoutput) {
 629            case 'error_log':
 630                error_log($str);
 631                break;
 632            case 'html':
 633                //Cleans up output a bit for a better looking display that's HTML-safe
 634                echo htmlentities(preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, $this->CharSet) . "<br>\n";
 635                break;
 636            case 'echo':
 637            default:
 638                echo $str."\n";
 639        }
 640    }
 641
 642    /**
 643     * Sets message type to HTML or plain.
 644     * @param bool $ishtml True for HTML mode.
 645     * @return void
 646     */
 647    public function isHTML($ishtml = true)
 648    {
 649        if ($ishtml) {
 650            $this->ContentType = 'text/html';
 651        } else {
 652            $this->ContentType = 'text/plain';
 653        }
 654    }
 655
 656    /**
 657     * Send messages using SMTP.
 658     * @return void
 659     */
 660    public function isSMTP()
 661    {
 662        $this->Mailer = 'smtp';
 663    }
 664
 665    /**
 666     * Send messages using PHP's mail() function.
 667     * @return void
 668     */
 669    public function isMail()
 670    {
 671        $this->Mailer = 'mail';
 672    }
 673
 674    /**
 675     * Send messages using $Sendmail.
 676     * @return void
 677     */
 678    public function isSendmail()
 679    {
 680        if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
 681            $this->Sendmail = '/usr/sbin/sendmail';
 682        }
 683        $this->Mailer = 'sendmail';
 684    }
 685
 686    /**
 687     * Send messages using qmail.
 688     * @return void
 689     */
 690    public function isQmail()
 691    {
 692        if (!stristr(ini_get('sendmail_path'), 'qmail')) {
 693            $this->Sendmail = '/var/qmail/bin/qmail-inject';
 694        }
 695        $this->Mailer = 'qmail';
 696    }
 697
 698    /**
 699     * Add a "To" address.
 700     * @param string $address
 701     * @param string $name
 702     * @return bool true on success, false if address already used
 703     */
 704    public function addAddress($address, $name = '')
 705    {
 706        return $this->addAnAddress('to', $address, $name);
 707    }
 708
 709    /**
 710     * Add a "CC" address.
 711     * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
 712     * @param string $address
 713     * @param string $name
 714     * @return bool true on success, false if address already used
 715     */
 716    public function addCC($address, $name = '')
 717    {
 718        return $this->addAnAddress('cc', $address, $name);
 719    }
 720
 721    /**
 722     * Add a "BCC" address.
 723     * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
 724     * @param string $address
 725     * @param string $name
 726     * @return bool true on success, false if address already used
 727     */
 728    public function addBCC($address, $name = '')
 729    {
 730        return $this->addAnAddress('bcc', $address, $name);
 731    }
 732
 733    /**
 734     * Add a "Reply-to" address.
 735     * @param string $address
 736     * @param string $name
 737     * @return bool
 738     */
 739    public function addReplyTo($address, $name = '')
 740    {
 741        return $this->addAnAddress('Reply-To', $address, $name);
 742    }
 743
 744    /**
 745     * Add an address to one of the recipient arrays.
 746     * Addresses that have been added already return false, but do not throw exceptions
 747     * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
 748     * @param string $address The email address to send to
 749     * @param string $name
 750     * @throws phpmailerException
 751     * @return bool true on success, false if address already used or invalid in some way
 752     * @access protected
 753     */
 754    protected function addAnAddress($kind, $address, $name = '')
 755    {
 756        if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
 757            $this->setError($this->lang('Invalid recipient array') . ': ' . $kind);
 758            $this->edebug($this->lang('Invalid recipient array') . ': ' . $kind);
 759            if ($this->exceptions) {
 760                throw new phpmailerException('Invalid recipient array: ' . $kind);
 761            }
 762            return false;
 763        }
 764        $address = trim($address);
 765        $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 766        if (!$this->validateAddress($address)) {
 767            $this->setError($this->lang('invalid_address') . ': ' . $address);
 768            $this->edebug($this->lang('invalid_address') . ': ' . $address);
 769            if ($this->exceptions) {
 770                throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
 771            }
 772            return false;
 773        }
 774        if ($kind != 'Reply-To') {
 775            if (!isset($this->all_recipients[strtolower($address)])) {
 776                array_push($this->$kind, array($address, $name));
 777                $this->all_recipients[strtolower($address)] = true;
 778                return true;
 779            }
 780        } else {
 781            if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
 782                $this->ReplyTo[strtolower($address)] = array($address, $name);
 783                return true;
 784            }
 785        }
 786        return false;
 787    }
 788
 789    /**
 790     * Set the From and FromName properties.
 791     * @param string $address
 792     * @param string $name
 793     * @param bool $auto Whether to also set the Sender address, defaults to true
 794     * @throws phpmailerException
 795     * @return bool
 796     */
 797    public function setFrom($address, $name = '', $auto = true)
 798    {
 799        $address = trim($address);
 800        $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 801        if (!$this->validateAddress($address)) {
 802            $this->setError($this->lang('invalid_address') . ': ' . $address);
 803            $this->edebug($this->lang('invalid_address') . ': ' . $address);
 804            if ($this->exceptions) {
 805                throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
 806            }
 807            return false;
 808        }
 809        $this->From = $address;
 810        $this->FromName = $name;
 811        if ($auto) {
 812            if (empty($this->Sender)) {
 813                $this->Sender = $address;
 814            }
 815        }
 816        return true;
 817    }
 818
 819    /**
 820     * Return the Message-ID header of the last email.
 821     * Technically this is the value from the last time the headers were created,
 822     * but it's also the message ID of the last sent message except in
 823     * pathological cases.
 824     * @return string
 825     */
 826    public function getLastMessageID()
 827    {
 828        return $this->lastMessageID;
 829    }
 830
 831    /**
 832     * Check that a string looks like an email address.
 833     * @param string $address The email address to check
 834     * @param string $patternselect A selector for the validation pattern to use :
 835     *   'auto' - pick best one automatically;
 836     *   'pcre8' - use the squiloople.com pattern, requires PCRE > 8.0, PHP >= 5.3.2, 5.2.14;
 837     *   'pcre' - use old PCRE implementation;
 838     *   'php' - use PHP built-in FILTER_VALIDATE_EMAIL; faster, less thorough;
 839     *   'noregex' - super fast, really dumb.
 840     * @return bool
 841     * @static
 842     * @access public
 843     */
 844    public static function validateAddress($address, $patternselect = 'auto')
 845    {
 846        if ($patternselect == 'auto') {
 847            if (defined(
 848                'PCRE_VERSION'
 849            )
 850            ) { //Check this instead of extension_loaded so it works when that function is disabled
 851                if (version_compare(PCRE_VERSION, '8.0') >= 0) {
 852                    $patternselect = 'pcre8';
 853                } else {
 854                    $patternselect = 'pcre';
 855                }
 856            } else {
 857                //Filter_var appeared in PHP 5.2.0 and does not require the PCRE extension
 858                if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
 859                    $patternselect = 'php';
 860                } else {
 861                    $patternselect = 'noregex';
 862                }
 863            }
 864        }
 865        switch ($patternselect) {
 866            case 'pcre8':
 867                /**
 868                 * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
 869                 * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
 870                 * not allow a@b type valid addresses :(
 871                 * @link http://squiloople.com/2009/12/20/email-address-validation/
 872                 * @copyright 2009-2010 Michael Rushton
 873                 * Feel free to use and redistribute this code. But please keep this copyright notice.
 874                 */
 875                return (bool)preg_match(
 876                    '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
 877                    '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
 878                    '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
 879                    '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
 880                    '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
 881                    '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
 882                    '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
 883                    '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
 884                    '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
 885                    $address
 886                );
 887                break;
 888            case 'pcre':
 889                //An older regex that doesn't need a recent PCRE
 890                return (bool)preg_match(
 891                    '/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>' .
 892                    '[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")' .
 893                    '(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*' .
 894                    '@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})' .
 895                    '(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:' .
 896                    '[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?' .
 897                    '::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:' .
 898                    '[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?' .
 899                    '::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
 900                    '|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD',
 901                    $address
 902                );
 903                break;
 904            case 'php':
 905            default:
 906                return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
 907                break;
 908            case 'noregex':
 909                //No PCRE! Do something _very_ approximate!
 910                //Check the address is 3 chars or longer and contains an @ that's not the first or last char
 911                return (strlen($address) >= 3
 912                    and strpos($address, '@') >= 1
 913                    and strpos($address, '@') != strlen($address) - 1);
 914                break;
 915        }
 916    }
 917
 918    /**
 919     * Create a message and send it.
 920     * Uses the sending method specified by $Mailer.
 921     * @throws phpmailerException
 922     * @return bool false on error - See the ErrorInfo property for details of the error.
 923     */
 924    public function send()
 925    {
 926        try {
 927            if (!$this->preSend()) {
 928                return false;
 929            }
 930            return $this->postSend();
 931        } catch (phpmailerException $e) {
 932            $this->mailHeader = '';
 933            $this->setError($e->getMessage());
 934            if ($this->exceptions) {
 935                throw $e;
 936            }
 937            return false;
 938        }
 939    }
 940
 941    /**
 942     * Prepare a message for sending.
 943     * @throws phpmailerException
 944     * @return bool
 945     */
 946    public function preSend()
 947    {
 948        try {
 949            $this->mailHeader = "";
 950            if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
 951                throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL);
 952            }
 953
 954            // Set whether the message is multipart/alternative
 955            if (!empty($this->AltBody)) {
 956                $this->ContentType = 'multipart/alternative';
 957            }
 958
 959            $this->error_count = 0; // reset errors
 960            $this->setMessageType();
 961            // Refuse to send an empty message unless we are specifically allowing it
 962            if (!$this->AllowEmpty and empty($this->Body)) {
 963                throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL);
 964            }
 965
 966            $this->MIMEHeader = $this->createHeader();
 967            $this->MIMEBody = $this->createBody();
 968
 969            // To capture the complete message when using mail(), create
 970            // an extra header list which createHeader() doesn't fold in
 971            if ($this->Mailer == 'mail') {
 972                if (count($this->to) > 0) {
 973                    $this->mailHeader .= $this->addrAppend("To", $this->to);
 974                } else {
 975                    $this->mailHeader .= $this->headerLine("To", "undisclosed-recipients:;");
 976                }
 977                $this->mailHeader .= $this->headerLine(
 978                    'Subject',
 979                    $this->encodeHeader($this->secureHeader(trim($this->Subject)))
 980                );
 981            }
 982
 983            // Sign with DKIM if enabled
 984            if (!empty($this->DKIM_domain)
 985                && !empty($this->DKIM_private)
 986                && !empty($this->DKIM_selector)
 987                && !empty($this->DKIM_domain)
 988                && file_exists($this->DKIM_private)) {
 989                $header_dkim = $this->DKIM_Add(
 990                    $this->MIMEHeader . $this->mailHeader,
 991                    $this->encodeHeader($this->secureHeader($this->Subject)),
 992                    $this->MIMEBody
 993                );
 994                $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . self::CRLF .
 995                    str_replace("\r\n", "\n", $header_dkim) . self::CRLF;
 996            }
 997            return true;
 998
 999        } catch (phpmailerException $e) {
1000            $this->setError($e->getMessage());
1001            if ($this->exceptions) {
1002                throw $e;
1003            }
1004            return false;
1005        }
1006    }
1007
1008    /**
1009     * Actually send a message.
1010     * Send the email via the selected mechanism
1011     * @throws phpmailerException
1012     * @return bool
1013     */
1014    public function postSend()
1015    {
1016        try {
1017            // Choose the mailer and send through it
1018            switch ($this->Mailer) {
1019                case 'sendmail':
1020                case 'qmail':
1021                    return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody);
1022                case 'smtp':
1023                    return $this->smtpSend($this->MIMEHeader, $this->MIMEBody);
1024                case 'mail':
1025                    return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
1026                default:
1027                    if (method_exists($this, $this->Mailer.'Send')) {
1028                        $sendMethod = $this->Mailer.'Send';
1029                        return $this->$sendMethod($this->MIMEHeader, $this->MIMEBody);
1030                    } else {
1031                        return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
1032                    }
1033            }
1034        } catch (phpmailerException $e) {
1035            $this->setError($e->getMessage());
1036            $this->edebug($e->getMessage());
1037            if ($this->exceptions) {
1038                throw $e;
1039            }
1040        }
1041        return false;
1042    }
1043
1044    /**
1045     * Send mail using the $Sendmail program.
1046     * @param string $header The message headers
1047     * @param string $body The message body
1048     * @see PHPMailer::$Sendmail
1049     * @throws phpmailerException
1050     * @access protected
1051     * @return bool
1052     */
1053    protected function sendmailSend($header, $body)
1054    {
1055        if ($this->Sender != '') {
1056            if ($this->Mailer == 'qmail') {
1057                $sendmail = sprintf("%s -f%s", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
1058            } else {
1059                $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
1060            }
1061        } else {
1062            if ($this->Mailer == 'qmail') {
1063                $sendmail = sprintf("%s", escapeshellcmd($this->Sendmail));
1064            } else {
1065                $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
1066            }
1067        }
1068        if ($this->SingleTo === true) {
1069            foreach ($this->SingleToArray as $val) {
1070                if (!@$mail = popen($sendmail, 'w')) {
1071                    throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1072                }
1073                fputs($mail, "To: " . $val . "\n");
1074                fputs($mail, $header);
1075                fputs($mail, $body);
1076                $result = pclose($mail);
1077                // implement call back function if it exists
1078                $isSent = ($result == 0) ? 1 : 0;
1079                $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1080                if ($result != 0) {
1081                    throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1082                }
1083            }
1084        } else {
1085            if (!@$mail = popen($sendmail, 'w')) {
1086                throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1087            }
1088            fputs($mail, $header);
1089            fputs($mail, $body);
1090            $result = pclose($mail);
1091            // implement call back function if it exists
1092            $isSent = ($result == 0) ? 1 : 0;
1093            $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1094            if ($result != 0) {
1095                throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1096            }
1097        }
1098        return true;
1099    }
1100
1101    /**
1102     * Send mail using the PHP mail() function.
1103     * @param string $header The message headers
1104     * @param string $body The message body
1105     * @link http://www.php.net/manual/en/book.mail.php
1106     * @throws phpmailerException
1107     * @access protected
1108     * @return bool
1109     */
1110    protected function mailSend($header, $body)
1111    {
1112        $toArr = array();
1113        foreach ($this->to as $t) {
1114            $toArr[] = $this->addrFormat($t);
1115        }
1116        $to = implode(', ', $toArr);
1117
1118        if (empty($this->Sender)) {
1119            $params = " ";
1120        } else {
1121            $params = sprintf("-f%s", $this->Sender);
1122        }
1123        if ($this->Sender != '' and !ini_get('safe_mode')) {
1124            $old_from = ini_get('sendmail_from');
1125            ini_set('sendmail_from', $this->Sender);
1126        }
1127        $rt = false;
1128        if ($this->SingleTo === true && count($toArr) > 1) {
1129            foreach ($toArr as $val) {
1130                $rt = $this->mailPassthru($val, $this->Subject, $body, $header, $params);
1131                // implement call back function if it exists
1132                $isSent = ($rt == 1) ? 1 : 0;
1133                $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1134            }
1135        } else {
1136            $rt = $this->mailPassthru($to, $this->Subject, $body, $header, $params);
1137            // implement call back function if it exists
1138            $isSent = ($rt == 1) ? 1 : 0;
1139            $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1140        }
1141        if (isset($old_from)) {
1142            ini_set('sendmail_from', $old_from);
1143        }
1144        if (!$rt) {
1145            throw new phpmailerException($this->lang('instantiate'), self::STOP_CRITICAL);
1146        }
1147        return true;
1148    }
1149
1150    /**
1151     * Get an instance to use for SMTP operations.
1152     * Override this function to load your own SMTP implementation
1153     * @return SMTP
1154     */
1155    public function getSMTPInstance()
1156    {
1157        if (!is_object($this->smtp)) {
1158            $this->smtp = new SMTP;
1159        }
1160        return $this->smtp;
1161    }
1162
1163    /**
1164     * Send mail via SMTP.
1165     * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
1166     * Uses the PHPMailerSMTP class by default.
1167     * @see PHPMailer::getSMTPInstance() to use a different class.
1168     * @param string $header The message headers
1169     * @param string $body The message body
1170     * @throws phpmailerException
1171     * @uses SMTP
1172     * @access protected
1173     * @return bool
1174     */
1175    protected function smtpSend($header, $body)
1176    {
1177        $bad_rcpt = array();
1178
1179        if (!$this->smtpConnect()) {
1180            throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
1181        }
1182        $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
1183        if (!$this->smtp->mail($smtp_from)) {
1184            $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError()));
1185            throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
1186        }
1187
1188        // Attempt to send to all recipients
1189        foreach ($this->to as $to) {
1190            if (!$this->smtp->recipient($to[0])) {
1191                $bad_rcpt[] = $to[0];
1192                $isSent = 0;
1193            } else {
1194                $isSent = 1;
1195            }
1196            $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body, $this->From);
1197        }
1198        foreach ($this->cc as $cc) {
1199            if (!$this->smtp->recipient($cc[0])) {
1200                $bad_rcpt[] = $cc[0];
1201                $isSent = 0;
1202            } else {
1203                $isSent = 1;
1204            }
1205            $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body, $this->From);
1206        }
1207        foreach ($this->bcc as $bcc) {
1208            if (!$this->smtp->recipient($bcc[0])) {
1209                $bad_rcpt[] = $bcc[0];
1210                $isSent = 0;
1211            } else {
1212                $isSent = 1;
1213            }
1214            $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body, $this->From);
1215        }
1216
1217        //Only send the DATA command if we have viable recipients
1218        if ((count($this->all_recipients) > count($bad_rcpt)) and !$this->smtp->data($header . $body)) {
1219            throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL);
1220        }
1221        if ($this->SMTPKeepAlive == true) {
1222            $this->smtp->reset();
1223        } else {
1224            $this->smtp->quit();
1225            $this->smtp->close();
1226        }
1227        if (count($bad_rcpt) > 0) { //Create error message for any bad addresses
1228            throw new phpmailerException(
1229                $this->lang('recipients_failed') . implode(', ', $bad_rcpt),
1230                self::STOP_CONTINUE
1231            );
1232        }
1233        return true;
1234    }
1235
1236    /**
1237     * Initiate a connection to an SMTP server.
1238     * Returns false if the operation failed.
1239     * @param array $options An array of options compatible with stream_context_create()
1240     * @uses SMTP
1241     * @access public
1242     * @throws phpmailerException
1243     * @return bool
1244     */
1245    public function smtpConnect($options = array())
1246    {
1247        if (is_null($this->smtp)) {
1248            $this->smtp = $this->getSMTPInstance();
1249        }
1250
1251        //Already connected?
1252        if ($this->smtp->connected()) {
1253            return true;
1254        }
1255
1256        $this->smtp->setTimeout($this->Timeout);
1257        $this->smtp->setDebugLevel($this->SMTPDebug);
1258        $this->smtp->setDebugOutput($this->Debugoutput);
1259        $this->smtp->setVerp($this->do_verp);
1260        $hosts = explode(';', $this->Host);
1261        $lastexception = null;
1262
1263        foreach ($hosts as $hostentry) {
1264            $hostinfo = array();
1265            if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) {
1266                //Not a valid host entry
1267                continue;
1268            }
1269            //$hostinfo[2]: optional ssl or tls prefix
1270            //$hostinfo[3]: the hostname
1271            //$hostinfo[4]: optional port number
1272            //The host string prefix can temporarily override the current setting for SMTPSecure
1273            //If it's not specified, the default value is used
1274            $prefix = '';
1275            $tls = ($this->SMTPSecure == 'tls');
1276            if ($hostinfo[2] == 'ssl' or ($hostinfo[2] == '' and $this->SMTPSecure == 'ssl')) {
1277                $prefix = 'ssl://';
1278                $tls = false; //Can't have SSL and TLS at once
1279            } elseif ($hostinfo[2] == 'tls') {
1280                $tls = true;
1281                //tls doesn't use a prefix
1282            }
1283            $host = $hostinfo[3];
1284            $port = $this->Port;
1285            $tport = (integer)$hostinfo[4];
1286            if ($tport > 0 and $tport < 65536) {
1287                $port = $tport;
1288            }
1289            if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) {
1290                try {
1291                    if ($this->Helo) {
1292                        $hello = $this->Helo;
1293                    } else {
1294                        $hello = $this->serverHostname();
1295                    }
1296                    $this->smtp->hello($hello);
1297
1298                    if ($tls) {
1299                        if (!$this->smtp->startTLS()) {
1300                            throw new phpmailerException($this->lang('connect_host'));
1301                        }
1302                        //We must resend HELO after tls negotiation
1303                        $this->smtp->hello($hello);
1304                    }
1305                    if ($this->SMTPAuth) {
1306                        if (!$this->smtp->authenticate(
1307                            $this->Username,
1308                            $this->Password,
1309                            $this->AuthType,
1310                            $this->Realm,
1311                            $this->Workstation
1312                        )
1313                        ) {
1314                            throw new phpmailerException($this->lang('authenticate'));
1315                        }
1316                    }
1317                    return true;
1318                } catch (phpmailerException $e) {
1319                    $lastexception = $e;
1320                    //We must have connected, but then failed TLS or Auth, so close connection nicely
1321                    $this->smtp->quit();
1322                }
1323            }
1324        }
1325        //If we get here, all connection attempts have failed, so close connection hard
1326        $this->smtp->close();
1327        //As we've caught all exceptions, just report whatever the last one was
1328        if ($this->exceptions and !is_null($lastexception)) {
1329            throw $lastexception;
1330        }
1331        return false;
1332    }
1333
1334    /**
1335     * Close the active SMTP session if one exists.
1336     * @return void
1337     */
1338    public function smtpClose()
1339    {
1340        if ($this->smtp !== null) {
1341            if ($this->smtp->connected()) {
1342                $this->smtp->quit();
1343                $this->smtp->close();
1344            }
1345        }
1346    }
1347
1348    /**
1349     * Set the language for error messages.
1350     * Returns false if it cannot load the language file.
1351     * The default language is English.
1352     * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr")
1353     * @param string $lang_path Path to the language file directory, with trailing separator (slash)
1354     * @return bool
1355     * @access public
1356     */
1357    public function setLanguage($langcode = 'en', $lang_path = 'language/')
1358    {
1359        //Define full set of translatable strings
1360        $PHPMAILER_LANG = array(
1361            'authenticate' => 'SMTP Error: Could not authenticate.',
1362            'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
1363            'data_not_accepted' => 'SMTP Error: data not accepted.',
1364            'empty_message' => 'Message body empty',
1365            'encoding' => 'Unknown encoding: ',
1366            'execute' => 'Could not execute: ',
1367            'file_access' => 'Could not access file: ',
1368            'file_open' => 'File Error: Could not open file: ',
1369            'from_failed' => 'The following From address failed: ',
1370            'instantiate' => 'Could not instantiate mail function.',
1371            'invalid_address' => 'Invalid address',
1372            'mailer_not_supported' => ' mailer is not supported.',
1373            'provide_address' => 'You must provide at least one recipient email address.',
1374            'recipients_failed' => 'SMTP Error: The following recipients failed: ',
1375            'signing' => 'Signing Error: ',
1376            'smtp_connect_failed' => 'SMTP connect() failed.',
1377            'smtp_error' => 'SMTP server error: ',
1378            'variable_set' => 'Cannot set or reset variable: '
1379        );
1380        //Overwrite language-specific strings.
1381        //This way we'll never have missing translations - no more "language string failed to load"!
1382        $l = true;
1383        $lang_file = $lang_path . 'phpmailer.lang-' . $langcode . '.php';
1384        if ($langcode != 'en') { //There is no English translation file
1385            //Make sure language file path is readable
1386            if (!is_readable($lang_file)) {
1387                $l = false;
1388            } else {
1389                $l = include $lang_file;
1390            }
1391        }
1392        $this->language = $PHPMAILER_LANG;
1393        return ($l == true); //Returns false if language not found
1394    }
1395
1396    /**
1397     * Get the array of strings for the current language.
1398     * @return array
1399     */
1400    public function getTranslations()
1401    {
1402        return $this->language;
1403    }
1404
1405    /**
1406     * Create recipient headers.
1407     * @access public
1408     * @param string $type
1409     * @param array $addr An array of recipient,
1410     * where each recipient is a 2-element indexed array with element 0 containing an address
1411     * and element 1 containing a name, like:
1412     * array(array('joe@example.com', 'Joe User'), array('zoe@example.com', 'Zoe User'))
1413     * @return string
1414     */
1415    public function addrAppend($type, $addr)
1416    {
1417        $addresses = array();
1418        foreach ($addr as $a) {
1419            $addresses[] = $this->addrFormat($a);
1420        }
1421        return $type . ': ' . implode(', ', $addresses) . $this->LE;
1422    }
1423
1424    /**
1425     * Format an address for use in a message header.
1426     * @access public
1427     * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name
1428     *      like array('joe@example.com', 'Joe User')
1429     * @return string
1430     */
1431    public function addrFormat($addr)
1432    {
1433        if (empty($addr[1])) { // No name provided
1434            return $this->secureHeader($addr[0]);
1435        } else {
1436            return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . " <" . $this->secureHeader(
1437                $addr[0]
1438            ) . ">";
1439        }
1440    }
1441
1442    /**
1443     * Word-wrap message.
1444     * For use with mailers that do not automatically perform wrapping
1445     * and for quoted-printable encoded messages.
1446     * Original written by philippe.
1447     * @param string $message The message to wrap
1448     * @param integer $length The line length to wrap to
1449     * @param bool $qp_mode Whether to run in Quoted-Printable mode
1450     * @access public
1451     * @return string
1452     */
1453    public function wrapText($message, $length, $qp_mode = false)
1454    {
1455        $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
1456        // If utf-8 encoding is used, we will need to make sure we don't
1457        // split multibyte characters when we wrap
1458        $is_utf8 = (strtolower($this->CharSet) == "utf-8");
1459        $lelen = strlen($this->LE);
1460        $crlflen = strlen(self::CRLF);
1461
1462        $message = $this->fixEOL($message);
1463        if (substr($message, -$lelen) == $this->LE) {
1464            $message = substr($message, 0, -$lelen);
1465        }
1466
1467        $line = explode($this->LE, $message); // Magic. We know fixEOL uses $LE
1468        $message = '';
1469        for ($i = 0; $i < count($line); $i++) {
1470            $line_part = explode(' ', $line[$i]);
1471            $buf = '';
1472            for ($e = 0; $e < count($line_part); $e++) {
1473                $word = $line_part[$e];
1474                if ($qp_mode and (strlen($word) > $length)) {
1475                    $space_left = $length - strlen($buf) - $crlflen;
1476                    if ($e != 0) {
1477                        if ($space_left > 20) {
1478                            $len = $space_left;
1479                            if ($is_utf8) {
1480                                $len = $this->utf8CharBoundary($word, $len);
1481                            } elseif (substr($word, $len - 1, 1) == "=") {
1482                                $len--;
1483                            } elseif (substr($word, $len - 2, 1) == "=") {
1484                                $len -= 2;
1485                            }
1486                            $part = substr($word, 0, $len);
1487                            $word = substr($word, $len);
1488                            $buf .= ' ' . $part;
1489                            $message .= $buf . sprintf("=%s", self::CRLF);
1490                        } else {
1491                            $message .= $buf . $soft_break;
1492                        }
1493                        $buf = '';
1494                    }
1495                    while (strlen($word) > 0) {
1496                        if ($length <= 0) {
1497                            break;
1498                        }
1499                        $len = $length;
1500                        if ($is_utf8) {
1501                            $len = $this->utf8CharBoundary($word, $len);
1502                        } elseif (substr($word, $len - 1, 1) == "=") {
1503                            $len--;
1504                        } elseif (substr($word, $len - 2, 1) == "=") {
1505                            $len -= 2;
1506                        }
1507                        $part = substr($word, 0, $len);
1508                        $word = substr($word, $len);
1509
1510                        if (strlen($word) > 0) {
1511                            $message .= $part . sprintf("=%s", self::CRLF);
1512                        } else {
1513                            $buf = $part;
1514                        }
1515                    }
1516                } else {
1517                    $buf_o = $buf;
1518                    $buf .= ($e == 0) ? $word : (' ' . $word);
1519
1520                    if (strlen($buf) > $length and $buf_o != '') {
1521                        $message .= $buf_o . $soft_break;
1522                        $buf = $word;
1523                    }
1524 

Large files files are truncated, but you can click here to view the full file