PageRenderTime 65ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/phpmailer/class.phpmailer.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 3268 lines | 2010 code | 212 blank | 1046 comment | 295 complexity | ac3321eee226d50ff78379aaa6af4de6 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0

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. //Make sure our autoloader is loaded
  499. if (!in_array('PHPMailerAutoload', spl_autoload_functions())) {
  500. require 'PHPMailerAutoload.php';
  501. }
  502. }
  503. /**
  504. * Destructor.
  505. */
  506. public function __destruct()
  507. {
  508. if ($this->Mailer == 'smtp') { //close any open SMTP connection nicely
  509. $this->smtpClose();
  510. }
  511. }
  512. /**
  513. * Call mail() in a safe_mode-aware fashion.
  514. * Also, unless sendmail_path points to sendmail (or something that
  515. * claims to be sendmail), don't pass params (not a perfect fix,
  516. * but it will do)
  517. * @param string $to To
  518. * @param string $subject Subject
  519. * @param string $body Message Body
  520. * @param string $header Additional Header(s)
  521. * @param string $params Params
  522. * @access private
  523. * @return bool
  524. */
  525. private function mailPassthru($to, $subject, $body, $header, $params)
  526. {
  527. if (ini_get('safe_mode') || !($this->UseSendmailOptions)) {
  528. $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header);
  529. } else {
  530. $rt = @mail($to, $this->encodeHeader($this->secureHeader($subject)), $body, $header, $params);
  531. }
  532. return $rt;
  533. }
  534. /**
  535. * Output debugging info via user-defined method.
  536. * Only if debug output is enabled.
  537. * @see PHPMailer::$Debugoutput
  538. * @see PHPMailer::$SMTPDebug
  539. * @param string $str
  540. */
  541. protected function edebug($str)
  542. {
  543. if (!$this->SMTPDebug) {
  544. return;
  545. }
  546. switch ($this->Debugoutput) {
  547. case 'error_log':
  548. error_log($str);
  549. break;
  550. case 'html':
  551. //Cleans up output a bit for a better looking display that's HTML-safe
  552. echo htmlentities(preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, $this->CharSet) . "<br>\n";
  553. break;
  554. case 'echo':
  555. default:
  556. //Just echoes exactly what was received
  557. echo $str;
  558. }
  559. }
  560. /**
  561. * Sets message type to HTML or plain.
  562. * @param bool $ishtml True for HTML mode.
  563. * @return void
  564. */
  565. public function isHTML($ishtml = true)
  566. {
  567. if ($ishtml) {
  568. $this->ContentType = 'text/html';
  569. } else {
  570. $this->ContentType = 'text/plain';
  571. }
  572. }
  573. /**
  574. * Send messages using SMTP.
  575. * @return void
  576. */
  577. public function isSMTP()
  578. {
  579. $this->Mailer = 'smtp';
  580. }
  581. /**
  582. * Send messages using PHP's mail() function.
  583. * @return void
  584. */
  585. public function isMail()
  586. {
  587. $this->Mailer = 'mail';
  588. }
  589. /**
  590. * Send messages using $Sendmail.
  591. * @return void
  592. */
  593. public function isSendmail()
  594. {
  595. if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
  596. $this->Sendmail = '/var/qmail/bin/sendmail';
  597. }
  598. $this->Mailer = 'sendmail';
  599. }
  600. /**
  601. * Send messages using qmail.
  602. * @return void
  603. */
  604. public function isQmail()
  605. {
  606. if (stristr(ini_get('sendmail_path'), 'qmail')) {
  607. $this->Sendmail = '/var/qmail/bin/sendmail';
  608. }
  609. $this->Mailer = 'sendmail';
  610. }
  611. /**
  612. * Add a "To" address.
  613. * @param string $address
  614. * @param string $name
  615. * @return bool true on success, false if address already used
  616. */
  617. public function addAddress($address, $name = '')
  618. {
  619. return $this->addAnAddress('to', $address, $name);
  620. }
  621. /**
  622. * Add a "CC" address.
  623. * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
  624. * @param string $address
  625. * @param string $name
  626. * @return bool true on success, false if address already used
  627. */
  628. public function addCC($address, $name = '')
  629. {
  630. return $this->addAnAddress('cc', $address, $name);
  631. }
  632. /**
  633. * Add a "BCC" address.
  634. * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
  635. * @param string $address
  636. * @param string $name
  637. * @return bool true on success, false if address already used
  638. */
  639. public function addBCC($address, $name = '')
  640. {
  641. return $this->addAnAddress('bcc', $address, $name);
  642. }
  643. /**
  644. * Add a "Reply-to" address.
  645. * @param string $address
  646. * @param string $name
  647. * @return bool
  648. */
  649. public function addReplyTo($address, $name = '')
  650. {
  651. return $this->addAnAddress('Reply-To', $address, $name);
  652. }
  653. /**
  654. * Add an address to one of the recipient arrays.
  655. * Addresses that have been added already return false, but do not throw exceptions
  656. * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
  657. * @param string $address The email address to send to
  658. * @param string $name
  659. * @throws phpmailerException
  660. * @return bool true on success, false if address already used or invalid in some way
  661. * @access protected
  662. */
  663. protected function addAnAddress($kind, $address, $name = '')
  664. {
  665. if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
  666. $this->setError($this->lang('Invalid recipient array') . ': ' . $kind);
  667. if ($this->exceptions) {
  668. throw new phpmailerException('Invalid recipient array: ' . $kind);
  669. }
  670. $this->edebug($this->lang('Invalid recipient array') . ': ' . $kind);
  671. return false;
  672. }
  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. $this->edebug($this->lang('invalid_address') . ': ' . $address);
  681. return false;
  682. }
  683. if ($kind != 'Reply-To') {
  684. if (!isset($this->all_recipients[strtolower($address)])) {
  685. array_push($this->$kind, array($address, $name));
  686. $this->all_recipients[strtolower($address)] = true;
  687. return true;
  688. }
  689. } else {
  690. if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
  691. $this->ReplyTo[strtolower($address)] = array($address, $name);
  692. return true;
  693. }
  694. }
  695. return false;
  696. }
  697. /**
  698. * Set the From and FromName properties.
  699. * @param string $address
  700. * @param string $name
  701. * @param bool $auto Whether to also set the Sender address, defaults to true
  702. * @throws phpmailerException
  703. * @return bool
  704. */
  705. public function setFrom($address, $name = '', $auto = true)
  706. {
  707. $address = trim($address);
  708. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
  709. if (!$this->validateAddress($address)) {
  710. $this->setError($this->lang('invalid_address') . ': ' . $address);
  711. if ($this->exceptions) {
  712. throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
  713. }
  714. $this->edebug($this->lang('invalid_address') . ': ' . $address);
  715. return false;
  716. }
  717. $this->From = $address;
  718. $this->FromName = $name;
  719. if ($auto) {
  720. if (empty($this->Sender)) {
  721. $this->Sender = $address;
  722. }
  723. }
  724. return true;
  725. }
  726. /**
  727. * Return the Message-ID header of the last email.
  728. * Technically this is the value from the last time the headers were created,
  729. * but it's also the message ID of the last sent message except in
  730. * pathological cases.
  731. * @return string
  732. */
  733. public function getLastMessageID()
  734. {
  735. return $this->lastMessageID;
  736. }
  737. /**
  738. * Check that a string looks like an email address.
  739. * @param string $address The email address to check
  740. * @param string $patternselect A selector for the validation pattern to use :
  741. * 'auto' - pick best one automatically;
  742. * 'pcre8' - use the squiloople.com pattern, requires PCRE > 8.0, PHP >= 5.3.2, 5.2.14;
  743. * 'pcre' - use old PCRE implementation;
  744. * 'php' - use PHP built-in FILTER_VALIDATE_EMAIL; faster, less thorough;
  745. * 'noregex' - super fast, really dumb.
  746. * @return bool
  747. * @static
  748. * @access public
  749. */
  750. public static function validateAddress($address, $patternselect = 'auto')
  751. {
  752. if ($patternselect == 'auto') {
  753. if (defined(
  754. 'PCRE_VERSION'
  755. )
  756. ) { //Check this instead of extension_loaded so it works when that function is disabled
  757. if (version_compare(PCRE_VERSION, '8.0') >= 0) {
  758. $patternselect = 'pcre8';
  759. } else {
  760. $patternselect = 'pcre';
  761. }
  762. } else {
  763. //Filter_var appeared in PHP 5.2.0 and does not require the PCRE extension
  764. if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
  765. $patternselect = 'php';
  766. } else {
  767. $patternselect = 'noregex';
  768. }
  769. }
  770. }
  771. switch ($patternselect) {
  772. case 'pcre8':
  773. /**
  774. * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
  775. * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
  776. * not allow a@b type valid addresses :(
  777. * @link http://squiloople.com/2009/12/20/email-address-validation/
  778. * @copyright 2009-2010 Michael Rushton
  779. * Feel free to use and redistribute this code. But please keep this copyright notice.
  780. */
  781. return (bool)preg_match(
  782. '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
  783. '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
  784. '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
  785. '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
  786. '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
  787. '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
  788. '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
  789. '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
  790. '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
  791. $address
  792. );
  793. break;
  794. case 'pcre':
  795. //An older regex that doesn't need a recent PCRE
  796. return (bool)preg_match(
  797. '/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>' .
  798. '[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")' .
  799. '(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*' .
  800. '@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})' .
  801. '(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:' .
  802. '[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?' .
  803. '::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:' .
  804. '[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?' .
  805. '::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
  806. '|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD',
  807. $address
  808. );
  809. break;
  810. case 'php':
  811. default:
  812. return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
  813. break;
  814. case 'noregex':
  815. //No PCRE! Do something _very_ approximate!
  816. //Check the address is 3 chars or longer and contains an @ that's not the first or last char
  817. return (strlen($address) >= 3
  818. and strpos($address, '@') >= 1
  819. and strpos($address, '@') != strlen($address) - 1);
  820. break;
  821. }
  822. }
  823. /**
  824. * Create a message and send it.
  825. * Uses the sending method specified by $Mailer.
  826. * Returns false on error - Use the ErrorInfo variable to view description of the error.
  827. * @throws phpmailerException
  828. * @return bool
  829. */
  830. public function send()
  831. {
  832. try {
  833. if (!$this->preSend()) {
  834. return false;
  835. }
  836. return $this->postSend();
  837. } catch (phpmailerException $e) {
  838. $this->mailHeader = '';
  839. $this->setError($e->getMessage());
  840. if ($this->exceptions) {
  841. throw $e;
  842. }
  843. return false;
  844. }
  845. }
  846. /**
  847. * Prepare a message for sending.
  848. * @throws phpmailerException
  849. * @return bool
  850. */
  851. public function preSend()
  852. {
  853. try {
  854. $this->mailHeader = "";
  855. if ((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
  856. throw new phpmailerException($this->lang('provide_address'), self::STOP_CRITICAL);
  857. }
  858. // Set whether the message is multipart/alternative
  859. if (!empty($this->AltBody)) {
  860. $this->ContentType = 'multipart/alternative';
  861. }
  862. $this->error_count = 0; // reset errors
  863. $this->setMessageType();
  864. // Refuse to send an empty message unless we are specifically allowing it
  865. if (!$this->AllowEmpty and empty($this->Body)) {
  866. throw new phpmailerException($this->lang('empty_message'), self::STOP_CRITICAL);
  867. }
  868. $this->MIMEHeader = $this->createHeader();
  869. $this->MIMEBody = $this->createBody();
  870. // To capture the complete message when using mail(), create
  871. // an extra header list which createHeader() doesn't fold in
  872. if ($this->Mailer == 'mail') {
  873. if (count($this->to) > 0) {
  874. $this->mailHeader .= $this->addrAppend("To", $this->to);
  875. } else {
  876. $this->mailHeader .= $this->headerLine("To", "undisclosed-recipients:;");
  877. }
  878. $this->mailHeader .= $this->headerLine(
  879. 'Subject',
  880. $this->encodeHeader($this->secureHeader(trim($this->Subject)))
  881. );
  882. }
  883. // Sign with DKIM if enabled
  884. if (!empty($this->DKIM_domain)
  885. && !empty($this->DKIM_private)
  886. && !empty($this->DKIM_selector)
  887. && !empty($this->DKIM_domain)
  888. && file_exists($this->DKIM_private)) {
  889. $header_dkim = $this->DKIM_Add(
  890. $this->MIMEHeader . $this->mailHeader,
  891. $this->encodeHeader($this->secureHeader($this->Subject)),
  892. $this->MIMEBody
  893. );
  894. $this->MIMEHeader = rtrim($this->MIMEHeader, "\r\n ") . self::CRLF .
  895. str_replace("\r\n", "\n", $header_dkim) . self::CRLF;
  896. }
  897. return true;
  898. } catch (phpmailerException $e) {
  899. $this->setError($e->getMessage());
  900. if ($this->exceptions) {
  901. throw $e;
  902. }
  903. return false;
  904. }
  905. }
  906. /**
  907. * Actually send a message.
  908. * Send the email via the selected mechanism
  909. * @throws phpmailerException
  910. * @return bool
  911. */
  912. public function postSend()
  913. {
  914. try {
  915. // Choose the mailer and send through it
  916. switch ($this->Mailer) {
  917. case 'sendmail':
  918. return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody);
  919. case 'smtp':
  920. return $this->smtpSend($this->MIMEHeader, $this->MIMEBody);
  921. case 'mail':
  922. return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
  923. default:
  924. return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
  925. }
  926. } catch (phpmailerException $e) {
  927. $this->setError($e->getMessage());
  928. if ($this->exceptions) {
  929. throw $e;
  930. }
  931. $this->edebug($e->getMessage() . "\n");
  932. }
  933. return false;
  934. }
  935. /**
  936. * Send mail using the $Sendmail program.
  937. * @param string $header The message headers
  938. * @param string $body The message body
  939. * @see PHPMailer::$Sendmail
  940. * @throws phpmailerException
  941. * @access protected
  942. * @return bool
  943. */
  944. protected function sendmailSend($header, $body)
  945. {
  946. if ($this->Sender != '') {
  947. $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  948. } else {
  949. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  950. }
  951. if ($this->SingleTo === true) {
  952. foreach ($this->SingleToArray as $val) {
  953. if (!@$mail = popen($sendmail, 'w')) {
  954. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  955. }
  956. fputs($mail, "To: " . $val . "\n");
  957. fputs($mail, $header);
  958. fputs($mail, $body);
  959. $result = pclose($mail);
  960. // implement call back function if it exists
  961. $isSent = ($result == 0) ? 1 : 0;
  962. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
  963. if ($result != 0) {
  964. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  965. }
  966. }
  967. } else {
  968. if (!@$mail = popen($sendmail, 'w')) {
  969. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  970. }
  971. fputs($mail, $header);
  972. fputs($mail, $body);
  973. $result = pclose($mail);
  974. // implement call back function if it exists
  975. $isSent = ($result == 0) ? 1 : 0;
  976. $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
  977. if ($result != 0) {
  978. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  979. }
  980. }
  981. return true;
  982. }
  983. /**
  984. * Send mail using the PHP mail() function.
  985. * @param string $header The message headers
  986. * @param string $body The message body
  987. * @link http://www.php.net/manual/en/book.mail.php
  988. * @throws phpmailerException
  989. * @access protected
  990. * @return bool
  991. */
  992. protected function mailSend($header, $body)
  993. {
  994. $toArr = array();
  995. foreach ($this->to as $t) {
  996. $toArr[] = $this->addrFormat($t);
  997. }
  998. $to = implode(', ', $toArr);
  999. if (empty($this->Sender)) {
  1000. $params = " ";
  1001. } else {
  1002. $params = sprintf("-f%s", $this->Sender);
  1003. }
  1004. if ($this->Sender != '' and !ini_get('safe_mode')) {
  1005. $old_from = ini_get('sendmail_from');
  1006. ini_set('sendmail_from', $this->Sender);
  1007. }
  1008. $rt = false;
  1009. if ($this->SingleTo === true && count($toArr) > 1) {
  1010. foreach ($toArr as $val) {
  1011. $rt = $this->mailPassthru($val, $this->Subject, $body, $header, $params);
  1012. // implement call back function if it exists
  1013. $isSent = ($rt == 1) ? 1 : 0;
  1014. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
  1015. }
  1016. } else {
  1017. $rt = $this->mailPassthru($to, $this->Subject, $body, $header, $params);
  1018. // implement call back function if it exists
  1019. $isSent = ($rt == 1) ? 1 : 0;
  1020. $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
  1021. }
  1022. if (isset($old_from)) {
  1023. ini_set('sendmail_from', $old_from);
  1024. }
  1025. if (!$rt) {
  1026. throw new phpmailerException($this->lang('instantiate'), self::STOP_CRITICAL);
  1027. }
  1028. return true;
  1029. }
  1030. /**
  1031. * Get an instance to use for SMTP operations.
  1032. * Override this function to load your own SMTP implementation
  1033. * @return SMTP
  1034. */
  1035. public function getSMTPInstance()
  1036. {
  1037. if (!is_object($this->smtp)) {
  1038. $this->smtp = new SMTP;
  1039. }
  1040. return $this->smtp;
  1041. }
  1042. /**
  1043. * Send mail via SMTP.
  1044. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
  1045. * Uses the PHPMailerSMTP class by default.
  1046. * @see PHPMailer::getSMTPInstance() to use a different class.
  1047. * @param string $header The message headers
  1048. * @param string $body The message body
  1049. * @throws phpmailerException
  1050. * @uses SMTP
  1051. * @access protected
  1052. * @return bool
  1053. */
  1054. protected function smtpSend($header, $body)
  1055. {
  1056. $bad_rcpt = array();
  1057. if (!$this->smtpConnect()) {
  1058. throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
  1059. }
  1060. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  1061. if (!$this->smtp->mail($smtp_from)) {
  1062. $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError()));
  1063. throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
  1064. }
  1065. // Attempt to send attach all recipients
  1066. foreach ($this->to as $to) {
  1067. if (!$this->smtp->recipient($to[0])) {
  1068. $bad_rcpt[] = $to[0];
  1069. $isSent = 0;
  1070. } else {
  1071. $isSent = 1;
  1072. }
  1073. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body, $this->From);
  1074. }
  1075. foreach ($this->cc as $cc) {
  1076. if (!$this->smtp->recipient($cc[0])) {
  1077. $bad_rcpt[] = $cc[0];
  1078. $isSent = 0;
  1079. } else {
  1080. $isSent = 1;
  1081. }
  1082. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body, $this->From);
  1083. }
  1084. foreach ($this->bcc as $bcc) {
  1085. if (!$this->smtp->recipient($bcc[0])) {
  1086. $bad_rcpt[] = $bcc[0];
  1087. $isSent = 0;
  1088. } else {
  1089. $isSent = 1;
  1090. }
  1091. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body, $this->From);
  1092. }
  1093. if (count($bad_rcpt) > 0) { //Create error message for any bad addresses
  1094. throw new phpmailerException($this->lang('recipients_failed') . implode(', ', $bad_rcpt));
  1095. }
  1096. if (!$this->smtp->data($header . $body)) {
  1097. throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL);
  1098. }
  1099. if ($this->SMTPKeepAlive == true) {
  1100. $this->smtp->reset();
  1101. } else {
  1102. $this->smtp->quit();
  1103. $this->smtp->close();
  1104. }
  1105. return true;
  1106. }
  1107. /**
  1108. * Initiate a connection to an SMTP server.
  1109. * Returns false if the operation failed.
  1110. * @param array $options An array of options compatible with stream_context_create()
  1111. * @uses SMTP
  1112. * @access public
  1113. * @throws phpmailerException
  1114. * @return bool
  1115. */
  1116. public function smtpConnect($options = array())
  1117. {
  1118. if (is_null($this->smtp)) {
  1119. $this->smtp = $this->getSMTPInstance();
  1120. }
  1121. //Already connected?
  1122. if ($this->smtp->connected()) {
  1123. return true;
  1124. }
  1125. $this->smtp->setTimeout($this->Timeout);
  1126. $this->smtp->setDebugLevel($this->SMTPDebug);
  1127. $this->smtp->setDebugOutput($this->Debugoutput);
  1128. $this->smtp->setVerp($this->do_verp);
  1129. $tls = ($this->SMTPSecure == 'tls');
  1130. $ssl = ($this->SMTPSecure == 'ssl');
  1131. $hosts = explode(';', $this->Host);
  1132. $lastexception = null;
  1133. foreach ($hosts as $hostentry) {
  1134. $hostinfo = array();
  1135. $host = $hostentry;
  1136. $port = $this->Port;
  1137. if (preg_match(
  1138. '/^(.+):([0-9]+)$/',
  1139. $hostentry,
  1140. $hostinfo
  1141. )
  1142. ) { //If $hostentry contains 'address:port', override default
  1143. $host = $hostinfo[1];
  1144. $port = $hostinfo[2];
  1145. }
  1146. if ($this->smtp->connect(($ssl ? 'ssl://' : '') . $host, $port, $this->Timeout, $options)) {
  1147. try {
  1148. if ($this->Helo) {
  1149. $hello = $this->Helo;
  1150. } else {
  1151. $hello = $this->serverHostname();
  1152. }
  1153. $this->smtp->hello($hello);
  1154. if ($tls) {
  1155. if (!$this->smtp->startTLS()) {
  1156. throw new phpmailerException($this->lang('connect_host'));
  1157. }
  1158. //We must resend HELO after tls negotiation
  1159. $this->smtp->hello($hello);
  1160. }
  1161. if ($this->SMTPAuth) {
  1162. if (!$this->smtp->authenticate(
  1163. $this->Username,
  1164. $this->Password,
  1165. $this->AuthType,
  1166. $this->Realm,
  1167. $this->Workstation
  1168. )
  1169. ) {
  1170. throw new phpmailerException($this->lang('authenticate'));
  1171. }
  1172. }
  1173. return true;
  1174. } catch (phpmailerException $e) {
  1175. $lastexception = $e;
  1176. //We must have connected, but then failed TLS or Auth, so close connection nicely
  1177. $this->smtp->quit();
  1178. }
  1179. }
  1180. }
  1181. //If we get here, all connection attempts have failed, so close connection hard
  1182. $this->smtp->close();
  1183. //As we've caught all exceptions, just report whatever the last one was
  1184. if ($this->exceptions and !is_null($lastexception)) {
  1185. throw $lastexception;
  1186. }
  1187. return false;
  1188. }
  1189. /**
  1190. * Close the active SMTP session if one exists.
  1191. * @return void
  1192. */
  1193. public function smtpClose()
  1194. {
  1195. if ($this->smtp !== null) {
  1196. if ($this->smtp->connected()) {
  1197. $this->smtp->quit();
  1198. $this->smtp->close();
  1199. }
  1200. }
  1201. }
  1202. /**
  1203. * Set the language for error messages.
  1204. * Returns false if it cannot load the language file.
  1205. * The default language is English.
  1206. * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr")
  1207. * @param string $lang_path Path to the language file directory, with trailing separator (slash)
  1208. * @return bool
  1209. * @access public
  1210. */
  1211. public function setLanguage($langcode = 'en', $lang_path = 'language/')
  1212. {
  1213. //Define full set of translatable strings
  1214. $PHPMAILER_LANG = array(
  1215. 'authenticate' => 'SMTP Error: Could not authenticate.',
  1216. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
  1217. 'data_not_accepted' => 'SMTP Error: data not accepted.',
  1218. 'empty_message' => 'Message body empty',
  1219. 'encoding' => 'Unknown encoding: ',
  1220. 'execute' => 'Could not execute: ',
  1221. 'file_access' => 'Could not access file: ',
  1222. 'file_open' => 'File Error: Could not open file: ',
  1223. 'from_failed' => 'The following From address failed: ',
  1224. 'instantiate' => 'Could not instantiate mail function.',
  1225. 'invalid_address' => 'Invalid address',
  1226. 'mailer_not_supported' => ' mailer is not supported.',
  1227. 'provide_address' => 'You must provide at least one recipient email address.',
  1228. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
  1229. 'signing' => 'Signing Error: ',
  1230. 'smtp_connect_failed' => 'SMTP connect() failed.',
  1231. 'smtp_error' => 'SMTP server error: ',
  1232. 'variable_set' => 'Cannot set or reset variable: '
  1233. );
  1234. //Overwrite language-specific strings.
  1235. //This way we'll never have missing translations - no more "language string failed to load"!
  1236. $l = true;
  1237. if ($langcode != 'en') { //There is no English translation file
  1238. $l = @include $lang_path . 'phpmailer.lang-' . $langcode . '.php';
  1239. }
  1240. $this->language = $PHPMAILER_LANG;
  1241. return ($l == true); //Returns false if language not found
  1242. }
  1243. /**
  1244. * Get the array of strings for the current language.
  1245. * @return array
  1246. */
  1247. public function getTranslations()
  1248. {
  1249. return $this->language;
  1250. }
  1251. /**
  1252. * Create recipient headers.
  1253. * @access public
  1254. * @param string $type
  1255. * @param array $addr An array of recipient,
  1256. * where each recipient is a 2-element indexed array with element 0 containing an address
  1257. * and element 1 containing a name, like:
  1258. * array(array('joe@example.com', 'Joe User'), array('zoe@example.com', 'Zoe User'))
  1259. * @return string
  1260. */
  1261. public function addrAppend($type, $addr)
  1262. {
  1263. $addresses = array();
  1264. foreach ($addr as $a) {
  1265. $addresses[] = $this->addrFormat($a);
  1266. }
  1267. return $type . ': ' . implode(', ', $addresses) . $this->LE;
  1268. }
  1269. /**
  1270. * Format an address for use in a message header.
  1271. * @access public
  1272. * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name
  1273. * like array('joe@example.com', 'Joe User')
  1274. * @return string
  1275. */
  1276. public function addrFormat($addr)
  1277. {
  1278. if (empty($addr[1])) { // No name provided
  1279. return $this->secureHeader($addr[0]);
  1280. } else {
  1281. return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . " <" . $this->secureHeader(
  1282. $addr[0]
  1283. ) . ">";
  1284. }
  1285. }
  1286. /**
  1287. * Word-wrap message.
  1288. * For use with mailers that do not automatically perform wrapping
  1289. * and for quoted-printable encoded messages.
  1290. * Original written by philippe.
  1291. * @param string $message The message to wrap
  1292. * @param integer $length The line length to wrap to
  1293. * @param bool $qp_mode Whether to run in Quoted-Printable mode
  1294. * @access public
  1295. * @return string
  1296. */
  1297. public function wrapText($message, $length, $qp_mode = false)
  1298. {
  1299. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  1300. // If utf-8 encoding is used, we will need to make sure we don't
  1301. // split multibyte characters when we wrap
  1302. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  1303. $lelen = strlen($this->LE);
  1304. $crlflen = strlen(self::CRLF);
  1305. $message = $this->fixEOL($message);
  1306. if (substr($message, -$lelen) == $this->LE) {
  1307. $message = substr($message, 0, -$lelen);
  1308. }
  1309. $line = explode($this->LE, $message); // Magic. We know fixEOL uses $LE
  1310. $message = '';
  1311. for ($i = 0; $i < count($line); $i++) {
  1312. $line_part = explode(' ', $line[$i]);
  1313. $buf = '';
  1314. for ($e = 0; $e < count($line_part); $e++) {
  1315. $word = $line_part[$e];
  1316. if ($qp_mode and (strlen($word) > $length)) {
  1317. $space_left = $length - strlen($buf) - $crlflen;
  1318. if ($e != 0) {
  1319. if ($space_left > 20) {
  1320. $len = $space_left;
  1321. if ($is_utf8) {
  1322. $len = $this->utf8CharBoundary($word, $len);
  1323. } elseif (substr($word, $len - 1, 1) == "=") {
  1324. $len--;
  1325. } elseif (substr($word, $len - 2, 1) == "=") {
  1326. $len -= 2;
  1327. }
  1328. $part = substr($word, 0, $len);
  1329. $word = substr($word, $len);
  1330. $buf .= ' ' . $part;
  1331. $message .= $buf . sprintf("=%s", self::CRLF);
  1332. } else {
  1333. $message .= $buf . $soft_break;
  1334. }
  1335. $buf = '';
  1336. }
  1337. while (strlen($word) > 0) {
  1338. if ($length <= 0) {
  1339. break;
  1340. }
  1341. $len = $length;
  1342. if ($is_utf8) {
  1343. $len = $this->utf8CharBoundary($word, $len);
  1344. } elseif (substr($word, $len - 1, 1) == "=") {
  1345. $len--;
  1346. } elseif (substr($word, $len - 2, 1) == "=") {
  1347. $len -= 2;
  1348. }
  1349. $part = substr($word, 0, $len);
  1350. $word = substr($word, $len);
  1351. if (strlen($word) > 0) {
  1352. $message .= $part . sprintf("=%s", self::CRLF);
  1353. } else {
  1354. $buf = $part;
  1355. }
  1356. }
  1357. } else {
  1358. $buf_o = $buf;
  1359. $buf .= ($e == 0) ? $word : (' ' . $word);
  1360. if (strlen($buf) > $length and $buf_o != '') {
  1361. $message .= $buf_o . $soft_break;
  1362. $buf = $word;
  1363. }
  1364. }
  1365. }
  1366. $message .= $buf . self::CRLF;
  1367. }
  1368. return $message;
  1369. }
  1370. /**
  1371. * Find the last character boundary prior to $maxLength in a utf-8
  1372. * quoted (printable) encoded string.
  1373. * Original written by Colin Brown.
  1374. * @access public
  1375. * @param string $encodedText utf-8 QP text
  1376. * @param int $maxLength find last character boundary prior to this length
  1377. * @return int
  1378. */
  1379. public function utf8CharBoundary($encodedText, $maxLength)
  1380. {
  1381. $foundSplitPos = false;
  1382. $lookBack = 3;
  1383. while (!$foundSplitPos) {
  1384. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  1385. $encodedCharPos = strpos($lastChunk, "=");
  1386. if ($encodedCharPos !== false) {
  1387. // Found start of encoded character byte within $lookBack block.
  1388. // Check the encoded byte value (the 2 chars after the '=')
  1389. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  1390. $dec = hexdec($hex);
  1391. if ($dec < 128) { // Single byte character.
  1392. // If the encoded char was found at pos 0, it will fit
  1393. // otherwise reduce maxLength to start of the encoded char
  1394. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  1395. $maxLength - ($lookBack - $encodedCharPos);
  1396. $foundSplitPos = true;
  1397. } elseif ($dec >= 192) { // First byte of a multi byte character
  1398. // Reduce maxLength to split at start of character
  1399. $maxLength = $maxLength - ($lookBack - $encodedCharPos);

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