PageRenderTime 52ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/class-phpmailer.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 2320 lines | 1580 code | 164 blank | 576 comment | 229 complexity | b003b97cdbbb985d8b566688383118c0 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0

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

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