PageRenderTime 60ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/greenhouse/wp-includes/class-phpmailer.php

https://github.com/sahlal/Ubiquitous-Computing-Project-4
PHP | 3265 lines | 2008 code | 212 blank | 1045 comment | 294 complexity | 4271124f375797d87661ee9f98693463 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

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

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

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