PageRenderTime 47ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/plugins/phpmailer/class.phpmailer.php

http://oraculum-php.googlecode.com/
PHP | 3302 lines | 2042 code | 211 blank | 1049 comment | 309 complexity | a02cb30a6c4cde3fa65cdc7c797b101b MD5 | raw file
Possible License(s): 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 any php callable: http://www.php.net/is_callable
  348. *
  349. * Parameters:
  350. * bool $result result of the send action
  351. * string $to email address of the recipient
  352. * string $cc cc email addresses
  353. * string $bcc bcc email addresses
  354. * string $subject the subject
  355. * string $body the email body
  356. * string $from email address of sender
  357. * @type string
  358. */
  359. public $action_function = '';
  360. /**
  361. * What to use in the X-Mailer header.
  362. * Options: null for default, whitespace for none, or a string to use
  363. * @type string
  364. */
  365. public $XMailer = '';
  366. /**
  367. * An instance of the SMTP sender class.
  368. * @type SMTP
  369. * @access protected
  370. */
  371. protected $smtp = null;
  372. /**
  373. * The array of 'to' addresses.
  374. * @type array
  375. * @access protected
  376. */
  377. protected $to = array();
  378. /**
  379. * The array of 'cc' addresses.
  380. * @type array
  381. * @access protected
  382. */
  383. protected $cc = array();
  384. /**
  385. * The array of 'bcc' addresses.
  386. * @type array
  387. * @access protected
  388. */
  389. protected $bcc = array();
  390. /**
  391. * The array of reply-to names and addresses.
  392. * @type array
  393. * @access protected
  394. */
  395. protected $ReplyTo = array();
  396. /**
  397. * An array of all kinds of addresses.
  398. * Includes all of $to, $cc, $bcc, $replyto
  399. * @type array
  400. * @access protected
  401. */
  402. protected $all_recipients = array();
  403. /**
  404. * The array of attachments.
  405. * @type array
  406. * @access protected
  407. */
  408. protected $attachment = array();
  409. /**
  410. * The array of custom headers.
  411. * @type array
  412. * @access protected
  413. */
  414. protected $CustomHeader = array();
  415. /**
  416. * The most recent Message-ID (including angular brackets).
  417. * @type string
  418. * @access protected
  419. */
  420. protected $lastMessageID = '';
  421. /**
  422. * The message's MIME type.
  423. * @type string
  424. * @access protected
  425. */
  426. protected $message_type = '';
  427. /**
  428. * The array of MIME boundary strings.
  429. * @type array
  430. * @access protected
  431. */
  432. protected $boundary = array();
  433. /**
  434. * The array of available languages.
  435. * @type array
  436. * @access protected
  437. */
  438. protected $language = array();
  439. /**
  440. * The number of errors encountered.
  441. * @type integer
  442. * @access protected
  443. */
  444. protected $error_count = 0;
  445. /**
  446. * The S/MIME certificate file path.
  447. * @type string
  448. * @access protected
  449. */
  450. protected $sign_cert_file = '';
  451. /**
  452. * The S/MIME key file path.
  453. * @type string
  454. * @access protected
  455. */
  456. protected $sign_key_file = '';
  457. /**
  458. * The S/MIME password for the key.
  459. * Used only if the key is encrypted.
  460. * @type string
  461. * @access protected
  462. */
  463. protected $sign_key_pass = '';
  464. /**
  465. * Whether to throw exceptions for errors.
  466. * @type bool
  467. * @access protected
  468. */
  469. protected $exceptions = false;
  470. /**
  471. * Error severity: message only, continue processing
  472. */
  473. const STOP_MESSAGE = 0;
  474. /**
  475. * Error severity: message, likely ok to continue processing
  476. */
  477. const STOP_CONTINUE = 1;
  478. /**
  479. * Error severity: message, plus full stop, critical error reached
  480. */
  481. const STOP_CRITICAL = 2;
  482. /**
  483. * SMTP RFC standard line ending
  484. */
  485. const CRLF = "\r\n";
  486. /**
  487. * Constructor
  488. * @param bool $exceptions Should we throw external exceptions?
  489. */
  490. public function __construct($exceptions = false)
  491. {
  492. $this->exceptions = ($exceptions == true);
  493. //Make sure our autoloader is loaded
  494. if (version_compare(PHP_VERSION, '5.1.2', '>=') and
  495. !spl_autoload_functions() || !in_array('PHPMailerAutoload', spl_autoload_functions())) {
  496. require 'PHPMailerAutoload.php';
  497. }
  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. //Check overloading of mail function to avoid double-encoding
  524. if (ini_get('mbstring.func_overload') & 1) {
  525. $subject = $this->secureHeader($subject);
  526. } else {
  527. $subject = $this->encodeHeader($this->secureHeader($subject));
  528. }
  529. if (ini_get('safe_mode') || !($this->UseSendmailOptions)) {
  530. $rt = @mail($to, $subject, $body, $header);
  531. } else {
  532. $rt = @mail($to, $subject, $body, $header, $params);
  533. }
  534. return $rt;
  535. }
  536. /**
  537. * Output debugging info via user-defined method.
  538. * Only if debug output is enabled.
  539. * @see PHPMailer::$Debugoutput
  540. * @see PHPMailer::$SMTPDebug
  541. * @param string $str
  542. */
  543. protected function edebug($str)
  544. {
  545. if (!$this->SMTPDebug) {
  546. return;
  547. }
  548. switch ($this->Debugoutput) {
  549. case 'error_log':
  550. error_log($str);
  551. break;
  552. case 'html':
  553. //Cleans up output a bit for a better looking display that's HTML-safe
  554. echo htmlentities(preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, $this->CharSet) . "<br>\n";
  555. break;
  556. case 'echo':
  557. default:
  558. echo $str."\n";
  559. }
  560. }
  561. /**
  562. * Sets message type to HTML or plain.
  563. * @param bool $ishtml True for HTML mode.
  564. * @return void
  565. */
  566. public function isHTML($ishtml = true)
  567. {
  568. if ($ishtml) {
  569. $this->ContentType = 'text/html';
  570. } else {
  571. $this->ContentType = 'text/plain';
  572. }
  573. }
  574. /**
  575. * Send messages using SMTP.
  576. * @return void
  577. */
  578. public function isSMTP()
  579. {
  580. $this->Mailer = 'smtp';
  581. }
  582. /**
  583. * Send messages using PHP's mail() function.
  584. * @return void
  585. */
  586. public function isMail()
  587. {
  588. $this->Mailer = 'mail';
  589. }
  590. /**
  591. * Send messages using $Sendmail.
  592. * @return void
  593. */
  594. public function isSendmail()
  595. {
  596. if (!stristr(ini_get('sendmail_path'), 'sendmail')) {
  597. $this->Sendmail = '/usr/sbin/sendmail';
  598. }
  599. $this->Mailer = 'sendmail';
  600. }
  601. /**
  602. * Send messages using qmail.
  603. * @return void
  604. */
  605. public function isQmail()
  606. {
  607. if (!stristr(ini_get('sendmail_path'), 'qmail')) {
  608. $this->Sendmail = '/var/qmail/bin/qmail-inject';
  609. }
  610. $this->Mailer = 'qmail';
  611. }
  612. /**
  613. * Add a "To" address.
  614. * @param string $address
  615. * @param string $name
  616. * @return bool true on success, false if address already used
  617. */
  618. public function addAddress($address, $name = '')
  619. {
  620. return $this->addAnAddress('to', $address, $name);
  621. }
  622. /**
  623. * Add a "CC" address.
  624. * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
  625. * @param string $address
  626. * @param string $name
  627. * @return bool true on success, false if address already used
  628. */
  629. public function addCC($address, $name = '')
  630. {
  631. return $this->addAnAddress('cc', $address, $name);
  632. }
  633. /**
  634. * Add a "BCC" address.
  635. * @note: This function works with the SMTP mailer on win32, not with the "mail" mailer.
  636. * @param string $address
  637. * @param string $name
  638. * @return bool true on success, false if address already used
  639. */
  640. public function addBCC($address, $name = '')
  641. {
  642. return $this->addAnAddress('bcc', $address, $name);
  643. }
  644. /**
  645. * Add a "Reply-to" address.
  646. * @param string $address
  647. * @param string $name
  648. * @return bool
  649. */
  650. public function addReplyTo($address, $name = '')
  651. {
  652. return $this->addAnAddress('Reply-To', $address, $name);
  653. }
  654. /**
  655. * Add an address to one of the recipient arrays.
  656. * Addresses that have been added already return false, but do not throw exceptions
  657. * @param string $kind One of 'to', 'cc', 'bcc', 'ReplyTo'
  658. * @param string $address The email address to send to
  659. * @param string $name
  660. * @throws phpmailerException
  661. * @return bool true on success, false if address already used or invalid in some way
  662. * @access protected
  663. */
  664. protected function addAnAddress($kind, $address, $name = '')
  665. {
  666. if (!preg_match('/^(to|cc|bcc|Reply-To)$/', $kind)) {
  667. $this->setError($this->lang('Invalid recipient array') . ': ' . $kind);
  668. $this->edebug($this->lang('Invalid recipient array') . ': ' . $kind);
  669. if ($this->exceptions) {
  670. throw new phpmailerException('Invalid recipient array: ' . $kind);
  671. }
  672. return false;
  673. }
  674. $address = trim($address);
  675. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
  676. if (!$this->validateAddress($address)) {
  677. $this->setError($this->lang('invalid_address') . ': ' . $address);
  678. $this->edebug($this->lang('invalid_address') . ': ' . $address);
  679. if ($this->exceptions) {
  680. throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
  681. }
  682. return false;
  683. }
  684. if ($kind != 'Reply-To') {
  685. if (!isset($this->all_recipients[strtolower($address)])) {
  686. array_push($this->$kind, array($address, $name));
  687. $this->all_recipients[strtolower($address)] = true;
  688. return true;
  689. }
  690. } else {
  691. if (!array_key_exists(strtolower($address), $this->ReplyTo)) {
  692. $this->ReplyTo[strtolower($address)] = array($address, $name);
  693. return true;
  694. }
  695. }
  696. return false;
  697. }
  698. /**
  699. * Set the From and FromName properties.
  700. * @param string $address
  701. * @param string $name
  702. * @param bool $auto Whether to also set the Sender address, defaults to true
  703. * @throws phpmailerException
  704. * @return bool
  705. */
  706. public function setFrom($address, $name = '', $auto = true)
  707. {
  708. $address = trim($address);
  709. $name = trim(preg_replace('/[\r\n]+/', '', $name)); //Strip breaks and trim
  710. if (!$this->validateAddress($address)) {
  711. $this->setError($this->lang('invalid_address') . ': ' . $address);
  712. $this->edebug($this->lang('invalid_address') . ': ' . $address);
  713. if ($this->exceptions) {
  714. throw new phpmailerException($this->lang('invalid_address') . ': ' . $address);
  715. }
  716. return false;
  717. }
  718. $this->From = $address;
  719. $this->FromName = $name;
  720. if ($auto) {
  721. if (empty($this->Sender)) {
  722. $this->Sender = $address;
  723. }
  724. }
  725. return true;
  726. }
  727. /**
  728. * Return the Message-ID header of the last email.
  729. * Technically this is the value from the last time the headers were created,
  730. * but it's also the message ID of the last sent message except in
  731. * pathological cases.
  732. * @return string
  733. */
  734. public function getLastMessageID()
  735. {
  736. return $this->lastMessageID;
  737. }
  738. /**
  739. * Check that a string looks like an email address.
  740. * @param string $address The email address to check
  741. * @param string $patternselect A selector for the validation pattern to use :
  742. * 'auto' - pick best one automatically;
  743. * 'pcre8' - use the squiloople.com pattern, requires PCRE > 8.0, PHP >= 5.3.2, 5.2.14;
  744. * 'pcre' - use old PCRE implementation;
  745. * 'php' - use PHP built-in FILTER_VALIDATE_EMAIL; faster, less thorough;
  746. * 'noregex' - super fast, really dumb.
  747. * @return bool
  748. * @static
  749. * @access public
  750. */
  751. public static function validateAddress($address, $patternselect = 'auto')
  752. {
  753. if ($patternselect == 'auto') {
  754. if (defined(
  755. 'PCRE_VERSION'
  756. )
  757. ) { //Check this instead of extension_loaded so it works when that function is disabled
  758. if (version_compare(PCRE_VERSION, '8.0') >= 0) {
  759. $patternselect = 'pcre8';
  760. } else {
  761. $patternselect = 'pcre';
  762. }
  763. } else {
  764. //Filter_var appeared in PHP 5.2.0 and does not require the PCRE extension
  765. if (version_compare(PHP_VERSION, '5.2.0') >= 0) {
  766. $patternselect = 'php';
  767. } else {
  768. $patternselect = 'noregex';
  769. }
  770. }
  771. }
  772. switch ($patternselect) {
  773. case 'pcre8':
  774. /**
  775. * Conforms to RFC5322: Uses *correct* regex on which FILTER_VALIDATE_EMAIL is
  776. * based; So why not use FILTER_VALIDATE_EMAIL? Because it was broken to
  777. * not allow a@b type valid addresses :(
  778. * @link http://squiloople.com/2009/12/20/email-address-validation/
  779. * @copyright 2009-2010 Michael Rushton
  780. * Feel free to use and redistribute this code. But please keep this copyright notice.
  781. */
  782. return (bool)preg_match(
  783. '/^(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){255,})(?!(?>(?1)"?(?>\\\[ -~]|[^"])"?(?1)){65,}@)' .
  784. '((?>(?>(?>((?>(?>(?>\x0D\x0A)?[\t ])+|(?>[\t ]*\x0D\x0A)?[\t ]+)?)(\((?>(?2)' .
  785. '(?>[\x01-\x08\x0B\x0C\x0E-\'*-\[\]-\x7F]|\\\[\x00-\x7F]|(?3)))*(?2)\)))+(?2))|(?2))?)' .
  786. '([!#-\'*+\/-9=?^-~-]+|"(?>(?2)(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\x7F]))*' .
  787. '(?2)")(?>(?1)\.(?1)(?4))*(?1)@(?!(?1)[a-z0-9-]{64,})(?1)(?>([a-z0-9](?>[a-z0-9-]*[a-z0-9])?)' .
  788. '(?>(?1)\.(?!(?1)[a-z0-9-]{64,})(?1)(?5)){0,126}|\[(?:(?>IPv6:(?>([a-f0-9]{1,4})(?>:(?6)){7}' .
  789. '|(?!(?:.*[a-f0-9][:\]]){8,})((?6)(?>:(?6)){0,6})?::(?7)?))|(?>(?>IPv6:(?>(?6)(?>:(?6)){5}:' .
  790. '|(?!(?:.*[a-f0-9]:){6,})(?8)?::(?>((?6)(?>:(?6)){0,4}):)?))?(25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
  791. '|[1-9]?[0-9])(?>\.(?9)){3}))\])(?1)$/isD',
  792. $address
  793. );
  794. break;
  795. case 'pcre':
  796. //An older regex that doesn't need a recent PCRE
  797. return (bool)preg_match(
  798. '/^(?!(?>"?(?>\\\[ -~]|[^"])"?){255,})(?!(?>"?(?>\\\[ -~]|[^"])"?){65,}@)(?>' .
  799. '[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*")' .
  800. '(?>\.(?>[!#-\'*+\/-9=?^-~-]+|"(?>(?>[\x01-\x08\x0B\x0C\x0E-!#-\[\]-\x7F]|\\\[\x00-\xFF]))*"))*' .
  801. '@(?>(?![a-z0-9-]{64,})(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)(?>\.(?![a-z0-9-]{64,})' .
  802. '(?>[a-z0-9](?>[a-z0-9-]*[a-z0-9])?)){0,126}|\[(?:(?>IPv6:(?>(?>[a-f0-9]{1,4})(?>:' .
  803. '[a-f0-9]{1,4}){7}|(?!(?:.*[a-f0-9][:\]]){8,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?' .
  804. '::(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,6})?))|(?>(?>IPv6:(?>[a-f0-9]{1,4}(?>:' .
  805. '[a-f0-9]{1,4}){5}:|(?!(?:.*[a-f0-9]:){6,})(?>[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4})?' .
  806. '::(?>(?:[a-f0-9]{1,4}(?>:[a-f0-9]{1,4}){0,4}):)?))?(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}' .
  807. '|[1-9]?[0-9])(?>\.(?>25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}))\])$/isD',
  808. $address
  809. );
  810. break;
  811. case 'php':
  812. default:
  813. return (bool)filter_var($address, FILTER_VALIDATE_EMAIL);
  814. break;
  815. case 'noregex':
  816. //No PCRE! Do something _very_ approximate!
  817. //Check the address is 3 chars or longer and contains an @ that's not the first or last char
  818. return (strlen($address) >= 3
  819. and strpos($address, '@') >= 1
  820. and strpos($address, '@') != strlen($address) - 1);
  821. break;
  822. }
  823. }
  824. /**
  825. * Create a message and send it.
  826. * Uses the sending method specified by $Mailer.
  827. * @throws phpmailerException
  828. * @return bool false on error - See the ErrorInfo property for details of the error.
  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. case 'qmail':
  919. return $this->sendmailSend($this->MIMEHeader, $this->MIMEBody);
  920. case 'smtp':
  921. return $this->smtpSend($this->MIMEHeader, $this->MIMEBody);
  922. case 'mail':
  923. return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
  924. default:
  925. if (method_exists($this, $this->Mailer.'Send')) {
  926. $sendMethod = $this->Mailer.'Send';
  927. return $this->$sendMethod($this->MIMEHeader, $this->MIMEBody);
  928. } else {
  929. return $this->mailSend($this->MIMEHeader, $this->MIMEBody);
  930. }
  931. }
  932. } catch (phpmailerException $e) {
  933. $this->setError($e->getMessage());
  934. $this->edebug($e->getMessage());
  935. if ($this->exceptions) {
  936. throw $e;
  937. }
  938. }
  939. return false;
  940. }
  941. /**
  942. * Send mail using the $Sendmail program.
  943. * @param string $header The message headers
  944. * @param string $body The message body
  945. * @see PHPMailer::$Sendmail
  946. * @throws phpmailerException
  947. * @access protected
  948. * @return bool
  949. */
  950. protected function sendmailSend($header, $body)
  951. {
  952. if ($this->Sender != '') {
  953. if ($this->Mailer == 'qmail') {
  954. $sendmail = sprintf("%s -f%s", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  955. } else {
  956. $sendmail = sprintf("%s -oi -f%s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  957. }
  958. } else {
  959. if ($this->Mailer == 'qmail') {
  960. $sendmail = sprintf("%s", escapeshellcmd($this->Sendmail));
  961. } else {
  962. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  963. }
  964. }
  965. if ($this->SingleTo === true) {
  966. foreach ($this->SingleToArray as $val) {
  967. if (!@$mail = popen($sendmail, 'w')) {
  968. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  969. }
  970. fputs($mail, "To: " . $val . "\n");
  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, $val, $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. } else {
  982. if (!@$mail = popen($sendmail, 'w')) {
  983. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  984. }
  985. fputs($mail, $header);
  986. fputs($mail, $body);
  987. $result = pclose($mail);
  988. // implement call back function if it exists
  989. $isSent = ($result == 0) ? 1 : 0;
  990. $this->doCallback($isSent, $this->to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
  991. if ($result != 0) {
  992. throw new phpmailerException($this->lang('execute') . $this->Sendmail, self::STOP_CRITICAL);
  993. }
  994. }
  995. return true;
  996. }
  997. /**
  998. * Send mail using the PHP mail() function.
  999. * @param string $header The message headers
  1000. * @param string $body The message body
  1001. * @link http://www.php.net/manual/en/book.mail.php
  1002. * @throws phpmailerException
  1003. * @access protected
  1004. * @return bool
  1005. */
  1006. protected function mailSend($header, $body)
  1007. {
  1008. $toArr = array();
  1009. foreach ($this->to as $t) {
  1010. $toArr[] = $this->addrFormat($t);
  1011. }
  1012. $to = implode(', ', $toArr);
  1013. if (empty($this->Sender)) {
  1014. $params = " ";
  1015. } else {
  1016. $params = sprintf("-f%s", $this->Sender);
  1017. }
  1018. if ($this->Sender != '' and !ini_get('safe_mode')) {
  1019. $old_from = ini_get('sendmail_from');
  1020. ini_set('sendmail_from', $this->Sender);
  1021. }
  1022. $rt = false;
  1023. if ($this->SingleTo === true && count($toArr) > 1) {
  1024. foreach ($toArr as $val) {
  1025. $rt = $this->mailPassthru($val, $this->Subject, $body, $header, $params);
  1026. // implement call back function if it exists
  1027. $isSent = ($rt == 1) ? 1 : 0;
  1028. $this->doCallback($isSent, $val, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
  1029. }
  1030. } else {
  1031. $rt = $this->mailPassthru($to, $this->Subject, $body, $header, $params);
  1032. // implement call back function if it exists
  1033. $isSent = ($rt == 1) ? 1 : 0;
  1034. $this->doCallback($isSent, $to, $this->cc, $this->bcc, $this->Subject, $body, $this->From);
  1035. }
  1036. if (isset($old_from)) {
  1037. ini_set('sendmail_from', $old_from);
  1038. }
  1039. if (!$rt) {
  1040. throw new phpmailerException($this->lang('instantiate'), self::STOP_CRITICAL);
  1041. }
  1042. return true;
  1043. }
  1044. /**
  1045. * Get an instance to use for SMTP operations.
  1046. * Override this function to load your own SMTP implementation
  1047. * @return SMTP
  1048. */
  1049. public function getSMTPInstance()
  1050. {
  1051. if (!is_object($this->smtp)) {
  1052. $this->smtp = new SMTP;
  1053. }
  1054. return $this->smtp;
  1055. }
  1056. /**
  1057. * Send mail via SMTP.
  1058. * Returns false if there is a bad MAIL FROM, RCPT, or DATA input.
  1059. * Uses the PHPMailerSMTP class by default.
  1060. * @see PHPMailer::getSMTPInstance() to use a different class.
  1061. * @param string $header The message headers
  1062. * @param string $body The message body
  1063. * @throws phpmailerException
  1064. * @uses SMTP
  1065. * @access protected
  1066. * @return bool
  1067. */
  1068. protected function smtpSend($header, $body)
  1069. {
  1070. $bad_rcpt = array();
  1071. if (!$this->smtpConnect()) {
  1072. throw new phpmailerException($this->lang('smtp_connect_failed'), self::STOP_CRITICAL);
  1073. }
  1074. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  1075. if (!$this->smtp->mail($smtp_from)) {
  1076. $this->setError($this->lang('from_failed') . $smtp_from . ' : ' . implode(',', $this->smtp->getError()));
  1077. throw new phpmailerException($this->ErrorInfo, self::STOP_CRITICAL);
  1078. }
  1079. // Attempt to send to all recipients
  1080. foreach ($this->to as $to) {
  1081. if (!$this->smtp->recipient($to[0])) {
  1082. $bad_rcpt[] = $to[0];
  1083. $isSent = 0;
  1084. } else {
  1085. $isSent = 1;
  1086. }
  1087. $this->doCallback($isSent, $to[0], '', '', $this->Subject, $body, $this->From);
  1088. }
  1089. foreach ($this->cc as $cc) {
  1090. if (!$this->smtp->recipient($cc[0])) {
  1091. $bad_rcpt[] = $cc[0];
  1092. $isSent = 0;
  1093. } else {
  1094. $isSent = 1;
  1095. }
  1096. $this->doCallback($isSent, '', $cc[0], '', $this->Subject, $body, $this->From);
  1097. }
  1098. foreach ($this->bcc as $bcc) {
  1099. if (!$this->smtp->recipient($bcc[0])) {
  1100. $bad_rcpt[] = $bcc[0];
  1101. $isSent = 0;
  1102. } else {
  1103. $isSent = 1;
  1104. }
  1105. $this->doCallback($isSent, '', '', $bcc[0], $this->Subject, $body, $this->From);
  1106. }
  1107. //Only send the DATA command if we have viable recipients
  1108. if ((count($this->all_recipients) > count($bad_rcpt)) and !$this->smtp->data($header . $body)) {
  1109. throw new phpmailerException($this->lang('data_not_accepted'), self::STOP_CRITICAL);
  1110. }
  1111. if ($this->SMTPKeepAlive == true) {
  1112. $this->smtp->reset();
  1113. } else {
  1114. $this->smtp->quit();
  1115. $this->smtp->close();
  1116. }
  1117. if (count($bad_rcpt) > 0) { //Create error message for any bad addresses
  1118. throw new phpmailerException(
  1119. $this->lang('recipients_failed') . implode(', ', $bad_rcpt),
  1120. self::STOP_CONTINUE
  1121. );
  1122. }
  1123. return true;
  1124. }
  1125. /**
  1126. * Initiate a connection to an SMTP server.
  1127. * Returns false if the operation failed.
  1128. * @param array $options An array of options compatible with stream_context_create()
  1129. * @uses SMTP
  1130. * @access public
  1131. * @throws phpmailerException
  1132. * @return bool
  1133. */
  1134. public function smtpConnect($options = array())
  1135. {
  1136. if (is_null($this->smtp)) {
  1137. $this->smtp = $this->getSMTPInstance();
  1138. }
  1139. //Already connected?
  1140. if ($this->smtp->connected()) {
  1141. return true;
  1142. }
  1143. $this->smtp->setTimeout($this->Timeout);
  1144. $this->smtp->setDebugLevel($this->SMTPDebug);
  1145. $this->smtp->setDebugOutput($this->Debugoutput);
  1146. $this->smtp->setVerp($this->do_verp);
  1147. $hosts = explode(';', $this->Host);
  1148. $lastexception = null;
  1149. foreach ($hosts as $hostentry) {
  1150. $hostinfo = array();
  1151. if (!preg_match('/^((ssl|tls):\/\/)*([a-zA-Z0-9\.-]*):?([0-9]*)$/', trim($hostentry), $hostinfo)) {
  1152. //Not a valid host entry
  1153. continue;
  1154. }
  1155. //$hostinfo[2]: optional ssl or tls prefix
  1156. //$hostinfo[3]: the hostname
  1157. //$hostinfo[4]: optional port number
  1158. //The host string prefix can temporarily override the current setting for SMTPSecure
  1159. //If it's not specified, the default value is used
  1160. $prefix = '';
  1161. $tls = ($this->SMTPSecure == 'tls');
  1162. if ($hostinfo[2] == 'ssl' or ($hostinfo[2] == '' and $this->SMTPSecure == 'ssl')) {
  1163. $prefix = 'ssl://';
  1164. $tls = false; //Can't have SSL and TLS at once
  1165. } elseif ($hostinfo[2] == 'tls') {
  1166. $tls = true;
  1167. //tls doesn't use a prefix
  1168. }
  1169. $host = $hostinfo[3];
  1170. $port = $this->Port;
  1171. $tport = (integer)$hostinfo[4];
  1172. if ($tport > 0 and $tport < 65536) {
  1173. $port = $tport;
  1174. }
  1175. if ($this->smtp->connect($prefix . $host, $port, $this->Timeout, $options)) {
  1176. try {
  1177. if ($this->Helo) {
  1178. $hello = $this->Helo;
  1179. } else {
  1180. $hello = $this->serverHostname();
  1181. }
  1182. $this->smtp->hello($hello);
  1183. if ($tls) {
  1184. if (!$this->smtp->startTLS()) {
  1185. throw new phpmailerException($this->lang('connect_host'));
  1186. }
  1187. //We must resend HELO after tls negotiation
  1188. $this->smtp->hello($hello);
  1189. }
  1190. if ($this->SMTPAuth) {
  1191. if (!$this->smtp->authenticate(
  1192. $this->Username,
  1193. $this->Password,
  1194. $this->AuthType,
  1195. $this->Realm,
  1196. $this->Workstation
  1197. )
  1198. ) {
  1199. throw new phpmailerException($this->lang('authenticate'));
  1200. }
  1201. }
  1202. return true;
  1203. } catch (phpmailerException $e) {
  1204. $lastexception = $e;
  1205. //We must have connected, but then failed TLS or Auth, so close connection nicely
  1206. $this->smtp->quit();
  1207. }
  1208. }
  1209. }
  1210. //If we get here, all connection attempts have failed, so close connection hard
  1211. $this->smtp->close();
  1212. //As we've caught all exceptions, just report whatever the last one was
  1213. if ($this->exceptions and !is_null($lastexception)) {
  1214. throw $lastexception;
  1215. }
  1216. return false;
  1217. }
  1218. /**
  1219. * Close the active SMTP session if one exists.
  1220. * @return void
  1221. */
  1222. public function smtpClose()
  1223. {
  1224. if ($this->smtp !== null) {
  1225. if ($this->smtp->connected()) {
  1226. $this->smtp->quit();
  1227. $this->smtp->close();
  1228. }
  1229. }
  1230. }
  1231. /**
  1232. * Set the language for error messages.
  1233. * Returns false if it cannot load the language file.
  1234. * The default language is English.
  1235. * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr")
  1236. * @param string $lang_path Path to the language file directory, with trailing separator (slash)
  1237. * @return bool
  1238. * @access public
  1239. */
  1240. public function setLanguage($langcode = 'en', $lang_path = 'language/')
  1241. {
  1242. //Define full set of translatable strings
  1243. $PHPMAILER_LANG = array(
  1244. 'authenticate' => 'SMTP Error: Could not authenticate.',
  1245. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
  1246. 'data_not_accepted' => 'SMTP Error: data not accepted.',
  1247. 'empty_message' => 'Message body empty',
  1248. 'encoding' => 'Unknown encoding: ',
  1249. 'execute' => 'Could not execute: ',
  1250. 'file_access' => 'Could not access file: ',
  1251. 'file_open' => 'File Error: Could not open file: ',
  1252. 'from_failed' => 'The following From address failed: ',
  1253. 'instantiate' => 'Could not instantiate mail function.',
  1254. 'invalid_address' => 'Invalid address',
  1255. 'mailer_not_supported' => ' mailer is not supported.',
  1256. 'provide_address' => 'You must provide at least one recipient email address.',
  1257. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
  1258. 'signing' => 'Signing Error: ',
  1259. 'smtp_connect_failed' => 'SMTP connect() failed.',
  1260. 'smtp_error' => 'SMTP server error: ',
  1261. 'variable_set' => 'Cannot set or reset variable: '
  1262. );
  1263. //Overwrite language-specific strings.
  1264. //This way we'll never have missing translations - no more "language string failed to load"!
  1265. $l = true;
  1266. if ($langcode != 'en') { //There is no English translation file
  1267. $l = @include $lang_path . 'phpmailer.lang-' . $langcode . '.php';
  1268. }
  1269. $this->language = $PHPMAILER_LANG;
  1270. return ($l == true); //Returns false if language not found
  1271. }
  1272. /**
  1273. * Get the array of strings for the current language.
  1274. * @return array
  1275. */
  1276. public function getTranslations()
  1277. {
  1278. return $this->language;
  1279. }
  1280. /**
  1281. * Create recipient headers.
  1282. * @access public
  1283. * @param string $type
  1284. * @param array $addr An array of recipient,
  1285. * where each recipient is a 2-element indexed array with element 0 containing an address
  1286. * and element 1 containing a name, like:
  1287. * array(array('joe@example.com', 'Joe User'), array('zoe@example.com', 'Zoe User'))
  1288. * @return string
  1289. */
  1290. public function addrAppend($type, $addr)
  1291. {
  1292. $addresses = array();
  1293. foreach ($addr as $a) {
  1294. $addresses[] = $this->addrFormat($a);
  1295. }
  1296. return $type . ': ' . implode(', ', $addresses) . $this->LE;
  1297. }
  1298. /**
  1299. * Format an address for use in a message header.
  1300. * @access public
  1301. * @param array $addr A 2-element indexed array, element 0 containing an address, element 1 containing a name
  1302. * like array('joe@example.com', 'Joe User')
  1303. * @return string
  1304. */
  1305. public function addrFormat($addr)
  1306. {
  1307. if (empty($addr[1])) { // No name provided
  1308. return $this->secureHeader($addr[0]);
  1309. } else {
  1310. return $this->encodeHeader($this->secureHeader($addr[1]), 'phrase') . " <" . $this->secureHeader(
  1311. $addr[0]
  1312. ) . ">";
  1313. }
  1314. }
  1315. /**
  1316. * Word-wrap message.
  1317. * For use with mailers that do not automatically perform wrapping
  1318. * and for quoted-printable encoded messages.
  1319. * Original written by philippe.
  1320. * @param string $message The message to wrap
  1321. * @param integer $length The line length to wrap to
  1322. * @param bool $qp_mode Whether to run in Quoted-Printable mode
  1323. * @access public
  1324. * @return string
  1325. */
  1326. public function wrapText($message, $length, $qp_mode = false)
  1327. {
  1328. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  1329. // If utf-8 encoding is used, we will need to make sure we don't
  1330. // split multibyte characters when we wrap
  1331. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  1332. $lelen = strlen($this->LE);
  1333. $crlflen = strlen(self::CRLF);
  1334. $message = $this->fixEOL($message);
  1335. if (substr($message, -$lelen) == $this->LE) {
  1336. $message = substr($message, 0, -$lelen);
  1337. }
  1338. $line = explode($this->LE, $message); // Magic. We know fixEOL uses $LE
  1339. $message = '';
  1340. for ($i = 0; $i < count($line); $i++) {
  1341. $line_part = explode(' ', $line[$i]);
  1342. $buf = '';
  1343. for ($e = 0; $e < count($line_part); $e++) {
  1344. $word = $line_part[$e];
  1345. if ($qp_mode and (strlen($word) > $length)) {
  1346. $space_left = $length - strlen($buf) - $crlflen;
  1347. if ($e != 0) {
  1348. if ($space_left > 20) {
  1349. $len = $space_left;
  1350. if ($is_utf8) {
  1351. $len = $this->utf8CharBoundary($word, $len);
  1352. } elseif (substr($word, $len - 1, 1) == "=") {
  1353. $len--;
  1354. } elseif (substr($word, $len - 2, 1) == "=") {
  1355. $len -= 2;
  1356. }
  1357. $part = substr($word, 0, $len);
  1358. $word = substr($word, $len);
  1359. $buf .= ' ' . $part;
  1360. $message .= $buf . sprintf("=%s", self::CRLF);
  1361. } else {
  1362. $message .= $buf . $soft_break;
  1363. }
  1364. $buf = '';
  1365. }
  1366. while (strlen($word) > 0) {
  1367. if ($length <= 0) {
  1368. break;
  1369. }
  1370. $len = $length;
  1371. if ($is_utf8) {
  1372. $len = $this->utf8CharBoundary($word, $len);
  1373. } elseif (substr($word, $len - 1, 1) == "=") {
  1374. $len--;
  1375. } elseif (substr($word, $len - 2, 1) == "=") {
  1376. $len -= 2;
  1377. }
  1378. $part = substr($word, 0, $len);
  1379. $word = substr($word, $len);
  1380. if (strlen($word) > 0) {
  1381. $message .= $part . sprintf("=%s", self::CRLF);
  1382. } else {
  1383. $buf = $part;
  1384. }
  1385. }
  1386. } else {
  1387. $buf_o = $buf;
  1388. $buf .= ($e == 0) ? $word : (' ' . $word);
  1389. if (strlen($buf) > $length and $buf_o != '') {
  1390. $message .= $buf_o . $soft_break;
  1391. $buf = $word;
  1392. }
  1393. }
  1394. }
  1395. $message .= $buf . self::CRLF;
  1396. }
  1397. return $message;
  1398. }
  1399. /**
  1400. * Find the last character boundary prior to $maxLength in a utf-8
  1401. * quoted (printable) enc…

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