PageRenderTime 88ms CodeModel.GetById 42ms app.highlight 34ms RepoModel.GetById 1ms app.codeStats 1ms

/plugins/phpmailer/class.phpmailer.php

http://oraculum-php.googlecode.com/
PHP | 3302 lines | 2042 code | 211 blank | 1049 comment | 309 complexity | a02cb30a6c4cde3fa65cdc7c797b101b 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 = 'iso-8859-1';
  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', '>=') and
 570            !spl_autoload_functions() || !in_array('PHPMailerAutoload', spl_autoload_functions())) {
 571            require 'PHPMailerAutoload.php';
 572        }
 573    }
 574
 575    /**
 576     * Destructor.
 577     */
 578    public function __destruct()
 579    {
 580        if ($this->Mailer == 'smtp') { //close any open SMTP connection nicely
 581            $this->smtpClose();
 582        }
 583    }
 584
 585    /**
 586     * Call mail() in a safe_mode-aware fashion.
 587     * Also, unless sendmail_path points to sendmail (or something that
 588     * claims to be sendmail), don't pass params (not a perfect fix,
 589     * but it will do)
 590     * @param string $to To
 591     * @param string $subject Subject
 592     * @param string $body Message Body
 593     * @param string $header Additional Header(s)
 594     * @param string $params Params
 595     * @access private
 596     * @return bool
 597     */
 598    private function mailPassthru($to, $subject, $body, $header, $params)
 599    {
 600        //Check overloading of mail function to avoid double-encoding
 601        if (ini_get('mbstring.func_overload') & 1) {
 602            $subject = $this->secureHeader($subject);
 603        } else {
 604            $subject = $this->encodeHeader($this->secureHeader($subject));
 605        }
 606        if (ini_get('safe_mode') || !($this->UseSendmailOptions)) {
 607            $rt = @mail($to, $subject, $body, $header);
 608        } else {
 609            $rt = @mail($to, $subject, $body, $header, $params);
 610        }
 611        return $rt;
 612    }
 613
 614    /**
 615     * Output debugging info via user-defined method.
 616     * Only if debug output is enabled.
 617     * @see PHPMailer::$Debugoutput
 618     * @see PHPMailer::$SMTPDebug
 619     * @param string $str
 620     */
 621    protected function edebug($str)
 622    {
 623        if (!$this->SMTPDebug) {
 624            return;
 625        }
 626        switch ($this->Debugoutput) {
 627            case 'error_log':
 628                error_log($str);
 629                break;
 630            case 'html':
 631                //Cleans up output a bit for a better looking display that's HTML-safe
 632                echo htmlentities(preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, $this->CharSet) . "<br>\n";
 633                break;
 634            case 'echo':
 635            default:
 636                echo $str."\n";
 637        }
 638    }
 639
 640    /**
 641     * Sets message type to HTML or plain.
 642     * @param bool $ishtml True for HTML mode.
 643     * @return void
 644     */
 645    public function isHTML($ishtml = true)
 646    {
 647        if ($ishtml) {
 648            $this->ContentType = 'text/html';
 649        } else {
 650            $this->ContentType = 'text/plain';
 651        }
 652    }
 653
 654    /**
 655     * Send messages using SMTP.
 656     * @return void
 657     */
 658    public function isSMTP()
 659    {
 660        $this->Mailer = 'smtp';
 661    }
 662
 663    /**
 664     * Send messages using PHP's mail() function.
 665     * @return void
 666     */
 667    public function isMail()
 668    {
 669        $this->Mailer = 'mail';
 670    }
 671
 672    /**
 673     * Send messages using $Sendmail.
 674     * @return void
 675     */
 676    public function isSendmail()
 677    {
 678        if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
 679            $this->Sendmail = '/usr/sbin/sendmail';
 680        }
 681        $this->Mailer = 'sendmail';
 682    }
 683
 684    /**
 685     * Send messages using qmail.
 686     * @return void
 687     */
 688    public function isQmail()
 689    {
 690        if (!stristr(ini_get('sendmail_path'), 'qmail')) {
 691            $this->Sendmail = '/var/qmail/bin/qmail-inject';
 692        }
 693        $this->Mailer = 'qmail';
 694    }
 695
 696    /**
 697     * Add a "To" address.
 698     * @param string $address
 699     * @param string $name
 700     * @return bool true on success, false if address already used
 701     */
 702    public function addAddress($address, $name = '')
 703    {
 704        return $this->addAnAddress('to', $address, $name);
 705    }
 706
 707    /**
 708     * Add a "CC" address.
 709     * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
 710     * @param string $address
 711     * @param string $name
 712     * @return bool true on success, false if address already used
 713     */
 714    public function addCC($address, $name = '')
 715    {
 716        return $this->addAnAddress('cc', $address, $name);
 717    }
 718
 719    /**
 720     * Add a "BCC" address.
 721     * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
 722     * @param string $address
 723     * @param string $name
 724     * @return bool true on success, false if address already used
 725     */
 726    public function addBCC($address, $name = '')
 727    {
 728        return $this->addAnAddress('bcc', $address, $name);
 729    }
 730
 731    /**
 732     * Add a "Reply-to" address.
 733     * @param string $address
 734     * @param string $name
 735     * @return bool
 736     */
 737    public function addReplyTo($address, $name = '')
 738    {
 739        return $this->addAnAddress('Reply-To', $address, $name);
 740    }
 741
 742    /**
 743     * Add an address to one of the recipient arrays.
 744     * Addresses that have been added already return false, but do not throw exceptions
 745     * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
 746     * @param string $address The email address to send to
 747     * @param string $name
 748     * @throws phpmailerException
 749     * @return bool true on success, false if address already used or invalid in some way
 750     * @access protected
 751     */
 752    protected function addAnAddress($kind, $address, $name = '')
 753    {
 754        if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
 755            $this->setError($this->lang('Invalid recipient array') . ': ' . $kind);
 756            $this->edebug($this->lang('Invalid recipient array') . ': ' . $kind);
 757            if ($this->exceptions) {
 758                throw new phpmailerException('Invalid recipient array: ' . $kind);
 759            }
 760            return false;
 761        }
 762        $address = trim($address);
 763        $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 764        if (!$this->validateAddress($address)) {
 765            $this->setError($this->lang('invalid_address') . ': ' . $address);
 766            $this->edebug($this->lang('invalid_address') . ': ' . $address);
 767            if ($this->exceptions) {
 768                throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
 769            }
 770            return false;
 771        }
 772        if ($kind != 'Reply-To') {
 773            if (!isset($this->all_recipients[strtolower($address)])) {
 774                array_push($this->$kind, array($address, $name));
 775                $this->all_recipients[strtolower($address)] = true;
 776                return true;
 777            }
 778        } else {
 779            if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
 780                $this->ReplyTo[strtolower($address)] = array($address, $name);
 781                return true;
 782            }
 783        }
 784        return false;
 785    }
 786
 787    /**
 788     * Set the From and FromName properties.
 789     * @param string $address
 790     * @param string $name
 791     * @param bool $auto Whether to also set the Sender address, defaults to true
 792     * @throws phpmailerException
 793     * @return bool
 794     */
 795    public function setFrom($address, $name = '', $auto = true)
 796    {
 797        $address = trim($address);
 798        $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
 799        if (!$this->validateAddress($address)) {
 800            $this->setError($this->lang('invalid_address') . ': ' . $address);
 801            $this->edebug($this->lang('invalid_address') . ': ' . $address);
 802            if ($this->exceptions) {
 803                throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
 804            }
 805            return false;
 806        }
 807        $this->From = $address;
 808        $this->FromName = $name;
 809        if ($auto) {
 810            if (empty($this->Sender)) {
 811                $this->Sender = $address;
 812            }
 813        }
 814        return true;
 815    }
 816
 817    /**
 818     * Return the Message-ID header of the last email.
 819     * Technically this is the value from the last time the headers were created,
 820     * but it's also the message ID of the last sent message except in
 821     * pathological cases.
 822     * @return string
 823     */
 824    public function getLastMessageID()
 825    {
 826        return $this->lastMessageID;
 827    }
 828
 829    /**
 830     * Check that a string looks like an email address.
 831     * @param string $address The email address to check
 832     * @param string $patternselect A selector for the validation pattern to use :
 833     *   'auto' - pick best one automatically;
 834     *   'pcre8' - use the squiloople.com pattern, requires PCRE > 8.0, PHP >= 5.3.2, 5.2.14;
 835     *   'pcre' - use old PCRE implementation;
 836     *   'php' - use PHP built-in FILTER_VALIDATE_EMAIL; faster, less thorough;
 837     *   'noregex' - super fast, really dumb.
 838     * @return bool
 839     * @static
 840     * @access public
 841     */
 842    public static function validateAddress($address, $patternselect = 'auto')
 843    {
 844        if ($patternselect == 'auto') {
 845            if (defined(
 846                'PCRE_VERSION'
 847            )
 848            ) { //Check this instead of extension_loaded so it works when that function is disabled
 849                if (version_compare(PCRE_VERSION, '8.0') >= 0) {
 850                    $patternselect = 'pcre8';
 851                } else {
 852                    $patternselect = 'pcre';
 853                }
 854            } else {
 855                //Filter_var appeared in PHP 5.2.0 and does not require the PCRE extension
 856                if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
 857                    $patternselect = 'php';
 858                } else {
 859                    $patternselect = 'noregex';
 860                }
 861            }
 862        }
 863        switch ($patternselect) {
 864            case 'pcre8':
 865                /**
 866                 * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
 867                 * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
 868                 * not allow a@b type valid addresses :(
 869                 * @link http://squiloople.com/2009/12/20/email-address-validation/
 870                 * @copyright 2009-2010 Michael Rushton
 871                 * Feel free to use and redistribute this code. But please keep this copyright notice.
 872                 */
 873                return (bool)preg_match(
 874                    '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
 875                    '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
 876                    '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
 877                    '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
 878                    '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
 879                    '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
 880                    '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
 881                    '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
 882                    '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
 883                    $address
 884                );
 885                break;
 886            case 'pcre':
 887                //An older regex that doesn't need a recent PCRE
 888                return (bool)preg_match(
 889                    '/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>' .
 890                    '[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")' .
 891                    '(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*' .
 892                    '@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})' .
 893                    '(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:' .
 894                    '[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?' .
 895                    '::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:' .
 896                    '[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?' .
 897                    '::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
 898                    '|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD',
 899                    $address
 900                );
 901                break;
 902            case 'php':
 903            default:
 904                return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
 905                break;
 906            case 'noregex':
 907                //No PCRE! Do something _very_ approximate!
 908                //Check the address is 3 chars or longer and contains an @ that's not the first or last char
 909                return (strlen($address) >= 3
 910                    and strpos($address, '@') >= 1
 911                    and strpos($address, '@') != strlen($address) - 1);
 912                break;
 913        }
 914    }
 915
 916    /**
 917     * Create a message and send it.
 918     * Uses the sending method specified by $Mailer.
 919     * @throws phpmailerException
 920     * @return bool false on error - See the ErrorInfo property for details of the error.
 921     */
 922    public function send()
 923    {
 924        try {
 925            if (!$this->preSend()) {
 926                return false;
 927            }
 928            return $this->postSend();
 929        } catch (phpmailerException $e) {
 930            $this->mailHeader = '';
 931            $this->setError($e->getMessage());
 932            if ($this->exceptions) {
 933                throw $e;
 934            }
 935            return false;
 936        }
 937    }
 938
 939    /**
 940     * Prepare a message for sending.
 941     * @throws phpmailerException
 942     * @return bool
 943     */
 944    public function preSend()
 945    {
 946        try {
 947            $this->mailHeader = "";
 948            if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
 949                throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL);
 950            }
 951
 952            // Set whether the message is multipart/alternative
 953            if (!empty($this->AltBody)) {
 954                $this->ContentType = 'multipart/alternative';
 955            }
 956
 957            $this->error_count = 0; // reset errors
 958            $this->setMessageType();
 959            // Refuse to send an empty message unless we are specifically allowing it
 960            if (!$this->AllowEmpty and empty($this->Body)) {
 961                throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL);
 962            }
 963
 964            $this->MIMEHeader = $this->createHeader();
 965            $this->MIMEBody = $this->createBody();
 966
 967            // To capture the complete message when using mail(), create
 968            // an extra header list which createHeader() doesn't fold in
 969            if ($this->Mailer == 'mail') {
 970                if (count($this->to) > 0) {
 971                    $this->mailHeader .= $this->addrAppend("To", $this->to);
 972                } else {
 973                    $this->mailHeader .= $this->headerLine("To", "undisclosed-recipients:;");
 974                }
 975                $this->mailHeader .= $this->headerLine(
 976                    'Subject',
 977                    $this->encodeHeader($this->secureHeader(trim($this->Subject)))
 978                );
 979            }
 980
 981            // Sign with DKIM if enabled
 982            if (!empty($this->DKIM_domain)
 983                && !empty($this->DKIM_private)
 984                && !empty($this->DKIM_selector)
 985                && !empty($this->DKIM_domain)
 986                && file_exists($this->DKIM_private)) {
 987                $header_dkim = $this->DKIM_Add(
 988                    $this->MIMEHeader . $this->mailHeader,
 989                    $this->encodeHeader($this->secureHeader($this->Subject)),
 990                    $this->MIMEBody
 991                );
 992                $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . self::CRLF .
 993                    str_replace("\r\n", "\n", $header_dkim) . self::CRLF;
 994            }
 995            return true;
 996
 997        } catch (phpmailerException $e) {
 998            $this->setError($e->getMessage());
 999            if ($this->exceptions) {
1000                throw $e;
1001            }
1002            return false;
1003        }
1004    }
1005
1006    /**
1007     * Actually send a message.
1008     * Send the email via the selected mechanism
1009     * @throws phpmailerException
1010     * @return bool
1011     */
1012    public function postSend()
1013    {
1014        try {
1015            // Choose the mailer and send through it
1016            switch ($this->Mailer) {
1017                case 'sendmail':
1018                case 'qmail':
1019                    return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody);
1020                case 'smtp':
1021                    return $this->smtpSend($this->MIMEHeader, $this->MIMEBody);
1022                case 'mail':
1023                    return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
1024                default:
1025                    if (method_exists($this, $this->Mailer.'Send')) {
1026                        $sendMethod = $this->Mailer.'Send';
1027                        return $this->$sendMethod($this->MIMEHeader, $this->MIMEBody);
1028                    } else {
1029                        return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
1030                    }
1031            }
1032        } catch (phpmailerException $e) {
1033            $this->setError($e->getMessage());
1034            $this->edebug($e->getMessage());
1035            if ($this->exceptions) {
1036                throw $e;
1037            }
1038        }
1039        return false;
1040    }
1041
1042    /**
1043     * Send mail using the $Sendmail program.
1044     * @param string $header The message headers
1045     * @param string $body The message body
1046     * @see PHPMailer::$Sendmail
1047     * @throws phpmailerException
1048     * @access protected
1049     * @return bool
1050     */
1051    protected function sendmailSend($header, $body)
1052    {
1053        if ($this->Sender != '') {
1054            if ($this->Mailer == 'qmail') {
1055                $sendmail = sprintf("%s -f%s", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
1056            } else {
1057                $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
1058            }
1059        } else {
1060            if ($this->Mailer == 'qmail') {
1061                $sendmail = sprintf("%s", escapeshellcmd($this->Sendmail));
1062            } else {
1063                $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
1064            }
1065        }
1066        if ($this->SingleTo === true) {
1067            foreach ($this->SingleToArray as $val) {
1068                if (!@$mail = popen($sendmail, 'w')) {
1069                    throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1070                }
1071                fputs($mail, "To: " . $val . "\n");
1072                fputs($mail, $header);
1073                fputs($mail, $body);
1074                $result = pclose($mail);
1075                // implement call back function if it exists
1076                $isSent = ($result == 0) ? 1 : 0;
1077                $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1078                if ($result != 0) {
1079                    throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1080                }
1081            }
1082        } else {
1083            if (!@$mail = popen($sendmail, 'w')) {
1084                throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1085            }
1086            fputs($mail, $header);
1087            fputs($mail, $body);
1088            $result = pclose($mail);
1089            // implement call back function if it exists
1090            $isSent = ($result == 0) ? 1 : 0;
1091            $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1092            if ($result != 0) {
1093                throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1094            }
1095        }
1096        return true;
1097    }
1098
1099    /**
1100     * Send mail using the PHP mail() function.
1101     * @param string $header The message headers
1102     * @param string $body The message body
1103     * @link http://www.php.net/manual/en/book.mail.php
1104     * @throws phpmailerException
1105     * @access protected
1106     * @return bool
1107     */
1108    protected function mailSend($header, $body)
1109    {
1110        $toArr = array();
1111        foreach ($this->to as $t) {
1112            $toArr[] = $this->addrFormat($t);
1113        }
1114        $to = implode(', ', $toArr);
1115
1116        if (empty($this->Sender)) {
1117            $params = " ";
1118        } else {
1119            $params = sprintf("-f%s", $this->Sender);
1120        }
1121        if ($this->Sender != '' and !ini_get('safe_mode')) {
1122            $old_from = ini_get('sendmail_from');
1123            ini_set('sendmail_from', $this->Sender);
1124        }
1125        $rt = false;
1126        if ($this->SingleTo === true && count($toArr) > 1) {
1127            foreach ($toArr as $val) {
1128                $rt = $this->mailPassthru($val, $this->Subject, $body, $header, $params);
1129                // implement call back function if it exists
1130                $isSent = ($rt == 1) ? 1 : 0;
1131                $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1132            }
1133        } else {
1134            $rt = $this->mailPassthru($to, $this->Subject, $body, $header, $params);
1135            // implement call back function if it exists
1136            $isSent = ($rt == 1) ? 1 : 0;
1137            $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1138        }
1139        if (isset($old_from)) {
1140            ini_set('sendmail_from', $old_from);
1141        }
1142        if (!$rt) {
1143            throw new phpmailerException($this->lang('instantiate'), self::STOP_CRITICAL);
1144        }
1145        return true;
1146    }
1147
1148    /**
1149     * Get an instance to use for SMTP operations.
1150     * Override this function to load your own SMTP implementation
1151     * @return SMTP
1152     */
1153    public function getSMTPInstance()
1154    {
1155        if (!is_object($this->smtp)) {
1156            $this->smtp = new SMTP;
1157        }
1158        return $this->smtp;
1159    }
1160
1161    /**
1162     * Send mail via SMTP.
1163     * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
1164     * Uses the PHPMailerSMTP class by default.
1165     * @see PHPMailer::getSMTPInstance() to use a different class.
1166     * @param string $header The message headers
1167     * @param string $body The message body
1168     * @throws phpmailerException
1169     * @uses SMTP
1170     * @access protected
1171     * @return bool
1172     */
1173    protected function smtpSend($header, $body)
1174    {
1175        $bad_rcpt = array();
1176
1177        if (!$this->smtpConnect()) {
1178            throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
1179        }
1180        $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
1181        if (!$this->smtp->mail($smtp_from)) {
1182            $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError()));
1183            throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
1184        }
1185
1186        // Attempt to send to all recipients
1187        foreach ($this->to as $to) {
1188            if (!$this->smtp->recipient($to[0])) {
1189                $bad_rcpt[] = $to[0];
1190                $isSent = 0;
1191            } else {
1192                $isSent = 1;
1193            }
1194            $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body, $this->From);
1195        }
1196        foreach ($this->cc as $cc) {
1197            if (!$this->smtp->recipient($cc[0])) {
1198                $bad_rcpt[] = $cc[0];
1199                $isSent = 0;
1200            } else {
1201                $isSent = 1;
1202            }
1203            $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body, $this->From);
1204        }
1205        foreach ($this->bcc as $bcc) {
1206            if (!$this->smtp->recipient($bcc[0])) {
1207                $bad_rcpt[] = $bcc[0];
1208                $isSent = 0;
1209            } else {
1210                $isSent = 1;
1211            }
1212            $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body, $this->From);
1213        }
1214
1215        //Only send the DATA command if we have viable recipients
1216        if ((count($this->all_recipients) > count($bad_rcpt)) and !$this->smtp->data($header . $body)) {
1217            throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL);
1218        }
1219        if ($this->SMTPKeepAlive == true) {
1220            $this->smtp->reset();
1221        } else {
1222            $this->smtp->quit();
1223            $this->smtp->close();
1224        }
1225        if (count($bad_rcpt) > 0) { //Create error message for any bad addresses
1226            throw new phpmailerException(
1227                $this->lang('recipients_failed') . implode(', ', $bad_rcpt),
1228                self::STOP_CONTINUE
1229            );
1230        }
1231        return true;
1232    }
1233
1234    /**
1235     * Initiate a connection to an SMTP server.
1236     * Returns false if the operation failed.
1237     * @param array $options An array of options compatible with stream_context_create()
1238     * @uses SMTP
1239     * @access public
1240     * @throws phpmailerException
1241     * @return bool
1242     */
1243    public function smtpConnect($options = array())
1244    {
1245        if (is_null($this->smtp)) {
1246            $this->smtp = $this->getSMTPInstance();
1247        }
1248
1249        //Already connected?
1250        if ($this->smtp->connected()) {
1251            return true;
1252        }
1253
1254        $this->smtp->setTimeout($this->Timeout);
1255        $this->smtp->setDebugLevel($this->SMTPDebug);
1256        $this->smtp->setDebugOutput($this->Debugoutput);
1257        $this->smtp->setVerp($this->do_verp);
1258        $hosts = explode(';', $this->Host);
1259        $lastexception = null;
1260
1261        foreach ($hosts as $hostentry) {
1262            $hostinfo = array();
1263            if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) {
1264                //Not a valid host entry
1265                continue;
1266            }
1267            //$hostinfo[2]: optional ssl or tls prefix
1268            //$hostinfo[3]: the hostname
1269            //$hostinfo[4]: optional port number
1270            //The host string prefix can temporarily override the current setting for SMTPSecure
1271            //If it's not specified, the default value is used
1272            $prefix = '';
1273            $tls = ($this->SMTPSecure == 'tls');
1274            if ($hostinfo[2] == 'ssl' or ($hostinfo[2] == '' and $this->SMTPSecure == 'ssl')) {
1275                $prefix = 'ssl://';
1276                $tls = false; //Can't have SSL and TLS at once
1277            } elseif ($hostinfo[2] == 'tls') {
1278                $tls = true;
1279                //tls doesn't use a prefix
1280            }
1281            $host = $hostinfo[3];
1282            $port = $this->Port;
1283            $tport = (integer)$hostinfo[4];
1284            if ($tport > 0 and $tport < 65536) {
1285                $port = $tport;
1286            }
1287            if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) {
1288                try {
1289                    if ($this->Helo) {
1290                        $hello = $this->Helo;
1291                    } else {
1292                        $hello = $this->serverHostname();
1293                    }
1294                    $this->smtp->hello($hello);
1295
1296                    if ($tls) {
1297                        if (!$this->smtp->startTLS()) {
1298                            throw new phpmailerException($this->lang('connect_host'));
1299                        }
1300                        //We must resend HELO after tls negotiation
1301                        $this->smtp->hello($hello);
1302                    }
1303                    if ($this->SMTPAuth) {
1304                        if (!$this->smtp->authenticate(
1305                            $this->Username,
1306                            $this->Password,
1307                            $this->AuthType,
1308                            $this->Realm,
1309                            $this->Workstation
1310                        )
1311                        ) {
1312                            throw new phpmailerException($this->lang('authenticate'));
1313                        }
1314                    }
1315                    return true;
1316                } catch (phpmailerException $e) {
1317                    $lastexception = $e;
1318                    //We must have connected, but then failed TLS or Auth, so close connection nicely
1319                    $this->smtp->quit();
1320                }
1321            }
1322        }
1323        //If we get here, all connection attempts have failed, so close connection hard
1324        $this->smtp->close();
1325        //As we've caught all exceptions, just report whatever the last one was
1326        if ($this->exceptions and !is_null($lastexception)) {
1327            throw $lastexception;
1328        }
1329        return false;
1330    }
1331
1332    /**
1333     * Close the active SMTP session if one exists.
1334     * @return void
1335     */
1336    public function smtpClose()
1337    {
1338        if ($this->smtp !== null) {
1339            if ($this->smtp->connected()) {
1340                $this->smtp->quit();
1341                $this->smtp->close();
1342            }
1343        }
1344    }
1345
1346    /**
1347     * Set the language for error messages.
1348     * Returns false if it cannot load the language file.
1349     * The default language is English.
1350     * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr")
1351     * @param string $lang_path Path to the language file directory, with trailing separator (slash)
1352     * @return bool
1353     * @access public
1354     */
1355    public function setLanguage($langcode = 'en', $lang_path = 'language/')
1356    {
1357        //Define full set of translatable strings
1358        $PHPMAILER_LANG = array(
1359            'authenticate' => 'SMTP Error: Could not authenticate.',
1360            'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
1361            'data_not_accepted' => 'SMTP Error: data not accepted.',
1362            'empty_message' => 'Message body empty',
1363            'encoding' => 'Unknown encoding: ',
1364            'execute' => 'Could not execute: ',
1365            'file_access' => 'Could not access file: ',
1366            'file_open' => 'File Error: Could not open file: ',
1367            'from_failed' => 'The following From address failed: ',
1368            'instantiate' => 'Could not instantiate mail function.',
1369            'invalid_address' => 'Invalid address',
1370            'mailer_not_supported' => ' mailer is not supported.',
1371            'provide_address' => 'You must provide at least one recipient email address.',
1372            'recipients_failed' => 'SMTP Error: The following recipients failed: ',
1373            'signing' => 'Signing Error: ',
1374            'smtp_connect_failed' => 'SMTP connect() failed.',
1375            'smtp_error' => 'SMTP server error: ',
1376            'variable_set' => 'Cannot set or reset variable: '
1377        );
1378        //Overwrite language-specific strings.
1379        //This way we'll never have missing translations - no more "language string failed to load"!
1380        $l = true;
1381        if ($langcode != 'en') { //There is no English translation file
1382            $l = @include $lang_path . 'phpmailer.lang-' . $langcode . '.php';
1383        }
1384        $this->language = $PHPMAILER_LANG;
1385        return ($l == true); //Returns false if language not found
1386    }
1387
1388    /**
1389     * Get the array of strings for the current language.
1390     * @return array
1391     */
1392    public function getTranslations()
1393    {
1394        return $this->language;
1395    }
1396
1397    /**
1398     * Create recipient headers.
1399     * @access public
1400     * @param string $type
1401     * @param array $addr An array of recipient,
1402     * where each recipient is a 2-element indexed array with element 0 containing an address
1403     * and element 1 containing a name, like:
1404     * array(array('joe@example.com', 'Joe User'), array('zoe@example.com', 'Zoe User'))
1405     * @return string
1406     */
1407    public function addrAppend($type, $addr)
1408    {
1409        $addresses = array();
1410        foreach ($addr as $a) {
1411            $addresses[] = $this->addrFormat($a);
1412        }
1413        return $type . ': ' . implode(', ', $addresses) . $this->LE;
1414    }
1415
1416    /**
1417     * Format an address for use in a message header.
1418     * @access public
1419     * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name
1420     *      like array('joe@example.com', 'Joe User')
1421     * @return string
1422     */
1423    public function addrFormat($addr)
1424    {
1425        if (empty($addr[1])) { // No name provided
1426            return $this->secureHeader($addr[0]);
1427        } else {
1428            return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . " <" . $this->secureHeader(
1429                $addr[0]
1430            ) . ">";
1431        }
1432    }
1433
1434    /**
1435     * Word-wrap message.
1436     * For use with mailers that do not automatically perform wrapping
1437     * and for quoted-printable encoded messages.
1438     * Original written by philippe.
1439     * @param string $message The message to wrap
1440     * @param integer $length The line length to wrap to
1441     * @param bool $qp_mode Whether to run in Quoted-Printable mode
1442     * @access public
1443     * @return string
1444     */
1445    public function wrapText($message, $length, $qp_mode = false)
1446    {
1447        $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
1448        // If utf-8 encoding is used, we will need to make sure we don't
1449        // split multibyte characters when we wrap
1450        $is_utf8 = (strtolower($this->CharSet) == "utf-8");
1451        $lelen = strlen($this->LE);
1452        $crlflen = strlen(self::CRLF);
1453
1454        $message = $this->fixEOL($message);
1455        if (substr($message, -$lelen) == $this->LE) {
1456            $message = substr($message, 0, -$lelen);
1457        }
1458
1459        $line = explode($this->LE, $message); // Magic. We know fixEOL uses $LE
1460        $message = '';
1461        for ($i = 0; $i < count($line); $i++) {
1462            $line_part = explode(' ', $line[$i]);
1463            $buf = '';
1464            for ($e = 0; $e < count($line_part); $e++) {
1465                $word = $line_part[$e];
1466                if ($qp_mode and (strlen($word) > $length)) {
1467                    $space_left = $length - strlen($buf) - $crlflen;
1468                    if ($e != 0) {
1469                        if ($space_left > 20) {
1470                            $len = $space_left;
1471                            if ($is_utf8) {
1472                                $len = $this->utf8CharBoundary($word, $len);
1473                            } elseif (substr($word, $len - 1, 1) == "=") {
1474                                $len--;
1475                            } elseif (substr($word, $len - 2, 1) == "=") {
1476                                $len -= 2;
1477                            }
1478                            $part = substr($word, 0, $len);
1479                            $word = substr($word, $len);
1480                            $buf .= ' ' . $part;
1481                            $message .= $buf . sprintf("=%s", self::CRLF);
1482                        } else {
1483                            $message .= $buf . $soft_break;
1484                        }
1485                        $buf = '';
1486                    }
1487                    while (strlen($word) > 0) {
1488                        if ($length <= 0) {
1489                            break;
1490                        }
1491                        $len = $length;
1492                        if ($is_utf8) {
1493                            $len = $this->utf8CharBoundary($word, $len);
1494                        } elseif (substr($word, $len - 1, 1) == "=") {
1495                            $len--;
1496                        } elseif (substr($word, $len - 2, 1) == "=") {
1497                            $len -= 2;
1498                        }
1499                        $part = substr($word, 0, $len);
1500                        $word = substr($word, $len);
1501
1502                        if (strlen($word) > 0) {
1503                            $message .= $part . sprintf("=%s", self::CRLF);
1504                        } else {
1505                            $buf = $part;
1506                        }
1507                    }
1508                } else {
1509                    $buf_o = $buf;
1510                    $buf .= ($e == 0) ? $word : (' ' . $word);
1511
1512                    if (strlen($buf) > $length and $buf_o != '') {
1513                        $message .= $buf_o . $soft_break;
1514                        $buf = $word;
1515                    }
1516                }
1517            }
1518            $message .= $buf . self::CRLF;
1519        }
1520
1521        return $message;
1522    }
1523
1524    /**
1525     * Find the last character boundary prior to $maxLength in a utf-8
1526     * quoted (printable) enc…

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