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

/vendor/PHPMailer/class.phpmailer.php

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

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