PageRenderTime 63ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/amazon-ses-and-dkim-mailer/php-mailer/class.phpmailer.php

https://bitbucket.org/broderboy/shannonbroder-wordpress
PHP | 3041 lines | 1808 code | 233 blank | 1000 comment | 294 complexity | 4b1bca0f8a005a0f460f10ea44242f04 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, AGPL-1.0

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

  1. <?php
  2. /*~ class.phpmailer.php
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 5.2.6 |
  6. | Site: https://github.com/PHPMailer/PHPMailer/ |
  7. | ------------------------------------------------------------------------- |
  8. | Admins: Marcus Bointon |
  9. | Admins: Jim Jagielski |
  10. | Authors: Andy Prevost (codeworxtech) codeworxtech@users.sourceforge.net |
  11. | : Marcus Bointon (coolbru) phpmailer@synchromedia.co.uk |
  12. | : Jim Jagielski (jimjag) jimjag@gmail.com |
  13. | Founder: Brent R. Matzelle (original founder) |
  14. | Copyright (c) 2010-2012, Jim Jagielski. All Rights Reserved. |
  15. | Copyright (c) 2004-2009, Andy Prevost. All Rights Reserved. |
  16. | Copyright (c) 2001-2003, Brent R. Matzelle |
  17. | ------------------------------------------------------------------------- |
  18. | License: Distributed under the Lesser General Public License (LGPL) |
  19. | http://www.gnu.org/copyleft/lesser.html |
  20. | This program is distributed in the hope that it will be useful - WITHOUT |
  21. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  22. | FITNESS FOR A PARTICULAR PURPOSE. |
  23. '---------------------------------------------------------------------------'
  24. */
  25. /**
  26. * PHPMailer - PHP email creation and transport class
  27. * NOTE: Requires PHP version 5 or later
  28. * @package PHPMailer
  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
  35. */
  36. if (version_compare(PHP_VERSION, '5.0.0', '<') ) {
  37. exit("Sorry, PHPMailer will only run on PHP version 5 or greater!\n");
  38. }
  39. /**
  40. * PHP email creation and transport class
  41. * @package PHPMailer
  42. */
  43. class PHPMailer {
  44. /////////////////////////////////////////////////
  45. // PROPERTIES, PUBLIC
  46. /////////////////////////////////////////////////
  47. /**
  48. * Email priority (1 = High, 3 = Normal, 5 = low).
  49. * @var int
  50. */
  51. public $Priority = 3;
  52. /**
  53. * Sets the CharSet of the message.
  54. * @var string
  55. */
  56. public $CharSet = 'iso-8859-1';
  57. /**
  58. * Sets the Content-type of the message.
  59. * @var string
  60. */
  61. public $ContentType = 'text/plain';
  62. /**
  63. * Sets the Encoding of the message. Options for this are
  64. * "8bit", "7bit", "binary", "base64", and "quoted-printable".
  65. * @var string
  66. */
  67. public $Encoding = '8bit';
  68. /**
  69. * Holds the most recent mailer error message.
  70. * @var string
  71. */
  72. public $ErrorInfo = '';
  73. /**
  74. * Sets the From email address for the message.
  75. * @var string
  76. */
  77. public $From = 'root@localhost';
  78. /**
  79. * Sets the From name of the message.
  80. * @var string
  81. */
  82. public $FromName = 'Root User';
  83. /**
  84. * Sets the Sender email (Return-Path) of the message.
  85. * If not empty, will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  86. * @var string
  87. */
  88. public $Sender = '';
  89. /**
  90. * Sets the Return-Path of the message. If empty, it will
  91. * be set to either From or Sender.
  92. * @var string
  93. */
  94. public $ReturnPath = '';
  95. /**
  96. * Sets the Subject of the message.
  97. * @var string
  98. */
  99. public $Subject = '';
  100. /**
  101. * An HTML or plain text message body.
  102. * If HTML then call IsHTML(true).
  103. * @var string
  104. */
  105. public $Body = '';
  106. /**
  107. * The plain-text message body.
  108. * This body can be read by mail clients that do not have HTML email
  109. * capability such as mutt & Eudora.
  110. * Clients that can read HTML will view the normal Body.
  111. * @var string
  112. */
  113. public $AltBody = '';
  114. /**
  115. * An iCal message part body
  116. * Only supported in simple alt or alt_inline message types
  117. * To generate iCal events, use the bundled extras/EasyPeasyICS.php class or iCalcreator
  118. * @link http://sprain.ch/blog/downloads/php-class-easypeasyics-create-ical-files-with-php/
  119. * @link http://kigkonsult.se/iCalcreator/
  120. * @var string
  121. */
  122. public $Ical = '';
  123. /**
  124. * Stores the complete compiled MIME message body.
  125. * @var string
  126. * @access protected
  127. */
  128. protected $MIMEBody = '';
  129. /**
  130. * Stores the complete compiled MIME message headers.
  131. * @var string
  132. * @access protected
  133. */
  134. protected $MIMEHeader = '';
  135. /**
  136. * Stores the extra header list which CreateHeader() doesn't fold in
  137. * @var string
  138. * @access protected
  139. */
  140. protected $mailHeader = '';
  141. /**
  142. * Sets word wrapping on the body of the message to a given number of
  143. * characters.
  144. * @var int
  145. */
  146. public $WordWrap = 0;
  147. /**
  148. * Method to send mail: ("mail", "sendmail", or "smtp").
  149. * @var string
  150. */
  151. public $Mailer = 'mail';
  152. /**
  153. * Sets the path of the sendmail program.
  154. * @var string
  155. */
  156. public $Sendmail = '/usr/sbin/sendmail';
  157. /**
  158. * Determine if mail() uses a fully sendmail compatible MTA that
  159. * supports sendmail's "-oi -f" options
  160. * @var boolean
  161. */
  162. public $UseSendmailOptions = true;
  163. /**
  164. * Path to PHPMailer plugins. Useful if the SMTP class
  165. * is in a different directory than the PHP include path.
  166. * @var string
  167. */
  168. public $PluginDir = '';
  169. /**
  170. * Sets the email address that a reading confirmation will be sent.
  171. * @var string
  172. */
  173. public $ConfirmReadingTo = '';
  174. /**
  175. * Sets the hostname to use in Message-Id and Received headers
  176. * and as default HELO string. If empty, the value returned
  177. * by SERVER_NAME is used or 'localhost.localdomain'.
  178. * @var string
  179. */
  180. public $Hostname = '';
  181. /**
  182. * Sets the message ID to be used in the Message-Id header.
  183. * If empty, a unique id will be generated.
  184. * @var string
  185. */
  186. public $MessageID = '';
  187. /**
  188. * Sets the message Date to be used in the Date header.
  189. * If empty, the current date will be added.
  190. * @var string
  191. */
  192. public $MessageDate = '';
  193. /////////////////////////////////////////////////
  194. // PROPERTIES FOR SMTP
  195. /////////////////////////////////////////////////
  196. /**
  197. * Sets the SMTP hosts.
  198. *
  199. * All hosts must be separated by a
  200. * semicolon. You can also specify a different port
  201. * for each host by using this format: [hostname:port]
  202. * (e.g. "smtp1.example.com:25;smtp2.example.com").
  203. * Hosts will be tried in order.
  204. * @var string
  205. */
  206. public $Host = 'localhost';
  207. /**
  208. * Sets the default SMTP server port.
  209. * @var int
  210. */
  211. public $Port = 25;
  212. /**
  213. * Sets the SMTP HELO of the message (Default is $Hostname).
  214. * @var string
  215. */
  216. public $Helo = '';
  217. /**
  218. * Sets connection prefix. Options are "", "ssl" or "tls"
  219. * @var string
  220. */
  221. public $SMTPSecure = '';
  222. /**
  223. * Sets SMTP authentication. Utilizes the Username and Password variables.
  224. * @var bool
  225. */
  226. public $SMTPAuth = false;
  227. /**
  228. * Sets SMTP username.
  229. * @var string
  230. */
  231. public $Username = '';
  232. /**
  233. * Sets SMTP password.
  234. * @var string
  235. */
  236. public $Password = '';
  237. /**
  238. * Sets SMTP auth type. Options are LOGIN | PLAIN | NTLM | CRAM-MD5 (default LOGIN)
  239. * @var string
  240. */
  241. public $AuthType = '';
  242. /**
  243. * Sets SMTP realm.
  244. * @var string
  245. */
  246. public $Realm = '';
  247. /**
  248. * Sets SMTP workstation.
  249. * @var string
  250. */
  251. public $Workstation = '';
  252. /**
  253. * Sets the SMTP server timeout in seconds.
  254. * This function will not work with the win32 version.
  255. * @var int
  256. */
  257. public $Timeout = 10;
  258. /**
  259. * Sets SMTP class debugging on or off.
  260. * @var bool
  261. */
  262. public $SMTPDebug = false;
  263. /**
  264. * Sets the function/method to use for debugging output.
  265. * Right now we only honor "echo" or "error_log"
  266. * @var string
  267. */
  268. public $Debugoutput = "echo";
  269. /**
  270. * Prevents the SMTP connection from being closed after each mail
  271. * sending. If this is set to true then to close the connection
  272. * requires an explicit call to SmtpClose().
  273. * @var bool
  274. */
  275. public $SMTPKeepAlive = false;
  276. /**
  277. * Provides the ability to have the TO field process individual
  278. * emails, instead of sending to entire TO addresses
  279. * @var bool
  280. */
  281. public $SingleTo = false;
  282. /**
  283. * Should we generate VERP addresses when sending via SMTP?
  284. * @link http://en.wikipedia.org/wiki/Variable_envelope_return_path
  285. * @var bool
  286. */
  287. public $do_verp = false;
  288. /**
  289. * If SingleTo is true, this provides the array to hold the email addresses
  290. * @var bool
  291. */
  292. public $SingleToArray = array();
  293. /**
  294. * Should we allow sending messages with empty body?
  295. * @var bool
  296. */
  297. public $AllowEmpty = false;
  298. /**
  299. * Provides the ability to change the generic line ending
  300. * NOTE: The default remains '\n'. We force CRLF where we KNOW
  301. * it must be used via self::CRLF
  302. * @var string
  303. */
  304. public $LE = "\n";
  305. /**
  306. * Used with DKIM Signing
  307. * required parameter if DKIM is enabled
  308. *
  309. * domain selector example domainkey
  310. * @var string
  311. */
  312. public $DKIM_selector = '';
  313. /**
  314. * Used with DKIM Signing
  315. * required if DKIM is enabled, in format of email address 'you@yourdomain.com' typically used as the source of the email
  316. * @var string
  317. */
  318. public $DKIM_identity = '';
  319. /**
  320. * Used with DKIM Signing
  321. * optional parameter if your private key requires a passphras
  322. * @var string
  323. */
  324. public $DKIM_passphrase = '';
  325. /**
  326. * Used with DKIM Singing
  327. * required if DKIM is enabled, in format of email address 'domain.com'
  328. * @var string
  329. */
  330. public $DKIM_domain = '';
  331. /**
  332. * Used with DKIM Signing
  333. * required if DKIM is enabled, path to private key file
  334. * @var string
  335. */
  336. public $DKIM_private = '';
  337. /**
  338. * Callback Action function name.
  339. * The function that handles the result of the send email action.
  340. * It is called out by Send() for each email sent.
  341. *
  342. * Value can be:
  343. * - 'function_name' for function names
  344. * - 'Class::Method' for static method calls
  345. * - array($object, 'Method') for calling methods on $object
  346. * See http://php.net/is_callable manual page for more details.
  347. *
  348. * Parameters:
  349. * bool $result result of the send action
  350. * string $to email address of the recipient
  351. * string $cc cc email addresses
  352. * string $bcc bcc email addresses
  353. * string $subject the subject
  354. * string $body the email body
  355. * string $from email address of sender
  356. * @var string
  357. */
  358. public $action_function = ''; //'callbackAction';
  359. /**
  360. * Sets the PHPMailer Version number
  361. * @var string
  362. */
  363. public $Version = '5.2.6';
  364. /**
  365. * What to use in the X-Mailer header
  366. * @var string NULL for default, whitespace for None, or actual string to use
  367. */
  368. public $XMailer = '';
  369. /////////////////////////////////////////////////
  370. // PROPERTIES, PRIVATE AND PROTECTED
  371. /////////////////////////////////////////////////
  372. /**
  373. * @var SMTP An instance of the SMTP sender class
  374. * @access protected
  375. */
  376. protected $smtp = null;
  377. /**
  378. * @var array An array of 'to' addresses
  379. * @access protected
  380. */
  381. protected $to = array();
  382. /**
  383. * @var array An array of 'cc' addresses
  384. * @access protected
  385. */
  386. protected $cc = array();
  387. /**
  388. * @var array An array of 'bcc' addresses
  389. * @access protected
  390. */
  391. protected $bcc = array();
  392. /**
  393. * @var array An array of reply-to name and address
  394. * @access protected
  395. */
  396. protected $ReplyTo = array();
  397. /**
  398. * @var array An array of all kinds of addresses: to, cc, bcc, replyto
  399. * @access protected
  400. */
  401. protected $all_recipients = array();
  402. /**
  403. * @var array An array of attachments
  404. * @access protected
  405. */
  406. protected $attachment = array();
  407. /**
  408. * @var array An array of custom headers
  409. * @access protected
  410. */
  411. protected $CustomHeader = array();
  412. /**
  413. * @var string The message's MIME type
  414. * @access protected
  415. */
  416. protected $message_type = '';
  417. /**
  418. * @var array An array of MIME boundary strings
  419. * @access protected
  420. */
  421. protected $boundary = array();
  422. /**
  423. * @var array An array of available languages
  424. * @access protected
  425. */
  426. protected $language = array();
  427. /**
  428. * @var integer The number of errors encountered
  429. * @access protected
  430. */
  431. protected $error_count = 0;
  432. /**
  433. * @var string The filename of a DKIM certificate file
  434. * @access protected
  435. */
  436. protected $sign_cert_file = '';
  437. /**
  438. * @var string The filename of a DKIM key file
  439. * @access protected
  440. */
  441. protected $sign_key_file = '';
  442. /**
  443. * @var string The password of a DKIM key
  444. * @access protected
  445. */
  446. protected $sign_key_pass = '';
  447. /**
  448. * @var boolean Whether to throw exceptions for errors
  449. * @access protected
  450. */
  451. protected $exceptions = false;
  452. /**Amazon SES**/
  453. private $amazonses_object = NULL;
  454. private $amazonses_use_destinations = TRUE;
  455. private $amazonses_use_source = TRUE;
  456. private $amazonses_use_names = TRUE;
  457. private $amazonses_debug = FALSE;
  458. /////////////////////////////////////////////////
  459. // CONSTANTS
  460. /////////////////////////////////////////////////
  461. const STOP_MESSAGE = 0; // message only, continue processing
  462. const STOP_CONTINUE = 1; // message?, likely ok to continue processing
  463. const STOP_CRITICAL = 2; // message, plus full stop, critical error reached
  464. const CRLF = "\r\n"; // SMTP RFC specified EOL
  465. /////////////////////////////////////////////////
  466. // METHODS, VARIABLES
  467. /////////////////////////////////////////////////
  468. /**
  469. * Calls actual mail() function, but in a safe_mode aware fashion
  470. * Also, unless sendmail_path points to sendmail (or something that
  471. * claims to be sendmail), don't pass params (not a perfect fix,
  472. * but it will do)
  473. * @param string $to To
  474. * @param string $subject Subject
  475. * @param string $body Message Body
  476. * @param string $header Additional Header(s)
  477. * @param string $params Params
  478. * @access private
  479. * @return bool
  480. */
  481. private function mail_passthru($to, $subject, $body, $header, $params) {
  482. if ( ini_get('safe_mode') || !($this->UseSendmailOptions) ) {
  483. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header);
  484. } else {
  485. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($subject)), $body, $header, $params);
  486. }
  487. return $rt;
  488. }
  489. /**
  490. * Outputs debugging info via user-defined method
  491. * @param string $str
  492. */
  493. protected function edebug($str) {
  494. switch ($this->Debugoutput) {
  495. case 'error_log':
  496. error_log($str);
  497. break;
  498. case 'html':
  499. //Cleans up output a bit for a better looking display that's HTML-safe
  500. echo htmlentities(preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, $this->CharSet)."<br>\n";
  501. break;
  502. case 'echo':
  503. default:
  504. //Just echoes exactly what was received
  505. echo $str;
  506. }
  507. }
  508. /**
  509. * Constructor
  510. * @param boolean $exceptions Should we throw external exceptions?
  511. */
  512. public function __construct($exceptions = false) {
  513. $this->exceptions = ($exceptions == true);
  514. }
  515. /**
  516. * Destructor
  517. */
  518. public function __destruct() {
  519. if ($this->Mailer == 'smtp') { //Close any open SMTP connection nicely
  520. $this->SmtpClose();
  521. }
  522. }
  523. /**
  524. * Sets message type to HTML.
  525. * @param bool $ishtml
  526. * @return void
  527. */
  528. public function IsHTML($ishtml = true) {
  529. if ($ishtml) {
  530. $this->ContentType = 'text/html';
  531. } else {
  532. $this->ContentType = 'text/plain';
  533. }
  534. }
  535. /**
  536. * Sets Mailer to send message using SMTP.
  537. * @return void
  538. */
  539. public function IsSMTP() {
  540. $this->Mailer = 'smtp';
  541. }
  542. /**
  543. * Sets Mailer to send message using PHP mail() function.
  544. * @return void
  545. */
  546. public function IsMail() {
  547. $this->Mailer = 'mail';
  548. }
  549. /**
  550. * Sets Mailer to send message using the $Sendmail program.
  551. * @return void
  552. */
  553. public function IsSendmail() {
  554. if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
  555. $this->Sendmail = '/var/qmail/bin/sendmail';
  556. }
  557. $this->Mailer = 'sendmail';
  558. }
  559. /**
  560. * Sets Mailer to send message using the qmail MTA.
  561. * @return void
  562. */
  563. public function IsQmail() {
  564. if (stristr(ini_get('sendmail_path'), 'qmail')) {
  565. $this->Sendmail = '/var/qmail/bin/sendmail';
  566. }
  567. $this->Mailer = 'sendmail';
  568. }
  569. /**
  570. * Sets Mailer to send message using Amazon Simple Email Service(SES).
  571. * @return void
  572. */
  573. public function IsAmazonSES() {
  574. $this->Mailer = 'amazonses';
  575. }
  576. /////////////////////////////////////////////////
  577. // METHODS, RECIPIENTS
  578. /////////////////////////////////////////////////
  579. /**
  580. * Adds a "To" address.
  581. * @param string $address
  582. * @param string $name
  583. * @return boolean true on success, false if address already used
  584. */
  585. public function AddAddress($address, $name = '') {
  586. return $this->AddAnAddress('to', $address, $name);
  587. }
  588. /**
  589. * Adds a "Cc" address.
  590. * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
  591. * @param string $address
  592. * @param string $name
  593. * @return boolean true on success, false if address already used
  594. */
  595. public function AddCC($address, $name = '') {
  596. return $this->AddAnAddress('cc', $address, $name);
  597. }
  598. /**
  599. * Adds a "Bcc" address.
  600. * Note: this function works with the SMTP mailer on win32, not with the "mail" mailer.
  601. * @param string $address
  602. * @param string $name
  603. * @return boolean true on success, false if address already used
  604. */
  605. public function AddBCC($address, $name = '') {
  606. return $this->AddAnAddress('bcc', $address, $name);
  607. }
  608. /**
  609. * Adds a "Reply-to" address.
  610. * @param string $address
  611. * @param string $name
  612. * @return boolean
  613. */
  614. public function AddReplyTo($address, $name = '') {
  615. return $this->AddAnAddress('Reply-To', $address, $name);
  616. }
  617. /**
  618. * Adds an address to one of the recipient arrays
  619. * Addresses that have been added already return false, but do not throw exceptions
  620. * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
  621. * @param string $address The email address to send to
  622. * @param string $name
  623. * @throws phpmailerException
  624. * @return boolean true on success, false if address already used or invalid in some way
  625. * @access protected
  626. */
  627. protected function AddAnAddress($kind, $address, $name = '') {
  628. if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
  629. $this->SetError($this->Lang('Invalid recipient array').': '.$kind);
  630. if ($this->exceptions) {
  631. throw new phpmailerException('Invalid recipient array: ' . $kind);
  632. }
  633. if ($this->SMTPDebug) {
  634. $this->edebug($this->Lang('Invalid recipient array').': '.$kind);
  635. }
  636. return false;
  637. }
  638. $address = trim($address);
  639. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
  640. if (!$this->ValidateAddress($address)) {
  641. $this->SetError($this->Lang('invalid_address').': '. $address);
  642. if ($this->exceptions) {
  643. throw new phpmailerException($this->Lang('invalid_address').': '.$address);
  644. }
  645. if ($this->SMTPDebug) {
  646. $this->edebug($this->Lang('invalid_address').': '.$address);
  647. }
  648. return false;
  649. }
  650. if ($kind != 'Reply-To') {
  651. if (!isset($this->all_recipients[strtolower($address)])) {
  652. array_push($this->$kind, array($address, $name));
  653. $this->all_recipients[strtolower($address)] = true;
  654. return true;
  655. }
  656. } else {
  657. if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
  658. $this->ReplyTo[strtolower($address)] = array($address, $name);
  659. return true;
  660. }
  661. }
  662. return false;
  663. }
  664. /**
  665. * Set the From and FromName properties
  666. * @param string $address
  667. * @param string $name
  668. * @param boolean $auto Whether to also set the Sender address, defaults to true
  669. * @throws phpmailerException
  670. * @return boolean
  671. */
  672. public function SetFrom($address, $name = '', $auto = true) {
  673. $address = trim($address);
  674. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
  675. if (!$this->ValidateAddress($address)) {
  676. $this->SetError($this->Lang('invalid_address').': '. $address);
  677. if ($this->exceptions) {
  678. throw new phpmailerException($this->Lang('invalid_address').': '.$address);
  679. }
  680. if ($this->SMTPDebug) {
  681. $this->edebug($this->Lang('invalid_address').': '.$address);
  682. }
  683. return false;
  684. }
  685. $this->From = $address;
  686. $this->FromName = $name;
  687. if ($auto) {
  688. if (empty($this->Sender)) {
  689. $this->Sender = $address;
  690. }
  691. }
  692. return true;
  693. }
  694. /**
  695. * Check that a string looks roughly like an email address should
  696. * Static so it can be used without instantiation, public so people can overload
  697. * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
  698. * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
  699. * not allow a@b type valid addresses :(
  700. * @link http://squiloople.com/2009/12/20/email-address-validation/
  701. * @copyright regex Copyright Michael Rushton 2009-10 | http://squiloople.com/ | Feel free to use and redistribute this code. But please keep this copyright notice.
  702. * @param string $address The email address to check
  703. * @return boolean
  704. * @static
  705. * @access public
  706. */
  707. public static function ValidateAddress($address) {
  708. if (defined('PCRE_VERSION')) { //Check this instead of extension_loaded so it works when that function is disabled
  709. if (version_compare(PCRE_VERSION, '8.0') >= 0) {
  710. return (boolean)preg_match('/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?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][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD', $address);
  711. } else {
  712. //Fall back to an older regex that doesn't need a recent PCRE
  713. return (boolean)preg_match('/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD', $address);
  714. }
  715. } else {
  716. //No PCRE! Do something _very_ approximate!
  717. //Check the address is 3 chars or longer and contains an @ that's not the first or last char
  718. return (strlen($address) >= 3 and strpos($address, '@') >= 1 and strpos($address, '@') != strlen($address) - 1);
  719. }
  720. }
  721. /////////////////////////////////////////////////
  722. // METHODS, MAIL SENDING
  723. /////////////////////////////////////////////////
  724. /**
  725. * Creates message and assigns Mailer. If the message is
  726. * not sent successfully then it returns false. Use the ErrorInfo
  727. * variable to view description of the error.
  728. * @throws phpmailerException
  729. * @return bool
  730. */
  731. public function Send() {
  732. try {
  733. if(!$this->PreSend()) return false;
  734. return $this->PostSend();
  735. } catch (phpmailerException $e) {
  736. $this->mailHeader = '';
  737. $this->SetError($e->getMessage());
  738. if ($this->exceptions) {
  739. throw $e;
  740. }
  741. return false;
  742. }
  743. }
  744. /**
  745. * Prep mail by constructing all message entities
  746. * @throws phpmailerException
  747. * @return bool
  748. */
  749. public function PreSend() {
  750. try {
  751. $this->mailHeader = "";
  752. if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
  753. throw new phpmailerException($this->Lang('provide_address'), self::STOP_CRITICAL);
  754. }
  755. // Set whether the message is multipart/alternative
  756. if(!empty($this->AltBody)) {
  757. $this->ContentType = 'multipart/alternative';
  758. }
  759. $this->error_count = 0; // reset errors
  760. $this->SetMessageType();
  761. //Refuse to send an empty message unless we are specifically allowing it
  762. if (!$this->AllowEmpty and empty($this->Body)) {
  763. throw new phpmailerException($this->Lang('empty_message'), self::STOP_CRITICAL);
  764. }
  765. $this->MIMEHeader = $this->CreateHeader();
  766. $this->MIMEBody = $this->CreateBody();
  767. // To capture the complete message when using mail(), create
  768. // an extra header list which CreateHeader() doesn't fold in
  769. if ($this->Mailer == 'mail') {
  770. if (count($this->to) > 0) {
  771. $this->mailHeader .= $this->AddrAppend("To", $this->to);
  772. } else {
  773. $this->mailHeader .= $this->HeaderLine("To", "undisclosed-recipients:;");
  774. }
  775. $this->mailHeader .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader(trim($this->Subject))));
  776. }
  777. // digitally sign with DKIM if enabled
  778. if (!empty($this->DKIM_domain) && !empty($this->DKIM_private) && !empty($this->DKIM_selector) && !empty($this->DKIM_domain) && file_exists($this->DKIM_private)) {
  779. $header_dkim = $this->DKIM_Add($this->MIMEHeader . $this->mailHeader, $this->EncodeHeader($this->SecureHeader($this->Subject)), $this->MIMEBody);
  780. $this->MIMEHeader = str_replace("\r\n", "\n", $header_dkim) . $this->MIMEHeader;
  781. }
  782. return true;
  783. } catch (phpmailerException $e) {
  784. $this->SetError($e->getMessage());
  785. if ($this->exceptions) {
  786. throw $e;
  787. }
  788. return false;
  789. }
  790. }
  791. /**
  792. * Actual Email transport function
  793. * Send the email via the selected mechanism
  794. * @throws phpmailerException
  795. * @return bool
  796. */
  797. public function PostSend() {
  798. try {
  799. // Choose the mailer and send through it
  800. switch($this->Mailer) {
  801. case 'sendmail':
  802. return $this->SendmailSend($this->MIMEHeader, $this->MIMEBody);
  803. case 'smtp':
  804. return $this->SmtpSend($this->MIMEHeader, $this->MIMEBody);
  805. case 'mail':
  806. return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
  807. case 'amazonses':
  808. return $this->AmazonSESSend($this->MIMEHeader, $this->MIMEBody);
  809. default:
  810. return $this->MailSend($this->MIMEHeader, $this->MIMEBody);
  811. }
  812. } catch (phpmailerException $e) {
  813. $this->SetError($e->getMessage());
  814. if ($this->exceptions) {
  815. throw $e;
  816. }
  817. if ($this->SMTPDebug) {
  818. $this->edebug($e->getMessage()."\n");
  819. }
  820. }
  821. return false;
  822. }
  823. public function AddAmazonSESKey($aws_access_key_id, $aws_secret_key) {
  824. $this->InitiateAmazonSESObject();
  825. $this->amazonses_object->aws_access_key_id = (string)$aws_access_key_id;
  826. $this->amazonses_object->aws_secret_key = (string)$aws_secret_key;
  827. return TRUE;
  828. }
  829. protected function InitiateAmazonSESObject() {
  830. require_once('class.amazonses.php');
  831. if (!isset($this->amazonses_object)) {
  832. $this->amazonses_object = new AmazonSES();
  833. if ($this->amazonses_debug === TRUE)
  834. { $this->amazonses_object->debug = TRUE; }
  835. }
  836. }
  837. protected function AmazonSESSend($header, $body) {
  838. $this->InitiateAmazonSESObject();
  839. $addr_with_name = create_function (
  840. '$addr_ar',
  841. 'return (trim($addr_ar[1]) !== "") ?
  842. "{$addr_ar[1]} <{$addr_ar[0]}>" : $addr_ar[0];'
  843. );
  844. $recipients = array();
  845. foreach (array_merge($this->to, $this->cc, $this->bcc) as $key => $to) {
  846. $recipients[] = ($this->amazonses_use_names === TRUE) ?
  847. $addr_with_name($to) : $to[0];
  848. }
  849. $source = FALSE;
  850. if ($this->amazonses_use_source === TRUE) {
  851. $source = ($this->amazonses_use_names === TRUE) ?
  852. $addr_with_name(array($this->From, $this->FromName)) :
  853. $this->From;
  854. }
  855. $destinations = FALSE;
  856. if ($this->amazonses_use_destinations === TRUE)
  857. { $destinations = $recipients; }
  858. $ret = $this->amazonses_object->send_mail
  859. ($header, $this->Subject, $body, $destinations, $source);
  860. $isSent = ($ret[0] === "200")?1:0;
  861. //print_r($ret);
  862. $this->doCallback($isSent, implode(',', $recipients),
  863. '', '', $this->Subject, $body);
  864. // Everything looks fine.
  865. if ($isSent === 1) {return TRUE; }
  866. // Some sort of error occured.
  867. throw new phpmailerException
  868. ($this->Lang('amazonses_error').$ret[1], self::STOP_CRITICAL);
  869. return FALSE;
  870. }
  871. /**
  872. * Sends mail using the $Sendmail program.
  873. * @param string $header The message headers
  874. * @param string $body The message body
  875. * @throws phpmailerException
  876. * @access protected
  877. * @return bool
  878. */
  879. protected function SendmailSend($header, $body) {
  880. if ($this->Sender != '') {
  881. $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  882. } else {
  883. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  884. }
  885. if ($this->SingleTo === true) {
  886. foreach ($this->SingleToArray as $val) {
  887. if(!@$mail = popen($sendmail, 'w')) {
  888. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  889. }
  890. fputs($mail, "To: " . $val . "\n");
  891. fputs($mail, $header);
  892. fputs($mail, $body);
  893. $result = pclose($mail);
  894. // implement call back function if it exists
  895. $isSent = ($result == 0) ? 1 : 0;
  896. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
  897. if($result != 0) {
  898. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  899. }
  900. }
  901. } else {
  902. if(!@$mail = popen($sendmail, 'w')) {
  903. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  904. }
  905. fputs($mail, $header);
  906. fputs($mail, $body);
  907. $result = pclose($mail);
  908. // implement call back function if it exists
  909. $isSent = ($result == 0) ? 1 : 0;
  910. $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body);
  911. if($result != 0) {
  912. throw new phpmailerException($this->Lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  913. }
  914. }
  915. return true;
  916. }
  917. /**
  918. * Sends mail using the PHP mail() function.
  919. * @param string $header The message headers
  920. * @param string $body The message body
  921. * @throws phpmailerException
  922. * @access protected
  923. * @return bool
  924. */
  925. protected function MailSend($header, $body) {
  926. $toArr = array();
  927. foreach($this->to as $t) {
  928. $toArr[] = $this->AddrFormat($t);
  929. }
  930. $to = implode(', ', $toArr);
  931. if (empty($this->Sender)) {
  932. $params = " ";
  933. } else {
  934. $params = sprintf("-f%s", $this->Sender);
  935. }
  936. if ($this->Sender != '' and !ini_get('safe_mode')) {
  937. $old_from = ini_get('sendmail_from');
  938. ini_set('sendmail_from', $this->Sender);
  939. }
  940. $rt = false;
  941. if ($this->SingleTo === true && count($toArr) > 1) {
  942. foreach ($toArr as $val) {
  943. $rt = $this->mail_passthru($val, $this->Subject, $body, $header, $params);
  944. // implement call back function if it exists
  945. $isSent = ($rt == 1) ? 1 : 0;
  946. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body);
  947. }
  948. } else {
  949. $rt = $this->mail_passthru($to, $this->Subject, $body, $header, $params);
  950. // implement call back function if it exists
  951. $isSent = ($rt == 1) ? 1 : 0;
  952. $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body);
  953. }
  954. if (isset($old_from)) {
  955. ini_set('sendmail_from', $old_from);
  956. }
  957. if(!$rt) {
  958. throw new phpmailerException($this->Lang('instantiate'), self::STOP_CRITICAL);
  959. }
  960. return true;
  961. }
  962. /**
  963. * Sends mail via SMTP using PhpSMTP
  964. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
  965. * @param string $header The message headers
  966. * @param string $body The message body
  967. * @throws phpmailerException
  968. * @uses SMTP
  969. * @access protected
  970. * @return bool
  971. */
  972. protected function SmtpSend($header, $body) {
  973. require_once $this->PluginDir . 'class.smtp.php';
  974. $bad_rcpt = array();
  975. if(!$this->SmtpConnect()) {
  976. throw new phpmailerException($this->Lang('smtp_connect_failed'), self::STOP_CRITICAL);
  977. }
  978. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  979. if(!$this->smtp->Mail($smtp_from)) {
  980. $this->SetError($this->Lang('from_failed') . $smtp_from . ' : ' .implode(',', $this->smtp->getError()));
  981. throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
  982. }
  983. // Attempt to send attach all recipients
  984. foreach($this->to as $to) {
  985. if (!$this->smtp->Recipient($to[0])) {
  986. $bad_rcpt[] = $to[0];
  987. // implement call back function if it exists
  988. $isSent = 0;
  989. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
  990. } else {
  991. // implement call back function if it exists
  992. $isSent = 1;
  993. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body);
  994. }
  995. }
  996. foreach($this->cc as $cc) {
  997. if (!$this->smtp->Recipient($cc[0])) {
  998. $bad_rcpt[] = $cc[0];
  999. // implement call back function if it exists
  1000. $isSent = 0;
  1001. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
  1002. } else {
  1003. // implement call back function if it exists
  1004. $isSent = 1;
  1005. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body);
  1006. }
  1007. }
  1008. foreach($this->bcc as $bcc) {
  1009. if (!$this->smtp->Recipient($bcc[0])) {
  1010. $bad_rcpt[] = $bcc[0];
  1011. // implement call back function if it exists
  1012. $isSent = 0;
  1013. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
  1014. } else {
  1015. // implement call back function if it exists
  1016. $isSent = 1;
  1017. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body);
  1018. }
  1019. }
  1020. if (count($bad_rcpt) > 0 ) { //Create error message for any bad addresses
  1021. $badaddresses = implode(', ', $bad_rcpt);
  1022. throw new phpmailerException($this->Lang('recipients_failed') . $badaddresses);
  1023. }
  1024. if(!$this->smtp->Data($header . $body)) {
  1025. throw new phpmailerException($this->Lang('data_not_accepted'), self::STOP_CRITICAL);
  1026. }
  1027. if($this->SMTPKeepAlive == true) {
  1028. $this->smtp->Reset();
  1029. } else {
  1030. $this->smtp->Quit();
  1031. $this->smtp->Close();
  1032. }
  1033. return true;
  1034. }
  1035. /**
  1036. * Initiates a connection to an SMTP server.
  1037. * Returns false if the operation failed.
  1038. * @param array $options An array of options compatible with stream_context_create()
  1039. * @uses SMTP
  1040. * @access public
  1041. * @throws phpmailerException
  1042. * @return bool
  1043. */
  1044. public function SmtpConnect($options = array()) {
  1045. if(is_null($this->smtp)) {
  1046. $this->smtp = new SMTP;
  1047. }
  1048. //Already connected?
  1049. if ($this->smtp->Connected()) {
  1050. return true;
  1051. }
  1052. $this->smtp->Timeout = $this->Timeout;
  1053. $this->smtp->do_debug = $this->SMTPDebug;
  1054. $this->smtp->Debugoutput = $this->Debugoutput;
  1055. $this->smtp->do_verp = $this->do_verp;
  1056. $index = 0;
  1057. $tls = ($this->SMTPSecure == 'tls');
  1058. $ssl = ($this->SMTPSecure == 'ssl');
  1059. $hosts = explode(';', $this->Host);
  1060. $lastexception = null;
  1061. foreach ($hosts as $hostentry) {
  1062. $hostinfo = array();
  1063. $host = $hostentry;
  1064. $port = $this->Port;
  1065. if (preg_match('/^(.+):([0-9]+)$/', $hostentry, $hostinfo)) { //If $hostentry contains 'address:port', override default
  1066. $host = $hostinfo[1];
  1067. $port = $hostinfo[2];
  1068. }
  1069. if ($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout, $options)) {
  1070. try {
  1071. if ($this->Helo) {
  1072. $hello = $this->Helo;
  1073. } else {
  1074. $hello = $this->ServerHostname();
  1075. }
  1076. $this->smtp->Hello($hello);
  1077. if ($tls) {
  1078. if (!$this->smtp->StartTLS()) {
  1079. throw new phpmailerException($this->Lang('connect_host'));
  1080. }
  1081. //We must resend HELO after tls negotiation
  1082. $this->smtp->Hello($hello);
  1083. }
  1084. if ($this->SMTPAuth) {
  1085. if (!$this->smtp->Authenticate($this->Username, $this->Password, $this->AuthType, $this->Realm, $this->Workstation)) {
  1086. throw new phpmailerException($this->Lang('authenticate'));
  1087. }
  1088. }
  1089. return true;
  1090. } catch (phpmailerException $e) {
  1091. $lastexception = $e;
  1092. //We must have connected, but then failed TLS or Auth, so close connection nicely
  1093. $this->smtp->Quit();
  1094. }
  1095. }
  1096. }
  1097. //If we get here, all connection attempts have failed, so close connection hard
  1098. $this->smtp->Close();
  1099. //As we've caught all exceptions, just report whatever the last one was
  1100. if ($this->exceptions and !is_null($lastexception)) {
  1101. throw $lastexception;
  1102. }
  1103. return false;
  1104. }
  1105. /**
  1106. * Closes the active SMTP session if one exists.
  1107. * @return void
  1108. */
  1109. public function SmtpClose() {
  1110. if ($this->smtp !== null) {
  1111. if($this->smtp->Connected()) {
  1112. $this->smtp->Quit();
  1113. $this->smtp->Close();
  1114. }
  1115. }
  1116. }
  1117. /**
  1118. * Sets the language for all class error messages.
  1119. * Returns false if it cannot load the language file. The default language is English.
  1120. * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
  1121. * @param string $lang_path Path to the language file directory
  1122. * @return bool
  1123. * @access public
  1124. */
  1125. function SetLanguage($langcode = 'en', $lang_path = 'language/') {
  1126. //Define full set of translatable strings
  1127. $PHPMAILER_LANG = array(
  1128. 'authenticate' => 'SMTP Error: Could not authenticate.',
  1129. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
  1130. 'data_not_accepted' => 'SMTP Error: Data not accepted.',
  1131. 'empty_message' => 'Message body empty',
  1132. 'encoding' => 'Unknown encoding: ',
  1133. 'execute' => 'Could not execute: ',
  1134. 'file_access' => 'Could not access file: ',
  1135. 'file_open' => 'File Error: Could not open file: ',
  1136. 'from_failed' => 'The following From address failed: ',
  1137. 'instantiate' => 'Could not instantiate mail function.',
  1138. 'invalid_address' => 'Invalid address',
  1139. 'mailer_not_supported' => ' mailer is not supported.',
  1140. 'provide_address' => 'You must provide at least one recipient email address.',
  1141. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
  1142. 'signing' => 'Signing Error: ',
  1143. 'smtp_connect_failed' => 'SMTP Connect() failed.',
  1144. 'smtp_error' => 'SMTP server error: ',
  1145. 'variable_set' => 'Cannot set or reset variable: ',
  1146. 'amazonses_error' => 'AmazonSES Error: '
  1147. );
  1148. //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
  1149. $l = true;
  1150. if ($langcode != 'en') { //There is no English translation file
  1151. $l = @include $lang_path.'phpmailer.lang-'.$langcode.'.php';
  1152. }
  1153. $this->language = $PHPMAILER_LANG;
  1154. return ($l == true); //Returns false if language not found
  1155. }
  1156. /**
  1157. * Return the current array of language strings
  1158. * @return array
  1159. */
  1160. public function GetTranslations() {
  1161. return $this->language;
  1162. }
  1163. /////////////////////////////////////////////////
  1164. // METHODS, MESSAGE CREATION
  1165. /////////////////////////////////////////////////
  1166. /**
  1167. * Creates recipient headers.
  1168. * @access public
  1169. * @param string $type
  1170. * @param array $addr
  1171. * @return string
  1172. */
  1173. public function AddrAppend($type, $addr) {
  1174. $addr_str = $type . ': ';
  1175. $addresses = array();
  1176. foreach ($addr as $a) {
  1177. $addresses[] = $this->AddrFormat($a);
  1178. }
  1179. $addr_str .= implode(', ', $addresses);
  1180. $addr_str .= $this->LE;
  1181. return $addr_str;
  1182. }
  1183. /**
  1184. * Formats an address correctly.
  1185. * @access public
  1186. * @param string $addr
  1187. * @return string
  1188. */
  1189. public function AddrFormat($addr) {
  1190. if (empty($addr[1])) {
  1191. return $this->SecureHeader($addr[0]);
  1192. } else {
  1193. return $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  1194. }
  1195. }
  1196. /**
  1197. * Wraps message for use with mailers that do not
  1198. * automatically perform wrapping and for quoted-printable.
  1199. * Original written by philippe.
  1200. * @param string $message The message to wrap
  1201. * @param integer $length The line length to wrap to
  1202. * @param boolean $qp_mode Whether to run in Quoted-Printable mode
  1203. * @access public
  1204. * @return string
  1205. */
  1206. public function WrapText($message, $length, $qp_mode = false) {
  1207. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  1208. // If utf-8 encoding is used, we will need to make sure we don't
  1209. // split multibyte characters when we wrap
  1210. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  1211. $lelen = strlen($this->LE);
  1212. $crlflen = strlen(self::CRLF);
  1213. $message = $this->FixEOL($message);
  1214. if (substr($message, -$lelen) == $this->LE) {
  1215. $message = substr($message, 0, -$lelen);
  1216. }
  1217. $line = explode($this->LE, $message); // Magic. We know FixEOL uses $LE
  1218. $message = '';
  1219. for ($i = 0 ;$i < count($line); $i++) {
  1220. $line_part = explode(' ', $line[$i]);
  1221. $buf = '';
  1222. for ($e = 0; $e<count($line_part); $e++) {
  1223. $word = $line_part[$e];
  1224. if ($qp_mode and (strlen($word) > $length)) {
  1225. $space_left = $length - strlen($buf) - $crlflen;
  1226. if ($e != 0) {
  1227. if ($space_left > 20) {
  1228. $len = $space_left;
  1229. if ($is_utf8) {
  1230. $len = $this->UTF8CharBoundary($word, $len);
  1231. } elseif (substr($word, $len - 1, 1) == "=") {
  1232. $len--;
  1233. } elseif (substr($word, $len - 2, 1) == "=") {
  1234. $len -= 2;
  1235. }
  1236. $part = substr($word, 0, $len);
  1237. $word = substr($word, $len);
  1238. $buf .= ' ' . $part;
  1239. $message .= $buf . sprintf("=%s", self::CRLF);
  1240. } else {
  1241. $message .= $buf . $soft_break;
  1242. }
  1243. $buf = '';
  1244. }
  1245. while (strlen($word) > 0) {
  1246. if ($length <= 0) {
  1247. break;
  1248. }
  1249. $len = $length;
  1250. if ($is_utf8) {
  1251. $len = $this->UTF8CharBoundary($word, $len);
  1252. } elseif (substr($word, $len - 1, 1) == "=") {
  1253. $len--;
  1254. } elseif (substr($word, $len - 2, 1) == "=") {
  1255. $len -= 2;
  1256. }
  1257. $part = substr($word, 0, $len);
  1258. $word = substr($word, $len);
  1259. if (strlen($word) > 0) {
  1260. $message .= $part . sprintf("=%s", self::CRLF);
  1261. } else {
  1262. $buf = $part;
  1263. }
  1264. }
  1265. } else {
  1266. $buf_o = $buf;
  1267. $buf .= ($e == 0) ? $word : (' ' . $word);
  1268. if (strlen($buf) > $length and $buf_o != '') {
  1269. $message .= $buf_o . $soft_break;
  1270. $buf = $word;
  1271. }
  1272. }
  1273. }
  1274. $message .= $buf . self::CRLF;
  1275. }
  1276. return $message;
  1277. }
  1278. /**
  1279. * Finds last character boundary prior to maxLength in a utf-8
  1280. * quoted (printable) encoded string.
  1281. * Original written by Colin Brown.
  1282. * @access public
  1283. * @param string $encodedText utf-8 QP text
  1284. * @param int $maxLength find last character boundary prior to this length
  1285. * @return int
  1286. */
  1287. public function UTF8CharBoundary($encodedText, $maxLength) {
  1288. $foundSplitPos = false;
  1289. $lookBack = 3;
  1290. while (!$foundSplitPos) {
  1291. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  1292. $encodedCharPos = strpos($lastChunk, "=");
  1293. if ($encodedCharPos !== false) {
  1294. // Found start of encoded character byte within $lookBack block.
  1295. // Check the encoded byte value (the 2 chars after the '=')
  1296. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  1297. $dec = hexdec($hex);
  1298. if ($dec < 128) { // Single byte character.
  1299. // If the encoded char was found at pos 0, it will fit
  1300. // otherwise reduce maxLength to start of the encoded char
  1301. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  1302. $maxLength - ($lookBack - $encodedCharPos);
  1303. $foundSplitPos = true;
  1304. } elseif ($dec >= 192) { // First byte of a multi byte character
  1305. // Reduce maxLength to split at start of character
  1306. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  1307. $foundSplitPos = true;
  1308. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  1309. $lookBack += 3;
  1310. }
  1311. } else {
  1312. // No encoded character found
  1313. $foundSplitPos = true;
  1314. }
  1315. }
  1316. return $maxLength;
  1317. }
  1318. /**
  1319. * Set the body wrapping.
  1320. * @access public
  1321. * @return void
  1322. */
  1323. public function SetWordWrap() {
  1324. if($this->WordWrap < 1) {
  1325. return;
  1326. }
  1327. switch($this->message_type) {
  1328. case 'alt':
  1329. case 'alt_inline':
  1330. case 'alt_attach':
  1331. case 'alt_inline_attach':
  1332. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  1333. break;
  1334. default:
  1335. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  1336. break;
  1337. }
  1338. }
  1339. /**
  1340. * Assembles message header.
  1341. * @access public
  1342. * @return string The assembled header
  1343. */
  1344. public function CreateHeader() {
  1345. $result = '';
  1346. // Set the boundaries
  1347. $uniq_id = md5(uniqid(time()));
  1348. $this->boundary[1] = 'b1_' . $uniq_id;
  1349. $this->boundary[2] = 'b2_' . $uniq_id;
  1350. $this->boundary[3] = 'b3_' . $uniq_id;
  1351. if ($this->MessageDate == '') {
  1352. $result .= $this->HeaderLine('Date', self::RFCDate());
  1353. } else {
  1354. $result .= $this->HeaderLine('Date', $this->MessageDate);
  1355. }
  1356. if ($this->ReturnPath) {
  1357. $result .= $this->HeaderLine('Return-Path', '<'.trim($this->ReturnPath).'>');
  1358. } elseif ($this->Sender == '') {
  1359. $result .= $this->HeaderLine('Return-Path', '<'.trim($this->From).'>');
  1360. } else {
  1361. $result .= $this->HeaderLine('Return-Path', '<'.trim($this->Sender).'>');
  1362. }
  1363. // To be created automatically by mail()
  1364. if($this->Mailer != 'mail') {
  1365. if ($this->SingleTo === true) {
  1366. foreach($this->to as $t) {
  1367. $this->SingleToArray[] = $this->AddrFormat($t);
  1368. }
  1369. } else {
  1370. if(count($this->to) > 0

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