PageRenderTime 56ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/class.phpmailer.php

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

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