Project

General

Profile

1 1463 Luisehahne
<?php
2
/**
3 2053 darkviper
 * PHPMailer - PHP email creation and transport class.
4
 * PHP Version 5.0.0
5
 * Version 5.2.7
6 1463 Luisehahne
 * @package PHPMailer
7 2053 darkviper
 * @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 1853 Luisehahne
 * @copyright 2010 - 2012 Jim Jagielski
14 1463 Luisehahne
 * @copyright 2004 - 2009 Andy Prevost
15 1539 Luisehahne
 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
16 2053 darkviper
 * @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 1463 Luisehahne
 */
20
21 2053 darkviper
if (version_compare(PHP_VERSION, '5.0.0', '<')) {
22
    exit("Sorry, PHPMailer will only run on PHP version 5 or greater!\n");
23
}
24 1539 Luisehahne
25 2053 darkviper
/**
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
 */
37
class PHPMailer
38
{
39
    /**
40
     * The PHPMailer Version number.
41
     * @type string
42
     */
43
    public $Version = '5.2.7';
44 1463 Luisehahne
45 2053 darkviper
    /**
46
     * Email priority.
47
     * Options: 1 = High, 3 = Normal, 5 = low.
48
     * @type int
49
     */
50
    public $Priority = 3;
51 1463 Luisehahne
52 2053 darkviper
    /**
53
     * The character set of the message.
54
     * @type string
55
     */
56
    public $CharSet = 'iso-8859-1';
57 1463 Luisehahne
58 2053 darkviper
    /**
59
     * The MIME Content-type of the message.
60
     * @type string
61
     */
62
    public $ContentType = 'text/plain';
63 1463 Luisehahne
64 2053 darkviper
    /**
65
     * The message encoding.
66
     * Options: "8bit", "7bit", "binary", "base64", and "quoted-printable".
67
     * @type string
68
     */
69
    public $Encoding = '8bit';
70 1463 Luisehahne
71 2053 darkviper
    /**
72
     * Holds the most recent mailer error message.
73
     * @type string
74
     */
75
    public $ErrorInfo = '';
76 1463 Luisehahne
77 2053 darkviper
    /**
78
     * The From email address for the message.
79
     * @type string
80
     */
81
    public $From = 'root@localhost';
82 1463 Luisehahne
83 2053 darkviper
    /**
84
     * The From name of the message.
85
     * @type string
86
     */
87
    public $FromName = 'Root User';
88 1463 Luisehahne
89 2053 darkviper
    /**
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 1463 Luisehahne
96 2053 darkviper
    /**
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 1463 Luisehahne
103 2053 darkviper
    /**
104
     * The Subject of the message.
105
     * @type string
106
     */
107
    public $Subject = '';
108 1463 Luisehahne
109 2053 darkviper
    /**
110
     * An HTML or plain text message body.
111
     * If HTML then call isHTML(true).
112
     * @type string
113
     */
114
    public $Body = '';
115 1463 Luisehahne
116 2053 darkviper
    /**
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 1463 Luisehahne
125 2053 darkviper
    /**
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 1550 Luisehahne
135 2053 darkviper
    /**
136
     * The complete compiled MIME message body.
137
     * @access protected
138
     * @type string
139
     */
140
    protected $MIMEBody = '';
141 1550 Luisehahne
142 2053 darkviper
    /**
143
     * The complete compiled MIME message headers.
144
     * @type string
145
     * @access protected
146
     */
147
    protected $MIMEHeader = '';
148 1853 Luisehahne
149 2053 darkviper
    /**
150
     * Extra headers that createHeader() doesn't fold in.
151
     * @type string
152
     * @access protected
153
     */
154
    protected $mailHeader = '';
155 1463 Luisehahne
156 2053 darkviper
    /**
157
     * Word-wrap the message body to this number of chars.
158
     * @type int
159
     */
160
    public $WordWrap = 0;
161 1463 Luisehahne
162 2053 darkviper
    /**
163
     * Which method to use to send mail.
164
     * Options: "mail", "sendmail", or "smtp".
165
     * @type string
166
     */
167
    public $Mailer = 'mail';
168 1463 Luisehahne
169 2053 darkviper
    /**
170
     * The path to the sendmail program.
171
     * @type string
172
     */
173
    public $Sendmail = '/usr/sbin/sendmail';
174 1463 Luisehahne
175 2053 darkviper
    /**
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 1463 Luisehahne
182 2053 darkviper
    /**
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 1463 Luisehahne
190 2053 darkviper
    /**
191
     * The email address that a reading confirmation should be sent to.
192
     * @type string
193
     */
194
    public $ConfirmReadingTo = '';
195 1463 Luisehahne
196 2053 darkviper
    /**
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 1463 Luisehahne
205 2053 darkviper
    /**
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 1463 Luisehahne
212 2053 darkviper
    /**
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 1463 Luisehahne
219 2053 darkviper
    /**
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 1463 Luisehahne
230 2053 darkviper
    /**
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 1463 Luisehahne
237 2053 darkviper
    /**
238
     * The SMTP HELO of the message.
239
     * Default is $Hostname.
240
     * @type string
241
     * @see PHPMailer::$Hostname
242
     */
243
    public $Helo = '';
244 1463 Luisehahne
245 2053 darkviper
    /**
246
     * The secure connection prefix.
247
     * Options: "", "ssl" or "tls"
248
     * @type string
249
     */
250
    public $SMTPSecure = '';
251 1463 Luisehahne
252 2053 darkviper
    /**
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 1463 Luisehahne
261 2053 darkviper
    /**
262
     * SMTP username.
263
     * @type string
264
     */
265
    public $Username = '';
266 1463 Luisehahne
267 2053 darkviper
    /**
268
     * SMTP password.
269
     * @type string
270
     */
271
    public $Password = '';
272 1463 Luisehahne
273 2053 darkviper
    /**
274
     * SMTP auth type.
275
     * Options are LOGIN (default), PLAIN, NTLM, CRAM-MD5
276
     * @type string
277
     */
278
    public $AuthType = '';
279 1463 Luisehahne
280 2053 darkviper
    /**
281
     * SMTP realm.
282
     * Used for NTLM auth
283
     * @type string
284
     */
285
    public $Realm = '';
286 1463 Luisehahne
287 2053 darkviper
    /**
288
     * SMTP workstation.
289
     * Used for NTLM auth
290
     * @type string
291
     */
292
    public $Workstation = '';
293 1539 Luisehahne
294 2053 darkviper
    /**
295
     * The SMTP server timeout in seconds.
296
     * @type int
297
     */
298
    public $Timeout = 10;
299 1539 Luisehahne
300 2053 darkviper
    /**
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 1539 Luisehahne
308 2053 darkviper
    /**
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 1539 Luisehahne
316 2053 darkviper
    /**
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 1550 Luisehahne
324 2053 darkviper
    /**
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 1539 Luisehahne
331 2053 darkviper
    /**
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 1539 Luisehahne
338 2053 darkviper
    /**
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 1539 Luisehahne
346 2053 darkviper
    /**
347
     * Whether to allow sending messages with an empty body.
348
     * @type bool
349
     */
350
    public $AllowEmpty = false;
351 1539 Luisehahne
352 2053 darkviper
    /**
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 1550 Luisehahne
360 2053 darkviper
    /**
361
     * DKIM selector.
362
     * @type string
363
     */
364
    public $DKIM_selector = '';
365 1463 Luisehahne
366 2053 darkviper
    /**
367
     * DKIM Identity.
368
     * Usually the email address used as the source of the email
369
     * @type string
370
     */
371
    public $DKIM_identity = '';
372 1463 Luisehahne
373 2053 darkviper
    /**
374
     * DKIM passphrase.
375
     * Used if your key is encrypted.
376
     * @type string
377
     */
378
    public $DKIM_passphrase = '';
379 1539 Luisehahne
380 2053 darkviper
    /**
381
     * DKIM signing domain name.
382
     * @example 'example.com'
383
     * @type string
384
     */
385
    public $DKIM_domain = '';
386 1539 Luisehahne
387 2053 darkviper
    /**
388
     * DKIM private key file path.
389
     * @type string
390
     */
391
    public $DKIM_private = '';
392 1463 Luisehahne
393 2053 darkviper
    /**
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:
400
     * - 'function_name' for function names
401
     * - 'Class::Method' for static method calls
402
     * - array($object, 'Method') for calling methods on $object
403
     * See http://php.net/is_callable manual page for more details.
404
     *
405
     * Parameters:
406
     *   bool    $result        result of the send action
407
     *   string  $to            email address of the recipient
408
     *   string  $cc            cc email addresses
409
     *   string  $bcc           bcc email addresses
410
     *   string  $subject       the subject
411
     *   string  $body          the email body
412
     *   string  $from          email address of sender
413
     *
414
     * @type string
415
     */
416
    public $action_function = '';
417 1539 Luisehahne
418 2053 darkviper
    /**
419
     * What to use in the X-Mailer header.
420
     * Options: null for default, whitespace for none, or a string to use
421
     * @type string
422
     */
423
    public $XMailer = '';
424 1463 Luisehahne
425 2053 darkviper
    /**
426
     * An instance of the SMTP sender class.
427
     * @type SMTP
428
     * @access protected
429
     */
430
    protected $smtp = null;
431 1463 Luisehahne
432 2053 darkviper
    /**
433
     * The array of 'to' addresses.
434
     * @type array
435
     * @access protected
436
     */
437
    protected $to = array();
438 1463 Luisehahne
439 2053 darkviper
    /**
440
     * The array of 'cc' addresses.
441
     * @type array
442
     * @access protected
443
     */
444
    protected $cc = array();
445 1463 Luisehahne
446 2053 darkviper
    /**
447
     * The array of 'bcc' addresses.
448
     * @type array
449
     * @access protected
450
     */
451
    protected $bcc = array();
452 1463 Luisehahne
453 2053 darkviper
    /**
454
     * The array of reply-to names and addresses.
455
     * @type array
456
     * @access protected
457
     */
458
    protected $ReplyTo = array();
459 1463 Luisehahne
460 2053 darkviper
    /**
461
     * An array of all kinds of addresses.
462
     * Includes all of $to, $cc, $bcc, $replyto
463
     * @type array
464
     * @access protected
465
     */
466
    protected $all_recipients = array();
467 1463 Luisehahne
468 2053 darkviper
    /**
469
     * The array of attachments.
470
     * @type array
471
     * @access protected
472
     */
473
    protected $attachment = array();
474 1463 Luisehahne
475 2053 darkviper
    /**
476
     * The array of custom headers.
477
     * @type array
478
     * @access protected
479
     */
480
    protected $CustomHeader = array();
481 1463 Luisehahne
482 2053 darkviper
    /**
483
     * The most recent Message-ID (including angular brackets).
484
     * @type string
485
     * @access protected
486
     */
487
    protected $lastMessageID = '';
488 1463 Luisehahne
489 2053 darkviper
    /**
490
     * The message's MIME type.
491
     * @type string
492
     * @access protected
493
     */
494
    protected $message_type = '';
495 1539 Luisehahne
496 2053 darkviper
    /**
497
     * The array of MIME boundary strings.
498
     * @type array
499
     * @access protected
500
     */
501
    protected $boundary = array();
502 1539 Luisehahne
503 2053 darkviper
    /**
504
     * The array of available languages.
505
     * @type array
506
     * @access protected
507
     */
508
    protected $language = array();
509 1539 Luisehahne
510 2053 darkviper
    /**
511
     * The number of errors encountered.
512
     * @type integer
513
     * @access protected
514
     */
515
    protected $error_count = 0;
516 1463 Luisehahne
517 2053 darkviper
    /**
518
     * The S/MIME certificate file path.
519
     * @type string
520
     * @access protected
521
     */
522
    protected $sign_cert_file = '';
523 1550 Luisehahne
524 2053 darkviper
    /**
525
     * The S/MIME key file path.
526
     * @type string
527
     * @access protected
528
     */
529
    protected $sign_key_file = '';
530 1463 Luisehahne
531 2053 darkviper
    /**
532
     * The S/MIME password for the key.
533
     * Used only if the key is encrypted.
534
     * @type string
535
     * @access protected
536
     */
537
    protected $sign_key_pass = '';
538 1463 Luisehahne
539 2053 darkviper
    /**
540
     * Whether to throw exceptions for errors.
541
     * @type bool
542
     * @access protected
543
     */
544
    protected $exceptions = false;
545 1463 Luisehahne
546 2053 darkviper
    /**
547
     * Error severity: message only, continue processing
548
     */
549
    const STOP_MESSAGE = 0;
550 1550 Luisehahne
551 2053 darkviper
    /**
552
     * Error severity: message, likely ok to continue processing
553
     */
554
    const STOP_CONTINUE = 1;
555 1550 Luisehahne
556 2053 darkviper
    /**
557
     * Error severity: message, plus full stop, critical error reached
558
     */
559
    const STOP_CRITICAL = 2;
560 1539 Luisehahne
561 2053 darkviper
    /**
562
     * SMTP RFC standard line ending
563
     */
564
    const CRLF = "\r\n";
565 1853 Luisehahne
566 2053 darkviper
    /**
567
     * Constructor
568
     * @param bool $exceptions Should we throw external exceptions?
569
     */
570
    public function __construct($exceptions = false)
571
    {
572
        $this->exceptions = ($exceptions == true);
573
        //Make sure our autoloader is loaded
574
        if (!in_array('PHPMailerAutoload', spl_autoload_functions())) {
575 2064 darkviper
            $sAutoLoaderFile = __DIR__.DIRECTORY_SEPARATOR.'PHPMailerAutoload.php';
576
            if (!is_readable($sAutoLoaderFile)) {
577
                throw new phpmailerException('Unable to find autoloader: [' . $sAutoLoaderFile . ']');
578
            }
579
            require($sAutoLoaderFile);
580 2053 darkviper
        }
581 1550 Luisehahne
    }
582
583 2053 darkviper
    /**
584
     * Destructor.
585
     */
586
    public function __destruct()
587
    {
588
        if ($this->Mailer == 'smtp') { //close any open SMTP connection nicely
589
            $this->smtpClose();
590
        }
591
    }
592 1539 Luisehahne
593 2053 darkviper
    /**
594
     * Call mail() in a safe_mode-aware fashion.
595
     * Also, unless sendmail_path points to sendmail (or something that
596
     * claims to be sendmail), don't pass params (not a perfect fix,
597
     * but it will do)
598
     * @param string $to To
599
     * @param string $subject Subject
600
     * @param string $body Message Body
601
     * @param string $header Additional Header(s)
602
     * @param string $params Params
603
     * @access private
604
     * @return bool
605
     */
606
    private function mailPassthru($to, $subject, $body, $header, $params)
607
    {
608
        if (ini_get('safe_mode') || !($this->UseSendmailOptions)) {
609
            $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header);
610
        } else {
611
            $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header, $params);
612
        }
613
        return $rt;
614 1463 Luisehahne
    }
615
616 2053 darkviper
    /**
617
     * Output debugging info via user-defined method.
618
     * Only if debug output is enabled.
619
     * @see PHPMailer::$Debugoutput
620
     * @see PHPMailer::$SMTPDebug
621
     * @param string $str
622
     */
623
    protected function edebug($str)
624
    {
625
        if (!$this->SMTPDebug) {
626
            return;
627 1539 Luisehahne
        }
628 2053 darkviper
        switch ($this->Debugoutput) {
629
            case 'error_log':
630
                error_log($str);
631
                break;
632
            case 'html':
633
                //Cleans up output a bit for a better looking display that's HTML-safe
634
                echo htmlentities(preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, $this->CharSet) . "<br>\n";
635
                break;
636
            case 'echo':
637
            default:
638
                //Just echoes exactly what was received
639
                echo $str;
640 1539 Luisehahne
        }
641 1463 Luisehahne
    }
642
643 2053 darkviper
    /**
644
     * Sets message type to HTML or plain.
645
     * @param bool $ishtml True for HTML mode.
646
     * @return void
647
     */
648
    public function isHTML($ishtml = true)
649
    {
650
        if ($ishtml) {
651
            $this->ContentType = 'text/html';
652
        } else {
653
            $this->ContentType = 'text/plain';
654
        }
655 1463 Luisehahne
    }
656
657 2053 darkviper
    /**
658
     * Send messages using SMTP.
659
     * @return void
660
     */
661
    public function isSMTP()
662
    {
663
        $this->Mailer = 'smtp';
664 1550 Luisehahne
    }
665 2053 darkviper
666
    /**
667
     * Send messages using PHP's mail() function.
668
     * @return void
669
     */
670
    public function isMail()
671
    {
672
        $this->Mailer = 'mail';
673
    }
674
675
    /**
676
     * Send messages using $Sendmail.
677
     * @return void
678
     */
679
    public function isSendmail()
680
    {
681
        if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
682
            $this->Sendmail = '/var/qmail/bin/sendmail';
683 1463 Luisehahne
        }
684 2053 darkviper
        $this->Mailer = 'sendmail';
685
    }
686
687
    /**
688
     * Send messages using qmail.
689
     * @return void
690
     */
691
    public function isQmail()
692
    {
693
        if (stristr(ini_get('sendmail_path'), 'qmail')) {
694
            $this->Sendmail = '/var/qmail/bin/sendmail';
695 1463 Luisehahne
        }
696 2053 darkviper
        $this->Mailer = 'sendmail';
697 1463 Luisehahne
    }
698
699 2053 darkviper
    /**
700
     * Add a "To" address.
701
     * @param string $address
702
     * @param string $name
703
     * @return bool true on success, false if address already used
704
     */
705
    public function addAddress($address, $name = '')
706
    {
707
        return $this->addAnAddress('to', $address, $name);
708 1463 Luisehahne
    }
709
710 2053 darkviper
    /**
711
     * Add a "CC" address.
712
     * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
713
     * @param string $address
714
     * @param string $name
715
     * @return bool true on success, false if address already used
716
     */
717
    public function addCC($address, $name = '')
718
    {
719
        return $this->addAnAddress('cc', $address, $name);
720 1463 Luisehahne
    }
721
722 2053 darkviper
    /**
723
     * Add a "BCC" address.
724
     * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
725
     * @param string $address
726
     * @param string $name
727
     * @return bool true on success, false if address already used
728
     */
729
    public function addBCC($address, $name = '')
730
    {
731
        return $this->addAnAddress('bcc', $address, $name);
732 1463 Luisehahne
    }
733
734 2053 darkviper
    /**
735
     * Add a "Reply-to" address.
736
     * @param string $address
737
     * @param string $name
738
     * @return bool
739
     */
740
    public function addReplyTo($address, $name = '')
741
    {
742
        return $this->addAnAddress('Reply-To', $address, $name);
743 1463 Luisehahne
    }
744
745 2053 darkviper
    /**
746
     * Add an address to one of the recipient arrays.
747
     * Addresses that have been added already return false, but do not throw exceptions
748
     * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
749
     * @param string $address The email address to send to
750
     * @param string $name
751
     * @throws phpmailerException
752
     * @return bool true on success, false if address already used or invalid in some way
753
     * @access protected
754
     */
755
    protected function addAnAddress($kind, $address, $name = '')
756
    {
757
        if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
758
            $this->setError($this->lang('Invalid recipient array') . ': ' . $kind);
759
            if ($this->exceptions) {
760
                throw new phpmailerException('Invalid recipient array: ' . $kind);
761
            }
762
            $this->edebug($this->lang('Invalid recipient array') . ': ' . $kind);
763
            return false;
764
        }
765
        $address = trim($address);
766
        $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
767
        if (!$this->validateAddress($address)) {
768
            $this->setError($this->lang('invalid_address') . ': ' . $address);
769
            if ($this->exceptions) {
770
                throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
771
            }
772
            $this->edebug($this->lang('invalid_address') . ': ' . $address);
773
            return false;
774
        }
775
        if ($kind != 'Reply-To') {
776
            if (!isset($this->all_recipients[strtolower($address)])) {
777
                array_push($this->$kind, array($address, $name));
778
                $this->all_recipients[strtolower($address)] = true;
779
                return true;
780
            }
781 1463 Luisehahne
        } else {
782 2053 darkviper
            if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
783
                $this->ReplyTo[strtolower($address)] = array($address, $name);
784
                return true;
785
            }
786 1463 Luisehahne
        }
787 2053 darkviper
        return false;
788
    }
789 1463 Luisehahne
790 2053 darkviper
    /**
791
     * Set the From and FromName properties.
792
     * @param string $address
793
     * @param string $name
794
     * @param bool $auto Whether to also set the Sender address, defaults to true
795
     * @throws phpmailerException
796
     * @return bool
797
     */
798
    public function setFrom($address, $name = '', $auto = true)
799
    {
800
        $address = trim($address);
801
        $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
802
        if (!$this->validateAddress($address)) {
803
            $this->setError($this->lang('invalid_address') . ': ' . $address);
804
            if ($this->exceptions) {
805
                throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
806 1539 Luisehahne
            }
807 2053 darkviper
            $this->edebug($this->lang('invalid_address') . ': ' . $address);
808
            return false;
809
        }
810
        $this->From = $address;
811
        $this->FromName = $name;
812
        if ($auto) {
813
            if (empty($this->Sender)) {
814
                $this->Sender = $address;
815
            }
816
        }
817
        return true;
818
    }
819 1539 Luisehahne
820 2053 darkviper
    /**
821
     * Return the Message-ID header of the last email.
822
     * Technically this is the value from the last time the headers were created,
823
     * but it's also the message ID of the last sent message except in
824
     * pathological cases.
825
     * @return string
826
     */
827
    public function getLastMessageID()
828
    {
829
        return $this->lastMessageID;
830
    }
831 1539 Luisehahne
832 2053 darkviper
    /**
833
     * Check that a string looks like an email address.
834
     * @param string $address The email address to check
835
     * @param string $patternselect A selector for the validation pattern to use :
836
     *   'auto' - pick best one automatically;
837
     *   'pcre8' - use the squiloople.com pattern, requires PCRE > 8.0, PHP >= 5.3.2, 5.2.14;
838
     *   'pcre' - use old PCRE implementation;
839
     *   'php' - use PHP built-in FILTER_VALIDATE_EMAIL; faster, less thorough;
840
     *   'noregex' - super fast, really dumb.
841
     * @return bool
842
     * @static
843
     * @access public
844
     */
845
    public static function validateAddress($address, $patternselect = 'auto')
846
    {
847
        if ($patternselect == 'auto') {
848
            if (defined(
849
                'PCRE_VERSION'
850
            )
851
            ) { //Check this instead of extension_loaded so it works when that function is disabled
852
                if (version_compare(PCRE_VERSION, '8.0') >= 0) {
853
                    $patternselect = 'pcre8';
854
                } else {
855
                    $patternselect = 'pcre';
856
                }
857
            } else {
858
                //Filter_var appeared in PHP 5.2.0 and does not require the PCRE extension
859
                if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
860
                    $patternselect = 'php';
861
                } else {
862
                    $patternselect = 'noregex';
863
                }
864 1539 Luisehahne
            }
865 1463 Luisehahne
        }
866 2053 darkviper
        switch ($patternselect) {
867
            case 'pcre8':
868
                /**
869
                 * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
870
                 * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
871
                 * not allow a@b type valid addresses :(
872
                 * @link http://squiloople.com/2009/12/20/email-address-validation/
873
                 * @copyright 2009-2010 Michael Rushton
874
                 * Feel free to use and redistribute this code. But please keep this copyright notice.
875
                 */
876
                return (bool)preg_match(
877
                    '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
878
                    '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
879
                    '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
880
                    '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
881
                    '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
882
                    '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
883
                    '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
884
                    '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
885
                    '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
886
                    $address
887
                );
888
                break;
889
            case 'pcre':
890
                //An older regex that doesn't need a recent PCRE
891
                return (bool)preg_match(
892
                    '/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>' .
893
                    '[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")' .
894
                    '(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*' .
895
                    '@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})' .
896
                    '(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:' .
897
                    '[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?' .
898
                    '::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:' .
899
                    '[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?' .
900
                    '::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
901
                    '|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD',
902
                    $address
903
                );
904
                break;
905
            case 'php':
906
            default:
907
                return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
908
                break;
909
            case 'noregex':
910
                //No PCRE! Do something _very_ approximate!
911
                //Check the address is 3 chars or longer and contains an @ that's not the first or last char
912
                return (strlen($address) >= 3
913
                    and strpos($address, '@') >= 1
914
                    and strpos($address, '@') != strlen($address) - 1);
915
                break;
916 1539 Luisehahne
        }
917 1463 Luisehahne
    }
918
919 2053 darkviper
    /**
920
     * Create a message and send it.
921
     * Uses the sending method specified by $Mailer.
922
     * Returns false on error - Use the ErrorInfo variable to view description of the error.
923
     * @throws phpmailerException
924
     * @return bool
925
     */
926
    public function send()
927
    {
928
        try {
929
            if (!$this->preSend()) {
930
                return false;
931
            }
932
            return $this->postSend();
933
        } catch (phpmailerException $e) {
934
            $this->mailHeader = '';
935
            $this->setError($e->getMessage());
936
            if ($this->exceptions) {
937
                throw $e;
938
            }
939
            return false;
940
        }
941 1463 Luisehahne
    }
942
943 2053 darkviper
    /**
944
     * Prepare a message for sending.
945
     * @throws phpmailerException
946
     * @return bool
947
     */
948
    public function preSend()
949
    {
950
        try {
951
            $this->mailHeader = "";
952
            if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
953
                throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL);
954
            }
955 1463 Luisehahne
956 2053 darkviper
            // Set whether the message is multipart/alternative
957
            if (!empty($this->AltBody)) {
958
                $this->ContentType = 'multipart/alternative';
959
            }
960 1463 Luisehahne
961 2053 darkviper
            $this->error_count = 0; // reset errors
962
            $this->setMessageType();
963
            // Refuse to send an empty message unless we are specifically allowing it
964
            if (!$this->AllowEmpty and empty($this->Body)) {
965
                throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL);
966
            }
967 1463 Luisehahne
968 2053 darkviper
            $this->MIMEHeader = $this->createHeader();
969
            $this->MIMEBody = $this->createBody();
970 1463 Luisehahne
971 2053 darkviper
            // To capture the complete message when using mail(), create
972
            // an extra header list which createHeader() doesn't fold in
973
            if ($this->Mailer == 'mail') {
974
                if (count($this->to) > 0) {
975
                    $this->mailHeader .= $this->addrAppend("To", $this->to);
976
                } else {
977
                    $this->mailHeader .= $this->headerLine("To", "undisclosed-recipients:;");
978
                }
979
                $this->mailHeader .= $this->headerLine(
980
                    'Subject',
981
                    $this->encodeHeader($this->secureHeader(trim($this->Subject)))
982
                );
983
            }
984 1463 Luisehahne
985 2053 darkviper
            // Sign with DKIM if enabled
986
            if (!empty($this->DKIM_domain)
987
                && !empty($this->DKIM_private)
988
                && !empty($this->DKIM_selector)
989
                && !empty($this->DKIM_domain)
990
                && file_exists($this->DKIM_private)) {
991
                $header_dkim = $this->DKIM_Add(
992
                    $this->MIMEHeader . $this->mailHeader,
993
                    $this->encodeHeader($this->secureHeader($this->Subject)),
994
                    $this->MIMEBody
995
                );
996
                $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . self::CRLF .
997
                    str_replace("\r\n", "\n", $header_dkim) . self::CRLF;
998
            }
999
            return true;
1000 1463 Luisehahne
1001 2053 darkviper
        } catch (phpmailerException $e) {
1002
            $this->setError($e->getMessage());
1003
            if ($this->exceptions) {
1004
                throw $e;
1005
            }
1006
            return false;
1007
        }
1008 1463 Luisehahne
    }
1009
1010 2053 darkviper
    /**
1011
     * Actually send a message.
1012
     * Send the email via the selected mechanism
1013
     * @throws phpmailerException
1014
     * @return bool
1015
     */
1016
    public function postSend()
1017
    {
1018
        try {
1019
            // Choose the mailer and send through it
1020
            switch ($this->Mailer) {
1021
                case 'sendmail':
1022
                    return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody);
1023
                case 'smtp':
1024
                    return $this->smtpSend($this->MIMEHeader, $this->MIMEBody);
1025
                case 'mail':
1026
                    return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
1027
                default:
1028
                    return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
1029 1463 Luisehahne
            }
1030 2053 darkviper
        } catch (phpmailerException $e) {
1031
            $this->setError($e->getMessage());
1032
            if ($this->exceptions) {
1033
                throw $e;
1034 1463 Luisehahne
            }
1035 2053 darkviper
            $this->edebug($e->getMessage() . "\n");
1036
        }
1037
        return false;
1038
    }
1039 1463 Luisehahne
1040 2053 darkviper
    /**
1041
     * Send mail using the $Sendmail program.
1042
     * @param string $header The message headers
1043
     * @param string $body The message body
1044
     * @see PHPMailer::$Sendmail
1045
     * @throws phpmailerException
1046
     * @access protected
1047
     * @return bool
1048
     */
1049
    protected function sendmailSend($header, $body)
1050
    {
1051
        if ($this->Sender != '') {
1052
            $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
1053
        } else {
1054
            $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
1055
        }
1056
        if ($this->SingleTo === true) {
1057
            foreach ($this->SingleToArray as $val) {
1058
                if (!@$mail = popen($sendmail, 'w')) {
1059
                    throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1060
                }
1061
                fputs($mail, "To: " . $val . "\n");
1062
                fputs($mail, $header);
1063
                fputs($mail, $body);
1064
                $result = pclose($mail);
1065
                // implement call back function if it exists
1066
                $isSent = ($result == 0) ? 1 : 0;
1067
                $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1068
                if ($result != 0) {
1069
                    throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1070
                }
1071 1463 Luisehahne
            }
1072
        } else {
1073 2053 darkviper
            if (!@$mail = popen($sendmail, 'w')) {
1074
                throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1075
            }
1076
            fputs($mail, $header);
1077
            fputs($mail, $body);
1078
            $result = pclose($mail);
1079
            // implement call back function if it exists
1080
            $isSent = ($result == 0) ? 1 : 0;
1081
            $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1082
            if ($result != 0) {
1083
                throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
1084
            }
1085 1463 Luisehahne
        }
1086 2053 darkviper
        return true;
1087 1463 Luisehahne
    }
1088
1089 2053 darkviper
    /**
1090
     * Send mail using the PHP mail() function.
1091
     * @param string $header The message headers
1092
     * @param string $body The message body
1093
     * @link http://www.php.net/manual/en/book.mail.php
1094
     * @throws phpmailerException
1095
     * @access protected
1096
     * @return bool
1097
     */
1098
    protected function mailSend($header, $body)
1099
    {
1100
        $toArr = array();
1101
        foreach ($this->to as $t) {
1102
            $toArr[] = $this->addrFormat($t);
1103
        }
1104
        $to = implode(', ', $toArr);
1105 1463 Luisehahne
1106 2053 darkviper
        if (empty($this->Sender)) {
1107
            $params = " ";
1108
        } else {
1109
            $params = sprintf("-f%s", $this->Sender);
1110 1463 Luisehahne
        }
1111 2053 darkviper
        if ($this->Sender != '' and !ini_get('safe_mode')) {
1112
            $old_from = ini_get('sendmail_from');
1113
            ini_set('sendmail_from', $this->Sender);
1114
        }
1115
        $rt = false;
1116
        if ($this->SingleTo === true && count($toArr) > 1) {
1117
            foreach ($toArr as $val) {
1118
                $rt = $this->mailPassthru($val, $this->Subject, $body, $header, $params);
1119
                // implement call back function if it exists
1120
                $isSent = ($rt == 1) ? 1 : 0;
1121
                $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1122
            }
1123
        } else {
1124
            $rt = $this->mailPassthru($to, $this->Subject, $body, $header, $params);
1125
            // implement call back function if it exists
1126
            $isSent = ($rt == 1) ? 1 : 0;
1127
            $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
1128
        }
1129
        if (isset($old_from)) {
1130
            ini_set('sendmail_from', $old_from);
1131
        }
1132
        if (!$rt) {
1133
            throw new phpmailerException($this->lang('instantiate'), self::STOP_CRITICAL);
1134
        }
1135
        return true;
1136 1463 Luisehahne
    }
1137
1138 2053 darkviper
    /**
1139
     * Get an instance to use for SMTP operations.
1140
     * Override this function to load your own SMTP implementation
1141
     * @return SMTP
1142
     */
1143
    public function getSMTPInstance()
1144
    {
1145
        if (!is_object($this->smtp)) {
1146
            $this->smtp = new SMTP;
1147
        }
1148
        return $this->smtp;
1149 1463 Luisehahne
    }
1150
1151 2053 darkviper
    /**
1152
     * Send mail via SMTP.
1153
     * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
1154
     * Uses the PHPMailerSMTP class by default.
1155
     * @see PHPMailer::getSMTPInstance() to use a different class.
1156
     * @param string $header The message headers
1157
     * @param string $body The message body
1158
     * @throws phpmailerException
1159
     * @uses SMTP
1160
     * @access protected
1161
     * @return bool
1162
     */
1163
    protected function smtpSend($header, $body)
1164
    {
1165
        $bad_rcpt = array();
1166 1463 Luisehahne
1167 2053 darkviper
        if (!$this->smtpConnect()) {
1168
            throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
1169
        }
1170
        $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
1171
        if (!$this->smtp->mail($smtp_from)) {
1172
            $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError()));
1173
            throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
1174
        }
1175 1463 Luisehahne
1176 2053 darkviper
        // Attempt to send attach all recipients
1177
        foreach ($this->to as $to) {
1178
            if (!$this->smtp->recipient($to[0])) {
1179
                $bad_rcpt[] = $to[0];
1180
                $isSent = 0;
1181
            } else {
1182
                $isSent = 1;
1183
            }
1184
            $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body, $this->From);
1185
        }
1186
        foreach ($this->cc as $cc) {
1187
            if (!$this->smtp->recipient($cc[0])) {
1188
                $bad_rcpt[] = $cc[0];
1189
                $isSent = 0;
1190
            } else {
1191
                $isSent = 1;
1192
            }
1193
            $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body, $this->From);
1194
        }
1195
        foreach ($this->bcc as $bcc) {
1196
            if (!$this->smtp->recipient($bcc[0])) {
1197
                $bad_rcpt[] = $bcc[0];
1198
                $isSent = 0;
1199
            } else {
1200
                $isSent = 1;
1201
            }
1202
            $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body, $this->From);
1203
        }
1204 1463 Luisehahne
1205 2053 darkviper
        if (count($bad_rcpt) > 0) { //Create error message for any bad addresses
1206
            throw new phpmailerException($this->lang('recipients_failed') . implode(', ', $bad_rcpt));
1207
        }
1208
        if (!$this->smtp->data($header . $body)) {
1209
            throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL);
1210
        }
1211
        if ($this->SMTPKeepAlive == true) {
1212
            $this->smtp->reset();
1213
        } else {
1214
            $this->smtp->quit();
1215
            $this->smtp->close();
1216
        }
1217
        return true;
1218 1463 Luisehahne
    }
1219
1220 2053 darkviper
    /**
1221
     * Initiate a connection to an SMTP server.
1222
     * Returns false if the operation failed.
1223
     * @param array $options An array of options compatible with stream_context_create()
1224
     * @uses SMTP
1225
     * @access public
1226
     * @throws phpmailerException
1227
     * @return bool
1228
     */
1229
    public function smtpConnect($options = array())
1230
    {
1231
        if (is_null($this->smtp)) {
1232
            $this->smtp = $this->getSMTPInstance();
1233 1539 Luisehahne
        }
1234 2053 darkviper
1235
        //Already connected?
1236
        if ($this->smtp->connected()) {
1237
            return true;
1238 1539 Luisehahne
        }
1239 1463 Luisehahne
1240 2053 darkviper
        $this->smtp->setTimeout($this->Timeout);
1241
        $this->smtp->setDebugLevel($this->SMTPDebug);
1242
        $this->smtp->setDebugOutput($this->Debugoutput);
1243
        $this->smtp->setVerp($this->do_verp);
1244
        $tls = ($this->SMTPSecure == 'tls');
1245
        $ssl = ($this->SMTPSecure == 'ssl');
1246
        $hosts = explode(';', $this->Host);
1247
        $lastexception = null;
1248 1463 Luisehahne
1249 2053 darkviper
        foreach ($hosts as $hostentry) {
1250
            $hostinfo = array();
1251
            $host = $hostentry;
1252
            $port = $this->Port;
1253
            if (preg_match(
1254
                '/^(.+):([0-9]+)$/',
1255
                $hostentry,
1256
                $hostinfo
1257
            )
1258
            ) { //If $hostentry contains 'address:port', override default
1259
                $host = $hostinfo[1];
1260
                $port = $hostinfo[2];
1261
            }
1262
            if ($this->smtp->connect(($ssl ? 'ssl://' : '') . $host, $port, $this->Timeout, $options)) {
1263
                try {
1264
                    if ($this->Helo) {
1265
                        $hello = $this->Helo;
1266
                    } else {
1267
                        $hello = $this->serverHostname();
1268
                    }
1269
                    $this->smtp->hello($hello);
1270 1463 Luisehahne
1271 2053 darkviper
                    if ($tls) {
1272
                        if (!$this->smtp->startTLS()) {
1273
                            throw new phpmailerException($this->lang('connect_host'));
1274
                        }
1275
                        //We must resend HELO after tls negotiation
1276
                        $this->smtp->hello($hello);
1277
                    }
1278
                    if ($this->SMTPAuth) {
1279
                        if (!$this->smtp->authenticate(
1280
                            $this->Username,
1281
                            $this->Password,
1282
                            $this->AuthType,
1283
                            $this->Realm,
1284
                            $this->Workstation
1285
                        )
1286
                        ) {
1287
                            throw new phpmailerException($this->lang('authenticate'));
1288
                        }
1289
                    }
1290
                    return true;
1291
                } catch (phpmailerException $e) {
1292
                    $lastexception = $e;
1293
                    //We must have connected, but then failed TLS or Auth, so close connection nicely
1294
                    $this->smtp->quit();
1295
                }
1296
            }
1297
        }
1298
        //If we get here, all connection attempts have failed, so close connection hard
1299
        $this->smtp->close();
1300
        //As we've caught all exceptions, just report whatever the last one was
1301
        if ($this->exceptions and !is_null($lastexception)) {
1302
            throw $lastexception;
1303
        }
1304
        return false;
1305 1463 Luisehahne
    }
1306
1307 2053 darkviper
    /**
1308
     * Close the active SMTP session if one exists.
1309
     * @return void
1310
     */
1311
    public function smtpClose()
1312
    {
1313
        if ($this->smtp !== null) {
1314
            if ($this->smtp->connected()) {
1315
                $this->smtp->quit();
1316
                $this->smtp->close();
1317
            }
1318
        }
1319 1463 Luisehahne
    }
1320
1321 2053 darkviper
    /**
1322
     * Set the language for error messages.
1323
     * Returns false if it cannot load the language file.
1324
     * The default language is English.
1325
     * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr")
1326
     * @param string $lang_path Path to the language file directory, with trailing separator (slash)
1327
     * @return bool
1328
     * @access public
1329
     */
1330
    public function setLanguage($langcode = 'en', $lang_path = 'language/')
1331
    {
1332
        //Define full set of translatable strings
1333
        $PHPMAILER_LANG = array(
1334
            'authenticate' => 'SMTP Error: Could not authenticate.',
1335
            'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
1336
            'data_not_accepted' => 'SMTP Error: data not accepted.',
1337
            'empty_message' => 'Message body empty',
1338
            'encoding' => 'Unknown encoding: ',
1339
            'execute' => 'Could not execute: ',
1340
            'file_access' => 'Could not access file: ',
1341
            'file_open' => 'File Error: Could not open file: ',
1342
            'from_failed' => 'The following From address failed: ',
1343
            'instantiate' => 'Could not instantiate mail function.',
1344
            'invalid_address' => 'Invalid address',
1345
            'mailer_not_supported' => ' mailer is not supported.',
1346
            'provide_address' => 'You must provide at least one recipient email address.',
1347
            'recipients_failed' => 'SMTP Error: The following recipients failed: ',
1348
            'signing' => 'Signing Error: ',
1349
            'smtp_connect_failed' => 'SMTP connect() failed.',
1350
            'smtp_error' => 'SMTP server error: ',
1351
            'variable_set' => 'Cannot set or reset variable: '
1352
        );
1353
        //Overwrite language-specific strings.
1354
        //This way we'll never have missing translations - no more "language string failed to load"!
1355
        $l = true;
1356
        if ($langcode != 'en') { //There is no English translation file
1357
            $l = @include $lang_path . 'phpmailer.lang-' . $langcode . '.php';
1358
        }
1359
        $this->language = $PHPMAILER_LANG;
1360
        return ($l == true); //Returns false if language not found
1361 1463 Luisehahne
    }
1362
1363 2053 darkviper
    /**
1364
     * Get the array of strings for the current language.
1365
     * @return array
1366
     */
1367
    public function getTranslations()
1368
    {
1369
        return $this->language;
1370 1463 Luisehahne
    }
1371
1372 2053 darkviper
    /**
1373
     * Create recipient headers.
1374
     * @access public
1375
     * @param string $type
1376
     * @param array $addr An array of recipient,
1377
     * where each recipient is a 2-element indexed array with element 0 containing an address
1378
     * and element 1 containing a name, like:
1379
     * array(array('joe@example.com', 'Joe User'), array('zoe@example.com', 'Zoe User'))
1380
     * @return string
1381
     */
1382
    public function addrAppend($type, $addr)
1383
    {
1384
        $addresses = array();
1385
        foreach ($addr as $a) {
1386
            $addresses[] = $this->addrFormat($a);
1387
        }
1388
        return $type . ': ' . implode(', ', $addresses) . $this->LE;
1389 1463 Luisehahne
    }
1390
1391 2053 darkviper
    /**
1392
     * Format an address for use in a message header.
1393
     * @access public
1394
     * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name
1395
     *      like array('joe@example.com', 'Joe User')
1396
     * @return string
1397
     */
1398
    public function addrFormat($addr)
1399
    {
1400
        if (empty($addr[1])) { // No name provided
1401
            return $this->secureHeader($addr[0]);
1402
        } else {
1403
            return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . " <" . $this->secureHeader(
1404
                $addr[0]
1405
            ) . ">";
1406
        }
1407 1463 Luisehahne
    }
1408
1409 2053 darkviper
    /**
1410
     * Word-wrap message.
1411
     * For use with mailers that do not automatically perform wrapping
1412
     * and for quoted-printable encoded messages.
1413
     * Original written by philippe.
1414
     * @param string $message The message to wrap
1415
     * @param integer $length The line length to wrap to
1416
     * @param bool $qp_mode Whether to run in Quoted-Printable mode
1417
     * @access public
1418
     * @return string
1419
     */
1420
    public function wrapText($message, $length, $qp_mode = false)
1421
    {
1422
        $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
1423
        // If utf-8 encoding is used, we will need to make sure we don't
1424
        // split multibyte characters when we wrap
1425
        $is_utf8 = (strtolower($this->CharSet) == "utf-8");
1426
        $lelen = strlen($this->LE);
1427
        $crlflen = strlen(self::CRLF);
1428 1463 Luisehahne
1429 2053 darkviper
        $message = $this->fixEOL($message);
1430
        if (substr($message, -$lelen) == $this->LE) {
1431
            $message = substr($message, 0, -$lelen);
1432
        }
1433 1463 Luisehahne
1434 2053 darkviper
        $line = explode($this->LE, $message); // Magic. We know fixEOL uses $LE
1435
        $message = '';
1436
        for ($i = 0; $i < count($line); $i++) {
1437
            $line_part = explode(' ', $line[$i]);
1438
            $buf = '';
1439
            for ($e = 0; $e < count($line_part); $e++) {
1440
                $word = $line_part[$e];
1441
                if ($qp_mode and (strlen($word) > $length)) {
1442
                    $space_left = $length - strlen($buf) - $crlflen;
1443
                    if ($e != 0) {
1444
                        if ($space_left > 20) {
1445
                            $len = $space_left;
1446
                            if ($is_utf8) {
1447
                                $len = $this->utf8CharBoundary($word, $len);
1448
                            } elseif (substr($word, $len - 1, 1) == "=") {
1449
                                $len--;
1450
                            } elseif (substr($word, $len - 2, 1) == "=") {
1451
                                $len -= 2;
1452
                            }
1453
                            $part = substr($word, 0, $len);
1454
                            $word = substr($word, $len);
1455
                            $buf .= ' ' . $part;
1456
                            $message .= $buf . sprintf("=%s", self::CRLF);
1457
                        } else {
1458
                            $message .= $buf . $soft_break;
1459
                        }
1460
                        $buf = '';
1461
                    }
1462
                    while (strlen($word) > 0) {
1463
                        if ($length <= 0) {
1464
                            break;
1465
                        }
1466
                        $len = $length;
1467
                        if ($is_utf8) {
1468
                            $len = $this->utf8CharBoundary($word, $len);
1469
                        } elseif (substr($word, $len - 1, 1) == "=") {
1470
                            $len--;
1471
                        } elseif (substr($word, $len - 2, 1) == "=") {
1472
                            $len -= 2;
1473
                        }
1474
                        $part = substr($word, 0, $len);
1475
                        $word = substr($word, $len);
1476 1463 Luisehahne
1477 2053 darkviper
                        if (strlen($word) > 0) {
1478
                            $message .= $part . sprintf("=%s", self::CRLF);
1479
                        } else {
1480
                            $buf = $part;
1481
                        }
1482
                    }
1483
                } else {
1484
                    $buf_o = $buf;
1485
                    $buf .= ($e == 0) ? $word : (' ' . $word);
1486 1463 Luisehahne
1487 2053 darkviper
                    if (strlen($buf) > $length and $buf_o != '') {
1488
                        $message .= $buf_o . $soft_break;
1489
                        $buf = $word;
1490
                    }
1491
                }
1492
            }
1493
            $message .= $buf . self::CRLF;
1494
        }
1495 1853 Luisehahne
1496 2053 darkviper
        return $message;
1497
    }
1498 1853 Luisehahne
1499 2053 darkviper
    /**
1500
     * Find the last character boundary prior to $maxLength in a utf-8
1501
     * quoted (printable) encoded string.
1502
     * Original written by Colin Brown.
1503
     * @access public
1504
     * @param string $encodedText utf-8 QP text
1505
     * @param int $maxLength   find last character boundary prior to this length
1506
     * @return int
1507
     */
1508
    public function utf8CharBoundary($encodedText, $maxLength)
1509
    {
1510
        $foundSplitPos = false;
1511
        $lookBack = 3;
1512
        while (!$foundSplitPos) {
1513
            $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
1514
            $encodedCharPos = strpos($lastChunk, "=");
1515
            if ($encodedCharPos !== false) {
1516
                // Found start of encoded character byte within $lookBack block.
1517
                // Check the encoded byte value (the 2 chars after the '=')
1518
                $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
1519
                $dec = hexdec($hex);
1520
                if ($dec < 128) { // Single byte character.
1521
                    // If the encoded char was found at pos 0, it will fit
1522
                    // otherwise reduce maxLength to start of the encoded char
1523
                    $maxLength = ($encodedCharPos == 0) ? $maxLength :
1524
                        $maxLength - ($lookBack - $encodedCharPos);
1525
                    $foundSplitPos = true;
1526
                } elseif ($dec >= 192) { // First byte of a multi byte character
1527
                    // Reduce maxLength to split at start of character
1528
                    $maxLength = $maxLength - ($lookBack - $encodedCharPos);
1529
                    $foundSplitPos = true;
1530
                } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
1531
                    $lookBack += 3;
1532
                }
1533
            } else {
1534
                // No encoded character found
1535
                $foundSplitPos = true;
1536
            }
1537
        }
1538
        return $maxLength;
1539 1463 Luisehahne
    }
1540
1541
1542 2053 darkviper
    /**
1543
     * Set the body wrapping.
1544
     * @access public
1545
     * @return void
1546
     */
1547
    public function setWordWrap()
1548
    {
1549
        if ($this->WordWrap < 1) {
1550
            return;
1551
        }
1552
1553
        switch ($this->message_type) {
1554
            case 'alt':
1555
            case 'alt_inline':
1556
            case 'alt_attach':
1557
            case 'alt_inline_attach':
1558
                $this->AltBody = $this->wrapText($this->AltBody, $this->WordWrap);
1559
                break;
1560
            default:
1561
                $this->Body = $this->wrapText($this->Body, $this->WordWrap);
1562
                break;
1563
        }
1564 1463 Luisehahne
    }
1565
1566 2053 darkviper
    /**
1567
     * Assemble message headers.
1568
     * @access public
1569
     * @return string The assembled headers
1570
     */
1571
    public function createHeader()
1572
    {
1573
        $result = '';
1574
1575
        // Set the boundaries
1576
        $uniq_id = md5(uniqid(time()));
1577
        $this->boundary[1] = 'b1_' . $uniq_id;
1578
        $this->boundary[2] = 'b2_' . $uniq_id;
1579
        $this->boundary[3] = 'b3_' . $uniq_id;
1580
1581
        if ($this->MessageDate == '') {
1582
            $result .= $this->headerLine('Date', self::rfcDate());
1583 1539 Luisehahne
        } else {
1584 2053 darkviper
            $result .= $this->headerLine('Date', $this->MessageDate);
1585 1463 Luisehahne
        }
1586 2053 darkviper
1587
        if ($this->ReturnPath) {
1588
            $result .= $this->headerLine('Return-Path', '<' . trim($this->ReturnPath) . '>');
1589
        } elseif ($this->Sender == '') {
1590
            $result .= $this->headerLine('Return-Path', '<' . trim($this->From) . '>');
1591
        } else {
1592
            $result .= $this->headerLine('Return-Path', '<' . trim($this->Sender) . '>');
1593 1539 Luisehahne
        }
1594 1463 Luisehahne
1595 2053 darkviper
        // To be created automatically by mail()
1596
        if ($this->Mailer != 'mail') {
1597
            if ($this->SingleTo === true) {
1598
                foreach ($this->to as $t) {
1599
                    $this->SingleToArray[] = $this->addrFormat($t);
1600
                }
1601
            } else {
1602
                if (count($this->to) > 0) {
1603
                    $result .= $this->addrAppend('To', $this->to);
1604
                } elseif (count($this->cc) == 0) {
1605
                    $result .= $this->headerLine('To', 'undisclosed-recipients:;');
1606
                }
1607
            }
1608
        }
1609 1463 Luisehahne
1610 2053 darkviper
        $result .= $this->addrAppend('From', array(array(trim($this->From), $this->FromName)));
1611 1463 Luisehahne
1612 2053 darkviper
        // sendmail and mail() extract Cc from the header before sending
1613
        if (count($this->cc) > 0) {
1614
            $result .= $this->addrAppend('Cc', $this->cc);
1615
        }
1616 1463 Luisehahne
1617 2053 darkviper
        // sendmail and mail() extract Bcc from the header before sending
1618
        if ((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
1619
            $result .= $this->addrAppend('Bcc', $this->bcc);
1620
        }
1621 1463 Luisehahne
1622 2053 darkviper
        if (count($this->ReplyTo) > 0) {
1623
            $result .= $this->addrAppend('Reply-To', $this->ReplyTo);
1624
        }
1625 1463 Luisehahne
1626 2053 darkviper
        // mail() sets the subject itself
1627
        if ($this->Mailer != 'mail') {
1628
            $result .= $this->headerLine('Subject', $this->encodeHeader($this->secureHeader($this->Subject)));
1629
        }
1630 1463 Luisehahne
1631 2053 darkviper
        if ($this->MessageID != '') {
1632
            $this->lastMessageID = $this->MessageID;
1633
        } else {
1634
            $this->lastMessageID = sprintf("<%s@%s>", $uniq_id, $this->ServerHostname());
1635
        }
1636
        $result .= $this->HeaderLine('Message-ID', $this->lastMessageID);
1637
        $result .= $this->headerLine('X-Priority', $this->Priority);
1638
        if ($this->XMailer == '') {
1639
            $result .= $this->headerLine(
1640
                'X-Mailer',
1641
                'PHPMailer ' . $this->Version . ' (https://github.com/PHPMailer/PHPMailer/)'
1642
            );
1643
        } else {
1644
            $myXmailer = trim($this->XMailer);
1645
            if ($myXmailer) {
1646
                $result .= $this->headerLine('X-Mailer', $myXmailer);
1647
            }
1648
        }
1649 1463 Luisehahne
1650 2053 darkviper
        if ($this->ConfirmReadingTo != '') {
1651
            $result .= $this->headerLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
1652
        }
1653 1463 Luisehahne
1654 2053 darkviper
        // Add custom headers
1655
        for ($index = 0; $index < count($this->CustomHeader); $index++) {
1656
            $result .= $this->headerLine(
1657
                trim($this->CustomHeader[$index][0]),
1658
                $this->encodeHeader(trim($this->CustomHeader[$index][1]))
1659
            );
1660
        }
1661
        if (!$this->sign_key_file) {
1662
            $result .= $this->headerLine('MIME-Version', '1.0');
1663
            $result .= $this->getMailMIME();
1664
        }
1665 1463 Luisehahne
1666 2053 darkviper
        return $result;
1667 1463 Luisehahne
    }
1668
1669 2053 darkviper
    /**
1670
     * Get the message MIME type headers.
1671
     * @access public
1672
     * @return string
1673
     */
1674
    public function getMailMIME()
1675
    {
1676
        $result = '';
1677
        switch ($this->message_type) {
1678
            case 'inline':
1679
                $result .= $this->headerLine('Content-Type', 'multipart/related;');
1680
                $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
1681
                break;
1682
            case 'attach':
1683
            case 'inline_attach':
1684
            case 'alt_attach':
1685
            case 'alt_inline_attach':
1686
                $result .= $this->headerLine('Content-Type', 'multipart/mixed;');
1687
                $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
1688
                break;
1689
            case 'alt':
1690
            case 'alt_inline':
1691
                $result .= $this->headerLine('Content-Type', 'multipart/alternative;');
1692
                $result .= $this->textLine("\tboundary=\"" . $this->boundary[1] . '"');
1693
                break;
1694
            default:
1695
                // Catches case 'plain': and case '':
1696
                $result .= $this->textLine('Content-Type: ' . $this->ContentType . '; charset=' . $this->CharSet);
1697
                break;
1698
        }
1699
        //RFC1341 part 5 says 7bit is assumed if not specified
1700
        if ($this->Encoding != '7bit') {
1701
            $result .= $this->headerLine('Content-Transfer-Encoding', $this->Encoding);
1702
        }
1703 1539 Luisehahne
1704 2053 darkviper
        if ($this->Mailer != 'mail') {
1705
            $result .= $this->LE;
1706 1550 Luisehahne
        }
1707 1463 Luisehahne
1708 2053 darkviper
        return $result;
1709
    }
1710 1463 Luisehahne
1711 2053 darkviper
    /**
1712
     * Returns the whole MIME message.
1713
     * Includes complete headers and body.
1714
     * Only valid post PreSend().
1715
     * @see PHPMailer::PreSend()
1716
     * @access public
1717
     * @return string
1718
     */
1719
    public function getSentMIMEMessage()
1720
    {
1721
        return $this->MIMEHeader . $this->mailHeader . self::CRLF . $this->MIMEBody;
1722
    }
1723 1463 Luisehahne
1724
1725 2053 darkviper
    /**
1726
     * Assemble the message body.
1727
     * Returns an empty string on failure.
1728
     * @access public
1729
     * @throws phpmailerException
1730
     * @return string The assembled message body
1731
     */
1732
    public function createBody()
1733
    {
1734
        $body = '';
1735 1463 Luisehahne
1736 2053 darkviper
        if ($this->sign_key_file) {
1737
            $body .= $this->getMailMIME() . $this->LE;
1738 1463 Luisehahne
        }
1739
1740 2053 darkviper
        $this->setWordWrap();
1741 1463 Luisehahne
1742 2053 darkviper
        switch ($this->message_type) {
1743
            case 'inline':
1744
                $body .= $this->getBoundary($this->boundary[1], '', '', '');
1745
                $body .= $this->encodeString($this->Body, $this->Encoding);
1746
                $body .= $this->LE . $this->LE;
1747
                $body .= $this->attachAll('inline', $this->boundary[1]);
1748
                break;
1749
            case 'attach':
1750
                $body .= $this->getBoundary($this->boundary[1], '', '', '');
1751
                $body .= $this->encodeString($this->Body, $this->Encoding);
1752
                $body .= $this->LE . $this->LE;
1753
                $body .= $this->attachAll('attachment', $this->boundary[1]);
1754
                break;
1755
            case 'inline_attach':
1756
                $body .= $this->textLine('--' . $this->boundary[1]);
1757
                $body .= $this->headerLine('Content-Type', 'multipart/related;');
1758
                $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
1759
                $body .= $this->LE;
1760
                $body .= $this->getBoundary($this->boundary[2], '', '', '');
1761
                $body .= $this->encodeString($this->Body, $this->Encoding);
1762
                $body .= $this->LE . $this->LE;
1763
                $body .= $this->attachAll('inline', $this->boundary[2]);
1764
                $body .= $this->LE;
1765
                $body .= $this->attachAll('attachment', $this->boundary[1]);
1766
                break;
1767
            case 'alt':
1768
                $body .= $this->getBoundary($this->boundary[1], '', 'text/plain', '');
1769
                $body .= $this->encodeString($this->AltBody, $this->Encoding);
1770
                $body .= $this->LE . $this->LE;
1771
                $body .= $this->getBoundary($this->boundary[1], '', 'text/html', '');
1772
                $body .= $this->encodeString($this->Body, $this->Encoding);
1773
                $body .= $this->LE . $this->LE;
1774
                if (!empty($this->Ical)) {
1775
                    $body .= $this->getBoundary($this->boundary[1], '', 'text/calendar; method=REQUEST', '');
1776
                    $body .= $this->encodeString($this->Ical, $this->Encoding);
1777
                    $body .= $this->LE . $this->LE;
1778
                }
1779
                $body .= $this->endBoundary($this->boundary[1]);
1780
                break;
1781
            case 'alt_inline':
1782
                $body .= $this->getBoundary($this->boundary[1], '', 'text/plain', '');
1783
                $body .= $this->encodeString($this->AltBody, $this->Encoding);
1784
                $body .= $this->LE . $this->LE;
1785
                $body .= $this->textLine('--' . $this->boundary[1]);
1786
                $body .= $this->headerLine('Content-Type', 'multipart/related;');
1787
                $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
1788
                $body .= $this->LE;
1789
                $body .= $this->getBoundary($this->boundary[2], '', 'text/html', '');
1790
                $body .= $this->encodeString($this->Body, $this->Encoding);
1791
                $body .= $this->LE . $this->LE;
1792
                $body .= $this->attachAll('inline', $this->boundary[2]);
1793
                $body .= $this->LE;
1794
                $body .= $this->endBoundary($this->boundary[1]);
1795
                break;
1796
            case 'alt_attach':
1797
                $body .= $this->textLine('--' . $this->boundary[1]);
1798
                $body .= $this->headerLine('Content-Type', 'multipart/alternative;');
1799
                $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
1800
                $body .= $this->LE;
1801
                $body .= $this->getBoundary($this->boundary[2], '', 'text/plain', '');
1802
                $body .= $this->encodeString($this->AltBody, $this->Encoding);
1803
                $body .= $this->LE . $this->LE;
1804
                $body .= $this->getBoundary($this->boundary[2], '', 'text/html', '');
1805
                $body .= $this->encodeString($this->Body, $this->Encoding);
1806
                $body .= $this->LE . $this->LE;
1807
                $body .= $this->endBoundary($this->boundary[2]);
1808
                $body .= $this->LE;
1809
                $body .= $this->attachAll('attachment', $this->boundary[1]);
1810
                break;
1811
            case 'alt_inline_attach':
1812
                $body .= $this->textLine('--' . $this->boundary[1]);
1813
                $body .= $this->headerLine('Content-Type', 'multipart/alternative;');
1814
                $body .= $this->textLine("\tboundary=\"" . $this->boundary[2] . '"');
1815
                $body .= $this->LE;
1816
                $body .= $this->getBoundary($this->boundary[2], '', 'text/plain', '');
1817
                $body .= $this->encodeString($this->AltBody, $this->Encoding);
1818
                $body .= $this->LE . $this->LE;
1819
                $body .= $this->textLine('--' . $this->boundary[2]);
1820
                $body .= $this->headerLine('Content-Type', 'multipart/related;');
1821
                $body .= $this->textLine("\tboundary=\"" . $this->boundary[3] . '"');
1822
                $body .= $this->LE;
1823
                $body .= $this->getBoundary($this->boundary[3], '', 'text/html', '');
1824
                $body .= $this->encodeString($this->Body, $this->Encoding);
1825
                $body .= $this->LE . $this->LE;
1826
                $body .= $this->attachAll('inline', $this->boundary[3]);
1827
                $body .= $this->LE;
1828
                $body .= $this->endBoundary($this->boundary[2]);
1829
                $body .= $this->LE;
1830
                $body .= $this->attachAll('attachment', $this->boundary[1]);
1831
                break;
1832
            default:
1833
                // catch case 'plain' and case ''
1834
                $body .= $this->encodeString($this->Body, $this->Encoding);
1835
                break;
1836
        }
1837 1463 Luisehahne
1838 2053 darkviper
        if ($this->isError()) {
1839
            $body = '';
1840
        } elseif ($this->sign_key_file) {
1841
            try {
1842
                if (!defined('PKCS7_TEXT')) {
1843
                    throw new phpmailerException($this->lang('signing') . ' OpenSSL extension missing.');
1844
                }
1845
                $file = tempnam(sys_get_temp_dir(), 'mail');
1846
                file_put_contents($file, $body); //TODO check this worked
1847
                $signed = tempnam(sys_get_temp_dir(), 'signed');
1848
                if (@openssl_pkcs7_sign(
1849
                    $file,
1850
                    $signed,
1851
                    'file://' . realpath($this->sign_cert_file),
1852
                    array('file://' . realpath($this->sign_key_file), $this->sign_key_pass),
1853
                    null
1854
                )
1855
                ) {
1856
                    @unlink($file);
1857
                    $body = file_get_contents($signed);
1858
                    @unlink($signed);
1859
                } else {
1860
                    @unlink($file);
1861
                    @unlink($signed);
1862
                    throw new phpmailerException($this->lang('signing') . openssl_error_string());
1863
                }
1864
            } catch (phpmailerException $e) {
1865
                $body = '';
1866
                if ($this->exceptions) {
1867
                    throw $e;
1868
                }
1869
            }
1870 1539 Luisehahne
        }
1871 2053 darkviper
        return $body;
1872 1463 Luisehahne
    }
1873
1874 2053 darkviper
    /**
1875
     * Return the start of a message boundary.
1876
     * @access protected
1877
     * @param string $boundary
1878
     * @param string $charSet
1879
     * @param string $contentType
1880
     * @param string $encoding
1881
     * @return string
1882
     */
1883
    protected function getBoundary($boundary, $charSet, $contentType, $encoding)
1884
    {
1885
        $result = '';
1886
        if ($charSet == '') {
1887
            $charSet = $this->CharSet;
1888
        }
1889
        if ($contentType == '') {
1890
            $contentType = $this->ContentType;
1891
        }
1892
        if ($encoding == '') {
1893
            $encoding = $this->Encoding;
1894
        }
1895
        $result .= $this->textLine('--' . $boundary);
1896
        $result .= sprintf("Content-Type: %s; charset=%s", $contentType, $charSet);
1897
        $result .= $this->LE;
1898
        $result .= $this->headerLine('Content-Transfer-Encoding', $encoding);
1899
        $result .= $this->LE;
1900
1901
        return $result;
1902 1463 Luisehahne
    }
1903
1904 2053 darkviper
    /**
1905
     * Return the end of a message boundary.
1906
     * @access protected
1907
     * @param string $boundary
1908
     * @return string
1909
     */
1910
    protected function endBoundary($boundary)
1911
    {
1912
        return $this->LE . '--' . $boundary . '--' . $this->LE;
1913
    }
1914 1463 Luisehahne
1915 2053 darkviper
    /**
1916
     * Set the message type.
1917
     * PHPMailer only supports some preset message types,
1918
     * not arbitrary MIME structures.
1919
     * @access protected
1920
     * @return void
1921
     */
1922
    protected function setMessageType()
1923
    {
1924
        $this->message_type = array();
1925
        if ($this->alternativeExists()) {
1926
            $this->message_type[] = "alt";
1927 1463 Luisehahne
        }
1928 2053 darkviper
        if ($this->inlineImageExists()) {
1929
            $this->message_type[] = "inline";
1930
        }
1931
        if ($this->attachmentExists()) {
1932
            $this->message_type[] = "attach";
1933
        }
1934
        $this->message_type = implode("_", $this->message_type);
1935
        if ($this->message_type == "") {
1936
            $this->message_type = "plain";
1937
        }
1938 1463 Luisehahne
    }
1939
1940 2053 darkviper
    /**
1941
     * Format a header line.
1942
     * @access public
1943
     * @param string $name
1944
     * @param string $value
1945
     * @return string
1946
     */
1947
    public function headerLine($name, $value)
1948
    {
1949
        return $name . ': ' . $value . $this->LE;
1950 1463 Luisehahne
    }
1951
1952 2053 darkviper
    /**
1953
     * Return a formatted mail line.
1954
     * @access public
1955
     * @param string $value
1956
     * @return string
1957
     */
1958
    public function textLine($value)
1959
    {
1960
        return $value . $this->LE;
1961 1463 Luisehahne
    }
1962
1963 2053 darkviper
    /**
1964
     * Add an attachment from a path on the filesystem.
1965
     * Returns false if the file could not be found or read.
1966
     * @param string $path Path to the attachment.
1967
     * @param string $name Overrides the attachment name.
1968
     * @param string $encoding File encoding (see $Encoding).
1969
     * @param string $type File extension (MIME) type.
1970
     * @param string $disposition Disposition to use
1971
     * @throws phpmailerException
1972
     * @return bool
1973
     */
1974
    public function addAttachment($path, $name = '', $encoding = 'base64', $type = '', $disposition = 'attachment')
1975
    {
1976
        try {
1977
            if (!@is_file($path)) {
1978
                throw new phpmailerException($this->lang('file_access') . $path, self::STOP_CONTINUE);
1979
            }
1980 1463 Luisehahne
1981 2053 darkviper
            //If a MIME type is not specified, try to work it out from the file name
1982
            if ($type == '') {
1983
                $type = self::filenameToType($path);
1984
            }
1985 1463 Luisehahne
1986 2053 darkviper
            $filename = basename($path);
1987
            if ($name == '') {
1988
                $name = $filename;
1989
            }
1990
1991
            $this->attachment[] = array(
1992
                0 => $path,
1993
                1 => $filename,
1994
                2 => $name,
1995
                3 => $encoding,
1996
                4 => $type,
1997
                5 => false, // isStringAttachment
1998
                6 => $disposition,
1999
                7 => 0
2000
            );
2001
2002
        } catch (phpmailerException $e) {
2003
            $this->setError($e->getMessage());
2004
            if ($this->exceptions) {
2005
                throw $e;
2006
            }
2007
            $this->edebug($e->getMessage() . "\n");
2008
            return false;
2009
        }
2010
        return true;
2011 1463 Luisehahne
    }
2012
2013 2053 darkviper
    /**
2014
     * Return the array of attachments.
2015
     * @return array
2016
     */
2017
    public function getAttachments()
2018
    {
2019
        return $this->attachment;
2020
    }
2021 1463 Luisehahne
2022 2053 darkviper
    /**
2023
     * Attach all file, string, and binary attachments to the message.
2024
     * Returns an empty string on failure.
2025
     * @access protected
2026
     * @param string $disposition_type
2027
     * @param string $boundary
2028
     * @return string
2029
     */
2030
    protected function attachAll($disposition_type, $boundary)
2031
    {
2032
        // Return text of body
2033
        $mime = array();
2034
        $cidUniq = array();
2035
        $incl = array();
2036 1463 Luisehahne
2037 2053 darkviper
        // Add all attachments
2038
        foreach ($this->attachment as $attachment) {
2039
            // Check if it is a valid disposition_filter
2040
            if ($attachment[6] == $disposition_type) {
2041
                // Check for string attachment
2042
                $string = '';
2043
                $path = '';
2044
                $bString = $attachment[5];
2045
                if ($bString) {
2046
                    $string = $attachment[0];
2047
                } else {
2048
                    $path = $attachment[0];
2049
                }
2050 1463 Luisehahne
2051 2053 darkviper
                $inclhash = md5(serialize($attachment));
2052
                if (in_array($inclhash, $incl)) {
2053
                    continue;
2054
                }
2055
                $incl[] = $inclhash;
2056
                $name = $attachment[2];
2057
                $encoding = $attachment[3];
2058
                $type = $attachment[4];
2059
                $disposition = $attachment[6];
2060
                $cid = $attachment[7];
2061
                if ($disposition == 'inline' && isset($cidUniq[$cid])) {
2062
                    continue;
2063
                }
2064
                $cidUniq[$cid] = true;
2065 1463 Luisehahne
2066 2053 darkviper
                $mime[] = sprintf("--%s%s", $boundary, $this->LE);
2067
                $mime[] = sprintf(
2068
                    "Content-Type: %s; name=\"%s\"%s",
2069
                    $type,
2070
                    $this->encodeHeader($this->secureHeader($name)),
2071
                    $this->LE
2072
                );
2073
                $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
2074
2075
                if ($disposition == 'inline') {
2076
                    $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
2077
                }
2078
2079
                // If a filename contains any of these chars, it should be quoted,
2080
                // but not otherwise: RFC2183 & RFC2045 5.1
2081
                // Fixes a warning in IETF's msglint MIME checker
2082
                // Allow for bypassing the Content-Disposition header totally
2083
                if (!(empty($disposition))) {
2084
                    if (preg_match('/[ \(\)<>@,;:\\"\/\[\]\?=]/', $name)) {
2085
                        $mime[] = sprintf(
2086
                            "Content-Disposition: %s; filename=\"%s\"%s",
2087
                            $disposition,
2088
                            $this->encodeHeader($this->secureHeader($name)),
2089
                            $this->LE . $this->LE
2090
                        );
2091
                    } else {
2092
                        $mime[] = sprintf(
2093
                            "Content-Disposition: %s; filename=%s%s",
2094
                            $disposition,
2095
                            $this->encodeHeader($this->secureHeader($name)),
2096
                            $this->LE . $this->LE
2097
                        );
2098
                    }
2099
                } else {
2100
                    $mime[] = $this->LE;
2101
                }
2102
2103
                // Encode as string attachment
2104
                if ($bString) {
2105
                    $mime[] = $this->encodeString($string, $encoding);
2106
                    if ($this->isError()) {
2107
                        return '';
2108
                    }
2109
                    $mime[] = $this->LE . $this->LE;
2110
                } else {
2111
                    $mime[] = $this->encodeFile($path, $encoding);
2112
                    if ($this->isError()) {
2113
                        return '';
2114
                    }
2115
                    $mime[] = $this->LE . $this->LE;
2116
                }
2117
            }
2118
        }
2119
2120
        $mime[] = sprintf("--%s--%s", $boundary, $this->LE);
2121
2122
        return implode("", $mime);
2123 1463 Luisehahne
    }
2124
2125 2053 darkviper
    /**
2126
     * Encode a file attachment in requested format.
2127
     * Returns an empty string on failure.
2128
     * @param string $path The full path to the file
2129
     * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
2130
     * @throws phpmailerException
2131
     * @see EncodeFile(encodeFile
2132
     * @access protected
2133
     * @return string
2134
     */
2135
    protected function encodeFile($path, $encoding = 'base64')
2136
    {
2137
        try {
2138
            if (!is_readable($path)) {
2139
                throw new phpmailerException($this->lang('file_open') . $path, self::STOP_CONTINUE);
2140
            }
2141
            $magic_quotes = get_magic_quotes_runtime();
2142
            if ($magic_quotes) {
2143
                if (version_compare(PHP_VERSION, '5.3.0', '<')) {
2144
                    set_magic_quotes_runtime(0);
2145
                } else {
2146
                    ini_set('magic_quotes_runtime', 0);
2147
                }
2148
            }
2149
            $file_buffer = file_get_contents($path);
2150
            $file_buffer = $this->encodeString($file_buffer, $encoding);
2151
            if ($magic_quotes) {
2152
                if (version_compare(PHP_VERSION, '5.3.0', '<')) {
2153
                    set_magic_quotes_runtime($magic_quotes);
2154
                } else {
2155
                    ini_set('magic_quotes_runtime', $magic_quotes);
2156
                }
2157
            }
2158
            return $file_buffer;
2159
        } catch (Exception $e) {
2160
            $this->setError($e->getMessage());
2161
            return '';
2162
        }
2163
    }
2164 1463 Luisehahne
2165 2053 darkviper
    /**
2166
     * Encode a string in requested format.
2167
     * Returns an empty string on failure.
2168
     * @param string $str The text to encode
2169
     * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
2170
     * @access public
2171
     * @return string
2172
     */
2173
    public function encodeString($str, $encoding = 'base64')
2174
    {
2175
        $encoded = '';
2176
        switch (strtolower($encoding)) {
2177
            case 'base64':
2178
                $encoded = chunk_split(base64_encode($str), 76, $this->LE);
2179
                break;
2180
            case '7bit':
2181
            case '8bit':
2182
                $encoded = $this->fixEOL($str);
2183
                //Make sure it ends with a line break
2184
                if (substr($encoded, -(strlen($this->LE))) != $this->LE) {
2185
                    $encoded .= $this->LE;
2186
                }
2187
                break;
2188
            case 'binary':
2189
                $encoded = $str;
2190
                break;
2191
            case 'quoted-printable':
2192
                $encoded = $this->encodeQP($str);
2193
                break;
2194
            default:
2195
                $this->setError($this->lang('encoding') . $encoding);
2196
                break;
2197 1463 Luisehahne
        }
2198 2053 darkviper
        return $encoded;
2199
    }
2200
2201
    /**
2202
     * Encode a header string optimally.
2203
     * Picks shortest of Q, B, quoted-printable or none.
2204
     * @access public
2205
     * @param string $str
2206
     * @param string $position
2207
     * @return string
2208
     */
2209
    public function encodeHeader($str, $position = 'text')
2210
    {
2211
        $x = 0;
2212
        switch (strtolower($position)) {
2213
            case 'phrase':
2214
                if (!preg_match('/[\200-\377]/', $str)) {
2215
                    // Can't use addslashes as we don't know what value has magic_quotes_sybase
2216
                    $encoded = addcslashes($str, "\0..\37\177\\\"");
2217
                    if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
2218
                        return ($encoded);
2219
                    } else {
2220
                        return ("\"$encoded\"");
2221
                    }
2222
                }
2223
                $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
2224
                break;
2225
            /** @noinspection PhpMissingBreakStatementInspection */
2226
            case 'comment':
2227
                $x = preg_match_all('/[()"]/', $str, $matches);
2228
                // Intentional fall-through
2229
            case 'text':
2230
            default:
2231
                $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
2232
                break;
2233 1463 Luisehahne
        }
2234 2053 darkviper
2235
        if ($x == 0) { //There are no chars that need encoding
2236
            return ($str);
2237 1463 Luisehahne
        }
2238
2239 2053 darkviper
        $maxlen = 75 - 7 - strlen($this->CharSet);
2240
        // Try to select the encoding which should produce the shortest output
2241
        if ($x > strlen($str) / 3) {
2242
            //More than a third of the content will need encoding, so B encoding will be most efficient
2243
            $encoding = 'B';
2244
            if (function_exists('mb_strlen') && $this->hasMultiBytes($str)) {
2245
                // Use a custom function which correctly encodes and wraps long
2246
                // multibyte strings without breaking lines within a character
2247
                $encoded = $this->base64EncodeWrapMB($str, "\n");
2248
            } else {
2249
                $encoded = base64_encode($str);
2250
                $maxlen -= $maxlen % 4;
2251
                $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
2252
            }
2253
        } else {
2254
            $encoding = 'Q';
2255
            $encoded = $this->encodeQ($str, $position);
2256
            $encoded = $this->wrapText($encoded, $maxlen, true);
2257
            $encoded = str_replace('=' . self::CRLF, "\n", trim($encoded));
2258
        }
2259
2260
        $encoded = preg_replace('/^(.*)$/m', " =?" . $this->CharSet . "?$encoding?\\1?=", $encoded);
2261
        $encoded = trim(str_replace("\n", $this->LE, $encoded));
2262
2263
        return $encoded;
2264 1539 Luisehahne
    }
2265 2053 darkviper
2266
    /**
2267
     * Check if a string contains multi-byte characters.
2268
     * @access public
2269
     * @param string $str multi-byte text to wrap encode
2270
     * @return bool
2271
     */
2272
    public function hasMultiBytes($str)
2273
    {
2274
        if (function_exists('mb_strlen')) {
2275
            return (strlen($str) > mb_strlen($str, $this->CharSet));
2276
        } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
2277
            return false;
2278
        }
2279 1539 Luisehahne
    }
2280
2281 2053 darkviper
    /**
2282
     * Encode and wrap long multibyte strings for mail headers
2283
     * without breaking lines within a character.
2284
     * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
2285
     * @access public
2286
     * @param string $str multi-byte text to wrap encode
2287
     * @param string $lf string to use as linefeed/end-of-line
2288
     * @return string
2289
     */
2290
    public function base64EncodeWrapMB($str, $lf = null)
2291
    {
2292
        $start = "=?" . $this->CharSet . "?B?";
2293
        $end = "?=";
2294
        $encoded = "";
2295
        if ($lf === null) {
2296
            $lf = $this->LE;
2297
        }
2298 1463 Luisehahne
2299 2053 darkviper
        $mb_length = mb_strlen($str, $this->CharSet);
2300
        // Each line must have length <= 75, including $start and $end
2301
        $length = 75 - strlen($start) - strlen($end);
2302
        // Average multi-byte ratio
2303
        $ratio = $mb_length / strlen($str);
2304
        // Base64 has a 4:3 ratio
2305
        $avgLength = floor($length * $ratio * .75);
2306 1463 Luisehahne
2307 2053 darkviper
        for ($i = 0; $i < $mb_length; $i += $offset) {
2308
            $lookBack = 0;
2309
            do {
2310
                $offset = $avgLength - $lookBack;
2311
                $chunk = mb_substr($str, $i, $offset, $this->CharSet);
2312
                $chunk = base64_encode($chunk);
2313
                $lookBack++;
2314
            } while (strlen($chunk) > $length);
2315
            $encoded .= $chunk . $lf;
2316
        }
2317 1463 Luisehahne
2318 2053 darkviper
        // Chomp the last linefeed
2319
        $encoded = substr($encoded, 0, -strlen($lf));
2320
        return $encoded;
2321
    }
2322 1463 Luisehahne
2323 2053 darkviper
    /**
2324
     * Encode a string in quoted-printable format.
2325
     * According to RFC2045 section 6.7.
2326
     * @access public
2327
     * @param string $string The text to encode
2328
     * @param integer $line_max Number of chars allowed on a line before wrapping
2329
     * @return string
2330
     * @link PHP version adapted from http://www.php.net/manual/en/function.quoted-printable-decode.php#89417
2331
     */
2332
    public function encodeQP($string, $line_max = 76)
2333
    {
2334
        if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
2335
            return quoted_printable_encode($string);
2336
        }
2337
        //Fall back to a pure PHP implementation
2338
        $string = str_replace(
2339
            array('%20', '%0D%0A.', '%0D%0A', '%'),
2340
            array(' ', "\r\n=2E", "\r\n", '='),
2341
            rawurlencode($string)
2342
        );
2343
        $string = preg_replace('/[^\r\n]{' . ($line_max - 3) . '}[^=\r\n]{2}/', "$0=\r\n", $string);
2344
        return $string;
2345
    }
2346 1463 Luisehahne
2347 2053 darkviper
    /**
2348
     * Backward compatibility wrapper for an old QP encoding function that was removed.
2349
     * @see PHPMailer::encodeQP()
2350
     * @access public
2351
     * @param string $string
2352
     * @param integer $line_max
2353
     * @param bool $space_conv
2354
     * @return string
2355
     * @deprecated Use encodeQP instead.
2356
     */
2357
    public function encodeQPphp(
2358
        $string,
2359
        $line_max = 76,
2360
        /** @noinspection PhpUnusedParameterInspection */ $space_conv = false
2361
    ) {
2362
        return $this->encodeQP($string, $line_max);
2363
    }
2364 1463 Luisehahne
2365 2053 darkviper
    /**
2366
     * Encode a string using Q encoding.
2367
     * @link http://tools.ietf.org/html/rfc2047
2368
     * @param string $str the text to encode
2369
     * @param string $position Where the text is going to be used, see the RFC for what that means
2370
     * @access public
2371
     * @return string
2372
     */
2373
    public function encodeQ($str, $position = 'text')
2374
    {
2375
        //There should not be any EOL in the string
2376
        $pattern = '';
2377
        $encoded = str_replace(array("\r", "\n"), '', $str);
2378
        switch (strtolower($position)) {
2379
            case 'phrase':
2380
                //RFC 2047 section 5.3
2381
                $pattern = '^A-Za-z0-9!*+\/ -';
2382
                break;
2383
            /** @noinspection PhpMissingBreakStatementInspection */
2384
            case 'comment':
2385
                //RFC 2047 section 5.2
2386
                $pattern = '\(\)"';
2387
                //intentional fall-through
2388
                //for this reason we build the $pattern without including delimiters and []
2389
            case 'text':
2390
            default:
2391
                //RFC 2047 section 5.1
2392
                //Replace every high ascii, control, =, ? and _ characters
2393
                $pattern = '\000-\011\013\014\016-\037\075\077\137\177-\377' . $pattern;
2394
                break;
2395
        }
2396
        $matches = array();
2397
        if (preg_match_all("/[{$pattern}]/", $encoded, $matches)) {
2398
            //If the string contains an '=', make sure it's the first thing we replace
2399
            //so as to avoid double-encoding
2400
            $s = array_search('=', $matches[0]);
2401
            if ($s !== false) {
2402
                unset($matches[0][$s]);
2403
                array_unshift($matches[0], '=');
2404
            }
2405
            foreach (array_unique($matches[0]) as $char) {
2406
                $encoded = str_replace($char, '=' . sprintf('%02X', ord($char)), $encoded);
2407
            }
2408
        }
2409
        //Replace every spaces to _ (more readable than =20)
2410
        return str_replace(' ', '_', $encoded);
2411 1463 Luisehahne
    }
2412
2413 2053 darkviper
2414
    /**
2415
     * Add a string or binary attachment (non-filesystem).
2416
     * This method can be used to attach ascii or binary data,
2417
     * such as a BLOB record from a database.
2418
     * @param string $string String attachment data.
2419
     * @param string $filename Name of the attachment.
2420
     * @param string $encoding File encoding (see $Encoding).
2421
     * @param string $type File extension (MIME) type.
2422
     * @param string $disposition Disposition to use
2423
     * @return void
2424
     */
2425
    public function addStringAttachment(
2426
        $string,
2427
        $filename,
2428
        $encoding = 'base64',
2429
        $type = '',
2430
        $disposition = 'attachment'
2431
    ) {
2432
        //If a MIME type is not specified, try to work it out from the file name
2433
        if ($type == '') {
2434
            $type = self::filenameToType($filename);
2435
        }
2436
        // Append to $attachment array
2437
        $this->attachment[] = array(
2438
            0 => $string,
2439
            1 => $filename,
2440
            2 => basename($filename),
2441
            3 => $encoding,
2442
            4 => $type,
2443
            5 => true, // isStringAttachment
2444
            6 => $disposition,
2445
            7 => 0
2446
        );
2447 1463 Luisehahne
    }
2448
2449 2053 darkviper
    /**
2450
     * Add an embedded (inline) attachment from a file.
2451
     * This can include images, sounds, and just about any other document type.
2452
     * These differ from 'regular' attachmants in that they are intended to be
2453
     * displayed inline with the message, not just attached for download.
2454
     * This is used in HTML messages that embed the images
2455
     * the HTML refers to using the $cid value.
2456
     * @param string $path Path to the attachment.
2457
     * @param string $cid Content ID of the attachment; Use this to reference
2458
     *        the content when using an embedded image in HTML.
2459
     * @param string $name Overrides the attachment name.
2460
     * @param string $encoding File encoding (see $Encoding).
2461
     * @param string $type File MIME type.
2462
     * @param string $disposition Disposition to use
2463
     * @return bool True on successfully adding an attachment
2464
     */
2465
    public function addEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = '', $disposition = 'inline')
2466
    {
2467
        if (!@is_file($path)) {
2468
            $this->setError($this->lang('file_access') . $path);
2469
            return false;
2470
        }
2471 1463 Luisehahne
2472 2053 darkviper
        //If a MIME type is not specified, try to work it out from the file name
2473
        if ($type == '') {
2474
            $type = self::filenameToType($path);
2475
        }
2476 1463 Luisehahne
2477 2053 darkviper
        $filename = basename($path);
2478
        if ($name == '') {
2479
            $name = $filename;
2480
        }
2481 1550 Luisehahne
2482 2053 darkviper
        // Append to $attachment array
2483
        $this->attachment[] = array(
2484
            0 => $path,
2485
            1 => $filename,
2486
            2 => $name,
2487
            3 => $encoding,
2488
            4 => $type,
2489
            5 => false, // isStringAttachment
2490
            6 => $disposition,
2491
            7 => $cid
2492
        );
2493 1539 Luisehahne
        return true;
2494 1463 Luisehahne
    }
2495
2496 2053 darkviper
    /**
2497
     * Add an embedded stringified attachment.
2498
     * This can include images, sounds, and just about any other document type.
2499
     * Be sure to set the $type to an image type for images:
2500
     * JPEG images use 'image/jpeg', GIF uses 'image/gif', PNG uses 'image/png'.
2501
     * @param string $string The attachment binary data.
2502
     * @param string $cid Content ID of the attachment; Use this to reference
2503
     *        the content when using an embedded image in HTML.
2504
     * @param string $name
2505
     * @param string $encoding File encoding (see $Encoding).
2506
     * @param string $type MIME type.
2507
     * @param string $disposition Disposition to use
2508
     * @return bool True on successfully adding an attachment
2509
     */
2510
    public function addStringEmbeddedImage(
2511
        $string,
2512
        $cid,
2513
        $name = '',
2514
        $encoding = 'base64',
2515
        $type = '',
2516
        $disposition = 'inline'
2517
    ) {
2518
        //If a MIME type is not specified, try to work it out from the name
2519
        if ($type == '') {
2520
            $type = self::filenameToType($name);
2521
        }
2522
2523
        // Append to $attachment array
2524
        $this->attachment[] = array(
2525
            0 => $string,
2526
            1 => $name,
2527
            2 => $name,
2528
            3 => $encoding,
2529
            4 => $type,
2530
            5 => true, // isStringAttachment
2531
            6 => $disposition,
2532
            7 => $cid
2533
        );
2534 1550 Luisehahne
        return true;
2535
    }
2536
2537 2053 darkviper
    /**
2538
     * Check if an inline attachment is present.
2539
     * @access public
2540
     * @return bool
2541
     */
2542
    public function inlineImageExists()
2543
    {
2544
        foreach ($this->attachment as $attachment) {
2545
            if ($attachment[6] == 'inline') {
2546
                return true;
2547
            }
2548
        }
2549
        return false;
2550
    }
2551 1550 Luisehahne
2552 2053 darkviper
    /**
2553
     * Check if an attachment (non-inline) is present.
2554
     * @return bool
2555
     */
2556
    public function attachmentExists()
2557
    {
2558
        foreach ($this->attachment as $attachment) {
2559
            if ($attachment[6] == 'attachment') {
2560
                return true;
2561
            }
2562
        }
2563
        return false;
2564
    }
2565 1463 Luisehahne
2566 2053 darkviper
    /**
2567
     * Check if this message has an alternative body set.
2568
     * @return bool
2569
     */
2570
    public function alternativeExists()
2571
    {
2572
        return !empty($this->AltBody);
2573 1539 Luisehahne
    }
2574 1463 Luisehahne
2575 2053 darkviper
    /**
2576
     * Clear all To recipients.
2577
     * @return void
2578
     */
2579
    public function clearAddresses()
2580
    {
2581
        foreach ($this->to as $to) {
2582
            unset($this->all_recipients[strtolower($to[0])]);
2583
        }
2584
        $this->to = array();
2585 1539 Luisehahne
    }
2586 1463 Luisehahne
2587 2053 darkviper
    /**
2588
     * Clear all CC recipients.
2589
     * @return void
2590
     */
2591
    public function clearCCs()
2592
    {
2593
        foreach ($this->cc as $cc) {
2594
            unset($this->all_recipients[strtolower($cc[0])]);
2595
        }
2596
        $this->cc = array();
2597 1539 Luisehahne
    }
2598 1463 Luisehahne
2599 2053 darkviper
    /**
2600
     * Clear all BCC recipients.
2601
     * @return void
2602
     */
2603
    public function clearBCCs()
2604
    {
2605
        foreach ($this->bcc as $bcc) {
2606
            unset($this->all_recipients[strtolower($bcc[0])]);
2607
        }
2608
        $this->bcc = array();
2609
    }
2610 1463 Luisehahne
2611 2053 darkviper
    /**
2612
     * Clear all ReplyTo recipients.
2613
     * @return void
2614
     */
2615
    public function clearReplyTos()
2616
    {
2617
        $this->ReplyTo = array();
2618
    }
2619 1463 Luisehahne
2620 2053 darkviper
    /**
2621
     * Clear all recipient types.
2622
     * @return void
2623
     */
2624
    public function clearAllRecipients()
2625
    {
2626
        $this->to = array();
2627
        $this->cc = array();
2628
        $this->bcc = array();
2629
        $this->all_recipients = array();
2630
    }
2631 1463 Luisehahne
2632 2053 darkviper
    /**
2633
     * Clear all filesystem, string, and binary attachments.
2634
     * @return void
2635
     */
2636
    public function clearAttachments()
2637
    {
2638
        $this->attachment = array();
2639
    }
2640 1463 Luisehahne
2641 2053 darkviper
    /**
2642
     * Clear all custom headers.
2643
     * @return void
2644
     */
2645
    public function clearCustomHeaders()
2646
    {
2647
        $this->CustomHeader = array();
2648
    }
2649 1463 Luisehahne
2650 2053 darkviper
    /**
2651
     * Add an error message to the error container.
2652
     * @access protected
2653
     * @param string $msg
2654
     * @return void
2655
     */
2656
    protected function setError($msg)
2657
    {
2658
        $this->error_count++;
2659
        if ($this->Mailer == 'smtp' and !is_null($this->smtp)) {
2660
            $lasterror = $this->smtp->getError();
2661
            if (!empty($lasterror) and array_key_exists('smtp_msg', $lasterror)) {
2662
                $msg .= '<p>' . $this->lang('smtp_error') . $lasterror['smtp_msg'] . "</p>\n";
2663
            }
2664
        }
2665
        $this->ErrorInfo = $msg;
2666 1539 Luisehahne
    }
2667 1463 Luisehahne
2668 2053 darkviper
    /**
2669
     * Return an RFC 822 formatted date.
2670
     * @access public
2671
     * @return string
2672
     * @static
2673
     */
2674
    public static function rfcDate()
2675
    {
2676
        //Set the time zone to whatever the default is to avoid 500 errors
2677
        //Will default to UTC if it's not set properly in php.ini
2678
        date_default_timezone_set(@date_default_timezone_get());
2679
        return date('D, j M Y H:i:s O');
2680
    }
2681 1463 Luisehahne
2682 2053 darkviper
    /**
2683
     * Get the server hostname.
2684
     * Returns 'localhost.localdomain' if unknown.
2685
     * @access protected
2686
     * @return string
2687
     */
2688
    protected function serverHostname()
2689
    {
2690
        if (!empty($this->Hostname)) {
2691
            $result = $this->Hostname;
2692
        } elseif (isset($_SERVER['SERVER_NAME'])) {
2693
            $result = $_SERVER['SERVER_NAME'];
2694
        } else {
2695
            $result = 'localhost.localdomain';
2696
        }
2697 1463 Luisehahne
2698 2053 darkviper
        return $result;
2699 1463 Luisehahne
    }
2700
2701 2053 darkviper
    /**
2702
     * Get an error message in the current language.
2703
     * @access protected
2704
     * @param string $key
2705
     * @return string
2706
     */
2707
    protected function lang($key)
2708
    {
2709
        if (count($this->language) < 1) {
2710
            $this->setLanguage('en'); // set the default language
2711
        }
2712 1463 Luisehahne
2713 2053 darkviper
        if (isset($this->language[$key])) {
2714
            return $this->language[$key];
2715
        } else {
2716
            return 'Language string failed to load: ' . $key;
2717
        }
2718 1463 Luisehahne
    }
2719
2720 2053 darkviper
    /**
2721
     * Check if an error occurred.
2722
     * @access public
2723
     * @return bool True if an error did occur.
2724
     */
2725
    public function isError()
2726
    {
2727
        return ($this->error_count > 0);
2728 1463 Luisehahne
    }
2729
2730 2053 darkviper
    /**
2731
     * Ensure consistent line endings in a string.
2732
     * Changes every end of line from CRLF, CR or LF to $this->LE.
2733
     * @access public
2734
     * @param string $str String to fixEOL
2735
     * @return string
2736
     */
2737
    public function fixEOL($str)
2738
    {
2739
        // Normalise to \n
2740
        $nstr = str_replace(array("\r\n", "\r"), "\n", $str);
2741
        // Now convert LE as needed
2742
        if ($this->LE !== "\n") {
2743
            $nstr = str_replace("\n", $this->LE, $nstr);
2744
        }
2745
        return $nstr;
2746
    }
2747 1463 Luisehahne
2748 2053 darkviper
    /**
2749
     * Add a custom header.
2750
     * $name value can be overloaded to contain
2751
     * both header name and value (name:value)
2752
     * @access public
2753
     * @param string $name Custom header name
2754
     * @param string $value Header value
2755
     * @return void
2756
     */
2757
    public function addCustomHeader($name, $value = null)
2758
    {
2759
        if ($value === null) {
2760
            // Value passed in as name:value
2761
            $this->CustomHeader[] = explode(':', $name, 2);
2762
        } else {
2763
            $this->CustomHeader[] = array($name, $value);
2764
        }
2765
    }
2766 1463 Luisehahne
2767 2053 darkviper
    /**
2768
     * Create a message from an HTML string.
2769
     * Automatically makes modifications for inline images and backgrounds
2770
     * and creates a plain-text version by converting the HTML.
2771
     * Overwrites any existing values in $this->Body and $this->AltBody
2772
     * @access public
2773
     * @param string $message HTML message string
2774
     * @param string $basedir baseline directory for path
2775
     * @param bool $advanced Whether to use the advanced HTML to text converter
2776
     * @return string $message
2777
     */
2778
    public function msgHTML($message, $basedir = '', $advanced = false)
2779
    {
2780
        preg_match_all("/(src|background)=[\"'](.*)[\"']/Ui", $message, $images);
2781
        if (isset($images[2])) {
2782
            foreach ($images[2] as $i => $url) {
2783
                // do not change urls for absolute images (thanks to corvuscorax)
2784
                if (!preg_match('#^[A-z]+://#', $url)) {
2785
                    $filename = basename($url);
2786
                    $directory = dirname($url);
2787
                    if ($directory == '.') {
2788
                        $directory = '';
2789
                    }
2790
                    $cid = md5($url) . '@phpmailer.0'; //RFC2392 S 2
2791
                    if (strlen($basedir) > 1 && substr($basedir, -1) != '/') {
2792
                        $basedir .= '/';
2793
                    }
2794
                    if (strlen($directory) > 1 && substr($directory, -1) != '/') {
2795
                        $directory .= '/';
2796
                    }
2797
                    if ($this->addEmbeddedImage(
2798
                        $basedir . $directory . $filename,
2799
                        $cid,
2800
                        $filename,
2801
                        'base64',
2802
                        self::_mime_types(self::mb_pathinfo($filename, PATHINFO_EXTENSION))
2803
                    )
2804
                    ) {
2805
                        $message = preg_replace(
2806
                            "/" . $images[1][$i] . "=[\"']" . preg_quote($url, '/') . "[\"']/Ui",
2807
                            $images[1][$i] . "=\"cid:" . $cid . "\"",
2808
                            $message
2809
                        );
2810
                    }
2811
                }
2812
            }
2813
        }
2814
        $this->isHTML(true);
2815
        if (empty($this->AltBody)) {
2816
            $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
2817
        }
2818
        //Convert all message body line breaks to CRLF, makes quoted-printable encoding work much better
2819
        $this->Body = $this->normalizeBreaks($message);
2820
        $this->AltBody = $this->normalizeBreaks($this->html2text($message, $advanced));
2821
        return $this->Body;
2822
    }
2823 1463 Luisehahne
2824 2053 darkviper
    /**
2825
     * Convert an HTML string into plain text.
2826
     * @param string $html The HTML text to convert
2827
     * @param bool $advanced Should this use the more complex html2text converter or just a simple one?
2828
     * @return string
2829
     */
2830
    public function html2text($html, $advanced = false)
2831
    {
2832
        if ($advanced) {
2833
            require_once 'extras/class.html2text.php';
2834
            $h = new html2text($html);
2835
            return $h->get_text();
2836 1463 Luisehahne
        }
2837 2053 darkviper
        return html_entity_decode(
2838
            trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/si', '', $html))),
2839
            ENT_QUOTES,
2840
            $this->CharSet
2841
        );
2842 1463 Luisehahne
    }
2843 2053 darkviper
2844
    /**
2845
     * Get the MIME type for a file extension.
2846
     * @param string $ext File extension
2847
     * @access public
2848
     * @return string MIME type of file.
2849
     * @static
2850
     */
2851
    public static function _mime_types($ext = '')
2852
    {
2853
        $mimes = array(
2854
            'xl' => 'application/excel',
2855
            'hqx' => 'application/mac-binhex40',
2856
            'cpt' => 'application/mac-compactpro',
2857
            'bin' => 'application/macbinary',
2858
            'doc' => 'application/msword',
2859
            'word' => 'application/msword',
2860
            'class' => 'application/octet-stream',
2861
            'dll' => 'application/octet-stream',
2862
            'dms' => 'application/octet-stream',
2863
            'exe' => 'application/octet-stream',
2864
            'lha' => 'application/octet-stream',
2865
            'lzh' => 'application/octet-stream',
2866
            'psd' => 'application/octet-stream',
2867
            'sea' => 'application/octet-stream',
2868
            'so' => 'application/octet-stream',
2869
            'oda' => 'application/oda',
2870
            'pdf' => 'application/pdf',
2871
            'ai' => 'application/postscript',
2872
            'eps' => 'application/postscript',
2873
            'ps' => 'application/postscript',
2874
            'smi' => 'application/smil',
2875
            'smil' => 'application/smil',
2876
            'mif' => 'application/vnd.mif',
2877
            'xls' => 'application/vnd.ms-excel',
2878
            'ppt' => 'application/vnd.ms-powerpoint',
2879
            'wbxml' => 'application/vnd.wap.wbxml',
2880
            'wmlc' => 'application/vnd.wap.wmlc',
2881
            'dcr' => 'application/x-director',
2882
            'dir' => 'application/x-director',
2883
            'dxr' => 'application/x-director',
2884
            'dvi' => 'application/x-dvi',
2885
            'gtar' => 'application/x-gtar',
2886
            'php3' => 'application/x-httpd-php',
2887
            'php4' => 'application/x-httpd-php',
2888
            'php' => 'application/x-httpd-php',
2889
            'phtml' => 'application/x-httpd-php',
2890
            'phps' => 'application/x-httpd-php-source',
2891
            'js' => 'application/x-javascript',
2892
            'swf' => 'application/x-shockwave-flash',
2893
            'sit' => 'application/x-stuffit',
2894
            'tar' => 'application/x-tar',
2895
            'tgz' => 'application/x-tar',
2896
            'xht' => 'application/xhtml+xml',
2897
            'xhtml' => 'application/xhtml+xml',
2898
            'zip' => 'application/zip',
2899
            'mid' => 'audio/midi',
2900
            'midi' => 'audio/midi',
2901
            'mp2' => 'audio/mpeg',
2902
            'mp3' => 'audio/mpeg',
2903
            'mpga' => 'audio/mpeg',
2904
            'aif' => 'audio/x-aiff',
2905
            'aifc' => 'audio/x-aiff',
2906
            'aiff' => 'audio/x-aiff',
2907
            'ram' => 'audio/x-pn-realaudio',
2908
            'rm' => 'audio/x-pn-realaudio',
2909
            'rpm' => 'audio/x-pn-realaudio-plugin',
2910
            'ra' => 'audio/x-realaudio',
2911
            'wav' => 'audio/x-wav',
2912
            'bmp' => 'image/bmp',
2913
            'gif' => 'image/gif',
2914
            'jpeg' => 'image/jpeg',
2915
            'jpe' => 'image/jpeg',
2916
            'jpg' => 'image/jpeg',
2917
            'png' => 'image/png',
2918
            'tiff' => 'image/tiff',
2919
            'tif' => 'image/tiff',
2920
            'eml' => 'message/rfc822',
2921
            'css' => 'text/css',
2922
            'html' => 'text/html',
2923
            'htm' => 'text/html',
2924
            'shtml' => 'text/html',
2925
            'log' => 'text/plain',
2926
            'text' => 'text/plain',
2927
            'txt' => 'text/plain',
2928
            'rtx' => 'text/richtext',
2929
            'rtf' => 'text/rtf',
2930
            'xml' => 'text/xml',
2931
            'xsl' => 'text/xml',
2932
            'mpeg' => 'video/mpeg',
2933
            'mpe' => 'video/mpeg',
2934
            'mpg' => 'video/mpeg',
2935
            'mov' => 'video/quicktime',
2936
            'qt' => 'video/quicktime',
2937
            'rv' => 'video/vnd.rn-realvideo',
2938
            'avi' => 'video/x-msvideo',
2939
            'movie' => 'video/x-sgi-movie'
2940
        );
2941
        return (array_key_exists(strtolower($ext), $mimes) ? $mimes[strtolower($ext)]: 'application/octet-stream');
2942 1463 Luisehahne
    }
2943
2944 2053 darkviper
    /**
2945
     * Map a file name to a MIME type.
2946
     * Defaults to 'application/octet-stream', i.e.. arbitrary binary data.
2947
     * @param string $filename A file name or full path, does not need to exist as a file
2948
     * @return string
2949
     * @static
2950
     */
2951
    public static function filenameToType($filename)
2952
    {
2953
        //In case the path is a URL, strip any query string before getting extension
2954
        $qpos = strpos($filename, '?');
2955
        if ($qpos !== false) {
2956
            $filename = substr($filename, 0, $qpos);
2957
        }
2958
        $pathinfo = self::mb_pathinfo($filename);
2959
        return self::_mime_types($pathinfo['extension']);
2960
    }
2961 1463 Luisehahne
2962 2053 darkviper
    /**
2963
     * Multi-byte-safe pathinfo replacement.
2964
     * Drop-in replacement for pathinfo(), but multibyte-safe, cross-platform-safe, old-version-safe.
2965
     * Works similarly to the one in PHP >= 5.2.0
2966
     * @link http://www.php.net/manual/en/function.pathinfo.php#107461
2967
     * @param string $path A filename or path, does not need to exist as a file
2968
     * @param integer|string $options Either a PATHINFO_* constant,
2969
     *      or a string name to return only the specified piece, allows 'filename' to work on PHP < 5.2
2970
     * @return string|array
2971
     * @static
2972
     */
2973
    public static function mb_pathinfo($path, $options = null)
2974
    {
2975
        $ret = array('dirname' => '', 'basename' => '', 'extension' => '', 'filename' => '');
2976
        $m = array();
2977
        preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
2978
        if (array_key_exists(1, $m)) {
2979
            $ret['dirname'] = $m[1];
2980
        }
2981
        if (array_key_exists(2, $m)) {
2982
            $ret['basename'] = $m[2];
2983
        }
2984
        if (array_key_exists(5, $m)) {
2985
            $ret['extension'] = $m[5];
2986
        }
2987
        if (array_key_exists(3, $m)) {
2988
            $ret['filename'] = $m[3];
2989
        }
2990
        switch ($options) {
2991
            case PATHINFO_DIRNAME:
2992
            case 'dirname':
2993
                return $ret['dirname'];
2994
                break;
2995
            case PATHINFO_BASENAME:
2996
            case 'basename':
2997
                return $ret['basename'];
2998
                break;
2999
            case PATHINFO_EXTENSION:
3000
            case 'extension':
3001
                return $ret['extension'];
3002
                break;
3003
            case PATHINFO_FILENAME:
3004
            case 'filename':
3005
                return $ret['filename'];
3006
                break;
3007
            default:
3008
                return $ret;
3009
        }
3010 1539 Luisehahne
    }
3011
3012 2053 darkviper
    /**
3013
     * Set or reset instance properties.
3014
     *
3015
     * Usage Example:
3016
     * $page->set('X-Priority', '3');
3017
     *
3018
     * @access public
3019
     * @param string $name
3020
     * @param mixed $value
3021
     * NOTE: will not work with arrays, there are no arrays to set/reset
3022
     * @throws phpmailerException
3023
     * @return bool
3024
     * @todo Should this not be using __set() magic function?
3025
     */
3026
    public function set($name, $value = '')
3027
    {
3028
        try {
3029
            if (isset($this->$name)) {
3030
                $this->$name = $value;
3031
            } else {
3032
                throw new phpmailerException($this->lang('variable_set') . $name, self::STOP_CRITICAL);
3033
            }
3034
        } catch (Exception $e) {
3035
            $this->setError($e->getMessage());
3036
            if ($e->getCode() == self::STOP_CRITICAL) {
3037
                return false;
3038
            }
3039
        }
3040
        return true;
3041
    }
3042 1539 Luisehahne
3043 2053 darkviper
    /**
3044
     * Strip newlines to prevent header injection.
3045
     * @access public
3046
     * @param string $str
3047
     * @return string
3048
     */
3049
    public function secureHeader($str)
3050
    {
3051
        return trim(str_replace(array("\r", "\n"), '', $str));
3052
    }
3053 1539 Luisehahne
3054 2053 darkviper
    /**
3055
     * Normalize line breaks in a string.
3056
     * Converts UNIX LF, Mac CR and Windows CRLF line breaks into a single line break format.
3057
     * Defaults to CRLF (for message bodies) and preserves consecutive breaks.
3058
     * @param string $text
3059
     * @param string $breaktype What kind of line break to use, defaults to CRLF
3060
     * @return string
3061
     * @access public
3062
     * @static
3063
     */
3064
    public static function normalizeBreaks($text, $breaktype = "\r\n")
3065
    {
3066
        return preg_replace('/(\r\n|\r|\n)/ms', $breaktype, $text);
3067 1463 Luisehahne
    }
3068
3069 2053 darkviper
3070
    /**
3071
     * Set the private key file and password for S/MIME signing.
3072
     * @access public
3073
     * @param string $cert_filename
3074
     * @param string $key_filename
3075
     * @param string $key_pass Password for private key
3076
     */
3077
    public function sign($cert_filename, $key_filename, $key_pass)
3078
    {
3079
        $this->sign_cert_file = $cert_filename;
3080
        $this->sign_key_file = $key_filename;
3081
        $this->sign_key_pass = $key_pass;
3082 1463 Luisehahne
    }
3083 2053 darkviper
3084
    /**
3085
     * Quoted-Printable-encode a DKIM header.
3086
     * @access public
3087
     * @param string $txt
3088
     * @return string
3089
     */
3090
    public function DKIM_QP($txt)
3091
    {
3092
        $line = '';
3093
        for ($i = 0; $i < strlen($txt); $i++) {
3094
            $ord = ord($txt[$i]);
3095
            if (((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E))) {
3096
                $line .= $txt[$i];
3097
            } else {
3098
                $line .= "=" . sprintf("%02X", $ord);
3099
            }
3100
        }
3101
        return $line;
3102 1539 Luisehahne
    }
3103 1463 Luisehahne
3104 2053 darkviper
    /**
3105
     * Generate a DKIM signature.
3106
     * @access public
3107
     * @param string $s Header
3108
     * @throws phpmailerException
3109
     * @return string
3110
     */
3111
    public function DKIM_Sign($s)
3112
    {
3113
        if (!defined('PKCS7_TEXT')) {
3114
            if ($this->exceptions) {
3115
                throw new phpmailerException($this->lang("signing") . ' OpenSSL extension missing.');
3116
            }
3117
            return '';
3118
        }
3119
        $privKeyStr = file_get_contents($this->DKIM_private);
3120
        if ($this->DKIM_passphrase != '') {
3121
            $privKey = openssl_pkey_get_private($privKeyStr, $this->DKIM_passphrase);
3122
        } else {
3123
            $privKey = $privKeyStr;
3124
        }
3125
        if (openssl_sign($s, $signature, $privKey)) {
3126
            return base64_encode($signature);
3127
        }
3128
        return '';
3129 1539 Luisehahne
    }
3130 1463 Luisehahne
3131 2053 darkviper
    /**
3132
     * Generate a DKIM canonicalization header.
3133
     * @access public
3134
     * @param string $s Header
3135
     * @return string
3136
     */
3137
    public function DKIM_HeaderC($s)
3138
    {
3139
        $s = preg_replace("/\r\n\s+/", " ", $s);
3140
        $lines = explode("\r\n", $s);
3141
        foreach ($lines as $key => $line) {
3142
            list($heading, $value) = explode(":", $line, 2);
3143
            $heading = strtolower($heading);
3144
            $value = preg_replace("/\s+/", " ", $value); // Compress useless spaces
3145
            $lines[$key] = $heading . ":" . trim($value); // Don't forget to remove WSP around the value
3146
        }
3147
        $s = implode("\r\n", $lines);
3148
        return $s;
3149 1539 Luisehahne
    }
3150 1463 Luisehahne
3151 2053 darkviper
    /**
3152
     * Generate a DKIM canonicalization body.
3153
     * @access public
3154
     * @param string $body Message Body
3155
     * @return string
3156
     */
3157
    public function DKIM_BodyC($body)
3158
    {
3159
        if ($body == '') {
3160
            return "\r\n";
3161
        }
3162
        // stabilize line endings
3163
        $body = str_replace("\r\n", "\n", $body);
3164
        $body = str_replace("\n", "\r\n", $body);
3165
        // END stabilize line endings
3166
        while (substr($body, strlen($body) - 4, 4) == "\r\n\r\n") {
3167
            $body = substr($body, 0, strlen($body) - 2);
3168
        }
3169
        return $body;
3170 1539 Luisehahne
    }
3171
3172 2053 darkviper
    /**
3173
     * Create the DKIM header and body in a new message header.
3174
     * @access public
3175
     * @param string $headers_line Header lines
3176
     * @param string $subject Subject
3177
     * @param string $body Body
3178
     * @return string
3179
     */
3180
    public function DKIM_Add($headers_line, $subject, $body)
3181
    {
3182
        $DKIMsignatureType = 'rsa-sha1'; // Signature & hash algorithms
3183
        $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
3184
        $DKIMquery = 'dns/txt'; // Query method
3185
        $DKIMtime = time(); // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
3186
        $subject_header = "Subject: $subject";
3187
        $headers = explode($this->LE, $headers_line);
3188
        $from_header = '';
3189
        $to_header = '';
3190
        $current = '';
3191
        foreach ($headers as $header) {
3192
            if (strpos($header, 'From:') === 0) {
3193
                $from_header = $header;
3194
                $current = 'from_header';
3195
            } elseif (strpos($header, 'To:') === 0) {
3196
                $to_header = $header;
3197
                $current = 'to_header';
3198
            } else {
3199
                if ($current && strpos($header, ' =?') === 0) {
3200
                    $current .= $header;
3201
                } else {
3202
                    $current = '';
3203
                }
3204
            }
3205
        }
3206
        $from = str_replace('|', '=7C', $this->DKIM_QP($from_header));
3207
        $to = str_replace('|', '=7C', $this->DKIM_QP($to_header));
3208
        $subject = str_replace(
3209
            '|',
3210
            '=7C',
3211
            $this->DKIM_QP($subject_header)
3212
        ); // Copied header fields (dkim-quoted-printable)
3213
        $body = $this->DKIM_BodyC($body);
3214
        $DKIMlen = strlen($body); // Length of body
3215
        $DKIMb64 = base64_encode(pack("H*", sha1($body))); // Base64 of packed binary SHA-1 hash of body
3216
        $ident = ($this->DKIM_identity == '') ? '' : " i=" . $this->DKIM_identity . ";";
3217
        $dkimhdrs = "DKIM-Signature: v=1; a=" .
3218
            $DKIMsignatureType . "; q=" .
3219
            $DKIMquery . "; l=" .
3220
            $DKIMlen . "; s=" .
3221
            $this->DKIM_selector .
3222
            ";\r\n" .
3223
            "\tt=" . $DKIMtime . "; c=" . $DKIMcanonicalization . ";\r\n" .
3224
            "\th=From:To:Subject;\r\n" .
3225
            "\td=" . $this->DKIM_domain . ";" . $ident . "\r\n" .
3226
            "\tz=$from\r\n" .
3227
            "\t|$to\r\n" .
3228
            "\t|$subject;\r\n" .
3229
            "\tbh=" . $DKIMb64 . ";\r\n" .
3230
            "\tb=";
3231
        $toSign = $this->DKIM_HeaderC(
3232
            $from_header . "\r\n" . $to_header . "\r\n" . $subject_header . "\r\n" . $dkimhdrs
3233
        );
3234
        $signed = $this->DKIM_Sign($toSign);
3235
        return $dkimhdrs . $signed . "\r\n";
3236 1539 Luisehahne
    }
3237 2053 darkviper
3238
    /**
3239
     * Perform a callback.
3240
     * @param bool $isSent
3241
     * @param string $to
3242
     * @param string $cc
3243
     * @param string $bcc
3244
     * @param string $subject
3245
     * @param string $body
3246
     * @param string $from
3247
     */
3248
    protected function doCallback($isSent, $to, $cc, $bcc, $subject, $body, $from = null)
3249
    {
3250
        if (!empty($this->action_function) && is_callable($this->action_function)) {
3251
            $params = array($isSent, $to, $cc, $bcc, $subject, $body, $from);
3252
            call_user_func_array($this->action_function, $params);
3253
        }
3254
    }
3255 1463 Luisehahne
}
3256
3257 2053 darkviper
/**
3258
 * PHPMailer exception handler
3259
 * @package PHPMailer
3260
 */
3261
class phpmailerException extends Exception
3262
{
3263
    /**
3264
     * Prettify error message output
3265
     * @return string
3266
     */
3267
    public function errorMessage()
3268
    {
3269
        $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
3270
        return $errorMsg;
3271
    }
3272 1539 Luisehahne
}