PageRenderTime 10ms CodeModel.GetById 8ms app.highlight 52ms RepoModel.GetById 1ms app.codeStats 1ms

/class.phpmailer.php

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

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