PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/protected/extensions/smtpmail/PHPMailer.php

https://bitbucket.org/y_widyatama/ijepa2
PHP | 2828 lines | 1651 code | 215 blank | 962 comment | 290 complexity | 88810409382c692e4e57a9a5cdc272e6 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause

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

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