annotate sites/all/modules/smtp/phpmailer/class.phpmailer.php @ 11:b0ee71395280

deleted .DS_Store files
author danieleb <danielebarchiesi@me.com>
date Mon, 28 Oct 2013 16:12:13 +0000
parents 830c812b520f
children
rev   line source
root@9 1 <?php
root@9 2 /*~ class.phpmailer.php
root@9 3 .---------------------------------------------------------------------------.
root@9 4 | Software: PHPMailer - PHP email class |
root@9 5 | Version: 5.1 |
root@9 6 | Contact: via sourceforge.net support pages (also www.worxware.com) |
root@9 7 | Info: http://phpmailer.sourceforge.net |
root@9 8 | Support: http://sourceforge.net/projects/phpmailer/ |
root@9 9 | ------------------------------------------------------------------------- |
root@9 10 | Admin: Andy Prevost (project admininistrator) |
root@9 11 | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
root@9 12 | : Marcus Bointon (coolbru) coolbru@users.sourceforge.net |
root@9 13 | Founder: Brent R. Matzelle (original founder) |
root@9 14 | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
root@9 15 | Copyright (c) 2001-2003, Brent R. Matzelle |
root@9 16 | ------------------------------------------------------------------------- |
root@9 17 | License: Distributed under the Lesser General Public License (LGPL) |
root@9 18 | http://www.gnu.org/copyleft/lesser.html |
root@9 19 | This program is distributed in the hope that it will be useful - WITHOUT |
root@9 20 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
root@9 21 | FITNESS FOR A PARTICULAR PURPOSE. |
root@9 22 | ------------------------------------------------------------------------- |
root@9 23 | We offer a number of paid services (www.worxware.com): |
root@9 24 | - Web Hosting on highly optimized fast and secure servers |
root@9 25 | - Technology Consulting |
root@9 26 | - Oursourcing (highly qualified programmers and graphic designers) |
root@9 27 '---------------------------------------------------------------------------'
root@9 28 */
root@9 29
root@9 30 /**
root@9 31 * PHPMailer - PHP email transport class
root@9 32 * NOTE: Requires PHP version 5 or later
root@9 33 * @package PHPMailer
root@9 34 * @author Andy Prevost
root@9 35 * @author Marcus Bointon
root@9 36 * @copyright 2004 - 2009 Andy Prevost
root@9 37 * @version $Id: class.phpmailer.php 447 2009-05-25 01:36:38Z codeworxtech $
root@9 38 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
root@9 39 */
root@9 40
root@9 41 if (version_compare(PHP_VERSION, '5.0.0', '<') ) exit("Sorry, this version of PHPMailer will only run on PHP version 5 or greater!\n");
root@9 42
root@9 43 class PHPMailer {
root@9 44
root@9 45 /////////////////////////////////////////////////
root@9 46 // PROPERTIES, PUBLIC
root@9 47 /////////////////////////////////////////////////
root@9 48
root@9 49 /**
root@9 50 * Email priority (1 = High, 3 = Normal, 5 = low).
root@9 51 * @var int
root@9 52 */
root@9 53 public $Priority = 3;
root@9 54
root@9 55 /**
root@9 56 * Sets the CharSet of the message.
root@9 57 * @var string
root@9 58 */
root@9 59 public $CharSet = 'iso-8859-1';
root@9 60
root@9 61 /**
root@9 62 * Sets the Content-type of the message.
root@9 63 * @var string
root@9 64 */
root@9 65 public $ContentType = 'text/plain';
root@9 66
root@9 67 /**
root@9 68 * Sets the Encoding of the message. Options for this are
root@9 69 * "8bit", "7bit", "binary", "base64", and "quoted-printable".
root@9 70 * @var string
root@9 71 */
root@9 72 public $Encoding = '8bit';
root@9 73
root@9 74 /**
root@9 75 * Holds the most recent mailer error message.
root@9 76 * @var string
root@9 77 */
root@9 78 public $ErrorInfo = '';
root@9 79
root@9 80 /**
root@9 81 * Sets the From email address for the message.
root@9 82 * @var string
root@9 83 */
root@9 84 public $From = 'root@localhost';
root@9 85
root@9 86 /**
root@9 87 * Sets the From name of the message.
root@9 88 * @var string
root@9 89 */
root@9 90 public $FromName = 'Root User';
root@9 91
root@9 92 /**
root@9 93 * Sets the Sender email (Return-Path) of the message. If not empty,
root@9 94 * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
root@9 95 * @var string
root@9 96 */
root@9 97 public $Sender = '';
root@9 98
root@9 99 /**
root@9 100 * Sets the Subject of the message.
root@9 101 * @var string
root@9 102 */
root@9 103 public $Subject = '';
root@9 104
root@9 105 /**
root@9 106 * Sets the Body of the message. This can be either an HTML or text body.
root@9 107 * If HTML then run IsHTML(true).
root@9 108 * @var string
root@9 109 */
root@9 110 public $Body = '';
root@9 111
root@9 112 /**
root@9 113 * Sets the text-only body of the message. This automatically sets the
root@9 114 * email to multipart/alternative. This body can be read by mail
root@9 115 * clients that do not have HTML email capability such as mutt. Clients
root@9 116 * that can read HTML will view the normal Body.
root@9 117 * @var string
root@9 118 */
root@9 119 public $AltBody = '';
root@9 120
root@9 121 /**
root@9 122 * Sets word wrapping on the body of the message to a given number of
root@9 123 * characters.
root@9 124 * @var int
root@9 125 */
root@9 126 public $WordWrap = 0;
root@9 127
root@9 128 /**
root@9 129 * Method to send mail: ("mail", "sendmail", or "smtp").
root@9 130 * @var string
root@9 131 */
root@9 132 public $Mailer = 'mail';
root@9 133
root@9 134 /**
root@9 135 * Sets the path of the sendmail program.
root@9 136 * @var string
root@9 137 */
root@9 138 public $Sendmail = '/usr/sbin/sendmail';
root@9 139
root@9 140 /**
root@9 141 * Path to PHPMailer plugins. Useful if the SMTP class
root@9 142 * is in a different directory than the PHP include path.
root@9 143 * @var string
root@9 144 */
root@9 145 public $PluginDir = '';
root@9 146
root@9 147 /**
root@9 148 * Sets the email address that a reading confirmation will be sent.
root@9 149 * @var string
root@9 150 */
root@9 151 public $ConfirmReadingTo = '';
root@9 152
root@9 153 /**
root@9 154 * Sets the hostname to use in Message-Id and Received headers
root@9 155 * and as default HELO string. If empty, the value returned
root@9 156 * by SERVER_NAME is used or 'localhost.localdomain'.
root@9 157 * @var string
root@9 158 */
root@9 159 public $Hostname = '';
root@9 160
root@9 161 /**
root@9 162 * Sets the message ID to be used in the Message-Id header.
root@9 163 * If empty, a unique id will be generated.
root@9 164 * @var string
root@9 165 */
root@9 166 public $MessageID = '';
root@9 167
root@9 168 /////////////////////////////////////////////////
root@9 169 // PROPERTIES FOR SMTP
root@9 170 /////////////////////////////////////////////////
root@9 171
root@9 172 /**
root@9 173 * Sets the SMTP hosts. All hosts must be separated by a
root@9 174 * semicolon. You can also specify a different port
root@9 175 * for each host by using this format: [hostname:port]
root@9 176 * (e.g. "smtp1.example.com:25;smtp2.example.com").
root@9 177 * Hosts will be tried in order.
root@9 178 * @var string
root@9 179 */
root@9 180 public $Host = 'localhost';
root@9 181
root@9 182 /**
root@9 183 * Sets the default SMTP server port.
root@9 184 * @var int
root@9 185 */
root@9 186 public $Port = 25;
root@9 187
root@9 188 /**
root@9 189 * Sets the SMTP HELO of the message (Default is $Hostname).
root@9 190 * @var string
root@9 191 */
root@9 192 public $Helo = '';
root@9 193
root@9 194 /**
root@9 195 * Sets connection prefix.
root@9 196 * Options are "", "ssl" or "tls"
root@9 197 * @var string
root@9 198 */
root@9 199 public $SMTPSecure = '';
root@9 200
root@9 201 /**
root@9 202 * Sets SMTP authentication. Utilizes the Username and Password variables.
root@9 203 * @var bool
root@9 204 */
root@9 205 public $SMTPAuth = false;
root@9 206
root@9 207 /**
root@9 208 * Sets SMTP username.
root@9 209 * @var string
root@9 210 */
root@9 211 public $Username = '';
root@9 212
root@9 213 /**
root@9 214 * Sets SMTP password.
root@9 215 * @var string
root@9 216 */
root@9 217 public $Password = '';
root@9 218
root@9 219 /**
root@9 220 * Sets the SMTP server timeout in seconds.
root@9 221 * This function will not work with the win32 version.
root@9 222 * @var int
root@9 223 */
root@9 224 public $Timeout = 10;
root@9 225
root@9 226 /**
root@9 227 * Sets SMTP class debugging on or off.
root@9 228 * @var bool
root@9 229 */
root@9 230 public $SMTPDebug = false;
root@9 231
root@9 232 /**
root@9 233 * Prevents the SMTP connection from being closed after each mail
root@9 234 * sending. If this is set to true then to close the connection
root@9 235 * requires an explicit call to SmtpClose().
root@9 236 * @var bool
root@9 237 */
root@9 238 public $SMTPKeepAlive = false;
root@9 239
root@9 240 /**
root@9 241 * Provides the ability to have the TO field process individual
root@9 242 * emails, instead of sending to entire TO addresses
root@9 243 * @var bool
root@9 244 */
root@9 245 public $SingleTo = false;
root@9 246
root@9 247 /**
root@9 248 * If SingleTo is true, this provides the array to hold the email addresses
root@9 249 * @var bool
root@9 250 */
root@9 251 public $SingleToArray = array();
root@9 252
root@9 253 /**
root@9 254 * Provides the ability to change the line ending
root@9 255 * @var string
root@9 256 */
root@9 257 public $LE = "\n";
root@9 258
root@9 259 /**
root@9 260 * Used with DKIM DNS Resource Record
root@9 261 * @var string
root@9 262 */
root@9 263 public $DKIM_selector = 'phpmailer';
root@9 264
root@9 265 /**
root@9 266 * Used with DKIM DNS Resource Record
root@9 267 * optional, in format of email address 'you@yourdomain.com'
root@9 268 * @var string
root@9 269 */
root@9 270 public $DKIM_identity = '';
root@9 271
root@9 272 /**
root@9 273 * Used with DKIM DNS Resource Record
root@9 274 * optional, in format of email address 'you@yourdomain.com'
root@9 275 * @var string
root@9 276 */
root@9 277 public $DKIM_domain = '';
root@9 278
root@9 279 /**
root@9 280 * Used with DKIM DNS Resource Record
root@9 281 * optional, in format of email address 'you@yourdomain.com'
root@9 282 * @var string
root@9 283 */
root@9 284 public $DKIM_private = '';
root@9 285
root@9 286 /**
root@9 287 * Callback Action function name
root@9 288 * the function that handles the result of the send email action. Parameters:
root@9 289 * bool $result result of the send action
root@9 290 * string $to email address of the recipient
root@9 291 * string $cc cc email addresses
root@9 292 * string $bcc bcc email addresses
root@9 293 * string $subject the subject
root@9 294 * string $body the email body
root@9 295 * @var string
root@9 296 */
root@9 297 public $action_function = ''; //'callbackAction';
root@9 298
root@9 299 /**
root@9 300 * Sets the PHPMailer Version number
root@9 301 * @var string
root@9 302 */
root@9 303 public $Version = '5.1';
root@9 304
root@9 305 /////////////////////////////////////////////////
root@9 306 // PROPERTIES, PRIVATE AND PROTECTED
root@9 307 /////////////////////////////////////////////////
root@9 308
root@9 309 private $smtp = NULL;
root@9 310 private $to = array();
root@9 311 private $cc = array();
root@9 312 private $bcc = array();
root@9 313 private $ReplyTo = array();
root@9 314 private $all_recipients = array();
root@9 315 private $attachment = array();
root@9 316 private $CustomHeader = array();
root@9 317 private $message_type = '';
root@9 318 private $boundary = array();
root@9 319 protected $language = array();
root@9 320 private $error_count = 0;
root@9 321 private $sign_cert_file = "";
root@9 322 private $sign_key_file = "";
root@9 323 private $sign_key_pass = "";
root@9 324 private $exceptions = false;
root@9 325
root@9 326 /////////////////////////////////////////////////
root@9 327 // CONSTANTS
root@9 328 /////////////////////////////////////////////////
root@9 329
root@9 330 const STOP_MESSAGE = 0; // message only, continue processing
root@9 331 const STOP_CONTINUE = 1; // message?, likely ok to continue processing
root@9 332 const STOP_CRITICAL = 2; // message, plus full stop, critical error reached
root@9 333
root@9 334 /////////////////////////////////////////////////
root@9 335 // METHODS, VARIABLES
root@9 336 /////////////////////////////////////////////////
root@9 337
root@9 338 /**
root@9 339 * Constructor
root@9 340 * @param boolean $exceptions Should we throw external exceptions?
root@9 341 */
root@9 342 public function __construct($exceptions = false) {
root@9 343 $this->exceptions = ($exceptions == true);
root@9 344 }
root@9 345
root@9 346 /**
root@9 347 * Sets message type to HTML.
root@9 348 * @param bool $ishtml
root@9 349 * @return void
root@9 350 */
root@9 351 public function IsHTML($ishtml = true) {
root@9 352 if ($ishtml) {
root@9 353 $this->ContentType = 'text/html';
root@9 354 } else {
root@9 355 $this->ContentType = 'text/plain';
root@9 356 }
root@9 357 }
root@9 358
root@9 359 /**
root@9 360 * Sets Mailer to send message using SMTP.
root@9 361 * @return void
root@9 362 */
root@9 363 public function IsSMTP() {
root@9 364 $this->Mailer = 'smtp';
root@9 365 }
root@9 366
root@9 367 /**
root@9 368 * Sets Mailer to send message using PHP mail() function.
root@9 369 * @return void
root@9 370 */
root@9 371 public function IsMail() {
root@9 372 $this->Mailer = 'mail';
root@9 373 }
root@9 374
root@9 375 /**
root@9 376 * Sets Mailer to send message using the $Sendmail program.
root@9 377 * @return void
root@9 378 */
root@9 379 public function IsSendmail() {
root@9 380 if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
root@9 381 $this->Sendmail = '/var/qmail/bin/sendmail';
root@9 382 }
root@9 383 $this->Mailer = 'sendmail';
root@9 384 }
root@9 385
root@9 386 /**
root@9 387 * Sets Mailer to send message using the qmail MTA.
root@9 388 * @return void
root@9 389 */
root@9 390 public function IsQmail() {
root@9 391 if (stristr(ini_get('sendmail_path'), 'qmail')) {
root@9 392 $this->Sendmail = '/var/qmail/bin/sendmail';
root@9 393 }
root@9 394 $this->Mailer = 'sendmail';
root@9 395 }
root@9 396
root@9 397 /////////////////////////////////////////////////
root@9 398 // METHODS, RECIPIENTS
root@9 399 /////////////////////////////////////////////////
root@9 400
root@9 401 /**
root@9 402 * Adds a "To" address.
root@9 403 * @param string $address
root@9 404 * @param string $name
root@9 405 * @return boolean true on success, false if address already used
root@9 406 */
root@9 407 public function AddAddress($address, $name = '') {
root@9 408 return $this->AddAnAddress('to', $address, $name);
root@9 409 }
root@9 410
root@9 411 /**
root@9 412 * Adds a "Cc" address.
root@9 413 * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
root@9 414 * @param string $address
root@9 415 * @param string $name
root@9 416 * @return boolean true on success, false if address already used
root@9 417 */
root@9 418 public function AddCC($address, $name = '') {
root@9 419 return $this->AddAnAddress('cc', $address, $name);
root@9 420 }
root@9 421
root@9 422 /**
root@9 423 * Adds a "Bcc" address.
root@9 424 * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
root@9 425 * @param string $address
root@9 426 * @param string $name
root@9 427 * @return boolean true on success, false if address already used
root@9 428 */
root@9 429 public function AddBCC($address, $name = '') {
root@9 430 return $this->AddAnAddress('bcc', $address, $name);
root@9 431 }
root@9 432
root@9 433 /**
root@9 434 * Adds a "Reply-to" address.
root@9 435 * @param string $address
root@9 436 * @param string $name
root@9 437 * @return boolean
root@9 438 */
root@9 439 public function AddReplyTo($address, $name = '') {
root@9 440 return $this->AddAnAddress('ReplyTo', $address, $name);
root@9 441 }
root@9 442
root@9 443 /**
root@9 444 * Adds an address to one of the recipient arrays
root@9 445 * Addresses that have been added already return false, but do not throw exceptions
root@9 446 * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
root@9 447 * @param string $address The email address to send to
root@9 448 * @param string $name
root@9 449 * @return boolean true on success, false if address already used or invalid in some way
root@9 450 * @access private
root@9 451 */
root@9 452 private function AddAnAddress($kind, $address, $name = '') {
root@9 453 if (!preg_match('/^(to|cc|bcc|ReplyTo)$/', $kind)) {
root@9 454 echo 'Invalid recipient array: ' . kind;
root@9 455 return false;
root@9 456 }
root@9 457 $address = trim($address);
root@9 458 $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
root@9 459 if (!self::ValidateAddress($address)) {
root@9 460 $this->SetError($this->Lang('invalid_address').': '. $address);
root@9 461 if ($this->exceptions) {
root@9 462 throw new phpmailerException($this->Lang('invalid_address').': '.$address);
root@9 463 }
root@9 464 echo $this->Lang('invalid_address').': '.$address;
root@9 465 return false;
root@9 466 }
root@9 467 if ($kind != 'ReplyTo') {
root@9 468 if (!isset($this->all_recipients[strtolower($address)])) {
root@9 469 array_push($this->$kind, array($address, $name));
root@9 470 $this->all_recipients[strtolower($address)] = true;
root@9 471 return true;
root@9 472 }
root@9 473 } else {
root@9 474 if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
root@9 475 $this->ReplyTo[strtolower($address)] = array($address, $name);
root@9 476 return true;
root@9 477 }
root@9 478 }
root@9 479 return false;
root@9 480 }
root@9 481
root@9 482 /**
root@9 483 * Set the From and FromName properties
root@9 484 * @param string $address
root@9 485 * @param string $name
root@9 486 * @return boolean
root@9 487 */
root@9 488 public function SetFrom($address, $name = '',$auto=1) {
root@9 489 $address = trim($address);
root@9 490 $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
root@9 491 if (!self::ValidateAddress($address)) {
root@9 492 $this->SetError($this->Lang('invalid_address').': '. $address);
root@9 493 if ($this->exceptions) {
root@9 494 throw new phpmailerException($this->Lang('invalid_address').': '.$address);
root@9 495 }
root@9 496 echo $this->Lang('invalid_address').': '.$address;
root@9 497 return false;
root@9 498 }
root@9 499 $this->From = $address;
root@9 500 $this->FromName = $name;
root@9 501 if ($auto) {
root@9 502 if (empty($this->ReplyTo)) {
root@9 503 $this->AddAnAddress('ReplyTo', $address, $name);
root@9 504 }
root@9 505 if (empty($this->Sender)) {
root@9 506 $this->Sender = $address;
root@9 507 }
root@9 508 }
root@9 509 return true;
root@9 510 }
root@9 511
root@9 512 /**
root@9 513 * Check that a string looks roughly like an email address should
root@9 514 * Static so it can be used without instantiation
root@9 515 * Tries to use PHP built-in validator in the filter extension (from PHP 5.2), falls back to a reasonably competent regex validator
root@9 516 * Conforms approximately to RFC2822
root@9 517 * @link http://www.hexillion.com/samples/#Regex Original pattern found here
root@9 518 * @param string $address The email address to check
root@9 519 * @return boolean
root@9 520 * @static
root@9 521 * @access public
root@9 522 */
root@9 523 public static function ValidateAddress($address) {
root@9 524 if (function_exists('filter_var')) { //Introduced in PHP 5.2
root@9 525 if(filter_var($address, FILTER_VALIDATE_EMAIL) === FALSE) {
root@9 526 return false;
root@9 527 } else {
root@9 528 return true;
root@9 529 }
root@9 530 } else {
root@9 531 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);
root@9 532 }
root@9 533 }
root@9 534
root@9 535 /////////////////////////////////////////////////
root@9 536 // METHODS, MAIL SENDING
root@9 537 /////////////////////////////////////////////////
root@9 538
root@9 539 /**
root@9 540 * Creates message and assigns Mailer. If the message is
root@9 541 * not sent successfully then it returns false. Use the ErrorInfo
root@9 542 * variable to view description of the error.
root@9 543 * @return bool
root@9 544 */
root@9 545 public function Send() {
root@9 546 try {
root@9 547 if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
root@9 548 throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
root@9 549 }
root@9 550
root@9 551 // Set whether the message is multipart/alternative
root@9 552 if(!empty($this->AltBody)) {
root@9 553 $this->ContentType = 'multipart/alternative';
root@9 554 }
root@9 555
root@9 556 $this->error_count = 0; // reset errors
root@9 557 $this->SetMessageType();
root@9 558 $header = $this->CreateHeader();
root@9 559 $body = $this->CreateBody();
root@9 560
root@9 561 if (empty($this->Body)) {
root@9 562 throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
root@9 563 }
root@9 564
root@9 565 // digitally sign with DKIM if enabled
root@9 566 if ($this->DKIM_domain && $this->DKIM_private) {
root@9 567 $header_dkim = $this->DKIM_Add($header,$this->Subject,$body);
root@9 568 $header = str_replace("\r\n","\n",$header_dkim) . $header;
root@9 569 }
root@9 570
root@9 571 // Choose the mailer and send through it
root@9 572 switch($this->Mailer) {
root@9 573 case 'sendmail':
root@9 574 return $this->SendmailSend($header, $body);
root@9 575 case 'smtp':
root@9 576 return $this->SmtpSend($header, $body);
root@9 577 default:
root@9 578 return $this->MailSend($header, $body);
root@9 579 }
root@9 580
root@9 581 } catch (phpmailerException $e) {
root@9 582 $this->SetError($e->getMessage());
root@9 583 if ($this->exceptions) {
root@9 584 throw $e;
root@9 585 }
root@9 586 echo $e->getMessage()."\n";
root@9 587 return false;
root@9 588 }
root@9 589 }
root@9 590
root@9 591 /**
root@9 592 * Sends mail using the $Sendmail program.
root@9 593 * @param string $header The message headers
root@9 594 * @param string $body The message body
root@9 595 * @access protected
root@9 596 * @return bool
root@9 597 */
root@9 598 protected function SendmailSend($header, $body) {
root@9 599 if ($this->Sender != '') {
root@9 600 $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
root@9 601 } else {
root@9 602 $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
root@9 603 }
root@9 604 if ($this->SingleTo === true) {
root@9 605 foreach ($this->SingleToArray as $key => $val) {
root@9 606 if(!@$mail = popen($sendmail, 'w')) {
root@9 607 throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
root@9 608 }
root@9 609 fputs($mail, "To: " . $val . "\n");
root@9 610 fputs($mail, $header);
root@9 611 fputs($mail, $body);
root@9 612 $result = pclose($mail);
root@9 613 // implement call back function if it exists
root@9 614 $isSent = ($result == 0) ? 1 : 0;
root@9 615 $this->doCallback($isSent,$val,$this->cc,$this->bcc,$this->Subject,$body);
root@9 616 if($result != 0) {
root@9 617 throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
root@9 618 }
root@9 619 }
root@9 620 } else {
root@9 621 if(!@$mail = popen($sendmail, 'w')) {
root@9 622 throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
root@9 623 }
root@9 624 fputs($mail, $header);
root@9 625 fputs($mail, $body);
root@9 626 $result = pclose($mail);
root@9 627 // implement call back function if it exists
root@9 628 $isSent = ($result == 0) ? 1 : 0;
root@9 629 $this->doCallback($isSent,$this->to,$this->cc,$this->bcc,$this->Subject,$body);
root@9 630 if($result != 0) {
root@9 631 throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
root@9 632 }
root@9 633 }
root@9 634 return true;
root@9 635 }
root@9 636
root@9 637 /**
root@9 638 * Sends mail using the PHP mail() function.
root@9 639 * @param string $header The message headers
root@9 640 * @param string $body The message body
root@9 641 * @access protected
root@9 642 * @return bool
root@9 643 */
root@9 644 protected function MailSend($header, $body) {
root@9 645 $toArr = array();
root@9 646 foreach($this->to as $t) {
root@9 647 $toArr[] = $this->AddrFormat($t);
root@9 648 }
root@9 649 $to = implode(', ', $toArr);
root@9 650
root@9 651 $params = sprintf("-oi -f %s", $this->Sender);
root@9 652 if ($this->Sender != '' && strlen(ini_get('safe_mode'))< 1) {
root@9 653 $old_from = ini_get('sendmail_from');
root@9 654 ini_set('sendmail_from', $this->Sender);
root@9 655 if ($this->SingleTo === true && count($toArr) > 1) {
root@9 656 foreach ($toArr as $key => $val) {
root@9 657 $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
root@9 658 // implement call back function if it exists
root@9 659 $isSent = ($rt == 1) ? 1 : 0;
root@9 660 $this->doCallback($isSent,$val,$this->cc,$this->bcc,$this->Subject,$body);
root@9 661 }
root@9 662 } else {
root@9 663 $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
root@9 664 // implement call back function if it exists
root@9 665 $isSent = ($rt == 1) ? 1 : 0;
root@9 666 $this->doCallback($isSent,$to,$this->cc,$this->bcc,$this->Subject,$body);
root@9 667 }
root@9 668 } else {
root@9 669 if ($this->SingleTo === true && count($toArr) > 1) {
root@9 670 foreach ($toArr as $key => $val) {
root@9 671 $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
root@9 672 // implement call back function if it exists
root@9 673 $isSent = ($rt == 1) ? 1 : 0;
root@9 674 $this->doCallback($isSent,$val,$this->cc,$this->bcc,$this->Subject,$body);
root@9 675 }
root@9 676 } else {
root@9 677 $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
root@9 678 // implement call back function if it exists
root@9 679 $isSent = ($rt == 1) ? 1 : 0;
root@9 680 $this->doCallback($isSent,$to,$this->cc,$this->bcc,$this->Subject,$body);
root@9 681 }
root@9 682 }
root@9 683 if (isset($old_from)) {
root@9 684 ini_set('sendmail_from', $old_from);
root@9 685 }
root@9 686 if(!$rt) {
root@9 687 throw new phpmailerException($this->Lang('instantiate'), self::STOP_CRITICAL);
root@9 688 }
root@9 689 return true;
root@9 690 }
root@9 691
root@9 692 /**
root@9 693 * Sends mail via SMTP using PhpSMTP
root@9 694 * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
root@9 695 * @param string $header The message headers
root@9 696 * @param string $body The message body
root@9 697 * @uses SMTP
root@9 698 * @access protected
root@9 699 * @return bool
root@9 700 */
root@9 701 protected function SmtpSend($header, $body) {
root@9 702 require_once $this->PluginDir . 'class.smtp.php';
root@9 703 $bad_rcpt = array();
root@9 704
root@9 705 if(!$this->SmtpConnect()) {
root@9 706 throw new phpmailerException($this->Lang('smtp_connect_failed'), self::STOP_CRITICAL);
root@9 707 }
root@9 708 $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
root@9 709 if(!$this->smtp->Mail($smtp_from)) {
root@9 710 throw new phpmailerException($this->Lang('from_failed') . $smtp_from, self::STOP_CRITICAL);
root@9 711 }
root@9 712
root@9 713 // Attempt to send attach all recipients
root@9 714 foreach($this->to as $to) {
root@9 715 if (!$this->smtp->Recipient($to[0])) {
root@9 716 $bad_rcpt[] = $to[0];
root@9 717 // implement call back function if it exists
root@9 718 $isSent = 0;
root@9 719 $this->doCallback($isSent,$to[0],'','',$this->Subject,$body);
root@9 720 } else {
root@9 721 // implement call back function if it exists
root@9 722 $isSent = 1;
root@9 723 $this->doCallback($isSent,$to[0],'','',$this->Subject,$body);
root@9 724 }
root@9 725 }
root@9 726 foreach($this->cc as $cc) {
root@9 727 if (!$this->smtp->Recipient($cc[0])) {
root@9 728 $bad_rcpt[] = $cc[0];
root@9 729 // implement call back function if it exists
root@9 730 $isSent = 0;
root@9 731 $this->doCallback($isSent,'',$cc[0],'',$this->Subject,$body);
root@9 732 } else {
root@9 733 // implement call back function if it exists
root@9 734 $isSent = 1;
root@9 735 $this->doCallback($isSent,'',$cc[0],'',$this->Subject,$body);
root@9 736 }
root@9 737 }
root@9 738 foreach($this->bcc as $bcc) {
root@9 739 if (!$this->smtp->Recipient($bcc[0])) {
root@9 740 $bad_rcpt[] = $bcc[0];
root@9 741 // implement call back function if it exists
root@9 742 $isSent = 0;
root@9 743 $this->doCallback($isSent,'','',$bcc[0],$this->Subject,$body);
root@9 744 } else {
root@9 745 // implement call back function if it exists
root@9 746 $isSent = 1;
root@9 747 $this->doCallback($isSent,'','',$bcc[0],$this->Subject,$body);
root@9 748 }
root@9 749 }
root@9 750
root@9 751
root@9 752 if (count($bad_rcpt) > 0 ) { //Create error message for any bad addresses
root@9 753 $badaddresses = implode(', ', $bad_rcpt);
root@9 754 throw new phpmailerException($this->Lang('recipients_failed') . $badaddresses);
root@9 755 }
root@9 756 if(!$this->smtp->Data($header . $body)) {
root@9 757 throw new phpmailerException($this->Lang('data_not_accepted'), self::STOP_CRITICAL);
root@9 758 }
root@9 759 if($this->SMTPKeepAlive == true) {
root@9 760 $this->smtp->Reset();
root@9 761 }
root@9 762 return true;
root@9 763 }
root@9 764
root@9 765 /**
root@9 766 * Initiates a connection to an SMTP server.
root@9 767 * Returns false if the operation failed.
root@9 768 * @uses SMTP
root@9 769 * @access public
root@9 770 * @return bool
root@9 771 */
root@9 772 public function SmtpConnect() {
root@9 773 if(is_null($this->smtp)) {
root@9 774 $this->smtp = new SMTP();
root@9 775 }
root@9 776
root@9 777 $this->smtp->do_debug = $this->SMTPDebug;
root@9 778 $hosts = explode(';', $this->Host);
root@9 779 $index = 0;
root@9 780 $connection = $this->smtp->Connected();
root@9 781
root@9 782 // Retry while there is no connection
root@9 783 try {
root@9 784 while($index < count($hosts) && !$connection) {
root@9 785 $hostinfo = array();
root@9 786 if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
root@9 787 $host = $hostinfo[1];
root@9 788 $port = $hostinfo[2];
root@9 789 } else {
root@9 790 $host = $hosts[$index];
root@9 791 $port = $this->Port;
root@9 792 }
root@9 793
root@9 794 $tls = ($this->SMTPSecure == 'tls');
root@9 795 $ssl = ($this->SMTPSecure == 'ssl');
root@9 796
root@9 797 if ($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
root@9 798
root@9 799 $hello = ($this->Helo != '' ? $this->Helo : $this->ServerHostname());
root@9 800 $this->smtp->Hello($hello);
root@9 801
root@9 802 if ($tls) {
root@9 803 if (!$this->smtp->StartTLS()) {
root@9 804 throw new phpmailerException($this->Lang('tls'));
root@9 805 }
root@9 806
root@9 807 //We must resend HELO after tls negotiation
root@9 808 $this->smtp->Hello($hello);
root@9 809 }
root@9 810
root@9 811 $connection = true;
root@9 812 if ($this->SMTPAuth) {
root@9 813 if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
root@9 814 throw new phpmailerException($this->Lang('authenticate'));
root@9 815 }
root@9 816 }
root@9 817 }
root@9 818 $index++;
root@9 819 if (!$connection) {
root@9 820 throw new phpmailerException($this->Lang('connect_host'));
root@9 821 }
root@9 822 }
root@9 823 } catch (phpmailerException $e) {
root@9 824 $this->smtp->Reset();
root@9 825 throw $e;
root@9 826 }
root@9 827 return true;
root@9 828 }
root@9 829
root@9 830 /**
root@9 831 * Closes the active SMTP session if one exists.
root@9 832 * @return void
root@9 833 */
root@9 834 public function SmtpClose() {
root@9 835 if(!is_null($this->smtp)) {
root@9 836 if($this->smtp->Connected()) {
root@9 837 $this->smtp->Quit();
root@9 838 $this->smtp->Close();
root@9 839 }
root@9 840 }
root@9 841 }
root@9 842
root@9 843 /**
root@9 844 * Sets the language for all class error messages.
root@9 845 * Returns false if it cannot load the language file. The default language is English.
root@9 846 * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
root@9 847 * @param string $lang_path Path to the language file directory
root@9 848 * @access public
root@9 849 */
root@9 850 function SetLanguage($langcode = 'en', $lang_path = 'language/') {
root@9 851 //Define full set of translatable strings
root@9 852 $PHPMAILER_LANG = array(
root@9 853 'provide_address' => 'You must provide at least one recipient email address.',
root@9 854 'mailer_not_supported' => ' mailer is not supported.',
root@9 855 'execute' => 'Could not execute: ',
root@9 856 'instantiate' => 'Could not instantiate mail function.',
root@9 857 'authenticate' => 'SMTP Error: Could not authenticate.',
root@9 858 'from_failed' => 'The following From address failed: ',
root@9 859 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
root@9 860 'data_not_accepted' => 'SMTP Error: Data not accepted.',
root@9 861 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
root@9 862 'file_access' => 'Could not access file: ',
root@9 863 'file_open' => 'File Error: Could not open file: ',
root@9 864 'encoding' => 'Unknown encoding: ',
root@9 865 'signing' => 'Signing Error: ',
root@9 866 'smtp_error' => 'SMTP server error: ',
root@9 867 'empty_message' => 'Message body empty',
root@9 868 'invalid_address' => 'Invalid address',
root@9 869 'variable_set' => 'Cannot set or reset variable: '
root@9 870 );
root@9 871 //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
root@9 872 $l = true;
root@9 873 if ($langcode != 'en') { //There is no English translation file
root@9 874 $l = @include $lang_path.'phpmailer.lang-'.$langcode.'.php';
root@9 875 }
root@9 876 $this->language = $PHPMAILER_LANG;
root@9 877 return ($l == true); //Returns false if language not found
root@9 878 }
root@9 879
root@9 880 /**
root@9 881 * Return the current array of language strings
root@9 882 * @return array
root@9 883 */
root@9 884 public function GetTranslations() {
root@9 885 return $this->language;
root@9 886 }
root@9 887
root@9 888 /////////////////////////////////////////////////
root@9 889 // METHODS, MESSAGE CREATION
root@9 890 /////////////////////////////////////////////////
root@9 891
root@9 892 /**
root@9 893 * Creates recipient headers.
root@9 894 * @access public
root@9 895 * @return string
root@9 896 */
root@9 897 public function AddrAppend($type, $addr) {
root@9 898 $addr_str = $type . ': ';
root@9 899 $addresses = array();
root@9 900 foreach ($addr as $a) {
root@9 901 $addresses[] = $this->AddrFormat($a);
root@9 902 }
root@9 903 $addr_str .= implode(', ', $addresses);
root@9 904 $addr_str .= $this->LE;
root@9 905
root@9 906 return $addr_str;
root@9 907 }
root@9 908
root@9 909 /**
root@9 910 * Formats an address correctly.
root@9 911 * @access public
root@9 912 * @return string
root@9 913 */
root@9 914 public function AddrFormat($addr) {
root@9 915 if (empty($addr[1])) {
root@9 916 return $this->SecureHeader($addr[0]);
root@9 917 } else {
root@9 918 return $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
root@9 919 }
root@9 920 }
root@9 921
root@9 922 /**
root@9 923 * Wraps message for use with mailers that do not
root@9 924 * automatically perform wrapping and for quoted-printable.
root@9 925 * Original written by philippe.
root@9 926 * @param string $message The message to wrap
root@9 927 * @param integer $length The line length to wrap to
root@9 928 * @param boolean $qp_mode Whether to run in Quoted-Printable mode
root@9 929 * @access public
root@9 930 * @return string
root@9 931 */
root@9 932 public function WrapText($message, $length, $qp_mode = false) {
root@9 933 $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
root@9 934 // If utf-8 encoding is used, we will need to make sure we don't
root@9 935 // split multibyte characters when we wrap
root@9 936 $is_utf8 = (strtolower($this->CharSet) == "utf-8");
root@9 937
root@9 938 $message = $this->FixEOL($message);
root@9 939 if (substr($message, -1) == $this->LE) {
root@9 940 $message = substr($message, 0, -1);
root@9 941 }
root@9 942
root@9 943 $line = explode($this->LE, $message);
root@9 944 $message = '';
root@9 945 for ($i=0 ;$i < count($line); $i++) {
root@9 946 $line_part = explode(' ', $line[$i]);
root@9 947 $buf = '';
root@9 948 for ($e = 0; $e<count($line_part); $e++) {
root@9 949 $word = $line_part[$e];
root@9 950 if ($qp_mode and (strlen($word) > $length)) {
root@9 951 $space_left = $length - strlen($buf) - 1;
root@9 952 if ($e != 0) {
root@9 953 if ($space_left > 20) {
root@9 954 $len = $space_left;
root@9 955 if ($is_utf8) {
root@9 956 $len = $this->UTF8CharBoundary($word, $len);
root@9 957 } elseif (substr($word, $len - 1, 1) == "=") {
root@9 958 $len--;
root@9 959 } elseif (substr($word, $len - 2, 1) == "=") {
root@9 960 $len -= 2;
root@9 961 }
root@9 962 $part = substr($word, 0, $len);
root@9 963 $word = substr($word, $len);
root@9 964 $buf .= ' ' . $part;
root@9 965 $message .= $buf . sprintf("=%s", $this->LE);
root@9 966 } else {
root@9 967 $message .= $buf . $soft_break;
root@9 968 }
root@9 969 $buf = '';
root@9 970 }
root@9 971 while (strlen($word) > 0) {
root@9 972 $len = $length;
root@9 973 if ($is_utf8) {
root@9 974 $len = $this->UTF8CharBoundary($word, $len);
root@9 975 } elseif (substr($word, $len - 1, 1) == "=") {
root@9 976 $len--;
root@9 977 } elseif (substr($word, $len - 2, 1) == "=") {
root@9 978 $len -= 2;
root@9 979 }
root@9 980 $part = substr($word, 0, $len);
root@9 981 $word = substr($word, $len);
root@9 982
root@9 983 if (strlen($word) > 0) {
root@9 984 $message .= $part . sprintf("=%s", $this->LE);
root@9 985 } else {
root@9 986 $buf = $part;
root@9 987 }
root@9 988 }
root@9 989 } else {
root@9 990 $buf_o = $buf;
root@9 991 $buf .= ($e == 0) ? $word : (' ' . $word);
root@9 992
root@9 993 if (strlen($buf) > $length and $buf_o != '') {
root@9 994 $message .= $buf_o . $soft_break;
root@9 995 $buf = $word;
root@9 996 }
root@9 997 }
root@9 998 }
root@9 999 $message .= $buf . $this->LE;
root@9 1000 }
root@9 1001
root@9 1002 return $message;
root@9 1003 }
root@9 1004
root@9 1005 /**
root@9 1006 * Finds last character boundary prior to maxLength in a utf-8
root@9 1007 * quoted (printable) encoded string.
root@9 1008 * Original written by Colin Brown.
root@9 1009 * @access public
root@9 1010 * @param string $encodedText utf-8 QP text
root@9 1011 * @param int $maxLength find last character boundary prior to this length
root@9 1012 * @return int
root@9 1013 */
root@9 1014 public function UTF8CharBoundary($encodedText, $maxLength) {
root@9 1015 $foundSplitPos = false;
root@9 1016 $lookBack = 3;
root@9 1017 while (!$foundSplitPos) {
root@9 1018 $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
root@9 1019 $encodedCharPos = strpos($lastChunk, "=");
root@9 1020 if ($encodedCharPos !== false) {
root@9 1021 // Found start of encoded character byte within $lookBack block.
root@9 1022 // Check the encoded byte value (the 2 chars after the '=')
root@9 1023 $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
root@9 1024 $dec = hexdec($hex);
root@9 1025 if ($dec < 128) { // Single byte character.
root@9 1026 // If the encoded char was found at pos 0, it will fit
root@9 1027 // otherwise reduce maxLength to start of the encoded char
root@9 1028 $maxLength = ($encodedCharPos == 0) ? $maxLength :
root@9 1029 $maxLength - ($lookBack - $encodedCharPos);
root@9 1030 $foundSplitPos = true;
root@9 1031 } elseif ($dec >= 192) { // First byte of a multi byte character
root@9 1032 // Reduce maxLength to split at start of character
root@9 1033 $maxLength = $maxLength - ($lookBack - $encodedCharPos);
root@9 1034 $foundSplitPos = true;
root@9 1035 } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
root@9 1036 $lookBack += 3;
root@9 1037 }
root@9 1038 } else {
root@9 1039 // No encoded character found
root@9 1040 $foundSplitPos = true;
root@9 1041 }
root@9 1042 }
root@9 1043 return $maxLength;
root@9 1044 }
root@9 1045
root@9 1046
root@9 1047 /**
root@9 1048 * Set the body wrapping.
root@9 1049 * @access public
root@9 1050 * @return void
root@9 1051 */
root@9 1052 public function SetWordWrap() {
root@9 1053 if($this->WordWrap < 1) {
root@9 1054 return;
root@9 1055 }
root@9 1056
root@9 1057 switch($this->message_type) {
root@9 1058 case 'alt':
root@9 1059 case 'alt_attachments':
root@9 1060 $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
root@9 1061 break;
root@9 1062 default:
root@9 1063 $this->Body = $this->WrapText($this->Body, $this->WordWrap);
root@9 1064 break;
root@9 1065 }
root@9 1066 }
root@9 1067
root@9 1068 /**
root@9 1069 * Assembles message header.
root@9 1070 * @access public
root@9 1071 * @return string The assembled header
root@9 1072 */
root@9 1073 public function CreateHeader() {
root@9 1074 $result = '';
root@9 1075
root@9 1076 // Set the boundaries
root@9 1077 $uniq_id = md5(uniqid(time()));
root@9 1078 $this->boundary[1] = 'b1_' . $uniq_id;
root@9 1079 $this->boundary[2] = 'b2_' . $uniq_id;
root@9 1080
root@9 1081 $result .= $this->HeaderLine('Date', self::RFCDate());
root@9 1082 if($this->Sender == '') {
root@9 1083 $result .= $this->HeaderLine('Return-Path', trim($this->From));
root@9 1084 } else {
root@9 1085 $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
root@9 1086 }
root@9 1087
root@9 1088 // To be created automatically by mail()
root@9 1089 if($this->Mailer != 'mail') {
root@9 1090 if ($this->SingleTo === true) {
root@9 1091 foreach($this->to as $t) {
root@9 1092 $this->SingleToArray[] = $this->AddrFormat($t);
root@9 1093 }
root@9 1094 } else {
root@9 1095 if(count($this->to) > 0) {
root@9 1096 $result .= $this->AddrAppend('To', $this->to);
root@9 1097 } elseif (count($this->cc) == 0) {
root@9 1098 $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
root@9 1099 }
root@9 1100 }
root@9 1101 }
root@9 1102
root@9 1103 $from = array();
root@9 1104 $from[0][0] = trim($this->From);
root@9 1105 $from[0][1] = $this->FromName;
root@9 1106 $result .= $this->AddrAppend('From', $from);
root@9 1107
root@9 1108 // sendmail and mail() extract Cc from the header before sending
root@9 1109 if(count($this->cc) > 0) {
root@9 1110 $result .= $this->AddrAppend('Cc', $this->cc);
root@9 1111 }
root@9 1112
root@9 1113 // sendmail and mail() extract Bcc from the header before sending
root@9 1114 if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
root@9 1115 $result .= $this->AddrAppend('Bcc', $this->bcc);
root@9 1116 }
root@9 1117
root@9 1118 if(count($this->ReplyTo) > 0) {
root@9 1119 $result .= $this->AddrAppend('Reply-to', $this->ReplyTo);
root@9 1120 }
root@9 1121
root@9 1122 // mail() sets the subject itself
root@9 1123 if($this->Mailer != 'mail') {
root@9 1124 $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
root@9 1125 }
root@9 1126
root@9 1127 if($this->MessageID != '') {
root@9 1128 $result .= $this->HeaderLine('Message-ID',$this->MessageID);
root@9 1129 } else {
root@9 1130 $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
root@9 1131 }
root@9 1132 $result .= $this->HeaderLine('X-Priority', $this->Priority);
root@9 1133 $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (phpmailer.sourceforge.net)');
root@9 1134
root@9 1135 if($this->ConfirmReadingTo != '') {
root@9 1136 $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
root@9 1137 }
root@9 1138
root@9 1139 // Add custom headers
root@9 1140 for($index = 0; $index < count($this->CustomHeader); $index++) {
root@9 1141 $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
root@9 1142 }
root@9 1143 if (!$this->sign_key_file) {
root@9 1144 $result .= $this->HeaderLine('MIME-Version', '1.0');
root@9 1145 $result .= $this->GetMailMIME();
root@9 1146 }
root@9 1147
root@9 1148 return $result;
root@9 1149 }
root@9 1150
root@9 1151 /**
root@9 1152 * Returns the message MIME.
root@9 1153 * @access public
root@9 1154 * @return string
root@9 1155 */
root@9 1156 public function GetMailMIME() {
root@9 1157 $result = '';
root@9 1158 switch($this->message_type) {
root@9 1159 case 'plain':
root@9 1160 $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
root@9 1161 $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
root@9 1162 break;
root@9 1163 case 'attachments':
root@9 1164 case 'alt_attachments':
root@9 1165 if($this->InlineImageExists()){
root@9 1166 $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
root@9 1167 } else {
root@9 1168 $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
root@9 1169 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
root@9 1170 }
root@9 1171 break;
root@9 1172 case 'alt':
root@9 1173 $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
root@9 1174 $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
root@9 1175 break;
root@9 1176 }
root@9 1177
root@9 1178 if($this->Mailer != 'mail') {
root@9 1179 $result .= $this->LE.$this->LE;
root@9 1180 }
root@9 1181
root@9 1182 return $result;
root@9 1183 }
root@9 1184
root@9 1185 /**
root@9 1186 * Assembles the message body. Returns an empty string on failure.
root@9 1187 * @access public
root@9 1188 * @return string The assembled message body
root@9 1189 */
root@9 1190 public function CreateBody() {
root@9 1191 $body = '';
root@9 1192
root@9 1193 if ($this->sign_key_file) {
root@9 1194 $body .= $this->GetMailMIME();
root@9 1195 }
root@9 1196
root@9 1197 $this->SetWordWrap();
root@9 1198
root@9 1199 switch($this->message_type) {
root@9 1200 case 'alt':
root@9 1201 $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
root@9 1202 $body .= $this->EncodeString($this->AltBody, $this->Encoding);
root@9 1203 $body .= $this->LE.$this->LE;
root@9 1204 $body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
root@9 1205 $body .= $this->EncodeString($this->Body, $this->Encoding);
root@9 1206 $body .= $this->LE.$this->LE;
root@9 1207 $body .= $this->EndBoundary($this->boundary[1]);
root@9 1208 break;
root@9 1209 case 'plain':
root@9 1210 $body .= $this->EncodeString($this->Body, $this->Encoding);
root@9 1211 break;
root@9 1212 case 'attachments':
root@9 1213 $body .= $this->GetBoundary($this->boundary[1], '', '', '');
root@9 1214 $body .= $this->EncodeString($this->Body, $this->Encoding);
root@9 1215 $body .= $this->LE;
root@9 1216 $body .= $this->AttachAll();
root@9 1217 break;
root@9 1218 case 'alt_attachments':
root@9 1219 $body .= sprintf("--%s%s", $this->boundary[1], $this->LE);
root@9 1220 $body .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
root@9 1221 $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
root@9 1222 $body .= $this->EncodeString($this->AltBody, $this->Encoding);
root@9 1223 $body .= $this->LE.$this->LE;
root@9 1224 $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
root@9 1225 $body .= $this->EncodeString($this->Body, $this->Encoding);
root@9 1226 $body .= $this->LE.$this->LE;
root@9 1227 $body .= $this->EndBoundary($this->boundary[2]);
root@9 1228 $body .= $this->AttachAll();
root@9 1229 break;
root@9 1230 }
root@9 1231
root@9 1232 if ($this->IsError()) {
root@9 1233 $body = '';
root@9 1234 } elseif ($this->sign_key_file) {
root@9 1235 try {
root@9 1236 $file = tempnam('', 'mail');
root@9 1237 file_put_contents($file, $body); //TODO check this worked
root@9 1238 $signed = tempnam("", "signed");
root@9 1239 if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) {
root@9 1240 @unlink($file);
root@9 1241 @unlink($signed);
root@9 1242 $body = file_get_contents($signed);
root@9 1243 } else {
root@9 1244 @unlink($file);
root@9 1245 @unlink($signed);
root@9 1246 throw new phpmailerException($this->Lang("signing").openssl_error_string());
root@9 1247 }
root@9 1248 } catch (phpmailerException $e) {
root@9 1249 $body = '';
root@9 1250 if ($this->exceptions) {
root@9 1251 throw $e;
root@9 1252 }
root@9 1253 }
root@9 1254 }
root@9 1255
root@9 1256 return $body;
root@9 1257 }
root@9 1258
root@9 1259 /**
root@9 1260 * Returns the start of a message boundary.
root@9 1261 * @access private
root@9 1262 */
root@9 1263 private function GetBoundary($boundary, $charSet, $contentType, $encoding) {
root@9 1264 $result = '';
root@9 1265 if($charSet == '') {
root@9 1266 $charSet = $this->CharSet;
root@9 1267 }
root@9 1268 if($contentType == '') {
root@9 1269 $contentType = $this->ContentType;
root@9 1270 }
root@9 1271 if($encoding == '') {
root@9 1272 $encoding = $this->Encoding;
root@9 1273 }
root@9 1274 $result .= $this->TextLine('--' . $boundary);
root@9 1275 $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
root@9 1276 $result .= $this->LE;
root@9 1277 $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
root@9 1278 $result .= $this->LE;
root@9 1279
root@9 1280 return $result;
root@9 1281 }
root@9 1282
root@9 1283 /**
root@9 1284 * Returns the end of a message boundary.
root@9 1285 * @access private
root@9 1286 */
root@9 1287 private function EndBoundary($boundary) {
root@9 1288 return $this->LE . '--' . $boundary . '--' . $this->LE;
root@9 1289 }
root@9 1290
root@9 1291 /**
root@9 1292 * Sets the message type.
root@9 1293 * @access private
root@9 1294 * @return void
root@9 1295 */
root@9 1296 private function SetMessageType() {
root@9 1297 if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
root@9 1298 $this->message_type = 'plain';
root@9 1299 } else {
root@9 1300 if(count($this->attachment) > 0) {
root@9 1301 $this->message_type = 'attachments';
root@9 1302 }
root@9 1303 if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
root@9 1304 $this->message_type = 'alt';
root@9 1305 }
root@9 1306 if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
root@9 1307 $this->message_type = 'alt_attachments';
root@9 1308 }
root@9 1309 }
root@9 1310 }
root@9 1311
root@9 1312 /**
root@9 1313 * Returns a formatted header line.
root@9 1314 * @access public
root@9 1315 * @return string
root@9 1316 */
root@9 1317 public function HeaderLine($name, $value) {
root@9 1318 return $name . ': ' . $value . $this->LE;
root@9 1319 }
root@9 1320
root@9 1321 /**
root@9 1322 * Returns a formatted mail line.
root@9 1323 * @access public
root@9 1324 * @return string
root@9 1325 */
root@9 1326 public function TextLine($value) {
root@9 1327 return $value . $this->LE;
root@9 1328 }
root@9 1329
root@9 1330 /////////////////////////////////////////////////
root@9 1331 // CLASS METHODS, ATTACHMENTS
root@9 1332 /////////////////////////////////////////////////
root@9 1333
root@9 1334 /**
root@9 1335 * Adds an attachment from a path on the filesystem.
root@9 1336 * Returns false if the file could not be found
root@9 1337 * or accessed.
root@9 1338 * @param string $path Path to the attachment.
root@9 1339 * @param string $name Overrides the attachment name.
root@9 1340 * @param string $encoding File encoding (see $Encoding).
root@9 1341 * @param string $type File extension (MIME) type.
root@9 1342 * @return bool
root@9 1343 */
root@9 1344 public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
root@9 1345 try {
root@9 1346 if ( !@is_file($path) ) {
root@9 1347 throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
root@9 1348 }
root@9 1349 $filename = basename($path);
root@9 1350 if ( $name == '' ) {
root@9 1351 $name = $filename;
root@9 1352 }
root@9 1353
root@9 1354 $this->attachment[] = array(
root@9 1355 0 => $path,
root@9 1356 1 => $filename,
root@9 1357 2 => $name,
root@9 1358 3 => $encoding,
root@9 1359 4 => $type,
root@9 1360 5 => false, // isStringAttachment
root@9 1361 6 => 'attachment',
root@9 1362 7 => 0
root@9 1363 );
root@9 1364
root@9 1365 } catch (phpmailerException $e) {
root@9 1366 $this->SetError($e->getMessage());
root@9 1367 if ($this->exceptions) {
root@9 1368 throw $e;
root@9 1369 }
root@9 1370 echo $e->getMessage()."\n";
root@9 1371 if ( $e->getCode() == self::STOP_CRITICAL ) {
root@9 1372 return false;
root@9 1373 }
root@9 1374 }
root@9 1375 return true;
root@9 1376 }
root@9 1377
root@9 1378 /**
root@9 1379 * Return the current array of attachments
root@9 1380 * @return array
root@9 1381 */
root@9 1382 public function GetAttachments() {
root@9 1383 return $this->attachment;
root@9 1384 }
root@9 1385
root@9 1386 /**
root@9 1387 * Attaches all fs, string, and binary attachments to the message.
root@9 1388 * Returns an empty string on failure.
root@9 1389 * @access private
root@9 1390 * @return string
root@9 1391 */
root@9 1392 private function AttachAll() {
root@9 1393 // Return text of body
root@9 1394 $mime = array();
root@9 1395 $cidUniq = array();
root@9 1396 $incl = array();
root@9 1397
root@9 1398 // Add all attachments
root@9 1399 foreach ($this->attachment as $attachment) {
root@9 1400 // Check for string attachment
root@9 1401 $bString = $attachment[5];
root@9 1402 if ($bString) {
root@9 1403 $string = $attachment[0];
root@9 1404 } else {
root@9 1405 $path = $attachment[0];
root@9 1406 }
root@9 1407
root@9 1408 if (in_array($attachment[0], $incl)) { continue; }
root@9 1409 $filename = $attachment[1];
root@9 1410 $name = $attachment[2];
root@9 1411 $encoding = $attachment[3];
root@9 1412 $type = $attachment[4];
root@9 1413 $disposition = $attachment[6];
root@9 1414 $cid = $attachment[7];
root@9 1415 $incl[] = $attachment[0];
root@9 1416 if ( $disposition == 'inline' && isset($cidUniq[$cid]) ) { continue; }
root@9 1417 $cidUniq[$cid] = true;
root@9 1418
root@9 1419 $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
root@9 1420 $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
root@9 1421 $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
root@9 1422
root@9 1423 if($disposition == 'inline') {
root@9 1424 $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
root@9 1425 }
root@9 1426
root@9 1427 $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
root@9 1428
root@9 1429 // Encode as string attachment
root@9 1430 if($bString) {
root@9 1431 $mime[] = $this->EncodeString($string, $encoding);
root@9 1432 if($this->IsError()) {
root@9 1433 return '';
root@9 1434 }
root@9 1435 $mime[] = $this->LE.$this->LE;
root@9 1436 } else {
root@9 1437 $mime[] = $this->EncodeFile($path, $encoding);
root@9 1438 if($this->IsError()) {
root@9 1439 return '';
root@9 1440 }
root@9 1441 $mime[] = $this->LE.$this->LE;
root@9 1442 }
root@9 1443 }
root@9 1444
root@9 1445 $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
root@9 1446
root@9 1447 return join('', $mime);
root@9 1448 }
root@9 1449
root@9 1450 /**
root@9 1451 * Encodes attachment in requested format.
root@9 1452 * Returns an empty string on failure.
root@9 1453 * @param string $path The full path to the file
root@9 1454 * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
root@9 1455 * @see EncodeFile()
root@9 1456 * @access private
root@9 1457 * @return string
root@9 1458 */
root@9 1459 private function EncodeFile($path, $encoding = 'base64') {
root@9 1460 try {
root@9 1461 if (!is_readable($path)) {
root@9 1462 throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE);
root@9 1463 }
root@9 1464 if (function_exists('get_magic_quotes')) {
root@9 1465 function get_magic_quotes() {
root@9 1466 return false;
root@9 1467 }
root@9 1468 }
root@9 1469 if (PHP_VERSION < 6) {
root@9 1470 $magic_quotes = get_magic_quotes_runtime();
root@9 1471 set_magic_quotes_runtime(0);
root@9 1472 }
root@9 1473 $file_buffer = file_get_contents($path);
root@9 1474 $file_buffer = $this->EncodeString($file_buffer, $encoding);
root@9 1475 if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); }
root@9 1476 return $file_buffer;
root@9 1477 } catch (Exception $e) {
root@9 1478 $this->SetError($e->getMessage());
root@9 1479 return '';
root@9 1480 }
root@9 1481 }
root@9 1482
root@9 1483 /**
root@9 1484 * Encodes string to requested format.
root@9 1485 * Returns an empty string on failure.
root@9 1486 * @param string $str The text to encode
root@9 1487 * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
root@9 1488 * @access public
root@9 1489 * @return string
root@9 1490 */
root@9 1491 public function EncodeString ($str, $encoding = 'base64') {
root@9 1492 $encoded = '';
root@9 1493 switch(strtolower($encoding)) {
root@9 1494 case 'base64':
root@9 1495 $encoded = chunk_split(base64_encode($str), 76, $this->LE);
root@9 1496 break;
root@9 1497 case '7bit':
root@9 1498 case '8bit':
root@9 1499 $encoded = $this->FixEOL($str);
root@9 1500 //Make sure it ends with a line break
root@9 1501 if (substr($encoded, -(strlen($this->LE))) != $this->LE)
root@9 1502 $encoded .= $this->LE;
root@9 1503 break;
root@9 1504 case 'binary':
root@9 1505 $encoded = $str;
root@9 1506 break;
root@9 1507 case 'quoted-printable':
root@9 1508 $encoded = $this->EncodeQP($str);
root@9 1509 break;
root@9 1510 default:
root@9 1511 $this->SetError($this->Lang('encoding') . $encoding);
root@9 1512 break;
root@9 1513 }
root@9 1514 return $encoded;
root@9 1515 }
root@9 1516
root@9 1517 /**
root@9 1518 * Encode a header string to best (shortest) of Q, B, quoted or none.
root@9 1519 * @access public
root@9 1520 * @return string
root@9 1521 */
root@9 1522 public function EncodeHeader($str, $position = 'text') {
root@9 1523 $x = 0;
root@9 1524
root@9 1525 switch (strtolower($position)) {
root@9 1526 case 'phrase':
root@9 1527 if (!preg_match('/[\200-\377]/', $str)) {
root@9 1528 // Can't use addslashes as we don't know what value has magic_quotes_sybase
root@9 1529 $encoded = addcslashes($str, "\0..\37\177\\\"");
root@9 1530 if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
root@9 1531 return ($encoded);
root@9 1532 } else {
root@9 1533 return ("\"$encoded\"");
root@9 1534 }
root@9 1535 }
root@9 1536 $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
root@9 1537 break;
root@9 1538 case 'comment':
root@9 1539 $x = preg_match_all('/[()"]/', $str, $matches);
root@9 1540 // Fall-through
root@9 1541 case 'text':
root@9 1542 default:
root@9 1543 $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
root@9 1544 break;
root@9 1545 }
root@9 1546
root@9 1547 if ($x == 0) {
root@9 1548 return ($str);
root@9 1549 }
root@9 1550
root@9 1551 $maxlen = 75 - 7 - strlen($this->CharSet);
root@9 1552 // Try to select the encoding which should produce the shortest output
root@9 1553 if (strlen($str)/3 < $x) {
root@9 1554 $encoding = 'B';
root@9 1555 if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
root@9 1556 // Use a custom function which correctly encodes and wraps long
root@9 1557 // multibyte strings without breaking lines within a character
root@9 1558 $encoded = $this->Base64EncodeWrapMB($str);
root@9 1559 } else {
root@9 1560 $encoded = base64_encode($str);
root@9 1561 $maxlen -= $maxlen % 4;
root@9 1562 $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
root@9 1563 }
root@9 1564 } else {
root@9 1565 $encoding = 'Q';
root@9 1566 $encoded = $this->EncodeQ($str, $position);
root@9 1567 $encoded = $this->WrapText($encoded, $maxlen, true);
root@9 1568 $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
root@9 1569 }
root@9 1570
root@9 1571 $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
root@9 1572 $encoded = trim(str_replace("\n", $this->LE, $encoded));
root@9 1573
root@9 1574 return $encoded;
root@9 1575 }
root@9 1576
root@9 1577 /**
root@9 1578 * Checks if a string contains multibyte characters.
root@9 1579 * @access public
root@9 1580 * @param string $str multi-byte text to wrap encode
root@9 1581 * @return bool
root@9 1582 */
root@9 1583 public function HasMultiBytes($str) {
root@9 1584 if (function_exists('mb_strlen')) {
root@9 1585 return (strlen($str) > mb_strlen($str, $this->CharSet));
root@9 1586 } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
root@9 1587 return false;
root@9 1588 }
root@9 1589 }
root@9 1590
root@9 1591 /**
root@9 1592 * Correctly encodes and wraps long multibyte strings for mail headers
root@9 1593 * without breaking lines within a character.
root@9 1594 * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
root@9 1595 * @access public
root@9 1596 * @param string $str multi-byte text to wrap encode
root@9 1597 * @return string
root@9 1598 */
root@9 1599 public function Base64EncodeWrapMB($str) {
root@9 1600 $start = "=?".$this->CharSet."?B?";
root@9 1601 $end = "?=";
root@9 1602 $encoded = "";
root@9 1603
root@9 1604 $mb_length = mb_strlen($str, $this->CharSet);
root@9 1605 // Each line must have length <= 75, including $start and $end
root@9 1606 $length = 75 - strlen($start) - strlen($end);
root@9 1607 // Average multi-byte ratio
root@9 1608 $ratio = $mb_length / strlen($str);
root@9 1609 // Base64 has a 4:3 ratio
root@9 1610 $offset = $avgLength = floor($length * $ratio * .75);
root@9 1611
root@9 1612 for ($i = 0; $i < $mb_length; $i += $offset) {
root@9 1613 $lookBack = 0;
root@9 1614
root@9 1615 do {
root@9 1616 $offset = $avgLength - $lookBack;
root@9 1617 $chunk = mb_substr($str, $i, $offset, $this->CharSet);
root@9 1618 $chunk = base64_encode($chunk);
root@9 1619 $lookBack++;
root@9 1620 }
root@9 1621 while (strlen($chunk) > $length);
root@9 1622
root@9 1623 $encoded .= $chunk . $this->LE;
root@9 1624 }
root@9 1625
root@9 1626 // Chomp the last linefeed
root@9 1627 $encoded = substr($encoded, 0, -strlen($this->LE));
root@9 1628 return $encoded;
root@9 1629 }
root@9 1630
root@9 1631 /**
root@9 1632 * Encode string to quoted-printable.
root@9 1633 * Only uses standard PHP, slow, but will always work
root@9 1634 * @access public
root@9 1635 * @param string $string the text to encode
root@9 1636 * @param integer $line_max Number of chars allowed on a line before wrapping
root@9 1637 * @return string
root@9 1638 */
root@9 1639 public function EncodeQPphp( $input = '', $line_max = 76, $space_conv = false) {
root@9 1640 $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
root@9 1641 $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
root@9 1642 $eol = "\r\n";
root@9 1643 $escape = '=';
root@9 1644 $output = '';
root@9 1645 while( list(, $line) = each($lines) ) {
root@9 1646 $linlen = strlen($line);
root@9 1647 $newline = '';
root@9 1648 for($i = 0; $i < $linlen; $i++) {
root@9 1649 $c = substr( $line, $i, 1 );
root@9 1650 $dec = ord( $c );
root@9 1651 if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
root@9 1652 $c = '=2E';
root@9 1653 }
root@9 1654 if ( $dec == 32 ) {
root@9 1655 if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
root@9 1656 $c = '=20';
root@9 1657 } else if ( $space_conv ) {
root@9 1658 $c = '=20';
root@9 1659 }
root@9 1660 } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
root@9 1661 $h2 = floor($dec/16);
root@9 1662 $h1 = floor($dec%16);
root@9 1663 $c = $escape.$hex[$h2].$hex[$h1];
root@9 1664 }
root@9 1665 if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
root@9 1666 $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
root@9 1667 $newline = '';
root@9 1668 // check if newline first character will be point or not
root@9 1669 if ( $dec == 46 ) {
root@9 1670 $c = '=2E';
root@9 1671 }
root@9 1672 }
root@9 1673 $newline .= $c;
root@9 1674 } // end of for
root@9 1675 $output .= $newline.$eol;
root@9 1676 } // end of while
root@9 1677 return $output;
root@9 1678 }
root@9 1679
root@9 1680 /**
root@9 1681 * Encode string to RFC2045 (6.7) quoted-printable format
root@9 1682 * Uses a PHP5 stream filter to do the encoding about 64x faster than the old version
root@9 1683 * Also results in same content as you started with after decoding
root@9 1684 * @see EncodeQPphp()
root@9 1685 * @access public
root@9 1686 * @param string $string the text to encode
root@9 1687 * @param integer $line_max Number of chars allowed on a line before wrapping
root@9 1688 * @param boolean $space_conv Dummy param for compatibility with existing EncodeQP function
root@9 1689 * @return string
root@9 1690 * @author Marcus Bointon
root@9 1691 */
root@9 1692 public function EncodeQP($string, $line_max = 76, $space_conv = false) {
root@9 1693 if (function_exists('quoted_printable_encode')) { //Use native function if it's available (>= PHP5.3)
root@9 1694 return quoted_printable_encode($string);
root@9 1695 }
root@9 1696 $filters = stream_get_filters();
root@9 1697 if (!in_array('convert.*', $filters)) { //Got convert stream filter?
root@9 1698 return $this->EncodeQPphp($string, $line_max, $space_conv); //Fall back to old implementation
root@9 1699 }
root@9 1700 $fp = fopen('php://temp/', 'r+');
root@9 1701 $string = preg_replace('/\r\n?/', $this->LE, $string); //Normalise line breaks
root@9 1702 $params = array('line-length' => $line_max, 'line-break-chars' => $this->LE);
root@9 1703 $s = stream_filter_append($fp, 'convert.quoted-printable-encode', STREAM_FILTER_READ, $params);
root@9 1704 fputs($fp, $string);
root@9 1705 rewind($fp);
root@9 1706 $out = stream_get_contents($fp);
root@9 1707 stream_filter_remove($s);
root@9 1708 $out = preg_replace('/^\./m', '=2E', $out); //Encode . if it is first char on a line, workaround for bug in Exchange
root@9 1709 fclose($fp);
root@9 1710 return $out;
root@9 1711 }
root@9 1712
root@9 1713 /**
root@9 1714 * Encode string to q encoding.
root@9 1715 * @link http://tools.ietf.org/html/rfc2047
root@9 1716 * @param string $str the text to encode
root@9 1717 * @param string $position Where the text is going to be used, see the RFC for what that means
root@9 1718 * @access public
root@9 1719 * @return string
root@9 1720 */
root@9 1721 public function EncodeQ ($str, $position = 'text') {
root@9 1722 // There should not be any EOL in the string
root@9 1723 $encoded = preg_replace('/[\r\n]*/', '', $str);
root@9 1724
root@9 1725 switch (strtolower($position)) {
root@9 1726 case 'phrase':
root@9 1727 $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
root@9 1728 break;
root@9 1729 case 'comment':
root@9 1730 $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
root@9 1731 case 'text':
root@9 1732 default:
root@9 1733 // Replace every high ascii, control =, ? and _ characters
root@9 1734 //TODO using /e (equivalent to eval()) is probably not a good idea
root@9 1735 $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
root@9 1736 "'='.sprintf('%02X', ord('\\1'))", $encoded);
root@9 1737 break;
root@9 1738 }
root@9 1739
root@9 1740 // Replace every spaces to _ (more readable than =20)
root@9 1741 $encoded = str_replace(' ', '_', $encoded);
root@9 1742
root@9 1743 return $encoded;
root@9 1744 }
root@9 1745
root@9 1746 /**
root@9 1747 * Adds a string or binary attachment (non-filesystem) to the list.
root@9 1748 * This method can be used to attach ascii or binary data,
root@9 1749 * such as a BLOB record from a database.
root@9 1750 * @param string $string String attachment data.
root@9 1751 * @param string $filename Name of the attachment.
root@9 1752 * @param string $encoding File encoding (see $Encoding).
root@9 1753 * @param string $type File extension (MIME) type.
root@9 1754 * @return void
root@9 1755 */
root@9 1756 public function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
root@9 1757 // Append to $attachment array
root@9 1758 $this->attachment[] = array(
root@9 1759 0 => $string,
root@9 1760 1 => $filename,
root@9 1761 2 => basename($filename),
root@9 1762 3 => $encoding,
root@9 1763 4 => $type,
root@9 1764 5 => true, // isStringAttachment
root@9 1765 6 => 'attachment',
root@9 1766 7 => 0
root@9 1767 );
root@9 1768 }
root@9 1769
root@9 1770 /**
root@9 1771 * Adds an embedded attachment. This can include images, sounds, and
root@9 1772 * just about any other document. Make sure to set the $type to an
root@9 1773 * image type. For JPEG images use "image/jpeg" and for GIF images
root@9 1774 * use "image/gif".
root@9 1775 * @param string $path Path to the attachment.
root@9 1776 * @param string $cid Content ID of the attachment. Use this to identify
root@9 1777 * the Id for accessing the image in an HTML form.
root@9 1778 * @param string $name Overrides the attachment name.
root@9 1779 * @param string $encoding File encoding (see $Encoding).
root@9 1780 * @param string $type File extension (MIME) type.
root@9 1781 * @return bool
root@9 1782 */
root@9 1783 public function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
root@9 1784
root@9 1785 if ( !@is_file($path) ) {
root@9 1786 $this->SetError($this->Lang('file_access') . $path);
root@9 1787 return false;
root@9 1788 }
root@9 1789
root@9 1790 $filename = basename($path);
root@9 1791 if ( $name == '' ) {
root@9 1792 $name = $filename;
root@9 1793 }
root@9 1794
root@9 1795 // Append to $attachment array
root@9 1796 $this->attachment[] = array(
root@9 1797 0 => $path,
root@9 1798 1 => $filename,
root@9 1799 2 => $name,
root@9 1800 3 => $encoding,
root@9 1801 4 => $type,
root@9 1802 5 => false, // isStringAttachment
root@9 1803 6 => 'inline',
root@9 1804 7 => $cid
root@9 1805 );
root@9 1806
root@9 1807 return true;
root@9 1808 }
root@9 1809
root@9 1810 /**
root@9 1811 * Returns true if an inline attachment is present.
root@9 1812 * @access public
root@9 1813 * @return bool
root@9 1814 */
root@9 1815 public function InlineImageExists() {
root@9 1816 foreach($this->attachment as $attachment) {
root@9 1817 if ($attachment[6] == 'inline') {
root@9 1818 return true;
root@9 1819 }
root@9 1820 }
root@9 1821 return false;
root@9 1822 }
root@9 1823
root@9 1824 /////////////////////////////////////////////////
root@9 1825 // CLASS METHODS, MESSAGE RESET
root@9 1826 /////////////////////////////////////////////////
root@9 1827
root@9 1828 /**
root@9 1829 * Clears all recipients assigned in the TO array. Returns void.
root@9 1830 * @return void
root@9 1831 */
root@9 1832 public function ClearAddresses() {
root@9 1833 foreach($this->to as $to) {
root@9 1834 unset($this->all_recipients[strtolower($to[0])]);
root@9 1835 }
root@9 1836 $this->to = array();
root@9 1837 }
root@9 1838
root@9 1839 /**
root@9 1840 * Clears all recipients assigned in the CC array. Returns void.
root@9 1841 * @return void
root@9 1842 */
root@9 1843 public function ClearCCs() {
root@9 1844 foreach($this->cc as $cc) {
root@9 1845 unset($this->all_recipients[strtolower($cc[0])]);
root@9 1846 }
root@9 1847 $this->cc = array();
root@9 1848 }
root@9 1849
root@9 1850 /**
root@9 1851 * Clears all recipients assigned in the BCC array. Returns void.
root@9 1852 * @return void
root@9 1853 */
root@9 1854 public function ClearBCCs() {
root@9 1855 foreach($this->bcc as $bcc) {
root@9 1856 unset($this->all_recipients[strtolower($bcc[0])]);
root@9 1857 }
root@9 1858 $this->bcc = array();
root@9 1859 }
root@9 1860
root@9 1861 /**
root@9 1862 * Clears all recipients assigned in the ReplyTo array. Returns void.
root@9 1863 * @return void
root@9 1864 */
root@9 1865 public function ClearReplyTos() {
root@9 1866 $this->ReplyTo = array();
root@9 1867 }
root@9 1868
root@9 1869 /**
root@9 1870 * Clears all recipients assigned in the TO, CC and BCC
root@9 1871 * array. Returns void.
root@9 1872 * @return void
root@9 1873 */
root@9 1874 public function ClearAllRecipients() {
root@9 1875 $this->to = array();
root@9 1876 $this->cc = array();
root@9 1877 $this->bcc = array();
root@9 1878 $this->all_recipients = array();
root@9 1879 }
root@9 1880
root@9 1881 /**
root@9 1882 * Clears all previously set filesystem, string, and binary
root@9 1883 * attachments. Returns void.
root@9 1884 * @return void
root@9 1885 */
root@9 1886 public function ClearAttachments() {
root@9 1887 $this->attachment = array();
root@9 1888 }
root@9 1889
root@9 1890 /**
root@9 1891 * Clears all custom headers. Returns void.
root@9 1892 * @return void
root@9 1893 */
root@9 1894 public function ClearCustomHeaders() {
root@9 1895 $this->CustomHeader = array();
root@9 1896 }
root@9 1897
root@9 1898 /////////////////////////////////////////////////
root@9 1899 // CLASS METHODS, MISCELLANEOUS
root@9 1900 /////////////////////////////////////////////////
root@9 1901
root@9 1902 /**
root@9 1903 * Adds the error message to the error container.
root@9 1904 * @access protected
root@9 1905 * @return void
root@9 1906 */
root@9 1907 protected function SetError($msg) {
root@9 1908 $this->error_count++;
root@9 1909 if ($this->Mailer == 'smtp' and !is_null($this->smtp)) {
root@9 1910 $lasterror = $this->smtp->getError();
root@9 1911 if (!empty($lasterror) and array_key_exists('smtp_msg', $lasterror)) {
root@9 1912 $msg .= '<p>' . $this->Lang('smtp_error') . $lasterror['smtp_msg'] . "</p>\n";
root@9 1913 }
root@9 1914 }
root@9 1915 $this->ErrorInfo = $msg;
root@9 1916 }
root@9 1917
root@9 1918 /**
root@9 1919 * Returns the proper RFC 822 formatted date.
root@9 1920 * @access public
root@9 1921 * @return string
root@9 1922 * @static
root@9 1923 */
root@9 1924 public static function RFCDate() {
root@9 1925 $tz = date('Z');
root@9 1926 $tzs = ($tz < 0) ? '-' : '+';
root@9 1927 $tz = abs($tz);
root@9 1928 $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
root@9 1929 $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
root@9 1930
root@9 1931 return $result;
root@9 1932 }
root@9 1933
root@9 1934 /**
root@9 1935 * Returns the server hostname or 'localhost.localdomain' if unknown.
root@9 1936 * @access private
root@9 1937 * @return string
root@9 1938 */
root@9 1939 private function ServerHostname() {
root@9 1940 if (!empty($this->Hostname)) {
root@9 1941 $result = $this->Hostname;
root@9 1942 } elseif (isset($_SERVER['SERVER_NAME'])) {
root@9 1943 $result = $_SERVER['SERVER_NAME'];
root@9 1944 } else {
root@9 1945 $result = 'localhost.localdomain';
root@9 1946 }
root@9 1947
root@9 1948 return $result;
root@9 1949 }
root@9 1950
root@9 1951 /**
root@9 1952 * Returns a message in the appropriate language.
root@9 1953 * @access private
root@9 1954 * @return string
root@9 1955 */
root@9 1956 private function Lang($key) {
root@9 1957 if(count($this->language) < 1) {
root@9 1958 $this->SetLanguage('en'); // set the default language
root@9 1959 }
root@9 1960
root@9 1961 if(isset($this->language[$key])) {
root@9 1962 return $this->language[$key];
root@9 1963 } else {
root@9 1964 return 'Language string failed to load: ' . $key;
root@9 1965 }
root@9 1966 }
root@9 1967
root@9 1968 /**
root@9 1969 * Returns true if an error occurred.
root@9 1970 * @access public
root@9 1971 * @return bool
root@9 1972 */
root@9 1973 public function IsError() {
root@9 1974 return ($this->error_count > 0);
root@9 1975 }
root@9 1976
root@9 1977 /**
root@9 1978 * Changes every end of line from CR or LF to CRLF.
root@9 1979 * @access private
root@9 1980 * @return string
root@9 1981 */
root@9 1982 private function FixEOL($str) {
root@9 1983 $str = str_replace("\r\n", "\n", $str);
root@9 1984 $str = str_replace("\r", "\n", $str);
root@9 1985 $str = str_replace("\n", $this->LE, $str);
root@9 1986 return $str;
root@9 1987 }
root@9 1988
root@9 1989 /**
root@9 1990 * Adds a custom header.
root@9 1991 * @access public
root@9 1992 * @return void
root@9 1993 */
root@9 1994 public function AddCustomHeader($custom_header) {
root@9 1995 $this->CustomHeader[] = explode(':', $custom_header, 2);
root@9 1996 }
root@9 1997
root@9 1998 /**
root@9 1999 * Evaluates the message and returns modifications for inline images and backgrounds
root@9 2000 * @access public
root@9 2001 * @return $message
root@9 2002 */
root@9 2003 public function MsgHTML($message, $basedir = '') {
root@9 2004 preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
root@9 2005 if(isset($images[2])) {
root@9 2006 foreach($images[2] as $i => $url) {
root@9 2007 // do not change urls for absolute images (thanks to corvuscorax)
root@9 2008 if (!preg_match('#^[A-z]+://#',$url)) {
root@9 2009 $filename = basename($url);
root@9 2010 $directory = dirname($url);
root@9 2011 ($directory == '.')?$directory='':'';
root@9 2012 $cid = 'cid:' . md5($filename);
root@9 2013 $ext = pathinfo($filename, PATHINFO_EXTENSION);
root@9 2014 $mimeType = self::_mime_types($ext);
root@9 2015 if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
root@9 2016 if ( strlen($directory) > 1 && substr($directory,-1) != '/') { $directory .= '/'; }
root@9 2017 if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {
root@9 2018 $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
root@9 2019 }
root@9 2020 }
root@9 2021 }
root@9 2022 }
root@9 2023 $this->IsHTML(true);
root@9 2024 $this->Body = $message;
root@9 2025 $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
root@9 2026 if (!empty($textMsg) && empty($this->AltBody)) {
root@9 2027 $this->AltBody = html_entity_decode($textMsg);
root@9 2028 }
root@9 2029 if (empty($this->AltBody)) {
root@9 2030 $this->AltBody = 'To view this email message, open it in a program that understands HTML!' . "\n\n";
root@9 2031 }
root@9 2032 }
root@9 2033
root@9 2034 /**
root@9 2035 * Gets the MIME type of the embedded or inline image
root@9 2036 * @param string File extension
root@9 2037 * @access public
root@9 2038 * @return string MIME type of ext
root@9 2039 * @static
root@9 2040 */
root@9 2041 public static function _mime_types($ext = '') {
root@9 2042 $mimes = array(
root@9 2043 'hqx' => 'application/mac-binhex40',
root@9 2044 'cpt' => 'application/mac-compactpro',
root@9 2045 'doc' => 'application/msword',
root@9 2046 'bin' => 'application/macbinary',
root@9 2047 'dms' => 'application/octet-stream',
root@9 2048 'lha' => 'application/octet-stream',
root@9 2049 'lzh' => 'application/octet-stream',
root@9 2050 'exe' => 'application/octet-stream',
root@9 2051 'class' => 'application/octet-stream',
root@9 2052 'psd' => 'application/octet-stream',
root@9 2053 'so' => 'application/octet-stream',
root@9 2054 'sea' => 'application/octet-stream',
root@9 2055 'dll' => 'application/octet-stream',
root@9 2056 'oda' => 'application/oda',
root@9 2057 'pdf' => 'application/pdf',
root@9 2058 'ai' => 'application/postscript',
root@9 2059 'eps' => 'application/postscript',
root@9 2060 'ps' => 'application/postscript',
root@9 2061 'smi' => 'application/smil',
root@9 2062 'smil' => 'application/smil',
root@9 2063 'mif' => 'application/vnd.mif',
root@9 2064 'xls' => 'application/vnd.ms-excel',
root@9 2065 'ppt' => 'application/vnd.ms-powerpoint',
root@9 2066 'wbxml' => 'application/vnd.wap.wbxml',
root@9 2067 'wmlc' => 'application/vnd.wap.wmlc',
root@9 2068 'dcr' => 'application/x-director',
root@9 2069 'dir' => 'application/x-director',
root@9 2070 'dxr' => 'application/x-director',
root@9 2071 'dvi' => 'application/x-dvi',
root@9 2072 'gtar' => 'application/x-gtar',
root@9 2073 'php' => 'application/x-httpd-php',
root@9 2074 'php4' => 'application/x-httpd-php',
root@9 2075 'php3' => 'application/x-httpd-php',
root@9 2076 'phtml' => 'application/x-httpd-php',
root@9 2077 'phps' => 'application/x-httpd-php-source',
root@9 2078 'js' => 'application/x-javascript',
root@9 2079 'swf' => 'application/x-shockwave-flash',
root@9 2080 'sit' => 'application/x-stuffit',
root@9 2081 'tar' => 'application/x-tar',
root@9 2082 'tgz' => 'application/x-tar',
root@9 2083 'xhtml' => 'application/xhtml+xml',
root@9 2084 'xht' => 'application/xhtml+xml',
root@9 2085 'zip' => 'application/zip',
root@9 2086 'mid' => 'audio/midi',
root@9 2087 'midi' => 'audio/midi',
root@9 2088 'mpga' => 'audio/mpeg',
root@9 2089 'mp2' => 'audio/mpeg',
root@9 2090 'mp3' => 'audio/mpeg',
root@9 2091 'aif' => 'audio/x-aiff',
root@9 2092 'aiff' => 'audio/x-aiff',
root@9 2093 'aifc' => 'audio/x-aiff',
root@9 2094 'ram' => 'audio/x-pn-realaudio',
root@9 2095 'rm' => 'audio/x-pn-realaudio',
root@9 2096 'rpm' => 'audio/x-pn-realaudio-plugin',
root@9 2097 'ra' => 'audio/x-realaudio',
root@9 2098 'rv' => 'video/vnd.rn-realvideo',
root@9 2099 'wav' => 'audio/x-wav',
root@9 2100 'bmp' => 'image/bmp',
root@9 2101 'gif' => 'image/gif',
root@9 2102 'jpeg' => 'image/jpeg',
root@9 2103 'jpg' => 'image/jpeg',
root@9 2104 'jpe' => 'image/jpeg',
root@9 2105 'png' => 'image/png',
root@9 2106 'tiff' => 'image/tiff',
root@9 2107 'tif' => 'image/tiff',
root@9 2108 'css' => 'text/css',
root@9 2109 'html' => 'text/html',
root@9 2110 'htm' => 'text/html',
root@9 2111 'shtml' => 'text/html',
root@9 2112 'txt' => 'text/plain',
root@9 2113 'text' => 'text/plain',
root@9 2114 'log' => 'text/plain',
root@9 2115 'rtx' => 'text/richtext',
root@9 2116 'rtf' => 'text/rtf',
root@9 2117 'xml' => 'text/xml',
root@9 2118 'xsl' => 'text/xml',
root@9 2119 'mpeg' => 'video/mpeg',
root@9 2120 'mpg' => 'video/mpeg',
root@9 2121 'mpe' => 'video/mpeg',
root@9 2122 'qt' => 'video/quicktime',
root@9 2123 'mov' => 'video/quicktime',
root@9 2124 'avi' => 'video/x-msvideo',
root@9 2125 'movie' => 'video/x-sgi-movie',
root@9 2126 'doc' => 'application/msword',
root@9 2127 'word' => 'application/msword',
root@9 2128 'xl' => 'application/excel',
root@9 2129 'eml' => 'message/rfc822'
root@9 2130 );
root@9 2131 return (!isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
root@9 2132 }
root@9 2133
root@9 2134 /**
root@9 2135 * Set (or reset) Class Objects (variables)
root@9 2136 *
root@9 2137 * Usage Example:
root@9 2138 * $page->set('X-Priority', '3');
root@9 2139 *
root@9 2140 * @access public
root@9 2141 * @param string $name Parameter Name
root@9 2142 * @param mixed $value Parameter Value
root@9 2143 * NOTE: will not work with arrays, there are no arrays to set/reset
root@9 2144 * @todo Should this not be using __set() magic function?
root@9 2145 */
root@9 2146 public function set($name, $value = '') {
root@9 2147 try {
root@9 2148 if (isset($this->$name) ) {
root@9 2149 $this->$name = $value;
root@9 2150 } else {
root@9 2151 throw new phpmailerException($this->Lang('variable_set') . $name, self::STOP_CRITICAL);
root@9 2152 }
root@9 2153 } catch (Exception $e) {
root@9 2154 $this->SetError($e->getMessage());
root@9 2155 if ($e->getCode() == self::STOP_CRITICAL) {
root@9 2156 return false;
root@9 2157 }
root@9 2158 }
root@9 2159 return true;
root@9 2160 }
root@9 2161
root@9 2162 /**
root@9 2163 * Strips newlines to prevent header injection.
root@9 2164 * @access public
root@9 2165 * @param string $str String
root@9 2166 * @return string
root@9 2167 */
root@9 2168 public function SecureHeader($str) {
root@9 2169 $str = str_replace("\r", '', $str);
root@9 2170 $str = str_replace("\n", '', $str);
root@9 2171 return trim($str);
root@9 2172 }
root@9 2173
root@9 2174 /**
root@9 2175 * Set the private key file and password to sign the message.
root@9 2176 *
root@9 2177 * @access public
root@9 2178 * @param string $key_filename Parameter File Name
root@9 2179 * @param string $key_pass Password for private key
root@9 2180 */
root@9 2181 public function Sign($cert_filename, $key_filename, $key_pass) {
root@9 2182 $this->sign_cert_file = $cert_filename;
root@9 2183 $this->sign_key_file = $key_filename;
root@9 2184 $this->sign_key_pass = $key_pass;
root@9 2185 }
root@9 2186
root@9 2187 /**
root@9 2188 * Set the private key file and password to sign the message.
root@9 2189 *
root@9 2190 * @access public
root@9 2191 * @param string $key_filename Parameter File Name
root@9 2192 * @param string $key_pass Password for private key
root@9 2193 */
root@9 2194 public function DKIM_QP($txt) {
root@9 2195 $tmp="";
root@9 2196 $line="";
root@9 2197 for ($i=0;$i<strlen($txt);$i++) {
root@9 2198 $ord=ord($txt[$i]);
root@9 2199 if ( ((0x21 <= $ord) && ($ord <= 0x3A)) || $ord == 0x3C || ((0x3E <= $ord) && ($ord <= 0x7E)) ) {
root@9 2200 $line.=$txt[$i];
root@9 2201 } else {
root@9 2202 $line.="=".sprintf("%02X",$ord);
root@9 2203 }
root@9 2204 }
root@9 2205 return $line;
root@9 2206 }
root@9 2207
root@9 2208 /**
root@9 2209 * Generate DKIM signature
root@9 2210 *
root@9 2211 * @access public
root@9 2212 * @param string $s Header
root@9 2213 */
root@9 2214 public function DKIM_Sign($s) {
root@9 2215 $privKeyStr = file_get_contents($this->DKIM_private);
root@9 2216 if ($this->DKIM_passphrase!='') {
root@9 2217 $privKey = openssl_pkey_get_private($privKeyStr,$this->DKIM_passphrase);
root@9 2218 } else {
root@9 2219 $privKey = $privKeyStr;
root@9 2220 }
root@9 2221 if (openssl_sign($s, $signature, $privKey)) {
root@9 2222 return base64_encode($signature);
root@9 2223 }
root@9 2224 }
root@9 2225
root@9 2226 /**
root@9 2227 * Generate DKIM Canonicalization Header
root@9 2228 *
root@9 2229 * @access public
root@9 2230 * @param string $s Header
root@9 2231 */
root@9 2232 public function DKIM_HeaderC($s) {
root@9 2233 $s=preg_replace("/\r\n\s+/"," ",$s);
root@9 2234 $lines=explode("\r\n",$s);
root@9 2235 foreach ($lines as $key=>$line) {
root@9 2236 list($heading,$value)=explode(":",$line,2);
root@9 2237 $heading=strtolower($heading);
root@9 2238 $value=preg_replace("/\s+/"," ",$value) ; // Compress useless spaces
root@9 2239 $lines[$key]=$heading.":".trim($value) ; // Don't forget to remove WSP around the value
root@9 2240 }
root@9 2241 $s=implode("\r\n",$lines);
root@9 2242 return $s;
root@9 2243 }
root@9 2244
root@9 2245 /**
root@9 2246 * Generate DKIM Canonicalization Body
root@9 2247 *
root@9 2248 * @access public
root@9 2249 * @param string $body Message Body
root@9 2250 */
root@9 2251 public function DKIM_BodyC($body) {
root@9 2252 if ($body == '') return "\r\n";
root@9 2253 // stabilize line endings
root@9 2254 $body=str_replace("\r\n","\n",$body);
root@9 2255 $body=str_replace("\n","\r\n",$body);
root@9 2256 // END stabilize line endings
root@9 2257 while (substr($body,strlen($body)-4,4) == "\r\n\r\n") {
root@9 2258 $body=substr($body,0,strlen($body)-2);
root@9 2259 }
root@9 2260 return $body;
root@9 2261 }
root@9 2262
root@9 2263 /**
root@9 2264 * Create the DKIM header, body, as new header
root@9 2265 *
root@9 2266 * @access public
root@9 2267 * @param string $headers_line Header lines
root@9 2268 * @param string $subject Subject
root@9 2269 * @param string $body Body
root@9 2270 */
root@9 2271 public function DKIM_Add($headers_line,$subject,$body) {
root@9 2272 $DKIMsignatureType = 'rsa-sha1'; // Signature & hash algorithms
root@9 2273 $DKIMcanonicalization = 'relaxed/simple'; // Canonicalization of header/body
root@9 2274 $DKIMquery = 'dns/txt'; // Query method
root@9 2275 $DKIMtime = time() ; // Signature Timestamp = seconds since 00:00:00 - Jan 1, 1970 (UTC time zone)
root@9 2276 $subject_header = "Subject: $subject";
root@9 2277 $headers = explode("\r\n",$headers_line);
root@9 2278 foreach($headers as $header) {
root@9 2279 if (strpos($header,'From:') === 0) {
root@9 2280 $from_header=$header;
root@9 2281 } elseif (strpos($header,'To:') === 0) {
root@9 2282 $to_header=$header;
root@9 2283 }
root@9 2284 }
root@9 2285 $from = str_replace('|','=7C',$this->DKIM_QP($from_header));
root@9 2286 $to = str_replace('|','=7C',$this->DKIM_QP($to_header));
root@9 2287 $subject = str_replace('|','=7C',$this->DKIM_QP($subject_header)) ; // Copied header fields (dkim-quoted-printable
root@9 2288 $body = $this->DKIM_BodyC($body);
root@9 2289 $DKIMlen = strlen($body) ; // Length of body
root@9 2290 $DKIMb64 = base64_encode(pack("H*", sha1($body))) ; // Base64 of packed binary SHA-1 hash of body
root@9 2291 $ident = ($this->DKIM_identity == '')? '' : " i=" . $this->DKIM_identity . ";";
root@9 2292 $dkimhdrs = "DKIM-Signature: v=1; a=" . $DKIMsignatureType . "; q=" . $DKIMquery . "; l=" . $DKIMlen . "; s=" . $this->DKIM_selector . ";\r\n".
root@9 2293 "\tt=" . $DKIMtime . "; c=" . $DKIMcanonicalization . ";\r\n".
root@9 2294 "\th=From:To:Subject;\r\n".
root@9 2295 "\td=" . $this->DKIM_domain . ";" . $ident . "\r\n".
root@9 2296 "\tz=$from\r\n".
root@9 2297 "\t|$to\r\n".
root@9 2298 "\t|$subject;\r\n".
root@9 2299 "\tbh=" . $DKIMb64 . ";\r\n".
root@9 2300 "\tb=";
root@9 2301 $toSign = $this->DKIM_HeaderC($from_header . "\r\n" . $to_header . "\r\n" . $subject_header . "\r\n" . $dkimhdrs);
root@9 2302 $signed = $this->DKIM_Sign($toSign);
root@9 2303 return "X-PHPMAILER-DKIM: phpmailer.worxware.com\r\n".$dkimhdrs.$signed."\r\n";
root@9 2304 }
root@9 2305
root@9 2306 protected function doCallback($isSent,$to,$cc,$bcc,$subject,$body) {
root@9 2307 if (!empty($this->action_function) && function_exists($this->action_function)) {
root@9 2308 $params = array($isSent,$to,$cc,$bcc,$subject,$body);
root@9 2309 call_user_func_array($this->action_function,$params);
root@9 2310 }
root@9 2311 }
root@9 2312 }
root@9 2313
root@9 2314 class phpmailerException extends Exception {
root@9 2315 public function errorMessage() {
root@9 2316 $errorMsg = '<strong>' . $this->getMessage() . "</strong><br />\n";
root@9 2317 return $errorMsg;
root@9 2318 }
root@9 2319 }
root@9 2320 ?>