PageRenderTime 86ms CodeModel.GetById 51ms RepoModel.GetById 0ms app.codeStats 1ms

/class.phpmailer.php

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

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