]>
git.wh0rd.org - tt-rss.git/blob - lib/phpmailer/class.phpmailer.php
9c286c9a100ce403401cb73c6b029d2af780df79
2 /*~ class.phpmailer.php
3 .---------------------------------------------------------------------------.
4 | Software: PHPMailer - PHP email class |
6 | Site: https://code.google.com/a/apache-extras.org/p/phpmailer/ |
7 | ------------------------------------------------------------------------- |
8 | Admin: Jim Jagielski (project admininistrator) |
9 | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
10 | : Marcus Bointon (coolbru) coolbru@users.sourceforge.net |
11 | : Jim Jagielski (jimjag) jimjag@gmail.com |
12 | Founder: Brent R. Matzelle (original founder) |
13 | Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved. |
14 | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
15 | Copyright (c) 2001-2003, Brent R. Matzelle |
16 | ------------------------------------------------------------------------- |
17 | License: Distributed under the Lesser General Public License (LGPL) |
18 | http://www.gnu.org/copyleft/lesser.html |
19 | This program is distributed in the hope that it will be useful - WITHOUT |
20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
21 | FITNESS FOR A PARTICULAR PURPOSE. |
22 '---------------------------------------------------------------------------'
26 * PHPMailer - PHP email creation and transport class
27 * NOTE: Requires PHP version 5 or later
29 * @author Andy Prevost
30 * @author Marcus Bointon
31 * @author Jim Jagielski
32 * @copyright 2010 - 2012 Jim Jagielski
33 * @copyright 2004 - 2009 Andy Prevost
34 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
37 if ( version_compare ( PHP_VERSION
, '5.0.0' , '<' ) ) exit ( "Sorry, this version of PHPMailer will only run on PHP version 5 or greater! \n " );
40 * PHP email creation and transport class
45 /////////////////////////////////////////////////
47 /////////////////////////////////////////////////
50 * Email priority (1 = High, 3 = Normal, 5 = low).
56 * Sets the CharSet of the message.
59 public $CharSet = 'iso-8859-1' ;
62 * Sets the Content-type of the message.
65 public $ContentType = 'text/plain' ;
68 * Sets the Encoding of the message. Options for this are
69 * "8bit", "7bit", "binary", "base64", and "quoted-printable".
72 public $Encoding = '8bit' ;
75 * Holds the most recent mailer error message.
78 public $ErrorInfo = '' ;
81 * Sets the From email address for the message.
84 public $From = 'root @localhost ' ;
87 * Sets the From name of the message.
90 public $FromName = 'Root User' ;
93 * Sets the Sender email (Return-Path) of the message. If not empty,
94 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
100 * Sets the Return-Path of the message. If empty, it will
101 * be set to either From or Sender.
104 public $ReturnPath = '' ;
107 * Sets the Subject of the message.
110 public $Subject = '' ;
113 * Sets the Body of the message. This can be either an HTML or text body.
114 * If HTML then run IsHTML(true).
120 * Sets the text-only body of the message. This automatically sets the
121 * email to multipart/alternative. This body can be read by mail
122 * clients that do not have HTML email capability such as mutt. Clients
123 * that can read HTML will view the normal Body.
126 public $AltBody = '' ;
129 * Stores the complete compiled MIME message body.
133 protected $MIMEBody = '' ;
136 * Stores the complete compiled MIME message headers.
140 protected $MIMEHeader = '' ;
143 * Stores the extra header list which CreateHeader() doesn't fold in
147 protected $mailHeader = '' ;
150 * Sets word wrapping on the body of the message to a given number of
154 public $WordWrap = 0 ;
157 * Method to send mail: ("mail", "sendmail", or "smtp").
160 public $Mailer = 'mail' ;
163 * Sets the path of the sendmail program.
166 public $Sendmail = '/usr/sbin/sendmail' ;
169 * Determine if mail() uses a fully sendmail compatible MTA that
170 * supports sendmail's "-oi -f" options
173 public $UseSendmailOptions = true ;
176 * Path to PHPMailer plugins. Useful if the SMTP class
177 * is in a different directory than the PHP include path.
180 public $PluginDir = '' ;
183 * Sets the email address that a reading confirmation will be sent.
186 public $ConfirmReadingTo = '' ;
189 * Sets the hostname to use in Message-Id and Received headers
190 * and as default HELO string. If empty, the value returned
191 * by SERVER_NAME is used or 'localhost.localdomain'.
194 public $Hostname = '' ;
197 * Sets the message ID to be used in the Message-Id header.
198 * If empty, a unique id will be generated.
201 public $MessageID = '' ;
204 * Sets the message Date to be used in the Date header.
205 * If empty, the current date will be added.
208 public $MessageDate = '' ;
210 /////////////////////////////////////////////////
211 // PROPERTIES FOR SMTP
212 /////////////////////////////////////////////////
215 * Sets the SMTP hosts.
217 * All hosts must be separated by a
218 * semicolon. You can also specify a different port
219 * for each host by using this format: [hostname:port]
220 * (e.g. "smtp1.example.com:25;smtp2.example.com").
221 * Hosts will be tried in order.
224 public $Host = 'localhost' ;
227 * Sets the default SMTP server port.
233 * Sets the SMTP HELO of the message (Default is $Hostname).
239 * Sets connection prefix. Options are "", "ssl" or "tls"
242 public $SMTPSecure = '' ;
245 * Sets SMTP authentication. Utilizes the Username and Password variables.
248 public $SMTPAuth = false ;
251 * Sets SMTP username.
254 public $Username = '' ;
257 * Sets SMTP password.
260 public $Password = '' ;
263 * Sets SMTP auth type. Options are LOGIN | PLAIN | NTLM (default LOGIN)
266 public $AuthType = '' ;
275 * Sets SMTP workstation.
278 public $Workstation = '' ;
281 * Sets the SMTP server timeout in seconds.
282 * This function will not work with the win32 version.
285 public $Timeout = 10 ;
288 * Sets SMTP class debugging on or off.
291 public $SMTPDebug = false ;
294 * Sets the function/method to use for debugging output.
295 * Right now we only honor "echo" or "error_log"
298 public $Debugoutput = "echo" ;
301 * Prevents the SMTP connection from being closed after each mail
302 * sending. If this is set to true then to close the connection
303 * requires an explicit call to SmtpClose().
306 public $SMTPKeepAlive = false ;
309 * Provides the ability to have the TO field process individual
310 * emails, instead of sending to entire TO addresses
313 public $SingleTo = false ;
316 * If SingleTo is true, this provides the array to hold the email addresses
319 public $SingleToArray = array ();
322 * Provides the ability to change the generic line ending
323 * NOTE: The default remains '\n'. We force CRLF where we KNOW
324 * it must be used via self::CRLF
330 * Used with DKIM Signing
331 * required parameter if DKIM is enabled
333 * domain selector example domainkey
336 public $DKIM_selector = '' ;
339 * Used with DKIM Signing
340 * required if DKIM is enabled, in format of email address 'you@yourdomain.com' typically used as the source of the email
343 public $DKIM_identity = '' ;
346 * Used with DKIM Signing
347 * optional parameter if your private key requires a passphras
350 public $DKIM_passphrase = '' ;
353 * Used with DKIM Singing
354 * required if DKIM is enabled, in format of email address 'domain.com'
357 public $DKIM_domain = '' ;
360 * Used with DKIM Signing
361 * required if DKIM is enabled, path to private key file
364 public $DKIM_private = '' ;
367 * Callback Action function name.
368 * The function that handles the result of the send email action.
369 * It is called out by Send() for each email sent.
372 * - 'function_name' for function names
373 * - 'Class::Method' for static method calls
374 * - array($object, 'Method') for calling methods on $object
375 * See http://php.net/is_callable manual page for more details.
378 * bool $result result of the send action
379 * string $to email address of the recipient
380 * string $cc cc email addresses
381 * string $bcc bcc email addresses
382 * string $subject the subject
383 * string $body the email body
384 * string $from email address of sender
387 public $action_function = '' ; //'callbackAction';
390 * Sets the PHPMailer Version number
393 public $Version = '5.2.4' ;
396 * What to use in the X-Mailer header
397 * @var string NULL for default, whitespace for None, or actual string to use
399 public $XMailer = '' ;
401 /////////////////////////////////////////////////
402 // PROPERTIES, PRIVATE AND PROTECTED
403 /////////////////////////////////////////////////
406 * @var SMTP An instance of the SMTP sender class
409 protected $smtp = null ;
411 * @var array An array of 'to' addresses
414 protected $to = array ();
416 * @var array An array of 'cc' addresses
419 protected $cc = array ();
421 * @var array An array of 'bcc' addresses
424 protected $bcc = array ();
426 * @var array An array of reply-to name and address
429 protected $ReplyTo = array ();
431 * @var array An array of all kinds of addresses: to, cc, bcc, replyto
434 protected $all_recipients = array ();
436 * @var array An array of attachments
439 protected $attachment = array ();
441 * @var array An array of custom headers
444 protected $CustomHeader = array ();
446 * @var string The message's MIME type
449 protected $message_type = '' ;
451 * @var array An array of MIME boundary strings
454 protected $boundary = array ();
456 * @var array An array of available languages
459 protected $language = array ();
461 * @var integer The number of errors encountered
464 protected $error_count = 0 ;
466 * @var string The filename of a DKIM certificate file
469 protected $sign_cert_file = '' ;
471 * @var string The filename of a DKIM key file
474 protected $sign_key_file = '' ;
476 * @var string The password of a DKIM key
479 protected $sign_key_pass = '' ;
481 * @var boolean Whether to throw exceptions for errors
484 protected $exceptions = false ;
486 /////////////////////////////////////////////////
488 /////////////////////////////////////////////////
490 const STOP_MESSAGE
= 0 ; // message only, continue processing
491 const STOP_CONTINUE
= 1 ; // message?, likely ok to continue processing
492 const STOP_CRITICAL
= 2 ; // message, plus full stop, critical error reached
493 const CRLF
= " \r\n " ; // SMTP RFC specified EOL
495 /////////////////////////////////////////////////
496 // METHODS, VARIABLES
497 /////////////////////////////////////////////////
500 * Calls actual mail() function, but in a safe_mode aware fashion
501 * Also, unless sendmail_path points to sendmail (or something that
502 * claims to be sendmail), don't pass params (not a perfect fix,
504 * @param string $to To
505 * @param string $subject Subject
506 * @param string $body Message Body
507 * @param string $header Additional Header(s)
508 * @param string $params Params
512 private function mail_passthru ( $to , $subject , $body , $header , $params ) {
513 if ( ini_get ( 'safe_mode' ) ||
!( $this -> UseSendmailOptions
) ) {
514 $rt = @mail
( $to , $this -> EncodeHeader ( $this -> SecureHeader ( $subject )), $body , $header );
516 $rt = @mail
( $to , $this -> EncodeHeader ( $this -> SecureHeader ( $subject )), $body , $header , $params );
522 * Outputs debugging info via user-defined method
525 private function edebug ( $str ) {
526 if ( $this -> Debugoutput
== "error_log" ) {
535 * @param boolean $exceptions Should we throw external exceptions?
537 public function __construct ( $exceptions = false ) {
538 $this -> exceptions
= ( $exceptions == true );
542 * Sets message type to HTML.
543 * @param bool $ishtml
546 public function IsHTML ( $ishtml = true ) {
548 $this -> ContentType
= 'text/html' ;
550 $this -> ContentType
= 'text/plain' ;
555 * Sets Mailer to send message using SMTP.
558 public function IsSMTP () {
559 $this -> Mailer
= 'smtp' ;
563 * Sets Mailer to send message using PHP mail() function.
566 public function IsMail () {
567 $this -> Mailer
= 'mail' ;
571 * Sets Mailer to send message using the $Sendmail program.
574 public function IsSendmail () {
575 if (! stristr ( ini_get ( 'sendmail_path' ), 'sendmail' )) {
576 $this -> Sendmail
= '/var/qmail/bin/sendmail' ;
578 $this -> Mailer
= 'sendmail' ;
582 * Sets Mailer to send message using the qmail MTA.
585 public function IsQmail () {
586 if ( stristr ( ini_get ( 'sendmail_path' ), 'qmail' )) {
587 $this -> Sendmail
= '/var/qmail/bin/sendmail' ;
589 $this -> Mailer
= 'sendmail' ;
592 /////////////////////////////////////////////////
593 // METHODS, RECIPIENTS
594 /////////////////////////////////////////////////
597 * Adds a "To" address.
598 * @param string $address
599 * @param string $name
600 * @return boolean true on success, false if address already used
602 public function AddAddress ( $address , $name = '' ) {
603 return $this -> AddAnAddress ( 'to' , $address , $name );
607 * Adds a "Cc" address.
608 * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
609 * @param string $address
610 * @param string $name
611 * @return boolean true on success, false if address already used
613 public function AddCC ( $address , $name = '' ) {
614 return $this -> AddAnAddress ( 'cc' , $address , $name );
618 * Adds a "Bcc" address.
619 * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
620 * @param string $address
621 * @param string $name
622 * @return boolean true on success, false if address already used
624 public function AddBCC ( $address , $name = '' ) {
625 return $this -> AddAnAddress ( 'bcc' , $address , $name );
629 * Adds a "Reply-to" address.
630 * @param string $address
631 * @param string $name
634 public function AddReplyTo ( $address , $name = '' ) {
635 return $this -> AddAnAddress ( 'Reply-To' , $address , $name );
639 * Adds an address to one of the recipient arrays
640 * Addresses that have been added already return false, but do not throw exceptions
641 * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
642 * @param string $address The email address to send to
643 * @param string $name
644 * @throws phpmailerException
645 * @return boolean true on success, false if address already used or invalid in some way
648 protected function AddAnAddress ( $kind , $address , $name = '' ) {
649 if (! preg_match ( '/^(to|cc|bcc|Reply-To)$/' , $kind )) {
650 $this -> SetError ( $this -> Lang ( 'Invalid recipient array' ). ': ' . $kind );
651 if ( $this -> exceptions
) {
652 throw new phpmailerException ( 'Invalid recipient array: ' . $kind );
654 if ( $this -> SMTPDebug
) {
655 $this -> edebug ( $this -> Lang ( 'Invalid recipient array' ). ': ' . $kind );
659 $address = trim ( $address );
660 $name = trim ( preg_replace ( '/[ \r\n ]+/' , '' , $name )); //Strip breaks and trim
661 if (! $this -> ValidateAddress ( $address )) {
662 $this -> SetError ( $this -> Lang ( 'invalid_address' ). ': ' . $address );
663 if ( $this -> exceptions
) {
664 throw new phpmailerException ( $this -> Lang ( 'invalid_address' ). ': ' . $address );
666 if ( $this -> SMTPDebug
) {
667 $this -> edebug ( $this -> Lang ( 'invalid_address' ). ': ' . $address );
671 if ( $kind != 'Reply-To' ) {
672 if (! isset ( $this -> all_recipients
[ strtolower ( $address )])) {
673 array_push ( $this -> $kind , array ( $address , $name ));
674 $this -> all_recipients
[ strtolower ( $address )] = true ;
678 if (! array_key_exists ( strtolower ( $address ), $this -> ReplyTo
)) {
679 $this -> ReplyTo
[ strtolower ( $address )] = array ( $address , $name );
687 * Set the From and FromName properties
688 * @param string $address
689 * @param string $name
690 * @param int $auto Also set Reply-To and Sender
691 * @throws phpmailerException
694 public function SetFrom ( $address , $name = '' , $auto = 1 ) {
695 $address = trim ( $address );
696 $name = trim ( preg_replace ( '/[ \r\n ]+/' , '' , $name )); //Strip breaks and trim
697 if (! $this -> ValidateAddress ( $address )) {
698 $this -> SetError ( $this -> Lang ( 'invalid_address' ). ': ' . $address );
699 if ( $this -> exceptions
) {
700 throw new phpmailerException ( $this -> Lang ( 'invalid_address' ). ': ' . $address );
702 if ( $this -> SMTPDebug
) {
703 $this -> edebug ( $this -> Lang ( 'invalid_address' ). ': ' . $address );
707 $this -> From
= $address ;
708 $this -> FromName
= $name ;
710 if ( empty ( $this -> ReplyTo
)) {
711 $this -> AddAnAddress ( 'Reply-To' , $address , $name );
713 if ( empty ( $this -> Sender
)) {
714 $this -> Sender
= $address ;
721 * Check that a string looks roughly like an email address should
722 * Static so it can be used without instantiation, public so people can overload
723 * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
724 * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
725 * not allow a@b type valid addresses :(
726 * Some Versions of PHP break on the regex though, likely due to PCRE, so use
727 * the older validation method for those users. (http://php.net/manual/en/pcre.installation.php)
728 * @link http://squiloople.com/2009/12/20/email-address-validation/
729 * @copyright regex Copyright Michael Rushton 2009-10 | http://squiloople.com/ | Feel free to use and redistribute this code. But please keep this copyright notice.
730 * @param string $address The email address to check
735 public static function ValidateAddress ( $address ) {
736 if (( defined ( 'PCRE_VERSION' )) && ( version_compare ( PCRE_VERSION
, '8.0' ) >= 0 )) {
737 return preg_match ( '/^(?!(?>(?1)"?(?> \\ \[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?> \\ \[ -~]|[^"])"?(?1)){65,}@)((?>(?>(?>((?>(?>(?> \x0D\x0A )?[ ])+|(?>[ ]* \x0D\x0A )?[ ]+)?)(\((?>(?2)(?>[ \x01 - \x08\x0B\x0C\x0E - \' *-\[\]- \x7F ]| \\ \[ \x00 - \x7F ]|(?3)))*(?2)\)))+(?2))|(?2))?)([!#- \' *+\/-9=?^-~-]+|"(?>(?2)(?>[ \x01 - \x08\x0B\x0C\x0E -!#-\[\]- \x7F ]| \\ \[ \x00 - \x7F ]))*(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}|(?!(?:.*[a-f0-9][:\]]){7,})((?6)(?>:(?6)){0,5})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:|(?!(?:.*[a-f0-9]:){5,})(?8)?::(?>((?6)(?>:(?6)){0,3}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD' , $address );
738 } elseif ( function_exists ( 'filter_var' )) { //Introduced in PHP 5.2
739 if ( filter_var ( $address , FILTER_VALIDATE_EMAIL
) === FALSE ) {
745 return preg_match ( '/^(?:[\w\!\#\$\%\& \' \*\+\-\/\= \? \^\`\{\|\}\~]+\.)*[\w\!\#\$\%\& \' \*\+\-\/\= \? \^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!\.)){0,61}[a-zA-Z0-9_-]?\.)+[a-zA-Z0-9_](?:[a-zA-Z0-9_\-](?!$)){0,61}[a-zA-Z0-9_]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/' , $address );
749 /////////////////////////////////////////////////
750 // METHODS, MAIL SENDING
751 /////////////////////////////////////////////////
754 * Creates message and assigns Mailer. If the message is
755 * not sent successfully then it returns false. Use the ErrorInfo
756 * variable to view description of the error.
757 * @throws phpmailerException
760 public function Send () {
762 if (! $this -> PreSend ()) return false ;
763 return $this -> PostSend ();
764 } catch ( phpmailerException
$e ) {
765 $this -> mailHeader
= '' ;
766 $this -> SetError ( $e -> getMessage ());
767 if ( $this -> exceptions
) {
775 * Prep mail by constructing all message entities
776 * @throws phpmailerException
779 public function PreSend () {
781 $this -> mailHeader
= "" ;
782 if (( count ( $this -> to
) +
count ( $this -> cc
) +
count ( $this -> bcc
)) < 1 ) {
783 throw new phpmailerException ( $this -> Lang ( 'provide_address' ), self
:: STOP_CRITICAL
);
786 // Set whether the message is multipart/alternative
787 if (! empty ( $this -> AltBody
)) {
788 $this -> ContentType
= 'multipart/alternative' ;
791 $this -> error_count
= 0 ; // reset errors
792 $this -> SetMessageType ();
793 //Refuse to send an empty message
794 if ( empty ( $this -> Body
)) {
795 throw new phpmailerException ( $this -> Lang ( 'empty_message' ), self
:: STOP_CRITICAL
);
798 $this -> MIMEHeader
= $this -> CreateHeader ();
799 $this -> MIMEBody
= $this -> CreateBody ();
801 // To capture the complete message when using mail(), create
802 // an extra header list which CreateHeader() doesn't fold in
803 if ( $this -> Mailer
== 'mail' ) {
804 if ( count ( $this -> to
) > 0 ) {
805 $this -> mailHeader
.= $this -> AddrAppend ( "To" , $this -> to
);
807 $this -> mailHeader
.= $this -> HeaderLine ( "To" , "undisclosed-recipients:;" );
809 $this -> mailHeader
.= $this -> HeaderLine ( 'Subject' , $this -> EncodeHeader ( $this -> SecureHeader ( trim ( $this -> Subject
))));
810 // if(count($this->cc) > 0) {
811 // $this->mailHeader .= $this->AddrAppend("Cc", $this->cc);
815 // digitally sign with DKIM if enabled
816 if (! empty ( $this -> DKIM_domain
) && ! empty ( $this -> DKIM_private
) && ! empty ( $this -> DKIM_selector
) && ! empty ( $this -> DKIM_domain
) && file_exists ( $this -> DKIM_private
)) {
817 $header_dkim = $this -> DKIM_Add ( $this -> MIMEHeader
, $this -> EncodeHeader ( $this -> SecureHeader ( $this -> Subject
)), $this -> MIMEBody
);
818 $this -> MIMEHeader
= str_replace ( " \r\n " , " \n " , $header_dkim ) . $this -> MIMEHeader
;
823 } catch ( phpmailerException
$e ) {
824 $this -> SetError ( $e -> getMessage ());
825 if ( $this -> exceptions
) {
833 * Actual Email transport function
834 * Send the email via the selected mechanism
835 * @throws phpmailerException
838 public function PostSend () {
840 // Choose the mailer and send through it
841 switch ( $this -> Mailer
) {
843 return $this -> SendmailSend ( $this -> MIMEHeader
, $this -> MIMEBody
);
845 return $this -> SmtpSend ( $this -> MIMEHeader
, $this -> MIMEBody
);
847 return $this -> MailSend ( $this -> MIMEHeader
, $this -> MIMEBody
);
849 return $this -> MailSend ( $this -> MIMEHeader
, $this -> MIMEBody
);
851 } catch ( phpmailerException
$e ) {
852 $this -> SetError ( $e -> getMessage ());
853 if ( $this -> exceptions
) {
856 if ( $this -> SMTPDebug
) {
857 $this -> edebug ( $e -> getMessage (). " \n " );
864 * Sends mail using the $Sendmail program.
865 * @param string $header The message headers
866 * @param string $body The message body
867 * @throws phpmailerException
871 protected function SendmailSend ( $header , $body ) {
872 if ( $this -> Sender
!= '' ) {
873 $sendmail = sprintf ( " %s -oi -f %s -t" , escapeshellcmd ( $this -> Sendmail
), escapeshellarg ( $this -> Sender
));
875 $sendmail = sprintf ( " %s -oi -t" , escapeshellcmd ( $this -> Sendmail
));
877 if ( $this -> SingleTo
=== true ) {
878 foreach ( $this -> SingleToArray
as $val ) {
879 if (!@ $mail = popen ( $sendmail , 'w' )) {
880 throw new phpmailerException ( $this -> Lang ( 'execute' ) . $this -> Sendmail
, self
:: STOP_CRITICAL
);
882 fputs ( $mail , "To: " . $val . " \n " );
883 fputs ( $mail , $header );
885 $result = pclose ( $mail );
886 // implement call back function if it exists
887 $isSent = ( $result == 0 ) ?
1 : 0 ;
888 $this -> doCallback ( $isSent , $val , $this -> cc
, $this -> bcc
, $this -> Subject
, $body );
890 throw new phpmailerException ( $this -> Lang ( 'execute' ) . $this -> Sendmail
, self
:: STOP_CRITICAL
);
894 if (!@ $mail = popen ( $sendmail , 'w' )) {
895 throw new phpmailerException ( $this -> Lang ( 'execute' ) . $this -> Sendmail
, self
:: STOP_CRITICAL
);
897 fputs ( $mail , $header );
899 $result = pclose ( $mail );
900 // implement call back function if it exists
901 $isSent = ( $result == 0 ) ?
1 : 0 ;
902 $this -> doCallback ( $isSent , $this -> to
, $this -> cc
, $this -> bcc
, $this -> Subject
, $body );
904 throw new phpmailerException ( $this -> Lang ( 'execute' ) . $this -> Sendmail
, self
:: STOP_CRITICAL
);
911 * Sends mail using the PHP mail() function.
912 * @param string $header The message headers
913 * @param string $body The message body
914 * @throws phpmailerException
918 protected function MailSend ( $header , $body ) {
920 foreach ( $this -> to
as $t ) {
921 $toArr [] = $this -> AddrFormat ( $t );
923 $to = implode ( ', ' , $toArr );
925 if ( empty ( $this -> Sender
)) {
928 $params = sprintf ( "-oi -f %s " , $this -> Sender
);
930 if ( $this -> Sender
!= '' and ! ini_get ( 'safe_mode' )) {
931 $old_from = ini_get ( 'sendmail_from' );
932 ini_set ( 'sendmail_from' , $this -> Sender
);
935 if ( $this -> SingleTo
=== true && count ( $toArr ) > 1 ) {
936 foreach ( $toArr as $val ) {
937 $rt = $this -> mail_passthru ( $val , $this -> Subject
, $body , $header , $params );
938 // implement call back function if it exists
939 $isSent = ( $rt == 1 ) ?
1 : 0 ;
940 $this -> doCallback ( $isSent , $val , $this -> cc
, $this -> bcc
, $this -> Subject
, $body );
943 $rt = $this -> mail_passthru ( $to , $this -> Subject
, $body , $header , $params );
944 // implement call back function if it exists
945 $isSent = ( $rt == 1 ) ?
1 : 0 ;
946 $this -> doCallback ( $isSent , $to , $this -> cc
, $this -> bcc
, $this -> Subject
, $body );
948 if ( isset ( $old_from )) {
949 ini_set ( 'sendmail_from' , $old_from );
952 throw new phpmailerException ( $this -> Lang ( 'instantiate' ), self
:: STOP_CRITICAL
);
958 * Sends mail via SMTP using PhpSMTP
959 * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
960 * @param string $header The message headers
961 * @param string $body The message body
962 * @throws phpmailerException
967 protected function SmtpSend ( $header , $body ) {
968 require_once $this -> PluginDir
. 'class.smtp.php' ;
971 if (! $this -> SmtpConnect ()) {
972 throw new phpmailerException ( $this -> Lang ( 'smtp_connect_failed' ), self
:: STOP_CRITICAL
);
974 $smtp_from = ( $this -> Sender
== '' ) ?
$this -> From
: $this -> Sender
;
975 if (! $this -> smtp
-> Mail ( $smtp_from )) {
976 $this -> SetError ( $this -> Lang ( 'from_failed' ) . $smtp_from . " : " . implode ( "," , $this -> smtp
-> getError ())) ;
977 throw new phpmailerException ( $this -> ErrorInfo
, self
:: STOP_CRITICAL
);
980 // Attempt to send attach all recipients
981 foreach ( $this -> to
as $to ) {
982 if (! $this -> smtp
-> Recipient ( $to [ 0 ])) {
983 $bad_rcpt [] = $to [ 0 ];
984 // implement call back function if it exists
986 $this -> doCallback ( $isSent , $to [ 0 ], '' , '' , $this -> Subject
, $body );
988 // implement call back function if it exists
990 $this -> doCallback ( $isSent , $to [ 0 ], '' , '' , $this -> Subject
, $body );
993 foreach ( $this -> cc
as $cc ) {
994 if (! $this -> smtp
-> Recipient ( $cc [ 0 ])) {
995 $bad_rcpt [] = $cc [ 0 ];
996 // implement call back function if it exists
998 $this -> doCallback ( $isSent , '' , $cc [ 0 ], '' , $this -> Subject
, $body );
1000 // implement call back function if it exists
1002 $this -> doCallback ( $isSent , '' , $cc [ 0 ], '' , $this -> Subject
, $body );
1005 foreach ( $this -> bcc
as $bcc ) {
1006 if (! $this -> smtp
-> Recipient ( $bcc [ 0 ])) {
1007 $bad_rcpt [] = $bcc [ 0 ];
1008 // implement call back function if it exists
1010 $this -> doCallback ( $isSent , '' , '' , $bcc [ 0 ], $this -> Subject
, $body );
1012 // implement call back function if it exists
1014 $this -> doCallback ( $isSent , '' , '' , $bcc [ 0 ], $this -> Subject
, $body );
1019 if ( count ( $bad_rcpt ) > 0 ) { //Create error message for any bad addresses
1020 $badaddresses = implode ( ', ' , $bad_rcpt );
1021 throw new phpmailerException ( $this -> Lang ( 'recipients_failed' ) . $badaddresses );
1023 if (! $this -> smtp
-> Data ( $header . $body )) {
1024 throw new phpmailerException ( $this -> Lang ( 'data_not_accepted' ), self
:: STOP_CRITICAL
);
1026 if ( $this -> SMTPKeepAlive
== true ) {
1027 $this -> smtp
-> Reset ();
1029 $this -> smtp
-> Quit ();
1030 $this -> smtp
-> Close ();
1036 * Initiates a connection to an SMTP server.
1037 * Returns false if the operation failed.
1040 * @throws phpmailerException
1043 public function SmtpConnect () {
1044 if ( is_null ( $this -> smtp
)) {
1045 $this -> smtp
= new SMTP
;
1048 $this -> smtp
-> Timeout
= $this -> Timeout
;
1049 $this -> smtp
-> do_debug
= $this -> SMTPDebug
;
1050 $hosts = explode ( ';' , $this -> Host
);
1052 $connection = $this -> smtp
-> Connected ();
1054 // Retry while there is no connection
1056 while ( $index < count ( $hosts ) && ! $connection ) {
1057 $hostinfo = array ();
1058 if ( preg_match ( '/^(.+):([0-9]+)$/' , $hosts [ $index ], $hostinfo )) {
1059 $host = $hostinfo [ 1 ];
1060 $port = $hostinfo [ 2 ];
1062 $host = $hosts [ $index ];
1063 $port = $this -> Port
;
1066 $tls = ( $this -> SMTPSecure
== 'tls' );
1067 $ssl = ( $this -> SMTPSecure
== 'ssl' );
1069 if ( $this -> smtp
-> Connect (( $ssl ?
'ssl://' : '' ). $host , $port , $this -> Timeout
)) {
1071 $hello = ( $this -> Helo
!= '' ?
$this -> Helo
: $this -> ServerHostname ());
1072 $this -> smtp
-> Hello ( $hello );
1075 if (! $this -> smtp
-> StartTLS ()) {
1076 throw new phpmailerException ( $this -> Lang ( 'connect_host' ));
1079 //We must resend HELO after tls negotiation
1080 $this -> smtp
-> Hello ( $hello );
1084 if ( $this -> SMTPAuth
) {
1085 if (! $this -> smtp
-> Authenticate ( $this -> Username
, $this -> Password
, $this -> AuthType
,
1086 $this -> Realm
, $this -> Workstation
)) {
1087 throw new phpmailerException ( $this -> Lang ( 'authenticate' ));
1093 throw new phpmailerException ( $this -> Lang ( 'connect_host' ));
1096 } catch ( phpmailerException
$e ) {
1097 $this -> smtp
-> Reset ();
1098 if ( $this -> exceptions
) {
1106 * Closes the active SMTP session if one exists.
1109 public function SmtpClose () {
1110 if ( $this -> smtp
!== null ) {
1111 if ( $this -> smtp
-> Connected ()) {
1112 $this -> smtp
-> Quit ();
1113 $this -> smtp
-> Close ();
1119 * Sets the language for all class error messages.
1120 * Returns false if it cannot load the language file. The default language is English.
1121 * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
1122 * @param string $lang_path Path to the language file directory
1126 function SetLanguage ( $langcode = 'en' , $lang_path = 'language/' ) {
1127 //Define full set of translatable strings
1128 $PHPMAILER_LANG = array (
1129 'authenticate' => 'SMTP Error: Could not authenticate.' ,
1130 'connect_host' => 'SMTP Error: Could not connect to SMTP host.' ,
1131 'data_not_accepted' => 'SMTP Error: Data not accepted.' ,
1132 'empty_message' => 'Message body empty' ,
1133 'encoding' => 'Unknown encoding: ' ,
1134 'execute' => 'Could not execute: ' ,
1135 'file_access' => 'Could not access file: ' ,
1136 'file_open' => 'File Error: Could not open file: ' ,
1137 'from_failed' => 'The following From address failed: ' ,
1138 'instantiate' => 'Could not instantiate mail function.' ,
1139 'invalid_address' => 'Invalid address' ,
1140 'mailer_not_supported' => ' mailer is not supported.' ,
1141 'provide_address' => 'You must provide at least one recipient email address.' ,
1142 'recipients_failed' => 'SMTP Error: The following recipients failed: ' ,
1143 'signing' => 'Signing Error: ' ,
1144 'smtp_connect_failed' => 'SMTP Connect() failed.' ,
1145 'smtp_error' => 'SMTP server error: ' ,
1146 'variable_set' => 'Cannot set or reset variable: '
1148 //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
1150 if ( $langcode != 'en' ) { //There is no English translation file
1151 $l = @include
$lang_path . 'phpmailer.lang-' . $langcode . '.php' ;
1153 $this -> language
= $PHPMAILER_LANG ;
1154 return ( $l == true ); //Returns false if language not found
1158 * Return the current array of language strings
1161 public function GetTranslations () {
1162 return $this -> language
;
1165 /////////////////////////////////////////////////
1166 // METHODS, MESSAGE CREATION
1167 /////////////////////////////////////////////////
1170 * Creates recipient headers.
1172 * @param string $type
1173 * @param array $addr
1176 public function AddrAppend ( $type , $addr ) {
1177 $addr_str = $type . ': ' ;
1178 $addresses = array ();
1179 foreach ( $addr as $a ) {
1180 $addresses [] = $this -> AddrFormat ( $a );
1182 $addr_str .= implode ( ', ' , $addresses );
1183 $addr_str .= $this -> LE
;
1189 * Formats an address correctly.
1191 * @param string $addr
1194 public function AddrFormat ( $addr ) {
1195 if ( empty ( $addr [ 1 ])) {
1196 return $this -> SecureHeader ( $addr [ 0 ]);
1198 return $this -> EncodeHeader ( $this -> SecureHeader ( $addr [ 1 ]), 'phrase' ) . " <" . $this -> SecureHeader ( $addr [ 0 ]) . ">" ;
1203 * Wraps message for use with mailers that do not
1204 * automatically perform wrapping and for quoted-printable.
1205 * Original written by philippe.
1206 * @param string $message The message to wrap
1207 * @param integer $length The line length to wrap to
1208 * @param boolean $qp_mode Whether to run in Quoted-Printable mode
1212 public function WrapText ( $message , $length , $qp_mode = false ) {
1213 $soft_break = ( $qp_mode ) ?
sprintf ( " = %s " , $this -> LE
) : $this -> LE
;
1214 // If utf-8 encoding is used, we will need to make sure we don't
1215 // split multibyte characters when we wrap
1216 $is_utf8 = ( strtolower ( $this -> CharSet
) == "utf-8" );
1217 $lelen = strlen ( $this -> LE
);
1218 $crlflen = strlen ( self
:: CRLF
);
1220 $message = $this -> FixEOL ( $message );
1221 if ( substr ( $message , - $lelen ) == $this -> LE
) {
1222 $message = substr ( $message , 0 , - $lelen );
1225 $line = explode ( $this -> LE
, $message ); // Magic. We know FixEOL uses $LE
1227 for ( $i = 0 ; $i < count ( $line ); $i ++
) {
1228 $line_part = explode ( ' ' , $line [ $i ]);
1230 for ( $e = 0 ; $e < count ( $line_part ); $e ++
) {
1231 $word = $line_part [ $e ];
1232 if ( $qp_mode and ( strlen ( $word ) > $length )) {
1233 $space_left = $length - strlen ( $buf ) - $crlflen ;
1235 if ( $space_left > 20 ) {
1238 $len = $this -> UTF8CharBoundary ( $word , $len );
1239 } elseif ( substr ( $word , $len - 1 , 1 ) == "=" ) {
1241 } elseif ( substr ( $word , $len - 2 , 1 ) == "=" ) {
1244 $part = substr ( $word , 0 , $len );
1245 $word = substr ( $word , $len );
1246 $buf .= ' ' . $part ;
1247 $message .= $buf . sprintf ( "= %s " , self
:: CRLF
);
1249 $message .= $buf . $soft_break ;
1253 while ( strlen ( $word ) > 0 ) {
1256 $len = $this -> UTF8CharBoundary ( $word , $len );
1257 } elseif ( substr ( $word , $len - 1 , 1 ) == "=" ) {
1259 } elseif ( substr ( $word , $len - 2 , 1 ) == "=" ) {
1262 $part = substr ( $word , 0 , $len );
1263 $word = substr ( $word , $len );
1265 if ( strlen ( $word ) > 0 ) {
1266 $message .= $part . sprintf ( "= %s " , self
:: CRLF
);
1273 $buf .= ( $e == 0 ) ?
$word : ( ' ' . $word );
1275 if ( strlen ( $buf ) > $length and $buf_o != '' ) {
1276 $message .= $buf_o . $soft_break ;
1281 $message .= $buf . self
:: CRLF
;
1288 * Finds last character boundary prior to maxLength in a utf-8
1289 * quoted (printable) encoded string.
1290 * Original written by Colin Brown.
1292 * @param string $encodedText utf-8 QP text
1293 * @param int $maxLength find last character boundary prior to this length
1296 public function UTF8CharBoundary ( $encodedText , $maxLength ) {
1297 $foundSplitPos = false ;
1299 while (! $foundSplitPos ) {
1300 $lastChunk = substr ( $encodedText , $maxLength - $lookBack , $lookBack );
1301 $encodedCharPos = strpos ( $lastChunk , "=" );
1302 if ( $encodedCharPos !== false ) {
1303 // Found start of encoded character byte within $lookBack block.
1304 // Check the encoded byte value (the 2 chars after the '=')
1305 $hex = substr ( $encodedText , $maxLength - $lookBack +
$encodedCharPos +
1 , 2 );
1306 $dec = hexdec ( $hex );
1307 if ( $dec < 128 ) { // Single byte character.
1308 // If the encoded char was found at pos 0, it will fit
1309 // otherwise reduce maxLength to start of the encoded char
1310 $maxLength = ( $encodedCharPos == 0 ) ?
$maxLength :
1311 $maxLength - ( $lookBack - $encodedCharPos );
1312 $foundSplitPos = true ;
1313 } elseif ( $dec >= 192 ) { // First byte of a multi byte character
1314 // Reduce maxLength to split at start of character
1315 $maxLength = $maxLength - ( $lookBack - $encodedCharPos );
1316 $foundSplitPos = true ;
1317 } elseif ( $dec < 192 ) { // Middle byte of a multi byte character, look further back
1321 // No encoded character found
1322 $foundSplitPos = true ;
1330 * Set the body wrapping.
1334 public function SetWordWrap () {
1335 if ( $this -> WordWrap
< 1 ) {
1339 switch ( $this -> message_type
) {
1343 case 'alt_inline_attach' :
1344 $this -> AltBody
= $this -> WrapText ( $this -> AltBody
, $this -> WordWrap
);
1347 $this -> Body
= $this -> WrapText ( $this -> Body
, $this -> WordWrap
);
1353 * Assembles message header.
1355 * @return string The assembled header
1357 public function CreateHeader () {
1360 // Set the boundaries
1361 $uniq_id = md5 ( uniqid ( time ()));
1362 $this -> boundary
[ 1 ] = 'b1_' . $uniq_id ;
1363 $this -> boundary
[ 2 ] = 'b2_' . $uniq_id ;
1364 $this -> boundary
[ 3 ] = 'b3_' . $uniq_id ;
1366 if ( $this -> MessageDate
== '' ) {
1367 $result .= $this -> HeaderLine ( 'Date' , self
:: RFCDate ());
1369 $result .= $this -> HeaderLine ( 'Date' , $this -> MessageDate
);
1372 if ( $this -> ReturnPath
) {
1373 $result .= $this -> HeaderLine ( 'Return-Path' , trim ( $this -> ReturnPath
));
1374 } elseif ( $this -> Sender
== '' ) {
1375 $result .= $this -> HeaderLine ( 'Return-Path' , trim ( $this -> From
));
1377 $result .= $this -> HeaderLine ( 'Return-Path' , trim ( $this -> Sender
));
1380 // To be created automatically by mail()
1381 if ( $this -> Mailer
!= 'mail' ) {
1382 if ( $this -> SingleTo
=== true ) {
1383 foreach ( $this -> to
as $t ) {
1384 $this -> SingleToArray
[] = $this -> AddrFormat ( $t );
1387 if ( count ( $this -> to
) > 0 ) {
1388 $result .= $this -> AddrAppend ( 'To' , $this -> to
);
1389 } elseif ( count ( $this -> cc
) == 0 ) {
1390 $result .= $this -> HeaderLine ( 'To' , 'undisclosed-recipients:;' );
1396 $from [ 0 ][ 0 ] = trim ( $this -> From
);
1397 $from [ 0 ][ 1 ] = $this -> FromName
;
1398 $result .= $this -> AddrAppend ( 'From' , $from );
1400 // sendmail and mail() extract Cc from the header before sending
1401 if ( count ( $this -> cc
) > 0 ) {
1402 $result .= $this -> AddrAppend ( 'Cc' , $this -> cc
);
1405 // sendmail and mail() extract Bcc from the header before sending
1406 if ((( $this -> Mailer
== 'sendmail' ) ||
( $this -> Mailer
== 'mail' )) && ( count ( $this -> bcc
) > 0 )) {
1407 $result .= $this -> AddrAppend ( 'Bcc' , $this -> bcc
);
1410 if ( count ( $this -> ReplyTo
) > 0 ) {
1411 $result .= $this -> AddrAppend ( 'Reply-To' , $this -> ReplyTo
);
1414 // mail() sets the subject itself
1415 if ( $this -> Mailer
!= 'mail' ) {
1416 $result .= $this -> HeaderLine ( 'Subject' , $this -> EncodeHeader ( $this -> SecureHeader ( $this -> Subject
)));
1419 if ( $this -> MessageID
!= '' ) {
1420 $result .= $this -> HeaderLine ( 'Message-ID' , $this -> MessageID
);
1422 $result .= sprintf ( "Message-ID: < %s @ %s > %s " , $uniq_id , $this -> ServerHostname (), $this -> LE
);
1424 $result .= $this -> HeaderLine ( 'X-Priority' , $this -> Priority
);
1425 if ( $this -> XMailer
== '' ) {
1426 $result .= $this -> HeaderLine ( 'X-Mailer' , 'PHPMailer ' . $this -> Version
. ' (http://code.google.com/a/apache-extras.org/p/phpmailer/)' );
1428 $myXmailer = trim ( $this -> XMailer
);
1430 $result .= $this -> HeaderLine ( 'X-Mailer' , $myXmailer );
1434 if ( $this -> ConfirmReadingTo
!= '' ) {
1435 $result .= $this -> HeaderLine ( 'Disposition-Notification-To' , '<' . trim ( $this -> ConfirmReadingTo
) . '>' );
1438 // Add custom headers
1439 for ( $index = 0 ; $index < count ( $this -> CustomHeader
); $index ++
) {
1440 $result .= $this -> HeaderLine ( trim ( $this -> CustomHeader
[ $index ][ 0 ]), $this -> EncodeHeader ( trim ( $this -> CustomHeader
[ $index ][ 1 ])));
1442 if (! $this -> sign_key_file
) {
1443 $result .= $this -> HeaderLine ( 'MIME-Version' , '1.0' );
1444 $result .= $this -> GetMailMIME ();
1451 * Returns the message MIME.
1455 public function GetMailMIME () {
1457 switch ( $this -> message_type
) {
1459 $result .= $this -> HeaderLine ( 'Content-Type' , 'multipart/related;' );
1460 $result .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 1 ] . '"' );
1463 case 'inline_attach' :
1465 case 'alt_inline_attach' :
1466 $result .= $this -> HeaderLine ( 'Content-Type' , 'multipart/mixed;' );
1467 $result .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 1 ] . '"' );
1471 $result .= $this -> HeaderLine ( 'Content-Type' , 'multipart/alternative;' );
1472 $result .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 1 ] . '"' );
1475 // Catches case 'plain': and case '':
1476 $result .= $this -> HeaderLine ( 'Content-Transfer-Encoding' , $this -> Encoding
);
1477 $result .= $this -> TextLine ( 'Content-Type: ' . $this -> ContentType
. '; charset=' . $this -> CharSet
);
1481 if ( $this -> Mailer
!= 'mail' ) {
1482 $result .= $this -> LE
;
1489 * Returns the MIME message (headers and body). Only really valid post PreSend().
1493 public function GetSentMIMEMessage () {
1494 return $this -> MIMEHeader
. $this -> mailHeader
. self
:: CRLF
. $this -> MIMEBody
;
1499 * Assembles the message body. Returns an empty string on failure.
1501 * @throws phpmailerException
1502 * @return string The assembled message body
1504 public function CreateBody () {
1507 if ( $this -> sign_key_file
) {
1508 $body .= $this -> GetMailMIME (). $this -> LE
;
1511 $this -> SetWordWrap ();
1513 switch ( $this -> message_type
) {
1515 $body .= $this -> GetBoundary ( $this -> boundary
[ 1 ], '' , '' , '' );
1516 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1517 $body .= $this -> LE
. $this -> LE
;
1518 $body .= $this -> AttachAll ( "inline" , $this -> boundary
[ 1 ]);
1521 $body .= $this -> GetBoundary ( $this -> boundary
[ 1 ], '' , '' , '' );
1522 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1523 $body .= $this -> LE
. $this -> LE
;
1524 $body .= $this -> AttachAll ( "attachment" , $this -> boundary
[ 1 ]);
1526 case 'inline_attach' :
1527 $body .= $this -> TextLine ( "--" . $this -> boundary
[ 1 ]);
1528 $body .= $this -> HeaderLine ( 'Content-Type' , 'multipart/related;' );
1529 $body .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 2 ] . '"' );
1531 $body .= $this -> GetBoundary ( $this -> boundary
[ 2 ], '' , '' , '' );
1532 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1533 $body .= $this -> LE
. $this -> LE
;
1534 $body .= $this -> AttachAll ( "inline" , $this -> boundary
[ 2 ]);
1536 $body .= $this -> AttachAll ( "attachment" , $this -> boundary
[ 1 ]);
1539 $body .= $this -> GetBoundary ( $this -> boundary
[ 1 ], '' , 'text/plain' , '' );
1540 $body .= $this -> EncodeString ( $this -> AltBody
, $this -> Encoding
);
1541 $body .= $this -> LE
. $this -> LE
;
1542 $body .= $this -> GetBoundary ( $this -> boundary
[ 1 ], '' , 'text/html' , '' );
1543 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1544 $body .= $this -> LE
. $this -> LE
;
1545 $body .= $this -> EndBoundary ( $this -> boundary
[ 1 ]);
1548 $body .= $this -> GetBoundary ( $this -> boundary
[ 1 ], '' , 'text/plain' , '' );
1549 $body .= $this -> EncodeString ( $this -> AltBody
, $this -> Encoding
);
1550 $body .= $this -> LE
. $this -> LE
;
1551 $body .= $this -> TextLine ( "--" . $this -> boundary
[ 1 ]);
1552 $body .= $this -> HeaderLine ( 'Content-Type' , 'multipart/related;' );
1553 $body .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 2 ] . '"' );
1555 $body .= $this -> GetBoundary ( $this -> boundary
[ 2 ], '' , 'text/html' , '' );
1556 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1557 $body .= $this -> LE
. $this -> LE
;
1558 $body .= $this -> AttachAll ( "inline" , $this -> boundary
[ 2 ]);
1560 $body .= $this -> EndBoundary ( $this -> boundary
[ 1 ]);
1563 $body .= $this -> TextLine ( "--" . $this -> boundary
[ 1 ]);
1564 $body .= $this -> HeaderLine ( 'Content-Type' , 'multipart/alternative;' );
1565 $body .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 2 ] . '"' );
1567 $body .= $this -> GetBoundary ( $this -> boundary
[ 2 ], '' , 'text/plain' , '' );
1568 $body .= $this -> EncodeString ( $this -> AltBody
, $this -> Encoding
);
1569 $body .= $this -> LE
. $this -> LE
;
1570 $body .= $this -> GetBoundary ( $this -> boundary
[ 2 ], '' , 'text/html' , '' );
1571 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1572 $body .= $this -> LE
. $this -> LE
;
1573 $body .= $this -> EndBoundary ( $this -> boundary
[ 2 ]);
1575 $body .= $this -> AttachAll ( "attachment" , $this -> boundary
[ 1 ]);
1577 case 'alt_inline_attach' :
1578 $body .= $this -> TextLine ( "--" . $this -> boundary
[ 1 ]);
1579 $body .= $this -> HeaderLine ( 'Content-Type' , 'multipart/alternative;' );
1580 $body .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 2 ] . '"' );
1582 $body .= $this -> GetBoundary ( $this -> boundary
[ 2 ], '' , 'text/plain' , '' );
1583 $body .= $this -> EncodeString ( $this -> AltBody
, $this -> Encoding
);
1584 $body .= $this -> LE
. $this -> LE
;
1585 $body .= $this -> TextLine ( "--" . $this -> boundary
[ 2 ]);
1586 $body .= $this -> HeaderLine ( 'Content-Type' , 'multipart/related;' );
1587 $body .= $this -> TextLine ( " \t boundary= \" " . $this -> boundary
[ 3 ] . '"' );
1589 $body .= $this -> GetBoundary ( $this -> boundary
[ 3 ], '' , 'text/html' , '' );
1590 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1591 $body .= $this -> LE
. $this -> LE
;
1592 $body .= $this -> AttachAll ( "inline" , $this -> boundary
[ 3 ]);
1594 $body .= $this -> EndBoundary ( $this -> boundary
[ 2 ]);
1596 $body .= $this -> AttachAll ( "attachment" , $this -> boundary
[ 1 ]);
1599 // catch case 'plain' and case ''
1600 $body .= $this -> EncodeString ( $this -> Body
, $this -> Encoding
);
1604 if ( $this -> IsError ()) {
1606 } elseif ( $this -> sign_key_file
) {
1608 $file = tempnam ( '' , 'mail' );
1609 file_put_contents ( $file , $body ); //TODO check this worked
1610 $signed = tempnam ( "" , "signed" );
1611 if ( @openssl_pkcs7_sign
( $file , $signed , "file://" . $this -> sign_cert_file
, array ( "file://" . $this -> sign_key_file
, $this -> sign_key_pass
), NULL )) {
1613 $body = file_get_contents ( $signed );
1618 throw new phpmailerException ( $this -> Lang ( "signing" ). openssl_error_string ());
1620 } catch ( phpmailerException
$e ) {
1622 if ( $this -> exceptions
) {
1632 * Returns the start of a message boundary.
1634 * @param string $boundary
1635 * @param string $charSet
1636 * @param string $contentType
1637 * @param string $encoding
1640 protected function GetBoundary ( $boundary , $charSet , $contentType , $encoding ) {
1642 if ( $charSet == '' ) {
1643 $charSet = $this -> CharSet
;
1645 if ( $contentType == '' ) {
1646 $contentType = $this -> ContentType
;
1648 if ( $encoding == '' ) {
1649 $encoding = $this -> Encoding
;
1651 $result .= $this -> TextLine ( '--' . $boundary );
1652 $result .= sprintf ( "Content-Type: %s ; charset= %s " , $contentType , $charSet );
1653 $result .= $this -> LE
;
1654 $result .= $this -> HeaderLine ( 'Content-Transfer-Encoding' , $encoding );
1655 $result .= $this -> LE
;
1661 * Returns the end of a message boundary.
1663 * @param string $boundary
1666 protected function EndBoundary ( $boundary ) {
1667 return $this -> LE
. '--' . $boundary . '--' . $this -> LE
;
1671 * Sets the message type.
1675 protected function SetMessageType () {
1676 $this -> message_type
= array ();
1677 if ( $this -> AlternativeExists ()) $this -> message_type
[] = "alt" ;
1678 if ( $this -> InlineImageExists ()) $this -> message_type
[] = "inline" ;
1679 if ( $this -> AttachmentExists ()) $this -> message_type
[] = "attach" ;
1680 $this -> message_type
= implode ( "_" , $this -> message_type
);
1681 if ( $this -> message_type
== "" ) $this -> message_type
= "plain" ;
1685 * Returns a formatted header line.
1687 * @param string $name
1688 * @param string $value
1691 public function HeaderLine ( $name , $value ) {
1692 return $name . ': ' . $value . $this -> LE
;
1696 * Returns a formatted mail line.
1698 * @param string $value
1701 public function TextLine ( $value ) {
1702 return $value . $this -> LE
;
1705 /////////////////////////////////////////////////
1706 // CLASS METHODS, ATTACHMENTS
1707 /////////////////////////////////////////////////
1710 * Adds an attachment from a path on the filesystem.
1711 * Returns false if the file could not be found
1713 * @param string $path Path to the attachment.
1714 * @param string $name Overrides the attachment name.
1715 * @param string $encoding File encoding (see $Encoding).
1716 * @param string $type File extension (MIME) type.
1717 * @throws phpmailerException
1720 public function AddAttachment ( $path , $name = '' , $encoding = 'base64' , $type = 'application/octet-stream' ) {
1722 if ( ! @is_file
( $path ) ) {
1723 throw new phpmailerException ( $this -> Lang ( 'file_access' ) . $path , self
:: STOP_CONTINUE
);
1725 $filename = basename ( $path );
1726 if ( $name == '' ) {
1730 $this -> attachment
[] = array (
1736 5 => false , // isStringAttachment
1741 } catch ( phpmailerException
$e ) {
1742 $this -> SetError ( $e -> getMessage ());
1743 if ( $this -> exceptions
) {
1746 if ( $this -> SMTPDebug
) {
1747 $this -> edebug ( $e -> getMessage (). " \n " );
1749 if ( $e -> getCode () == self
:: STOP_CRITICAL
) {
1757 * Return the current array of attachments
1760 public function GetAttachments () {
1761 return $this -> attachment
;
1765 * Attaches all fs, string, and binary attachments to the message.
1766 * Returns an empty string on failure.
1768 * @param string $disposition_type
1769 * @param string $boundary
1772 protected function AttachAll ( $disposition_type , $boundary ) {
1773 // Return text of body
1778 // Add all attachments
1779 foreach ( $this -> attachment
as $attachment ) {
1780 // CHECK IF IT IS A VALID DISPOSITION_FILTER
1781 if ( $attachment [ 6 ] == $disposition_type ) {
1782 // Check for string attachment
1785 $bString = $attachment [ 5 ];
1787 $string = $attachment [ 0 ];
1789 $path = $attachment [ 0 ];
1792 $inclhash = md5 ( serialize ( $attachment ));
1793 if ( in_array ( $inclhash , $incl )) { continue ; }
1794 $incl [] = $inclhash ;
1795 $filename = $attachment [ 1 ];
1796 $name = $attachment [ 2 ];
1797 $encoding = $attachment [ 3 ];
1798 $type = $attachment [ 4 ];
1799 $disposition = $attachment [ 6 ];
1800 $cid = $attachment [ 7 ];
1801 if ( $disposition == 'inline' && isset ( $cidUniq [ $cid ]) ) { continue ; }
1802 $cidUniq [ $cid ] = true ;
1804 $mime [] = sprintf ( "-- %s%s " , $boundary , $this -> LE
);
1805 $mime [] = sprintf ( "Content-Type: %s ; name= \" %s\"%s " , $type , $this -> EncodeHeader ( $this -> SecureHeader ( $name )), $this -> LE
);
1806 $mime [] = sprintf ( "Content-Transfer-Encoding: %s%s " , $encoding , $this -> LE
);
1808 if ( $disposition == 'inline' ) {
1809 $mime [] = sprintf ( "Content-ID: < %s > %s " , $cid , $this -> LE
);
1812 $mime [] = sprintf ( "Content-Disposition: %s ; filename= \" %s\"%s " , $disposition , $this -> EncodeHeader ( $this -> SecureHeader ( $name )), $this -> LE
. $this -> LE
);
1814 // Encode as string attachment
1816 $mime [] = $this -> EncodeString ( $string , $encoding );
1817 if ( $this -> IsError ()) {
1820 $mime [] = $this -> LE
. $this -> LE
;
1822 $mime [] = $this -> EncodeFile ( $path , $encoding );
1823 if ( $this -> IsError ()) {
1826 $mime [] = $this -> LE
. $this -> LE
;
1831 $mime [] = sprintf ( "-- %s -- %s " , $boundary , $this -> LE
);
1833 return implode ( "" , $mime );
1837 * Encodes attachment in requested format.
1838 * Returns an empty string on failure.
1839 * @param string $path The full path to the file
1840 * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
1841 * @throws phpmailerException
1846 protected function EncodeFile ( $path , $encoding = 'base64' ) {
1848 if (! is_readable ( $path )) {
1849 throw new phpmailerException ( $this -> Lang ( 'file_open' ) . $path , self
:: STOP_CONTINUE
);
1851 // if (!function_exists('get_magic_quotes')) {
1852 // function get_magic_quotes() {
1856 /*$magic_quotes = get_magic_quotes_runtime();
1857 if ($magic_quotes) {
1858 if (version_compare(PHP_VERSION, '5.3.0', '<')) {
1859 set_magic_quotes_runtime(0);
1861 ini_set('magic_quotes_runtime', 0);
1864 $file_buffer = file_get_contents ( $path );
1865 $file_buffer = $this -> EncodeString ( $file_buffer , $encoding );
1866 /*if ($magic_quotes) {
1867 if (version_compare(PHP_VERSION, '5.3.0', '<')) {
1868 set_magic_quotes_runtime($magic_quotes);
1870 ini_set('magic_quotes_runtime', $magic_quotes);
1873 return $file_buffer ;
1874 } catch ( Exception
$e ) {
1875 $this -> SetError ( $e -> getMessage ());
1881 * Encodes string to requested format.
1882 * Returns an empty string on failure.
1883 * @param string $str The text to encode
1884 * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
1888 public function EncodeString ( $str , $encoding = 'base64' ) {
1890 switch ( strtolower ( $encoding )) {
1892 $encoded = chunk_split ( base64_encode ( $str ), 76 , $this -> LE
);
1896 $encoded = $this -> FixEOL ( $str );
1897 //Make sure it ends with a line break
1898 if ( substr ( $encoded , -( strlen ( $this -> LE
))) != $this -> LE
)
1899 $encoded .= $this -> LE
;
1904 case 'quoted-printable' :
1905 $encoded = $this -> EncodeQP ( $str );
1908 $this -> SetError ( $this -> Lang ( 'encoding' ) . $encoding );
1915 * Encode a header string to best (shortest) of Q, B, quoted or none.
1917 * @param string $str
1918 * @param string $position
1921 public function EncodeHeader ( $str , $position = 'text' ) {
1924 switch ( strtolower ( $position )) {
1926 if (! preg_match ( '/[ \200 - \377 ]/' , $str )) {
1927 // Can't use addslashes as we don't know what value has magic_quotes_sybase
1928 $encoded = addcslashes ( $str , "\0..\37 \177\\\" " );
1929 if (( $str == $encoded ) && ! preg_match ( '/[^A-Za-z0-9!#$%& \' *+\/=?^_`{|}~ -]/' , $str )) {
1932 return ( " \" $encoded\" " );
1935 $x = preg_match_all ( '/[^ \040\041\043 - \133\135 - \176 ]/' , $str , $matches );
1938 $x = preg_match_all ( '/[()"]/' , $str , $matches );
1942 $x +
= preg_match_all ( '/[ \000 - \010\013\014\016 - \037\177 - \377 ]/' , $str , $matches );
1950 $maxlen = 75 - 7 - strlen ( $this -> CharSet
);
1951 // Try to select the encoding which should produce the shortest output
1952 if ( strlen ( $str )/ 3 < $x ) {
1954 if ( function_exists ( 'mb_strlen' ) && $this -> HasMultiBytes ( $str )) {
1955 // Use a custom function which correctly encodes and wraps long
1956 // multibyte strings without breaking lines within a character
1957 $encoded = $this -> Base64EncodeWrapMB ( $str , " \n " );
1959 $encoded = base64_encode ( $str );
1960 $maxlen -= $maxlen %
4 ;
1961 $encoded = trim ( chunk_split ( $encoded , $maxlen , " \n " ));
1965 $encoded = $this -> EncodeQ ( $str , $position );
1966 $encoded = $this -> WrapText ( $encoded , $maxlen , true );
1967 $encoded = str_replace ( '=' . self
:: CRLF
, " \n " , trim ( $encoded ));
1970 $encoded = preg_replace ( '/^(.*)$/m' , " =?" . $this -> CharSet
. "? $encoding ? \\ 1?=" , $encoded );
1971 $encoded = trim ( str_replace ( " \n " , $this -> LE
, $encoded ));
1977 * Checks if a string contains multibyte characters.
1979 * @param string $str multi-byte text to wrap encode
1982 public function HasMultiBytes ( $str ) {
1983 if ( function_exists ( 'mb_strlen' )) {
1984 return ( strlen ( $str ) > mb_strlen ( $str , $this -> CharSet
));
1985 } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
1991 * Correctly encodes and wraps long multibyte strings for mail headers
1992 * without breaking lines within a character.
1993 * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
1995 * @param string $str multi-byte text to wrap encode
1996 * @param string $lf string to use as linefeed/end-of-line
1999 public function Base64EncodeWrapMB ( $str , $lf = null ) {
2000 $start = "=?" . $this -> CharSet
. "?B?" ;
2007 $mb_length = mb_strlen ( $str , $this -> CharSet
);
2008 // Each line must have length <= 75, including $start and $end
2009 $length = 75 - strlen ( $start ) - strlen ( $end );
2010 // Average multi-byte ratio
2011 $ratio = $mb_length / strlen ( $str );
2012 // Base64 has a 4:3 ratio
2013 $offset = $avgLength = floor ( $length * $ratio * .75 );
2015 for ( $i = 0 ; $i < $mb_length ; $i +
= $offset ) {
2019 $offset = $avgLength - $lookBack ;
2020 $chunk = mb_substr ( $str , $i , $offset , $this -> CharSet
);
2021 $chunk = base64_encode ( $chunk );
2024 while ( strlen ( $chunk ) > $length );
2026 $encoded .= $chunk . $lf ;
2029 // Chomp the last linefeed
2030 $encoded = substr ( $encoded , 0 , - strlen ( $lf ));
2035 * Encode string to quoted-printable.
2036 * Only uses standard PHP, slow, but will always work
2038 * @param string $input
2039 * @param integer $line_max Number of chars allowed on a line before wrapping
2040 * @param bool $space_conv
2041 * @internal param string $string the text to encode
2044 public function EncodeQPphp ( $input = '' , $line_max = 76 , $space_conv = false ) {
2045 $hex = array ( '0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F' );
2046 $lines = preg_split ( '/(?: \r\n | \r | \n )/' , $input );
2050 while ( list (, $line ) = each ( $lines ) ) {
2051 $linlen = strlen ( $line );
2053 for ( $i = 0 ; $i < $linlen ; $i ++
) {
2054 $c = substr ( $line , $i , 1 );
2056 if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
2060 if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
2062 } else if ( $space_conv ) {
2065 } elseif ( ( $dec == 61 ) ||
( $dec < 32 ) ||
( $dec > 126 ) ) { // always encode "\t", which is *not* required
2066 $h2 = ( integer ) floor ( $dec / 16 );
2067 $h1 = ( integer ) floor ( $dec %16
);
2068 $c = $escape . $hex [ $h2 ]. $hex [ $h1 ];
2070 if ( ( strlen ( $newline ) +
strlen ( $c )) >= $line_max ) { // CRLF is not counted
2071 $output .= $newline . $escape . $eol ; // soft line break; " =\r\n" is okay
2073 // check if newline first character will be point or not
2080 $output .= $newline . $eol ;
2086 * Encode string to RFC2045 (6.7) quoted-printable format
2087 * Uses a PHP5 stream filter to do the encoding about 64x faster than the old version
2088 * Also results in same content as you started with after decoding
2089 * @see EncodeQPphp()
2091 * @param string $string the text to encode
2092 * @param integer $line_max Number of chars allowed on a line before wrapping
2093 * @param boolean $space_conv Dummy param for compatibility with existing EncodeQP function
2095 * @author Marcus Bointon
2097 public function EncodeQP ( $string , $line_max = 76 , $space_conv = false ) {
2098 if ( function_exists ( 'quoted_printable_encode' )) { //Use native function if it's available (>= PHP5.3)
2099 return quoted_printable_encode ( $string );
2101 $filters = stream_get_filters ();
2102 if (! in_array ( 'convert.*' , $filters )) { //Got convert stream filter?
2103 return $this -> EncodeQPphp ( $string , $line_max , $space_conv ); //Fall back to old implementation
2105 $fp = fopen ( 'php://temp/' , 'r+' );
2106 $string = preg_replace ( '/ \r\n ?/' , $this -> LE
, $string ); //Normalise line breaks
2107 $params = array ( 'line-length' => $line_max , 'line-break-chars' => $this -> LE
);
2108 $s = stream_filter_append ( $fp , 'convert.quoted-printable-encode' , STREAM_FILTER_READ
, $params );
2109 fputs ( $fp , $string );
2111 $out = stream_get_contents ( $fp );
2112 stream_filter_remove ( $s );
2113 $out = preg_replace ( '/^\./m' , '=2E' , $out ); //Encode . if it is first char on a line, workaround for bug in Exchange
2119 * Encode string to q encoding.
2120 * @link http://tools.ietf.org/html/rfc2047
2121 * @param string $str the text to encode
2122 * @param string $position Where the text is going to be used, see the RFC for what that means
2126 public function EncodeQ ( $str , $position = 'text' ) {
2127 //There should not be any EOL in the string
2129 $encoded = str_replace ( array ( " \r " , " \n " ), '' , $str );
2130 switch ( strtolower ( $position )) {
2132 $pattern = '^A-Za-z0-9!*+\/ -' ;
2137 //note that we dont break here!
2138 //for this reason we build the $pattern withoud including delimiters and []
2142 //Replace every high ascii, control =, ? and _ characters
2143 //We put \075 (=) as first value to make sure it's the first one in being converted, preventing double encode
2144 $pattern = ' \075\000 - \011\013\014\016 - \037\077\137\177 - \377 ' . $pattern ;
2148 if ( preg_match_all ( "/[{ $pattern }]/" , $encoded , $matches )) {
2149 foreach ( array_unique ( $matches [ 0 ]) as $char ) {
2150 $encoded = str_replace ( $char , '=' . sprintf ( ' %02X ' , ord ( $char )), $encoded );
2154 //Replace every spaces to _ (more readable than =20)
2155 return str_replace ( ' ' , '_' , $encoded );
2160 * Adds a string or binary attachment (non-filesystem) to the list.
2161 * This method can be used to attach ascii or binary data,
2162 * such as a BLOB record from a database.
2163 * @param string $string String attachment data.
2164 * @param string $filename Name of the attachment.
2165 * @param string $encoding File encoding (see $Encoding).
2166 * @param string $type File extension (MIME) type.
2169 public function AddStringAttachment ( $string , $filename , $encoding = 'base64' , $type = 'application/octet-stream' ) {
2170 // Append to $attachment array
2171 $this -> attachment
[] = array (
2174 2 => basename ( $filename ),
2177 5 => true , // isStringAttachment
2184 * Adds an embedded attachment. This can include images, sounds, and
2185 * just about any other document. Make sure to set the $type to an
2186 * image type. For JPEG images use "image/jpeg" and for GIF images
2188 * @param string $path Path to the attachment.
2189 * @param string $cid Content ID of the attachment. Use this to identify
2190 * the Id for accessing the image in an HTML form.
2191 * @param string $name Overrides the attachment name.
2192 * @param string $encoding File encoding (see $Encoding).
2193 * @param string $type File extension (MIME) type.
2196 public function AddEmbeddedImage ( $path , $cid , $name = '' , $encoding = 'base64' , $type = 'application/octet-stream' ) {
2198 if ( ! @is_file
( $path ) ) {
2199 $this -> SetError ( $this -> Lang ( 'file_access' ) . $path );
2203 $filename = basename ( $path );
2204 if ( $name == '' ) {
2208 // Append to $attachment array
2209 $this -> attachment
[] = array (
2215 5 => false , // isStringAttachment
2224 * Adds an embedded stringified attachment. This can include images, sounds, and
2225 * just about any other document. Make sure to set the $type to an
2226 * image type. For JPEG images use "image/jpeg" and for GIF images
2228 * @param string $string The attachment.
2229 * @param string $cid Content ID of the attachment. Use this to identify
2230 * the Id for accessing the image in an HTML form.
2231 * @param string $name Overrides the attachment name.
2232 * @param string $encoding File encoding (see $Encoding).
2233 * @param string $type File extension (MIME) type.
2236 public function AddStringEmbeddedImage ( $string , $cid , $name = '' , $encoding = 'base64' , $type = 'application/octet-stream' ) {
2237 // Append to $attachment array
2238 $this -> attachment
[] = array (
2244 5 => true , // isStringAttachment
2251 * Returns true if an inline attachment is present.
2255 public function InlineImageExists () {
2256 foreach ( $this -> attachment
as $attachment ) {
2257 if ( $attachment [ 6 ] == 'inline' ) {
2265 * Returns true if an attachment (non-inline) is present.
2268 public function AttachmentExists () {
2269 foreach ( $this -> attachment
as $attachment ) {
2270 if ( $attachment [ 6 ] == 'attachment' ) {
2278 * Does this message have an alternative body set?
2281 public function AlternativeExists () {
2282 return ! empty ( $this -> AltBody
);
2285 /////////////////////////////////////////////////
2286 // CLASS METHODS, MESSAGE RESET
2287 /////////////////////////////////////////////////
2290 * Clears all recipients assigned in the TO array. Returns void.
2293 public function ClearAddresses () {
2294 foreach ( $this -> to
as $to ) {
2295 unset ( $this -> all_recipients
[ strtolower ( $to [ 0 ])]);
2297 $this -> to
= array ();
2301 * Clears all recipients assigned in the CC array. Returns void.
2304 public function ClearCCs () {
2305 foreach ( $this -> cc
as $cc ) {
2306 unset ( $this -> all_recipients
[ strtolower ( $cc [ 0 ])]);
2308 $this -> cc
= array ();
2312 * Clears all recipients assigned in the BCC array. Returns void.
2315 public function ClearBCCs () {
2316 foreach ( $this -> bcc
as $bcc ) {
2317 unset ( $this -> all_recipients
[ strtolower ( $bcc [ 0 ])]);
2319 $this -> bcc
= array ();
2323 * Clears all recipients assigned in the ReplyTo array. Returns void.
2326 public function ClearReplyTos () {
2327 $this -> ReplyTo
= array ();
2331 * Clears all recipients assigned in the TO, CC and BCC
2332 * array. Returns void.
2335 public function ClearAllRecipients () {
2336 $this -> to
= array ();
2337 $this -> cc
= array ();
2338 $this -> bcc
= array ();
2339 $this -> all_recipients
= array ();
2343 * Clears all previously set filesystem, string, and binary
2344 * attachments. Returns void.
2347 public function ClearAttachments () {
2348 $this -> attachment
= array ();
2352 * Clears all custom headers. Returns void.
2355 public function ClearCustomHeaders () {
2356 $this -> CustomHeader
= array ();
2359 /////////////////////////////////////////////////
2360 // CLASS METHODS, MISCELLANEOUS
2361 /////////////////////////////////////////////////
2364 * Adds the error message to the error container.
2366 * @param string $msg
2369 protected function SetError ( $msg ) {
2370 $this -> error_count++
;
2371 if ( $this -> Mailer
== 'smtp' and ! is_null ( $this -> smtp
)) {
2372 $lasterror = $this -> smtp
-> getError ();
2373 if (! empty ( $lasterror ) and array_key_exists ( 'smtp_msg' , $lasterror )) {
2374 $msg .= '<p>' . $this -> Lang ( 'smtp_error' ) . $lasterror [ 'smtp_msg' ] . "</p> \n " ;
2377 $this -> ErrorInfo
= $msg ;
2381 * Returns the proper RFC 822 formatted date.
2386 public static function RFCDate () {
2388 $tzs = ( $tz < 0 ) ?
'-' : '+' ;
2390 $tz = ( int )( $tz / 3600 )* 100 +
( $tz %3600
)/ 60 ;
2391 $result = sprintf ( " %s %s%04d " , date ( 'D, j M Y H:i:s' ), $tzs , $tz );
2397 * Returns the server hostname or 'localhost.localdomain' if unknown.
2401 protected function ServerHostname () {
2402 if (! empty ( $this -> Hostname
)) {
2403 $result = $this -> Hostname
;
2404 } elseif ( isset ( $_SERVER [ 'SERVER_NAME' ])) {
2405 $result = $_SERVER [ 'SERVER_NAME' ];
2407 $result = 'localhost.localdomain' ;
2414 * Returns a message in the appropriate language.
2416 * @param string $key
2419 protected function Lang ( $key ) {
2420 if ( count ( $this -> language
) < 1 ) {
2421 $this -> SetLanguage ( 'en' ); // set the default language
2424 if ( isset ( $this -> language
[ $key ])) {
2425 return $this -> language
[ $key ];
2427 return 'Language string failed to load: ' . $key ;
2432 * Returns true if an error occurred.
2436 public function IsError () {
2437 return ( $this -> error_count
> 0 );
2441 * Changes every end of line from CRLF, CR or LF to $this->LE.
2443 * @param string $str String to FixEOL
2446 public function FixEOL ( $str ) {
2447 // condense down to \n
2448 $nstr = str_replace ( array ( " \r\n " , " \r " ), " \n " , $str );
2449 // Now convert LE as needed
2450 if ( $this -> LE
!== " \n " ) {
2451 $nstr = str_replace ( " \n " , $this -> LE
, $nstr );
2457 * Adds a custom header. $name value can be overloaded to contain
2458 * both header name and value (name:value)
2460 * @param string $name custom header name
2461 * @param string $value header value
2464 public function AddCustomHeader ( $name , $value = null ) {
2465 if ( $value === null ) {
2466 // Value passed in as name:value
2467 $this -> CustomHeader
[] = explode ( ':' , $name , 2 );
2469 $this -> CustomHeader
[] = array ( $name , $value );
2474 * Evaluates the message and returns modifications for inline images and backgrounds
2476 * @param string $message Text to be HTML modified
2477 * @param string $basedir baseline directory for path
2478 * @return string $message
2480 public function MsgHTML ( $message , $basedir = '' ) {
2481 preg_match_all ( "/(src|background)=[ \" '](.*)[ \" ']/Ui" , $message , $images );
2482 if ( isset ( $images [ 2 ])) {
2483 foreach ( $images [ 2 ] as $i => $url ) {
2484 // do not change urls for absolute images (thanks to corvuscorax)
2485 if (! preg_match ( '#^[A-z]+://#' , $url )) {
2486 $filename = basename ( $url );
2487 $directory = dirname ( $url );
2488 if ( $directory == '.' ) {
2491 $cid = 'cid:' . md5 ( $url );
2492 $ext = pathinfo ( $filename , PATHINFO_EXTENSION
);
2493 $mimeType = self
:: _mime_types ( $ext );
2494 if ( strlen ( $basedir ) > 1 && substr ( $basedir , - 1 ) != '/' ) { $basedir .= '/' ; }
2495 if ( strlen ( $directory ) > 1 && substr ( $directory , - 1 ) != '/' ) { $directory .= '/' ; }
2496 if ( $this -> AddEmbeddedImage ( $basedir . $directory . $filename , md5 ( $url ), $filename , 'base64' , $mimeType ) ) {
2497 $message = preg_replace ( "/" . $images [ 1 ][ $i ]. "=[ \" ']" . preg_quote ( $url , '/' ). "[ \" ']/Ui" , $images [ 1 ][ $i ]. "= \" " . $cid . " \" " , $message );
2502 $this -> IsHTML ( true );
2503 $this -> Body
= $message ;
2504 if ( empty ( $this -> AltBody
)) {
2505 $textMsg = trim ( strip_tags ( preg_replace ( '/<(head|title|style|script)[^>]*>.*?<\/ \\ 1>/s' , '' , $message )));
2506 if (! empty ( $textMsg )) {
2507 $this -> AltBody
= html_entity_decode ( $textMsg , ENT_QUOTES
, $this -> CharSet
);
2510 if ( empty ( $this -> AltBody
)) {
2511 $this -> AltBody
= 'To view this email message, open it in a program that understands HTML!' . " \n\n " ;
2517 * Gets the MIME type of the embedded or inline image
2518 * @param string $ext File extension
2520 * @return string MIME type of ext
2523 public static function _mime_types ( $ext = '' ) {
2525 'xl' => 'application/excel' ,
2526 'hqx' => 'application/mac-binhex40' ,
2527 'cpt' => 'application/mac-compactpro' ,
2528 'bin' => 'application/macbinary' ,
2529 'doc' => 'application/msword' ,
2530 'word' => 'application/msword' ,
2531 'class' => 'application/octet-stream' ,
2532 'dll' => 'application/octet-stream' ,
2533 'dms' => 'application/octet-stream' ,
2534 'exe' => 'application/octet-stream' ,
2535 'lha' => 'application/octet-stream' ,
2536 'lzh' => 'application/octet-stream' ,
2537 'psd' => 'application/octet-stream' ,
2538 'sea' => 'application/octet-stream' ,
2539 'so' => 'application/octet-stream' ,
2540 'oda' => 'application/oda' ,
2541 'pdf' => 'application/pdf' ,
2542 'ai' => 'application/postscript' ,
2543 'eps' => 'application/postscript' ,
2544 'ps' => 'application/postscript' ,
2545 'smi' => 'application/smil' ,
2546 'smil' => 'application/smil' ,
2547 'mif' => 'application/vnd.mif' ,
2548 'xls' => 'application/vnd.ms-excel' ,
2549 'ppt' => 'application/vnd.ms-powerpoint' ,
2550 'wbxml' => 'application/vnd.wap.wbxml' ,
2551 'wmlc' => 'application/vnd.wap.wmlc' ,
2552 'dcr' => 'application/x-director' ,
2553 'dir' => 'application/x-director' ,
2554 'dxr' => 'application/x-director' ,
2555 'dvi' => 'application/x-dvi' ,
2556 'gtar' => 'application/x-gtar' ,
2557 'php3' => 'application/x-httpd-php' ,
2558 'php4' => 'application/x-httpd-php' ,
2559 'php' => 'application/x-httpd-php' ,
2560 'phtml' => 'application/x-httpd-php' ,
2561 'phps' => 'application/x-httpd-php-source' ,
2562 'js' => 'application/x-javascript' ,
2563 'swf' => 'application/x-shockwave-flash' ,
2564 'sit' => 'application/x-stuffit' ,
2565 'tar' => 'application/x-tar' ,
2566 'tgz' => 'application/x-tar' ,
2567 'xht' => 'application/xhtml+xml' ,
2568 'xhtml' => 'application/xhtml+xml' ,
2569 'zip' => 'application/zip' ,
2570 'mid' => 'audio/midi' ,
2571 'midi' => 'audio/midi' ,
2572 'mp2' => 'audio/mpeg' ,
2573 'mp3' => 'audio/mpeg' ,
2574 'mpga' => 'audio/mpeg' ,
2575 'aif' => 'audio/x-aiff' ,
2576 'aifc' => 'audio/x-aiff' ,
2577 'aiff' => 'audio/x-aiff' ,
2578 'ram' => 'audio/x-pn-realaudio' ,
2579 'rm' => 'audio/x-pn-realaudio' ,
2580 'rpm' => 'audio/x-pn-realaudio-plugin' ,
2581 'ra' => 'audio/x-realaudio' ,
2582 'wav' => 'audio/x-wav' ,
2583 'bmp' => 'image/bmp' ,
2584 'gif' => 'image/gif' ,
2585 'jpeg' => 'image/jpeg' ,
2586 'jpe' => 'image/jpeg' ,
2587 'jpg' => 'image/jpeg' ,
2588 'png' => 'image/png' ,
2589 'tiff' => 'image/tiff' ,
2590 'tif' => 'image/tiff' ,
2591 'eml' => 'message/rfc822' ,
2592 'css' => 'text/css' ,
2593 'html' => 'text/html' ,
2594 'htm' => 'text/html' ,
2595 'shtml' => 'text/html' ,
2596 'log' => 'text/plain' ,
2597 'text' => 'text/plain' ,
2598 'txt' => 'text/plain' ,
2599 'rtx' => 'text/richtext' ,
2600 'rtf' => 'text/rtf' ,
2601 'xml' => 'text/xml' ,
2602 'xsl' => 'text/xml' ,
2603 'mpeg' => 'video/mpeg' ,
2604 'mpe' => 'video/mpeg' ,
2605 'mpg' => 'video/mpeg' ,
2606 'mov' => 'video/quicktime' ,
2607 'qt' => 'video/quicktime' ,
2608 'rv' => 'video/vnd.rn-realvideo' ,
2609 'avi' => 'video/x-msvideo' ,
2610 'movie' => 'video/x-sgi-movie'
2612 return (! isset ( $mimes [ strtolower ( $ext )])) ?
'application/octet-stream' : $mimes [ strtolower ( $ext )];
2616 * Set (or reset) Class Objects (variables)
2619 * $page->set('X-Priority', '3');
2622 * @param string $name Parameter Name
2623 * @param mixed $value Parameter Value
2624 * NOTE: will not work with arrays, there are no arrays to set/reset
2625 * @throws phpmailerException
2627 * @todo Should this not be using __set() magic function?
2629 public function set ( $name , $value = '' ) {
2631 if ( isset ( $this -> $name ) ) {
2632 $this -> $name = $value ;
2634 throw new phpmailerException ( $this -> Lang ( 'variable_set' ) . $name , self
:: STOP_CRITICAL
);
2636 } catch ( Exception
$e ) {
2637 $this -> SetError ( $e -> getMessage ());
2638 if ( $e -> getCode () == self
:: STOP_CRITICAL
) {
2646 * Strips newlines to prevent header injection.
2648 * @param string $str String
2651 public function SecureHeader ( $str ) {
2652 return trim ( str_replace ( array ( " \r " , " \n " ), '' , $str ));
2656 * Set the private key file and password to sign the message.
2659 * @param $cert_filename
2660 * @param string $key_filename Parameter File Name
2661 * @param string $key_pass Password for private key
2663 public function Sign ( $cert_filename , $key_filename , $key_pass ) {
2664 $this -> sign_cert_file
= $cert_filename ;
2665 $this -> sign_key_file
= $key_filename ;
2666 $this -> sign_key_pass
= $key_pass ;
2670 * Set the private key file and password to sign the message.
2673 * @param string $txt
2676 public function DKIM_QP ( $txt ) {
2678 for ( $i = 0 ; $i < strlen ( $txt ); $i ++
) {
2679 $ord = ord ( $txt [ $i ]);
2680 if ( (( 0x21 <= $ord ) && ( $ord <= 0x3A )) ||
$ord == 0x3C ||
(( 0x3E <= $ord ) && ( $ord <= 0x7E )) ) {
2683 $line .= "=" . sprintf ( " %02X " , $ord );
2690 * Generate DKIM signature
2693 * @param string $s Header
2696 public function DKIM_Sign ( $s ) {
2697 $privKeyStr = file_get_contents ( $this -> DKIM_private
);
2698 if ( $this -> DKIM_passphrase
!= '' ) {
2699 $privKey = openssl_pkey_get_private ( $privKeyStr , $this -> DKIM_passphrase
);
2701 $privKey = $privKeyStr ;
2703 if ( openssl_sign ( $s , $signature , $privKey )) {
2704 return base64_encode ( $signature );
2710 * Generate DKIM Canonicalization Header
2713 * @param string $s Header
2716 public function DKIM_HeaderC ( $s ) {
2717 $s = preg_replace ( "/ \r\n \s+/" , " " , $s );
2718 $lines = explode ( " \r\n " , $s );
2719 foreach ( $lines as $key => $line ) {
2720 list ( $heading , $value ) = explode ( ":" , $line , 2 );
2721 $heading = strtolower ( $heading );
2722 $value = preg_replace ( "/\s+/" , " " , $value ) ; // Compress useless spaces
2723 $lines [ $key ] = $heading . ":" . trim ( $value ) ; // Don't forget to remove WSP around the value
2725 $s = implode ( " \r\n " , $lines );
2730 * Generate DKIM Canonicalization Body
2733 * @param string $body Message Body
2736 public function DKIM_BodyC ( $body ) {
2737 if ( $body == '' ) return " \r\n " ;
2738 // stabilize line endings
2739 $body = str_replace ( " \r\n " , " \n " , $body );
2740 $body = str_replace ( " \n " , " \r\n " , $body );
2741 // END stabilize line endings
2742 while ( substr ( $body , strlen ( $body ) - 4 , 4 ) == " \r\n\r\n " ) {
2743 $body = substr ( $body , 0 , strlen ( $body ) - 2 );
2749 * Create the DKIM header, body, as new header
2752 * @param string $headers_line Header lines
2753 * @param string $subject Subject
2754 * @param string $body Body
2757 public function DKIM_Add ( $headers_line , $subject , $body ) {
2758 $DKIMsignatureType = 'rsa-sha1' ; // Signature & hash algorithms
2759 $DKIMcanonicalization = 'relaxed/simple' ; // Canonicalization of header/body
2760 $DKIMquery = 'dns/txt' ; // Query method
2761 $DKIMtime = time () ; // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
2762 $subject_header = "Subject: $subject " ;
2763 $headers = explode ( $this -> LE
, $headers_line );
2766 foreach ( $headers as $header ) {
2767 if ( strpos ( $header , 'From:' ) === 0 ) {
2768 $from_header = $header ;
2769 } elseif ( strpos ( $header , 'To:' ) === 0 ) {
2770 $to_header = $header ;
2773 $from = str_replace ( '|' , '=7C' , $this -> DKIM_QP ( $from_header ));
2774 $to = str_replace ( '|' , '=7C' , $this -> DKIM_QP ( $to_header ));
2775 $subject = str_replace ( '|' , '=7C' , $this -> DKIM_QP ( $subject_header )) ; // Copied header fields (dkim-quoted-printable
2776 $body = $this -> DKIM_BodyC ( $body );
2777 $DKIMlen = strlen ( $body ) ; // Length of body
2778 $DKIMb64 = base64_encode ( pack ( "H*" , sha1 ( $body ))) ; // Base64 of packed binary SHA-1 hash of body
2779 $ident = ( $this -> DKIM_identity
== '' ) ?
'' : " i=" . $this -> DKIM_identity
. ";" ;
2780 $dkimhdrs = "DKIM-Signature: v=1; a=" . $DKIMsignatureType . "; q=" . $DKIMquery . "; l=" . $DKIMlen . "; s=" . $this -> DKIM_selector
. "; \r\n " .
2781 " \t t=" . $DKIMtime . "; c=" . $DKIMcanonicalization . "; \r\n " .
2782 " \t h=From:To:Subject; \r\n " .
2783 " \t d=" . $this -> DKIM_domain
. ";" . $ident . " \r\n " .
2787 " \t bh=" . $DKIMb64 . "; \r\n " .
2789 $toSign = $this -> DKIM_HeaderC ( $from_header . " \r\n " . $to_header . " \r\n " . $subject_header . " \r\n " . $dkimhdrs );
2790 $signed = $this -> DKIM_Sign ( $toSign );
2791 return "X-PHPMAILER-DKIM: code.google.com/a/apache-extras.org/p/phpmailer/ \r\n " . $dkimhdrs . $signed . " \r\n " ;
2796 * @param boolean $isSent
2799 * @param string $bcc
2800 * @param string $subject
2801 * @param string $body
2802 * @param string $from
2804 protected function doCallback ( $isSent , $to , $cc , $bcc , $subject , $body , $from = null ) {
2805 if (! empty ( $this -> action_function
) && is_callable ( $this -> action_function
)) {
2806 $params = array ( $isSent , $to , $cc , $bcc , $subject , $body , $from );
2807 call_user_func_array ( $this -> action_function
, $params );
2813 * Exception handler for PHPMailer
2814 * @package PHPMailer
2816 class phpmailerException
extends Exception
{
2818 * Prettify error message output
2821 public function errorMessage () {
2822 $errorMsg = '<strong>' . $this -> getMessage () . "</strong><br /> \n " ;