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

/protected/libs/PHPMailer_v5.1/class.phpmailer.php

https://bitbucket.org/graaaf/erso
PHP | 2322 lines | 1581 code | 165 blank | 576 comment | 229 complexity | 31e1d7354e4a35e90551252594aae7b3 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-3.0, LGPL-2.1, BSD-3-Clause, BSD-2-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.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@fighters.sourceforge.net |
  12. | : Marcus Bointon (coolbru) coolbru@fighters.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 = 'UTF-8';
  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. require_once $this->PluginDir . 'class.smtp.php';
  697. $this->smtp = new SMTP();
  698. }
  699. $this->smtp->do_debug = $this->SMTPDebug;
  700. $hosts = explode(';', $this->Host);
  701. $index = 0;
  702. $connection = $this->smtp->Connected();
  703. // Retry while there is no connection
  704. try {
  705. while($index < count($hosts) && !$connection) {
  706. $hostinfo = array();
  707. if (preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
  708. $host = $hostinfo[1];
  709. $port = $hostinfo[2];
  710. } else {
  711. $host = $hosts[$index];
  712. $port = $this->Port;
  713. }
  714. $tls = ($this->SMTPSecure == 'tls');
  715. $ssl = ($this->SMTPSecure == 'ssl');
  716. if ($this->smtp->Connect(($ssl ? 'ssl://':'').$host, $port, $this->Timeout)) {
  717. $hello = ($this->Helo != '' ? $this->Helo : $this->ServerHostname());
  718. $this->smtp->Hello($hello);
  719. if ($tls) {
  720. if (!$this->smtp->StartTLS()) {
  721. throw new phpmailerException($this->Lang('tls'));
  722. }
  723. //We must resend HELO after tls negotiation
  724. $this->smtp->Hello($hello);
  725. }
  726. $connection = true;
  727. if ($this->SMTPAuth) {
  728. if (!$this->smtp->Authenticate($this->Username, $this->Password)) {
  729. throw new phpmailerException($this->Lang('authenticate'));
  730. }
  731. }
  732. }
  733. $index++;
  734. if (!$connection) {
  735. throw new phpmailerException($this->Lang('connect_host'));
  736. }
  737. }
  738. } catch (phpmailerException $e) {
  739. $this->smtp->Reset();
  740. throw $e;
  741. }
  742. return true;
  743. }
  744. /**
  745. * Closes the active SMTP session if one exists.
  746. * @return void
  747. */
  748. public function SmtpClose() {
  749. if(!is_null($this->smtp)) {
  750. if($this->smtp->Connected()) {
  751. $this->smtp->Quit();
  752. $this->smtp->Close();
  753. }
  754. }
  755. }
  756. /**
  757. * Sets the language for all class error messages.
  758. * Returns false if it cannot load the language file. The default language is English.
  759. * @param string $langcode ISO 639-1 2-character language code (e.g. Portuguese: "br")
  760. * @param string $lang_path Path to the language file directory
  761. * @access public
  762. */
  763. function SetLanguage($langcode = 'en', $lang_path = 'language/') {
  764. //Define full set of translatable strings
  765. $PHPMAILER_LANG = array(
  766. 'provide_address' => 'You must provide at least one recipient email address.',
  767. 'mailer_not_supported' => ' mailer is not supported.',
  768. 'execute' => 'Could not execute: ',
  769. 'instantiate' => 'Could not instantiate mail function.',
  770. 'authenticate' => 'SMTP Error: Could not authenticate.',
  771. 'from_failed' => 'The following From address failed: ',
  772. 'recipients_failed' => 'SMTP Error: The following recipients failed: ',
  773. 'data_not_accepted' => 'SMTP Error: Data not accepted.',
  774. 'connect_host' => 'SMTP Error: Could not connect to SMTP host.',
  775. 'file_access' => 'Could not access file: ',
  776. 'file_open' => 'File Error: Could not open file: ',
  777. 'encoding' => 'Unknown encoding: ',
  778. 'signing' => 'Signing Error: ',
  779. 'smtp_error' => 'SMTP server error: ',
  780. 'empty_message' => 'Message body empty',
  781. 'invalid_address' => 'Invalid address',
  782. 'variable_set' => 'Cannot set or reset variable: '
  783. );
  784. //Overwrite language-specific strings. This way we'll never have missing translations - no more "language string failed to load"!
  785. $l = true;
  786. if ($langcode != 'en') { //There is no English translation file
  787. $l = @include $lang_path.'phpmailer.lang-'.$langcode.'.php';
  788. }
  789. $this->language = $PHPMAILER_LANG;
  790. return ($l == true); //Returns false if language not found
  791. }
  792. /**
  793. * Return the current array of language strings
  794. * @return array
  795. */
  796. public function GetTranslations() {
  797. return $this->language;
  798. }
  799. /////////////////////////////////////////////////
  800. // METHODS, MESSAGE CREATION
  801. /////////////////////////////////////////////////
  802. /**
  803. * Creates recipient headers.
  804. * @access public
  805. * @return string
  806. */
  807. public function AddrAppend($type, $addr) {
  808. $addr_str = $type . ': ';
  809. $addresses = array();
  810. foreach ($addr as $a) {
  811. $addresses[] = $this->AddrFormat($a);
  812. }
  813. $addr_str .= implode(', ', $addresses);
  814. $addr_str .= $this->LE;
  815. return $addr_str;
  816. }
  817. /**
  818. * Formats an address correctly.
  819. * @access public
  820. * @return string
  821. */
  822. public function AddrFormat($addr) {
  823. if (empty($addr[1])) {
  824. return $this->SecureHeader($addr[0]);
  825. } else {
  826. return $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  827. }
  828. }
  829. /**
  830. * Wraps message for use with mailers that do not
  831. * automatically perform wrapping and for quoted-printable.
  832. * Original written by philippe.
  833. * @param string $message The message to wrap
  834. * @param integer $length The line length to wrap to
  835. * @param boolean $qp_mode Whether to run in Quoted-Printable mode
  836. * @access public
  837. * @return string
  838. */
  839. public function WrapText($message, $length, $qp_mode = false) {
  840. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  841. // If utf-8 encoding is used, we will need to make sure we don't
  842. // split multibyte characters when we wrap
  843. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  844. $message = $this->FixEOL($message);
  845. if (substr($message, -1) == $this->LE) {
  846. $message = substr($message, 0, -1);
  847. }
  848. $line = explode($this->LE, $message);
  849. $message = '';
  850. for ($i=0 ;$i < count($line); $i++) {
  851. $line_part = explode(' ', $line[$i]);
  852. $buf = '';
  853. for ($e = 0; $e<count($line_part); $e++) {
  854. $word = $line_part[$e];
  855. if ($qp_mode and (strlen($word) > $length)) {
  856. $space_left = $length - strlen($buf) - 1;
  857. if ($e != 0) {
  858. if ($space_left > 20) {
  859. $len = $space_left;
  860. if ($is_utf8) {
  861. $len = $this->UTF8CharBoundary($word, $len);
  862. } elseif (substr($word, $len - 1, 1) == "=") {
  863. $len--;
  864. } elseif (substr($word, $len - 2, 1) == "=") {
  865. $len -= 2;
  866. }
  867. $part = substr($word, 0, $len);
  868. $word = substr($word, $len);
  869. $buf .= ' ' . $part;
  870. $message .= $buf . sprintf("=%s", $this->LE);
  871. } else {
  872. $message .= $buf . $soft_break;
  873. }
  874. $buf = '';
  875. }
  876. while (strlen($word) > 0) {
  877. $len = $length;
  878. if ($is_utf8) {
  879. $len = $this->UTF8CharBoundary($word, $len);
  880. } elseif (substr($word, $len - 1, 1) == "=") {
  881. $len--;
  882. } elseif (substr($word, $len - 2, 1) == "=") {
  883. $len -= 2;
  884. }
  885. $part = substr($word, 0, $len);
  886. $word = substr($word, $len);
  887. if (strlen($word) > 0) {
  888. $message .= $part . sprintf("=%s", $this->LE);
  889. } else {
  890. $buf = $part;
  891. }
  892. }
  893. } else {
  894. $buf_o = $buf;
  895. $buf .= ($e == 0) ? $word : (' ' . $word);
  896. if (strlen($buf) > $length and $buf_o != '') {
  897. $message .= $buf_o . $soft_break;
  898. $buf = $word;
  899. }
  900. }
  901. }
  902. $message .= $buf . $this->LE;
  903. }
  904. return $message;
  905. }
  906. /**
  907. * Finds last character boundary prior to maxLength in a utf-8
  908. * quoted (printable) encoded string.
  909. * Original written by Colin Brown.
  910. * @access public
  911. * @param string $encodedText utf-8 QP text
  912. * @param int $maxLength find last character boundary prior to this length
  913. * @return int
  914. */
  915. public function UTF8CharBoundary($encodedText, $maxLength) {
  916. $foundSplitPos = false;
  917. $lookBack = 3;
  918. while (!$foundSplitPos) {
  919. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  920. $encodedCharPos = strpos($lastChunk, "=");
  921. if ($encodedCharPos !== false) {
  922. // Found start of encoded character byte within $lookBack block.
  923. // Check the encoded byte value (the 2 chars after the '=')
  924. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  925. $dec = hexdec($hex);
  926. if ($dec < 128) { // Single byte character.
  927. // If the encoded char was found at pos 0, it will fit
  928. // otherwise reduce maxLength to start of the encoded char
  929. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  930. $maxLength - ($lookBack - $encodedCharPos);
  931. $foundSplitPos = true;
  932. } elseif ($dec >= 192) { // First byte of a multi byte character
  933. // Reduce maxLength to split at start of character
  934. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  935. $foundSplitPos = true;
  936. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  937. $lookBack += 3;
  938. }
  939. } else {
  940. // No encoded character found
  941. $foundSplitPos = true;
  942. }
  943. }
  944. return $maxLength;
  945. }
  946. /**
  947. * Set the body wrapping.
  948. * @access public
  949. * @return void
  950. */
  951. public function SetWordWrap() {
  952. if($this->WordWrap < 1) {
  953. return;
  954. }
  955. switch($this->message_type) {
  956. case 'alt':
  957. case 'alt_attachments':
  958. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  959. break;
  960. default:
  961. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  962. break;
  963. }
  964. }
  965. /**
  966. * Assembles message header.
  967. * @access public
  968. * @return string The assembled header
  969. */
  970. public function CreateHeader() {
  971. $result = '';
  972. // Set the boundaries
  973. $uniq_id = md5(uniqid(time()));
  974. $this->boundary[1] = 'b1_' . $uniq_id;
  975. $this->boundary[2] = 'b2_' . $uniq_id;
  976. $result .= $this->HeaderLine('Date', self::RFCDate());
  977. if($this->Sender == '') {
  978. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  979. } else {
  980. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  981. }
  982. // To be created automatically by mail()
  983. if($this->Mailer != 'mail') {
  984. if ($this->SingleTo === true) {
  985. foreach($this->to as $t) {
  986. $this->SingleToArray[] = $this->AddrFormat($t);
  987. }
  988. } else {
  989. if(count($this->to) > 0) {
  990. $result .= $this->AddrAppend('To', $this->to);
  991. } elseif (count($this->cc) == 0) {
  992. $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
  993. }
  994. }
  995. }
  996. $from = array();
  997. $from[0][0] = trim($this->From);
  998. $from[0][1] = $this->FromName;
  999. $result .= $this->AddrAppend('From', $from);
  1000. // sendmail and mail() extract Cc from the header before sending
  1001. if(count($this->cc) > 0) {
  1002. $result .= $this->AddrAppend('Cc', $this->cc);
  1003. }
  1004. // sendmail and mail() extract Bcc from the header before sending
  1005. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  1006. $result .= $this->AddrAppend('Bcc', $this->bcc);
  1007. }
  1008. if(count($this->ReplyTo) > 0) {
  1009. $result .= $this->AddrAppend('Reply-to', $this->ReplyTo);
  1010. }
  1011. // mail() sets the subject itself
  1012. if($this->Mailer != 'mail') {
  1013. $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
  1014. }
  1015. if($this->MessageID != '') {
  1016. $result .= $this->HeaderLine('Message-ID',$this->MessageID);
  1017. } else {
  1018. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  1019. }
  1020. $result .= $this->HeaderLine('X-Priority', $this->Priority);
  1021. $result .= $this->HeaderLine('X-Mailer', 'PHPMailer '.$this->Version.' (phpmailer.sourceforge.net)');
  1022. if($this->ConfirmReadingTo != '') {
  1023. $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
  1024. }
  1025. // Add custom headers
  1026. for($index = 0; $index < count($this->CustomHeader); $index++) {
  1027. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  1028. }
  1029. if (!$this->sign_key_file) {
  1030. $result .= $this->HeaderLine('MIME-Version', '1.0');
  1031. $result .= $this->GetMailMIME();
  1032. }
  1033. return $result;
  1034. }
  1035. /**
  1036. * Returns the message MIME.
  1037. * @access public
  1038. * @return string
  1039. */
  1040. public function GetMailMIME() {
  1041. $result = '';
  1042. switch($this->message_type) {
  1043. case 'plain':
  1044. $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
  1045. $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
  1046. break;
  1047. case 'attachments':
  1048. case 'alt_attachments':
  1049. if($this->InlineImageExists()){
  1050. $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
  1051. } else {
  1052. $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
  1053. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1054. }
  1055. break;
  1056. case 'alt':
  1057. $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  1058. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  1059. break;
  1060. }
  1061. if($this->Mailer != 'mail') {
  1062. $result .= $this->LE.$this->LE;
  1063. }
  1064. return $result;
  1065. }
  1066. /**
  1067. * Assembles the message body. Returns an empty string on failure.
  1068. * @access public
  1069. * @return string The assembled message body
  1070. */
  1071. public function CreateBody() {
  1072. $body = '';
  1073. if ($this->sign_key_file) {
  1074. $body .= $this->GetMailMIME();
  1075. }
  1076. $this->SetWordWrap();
  1077. switch($this->message_type) {
  1078. case 'alt':
  1079. $body .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  1080. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1081. $body .= $this->LE.$this->LE;
  1082. $body .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
  1083. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1084. $body .= $this->LE.$this->LE;
  1085. $body .= $this->EndBoundary($this->boundary[1]);
  1086. break;
  1087. case 'plain':
  1088. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1089. break;
  1090. case 'attachments':
  1091. $body .= $this->GetBoundary($this->boundary[1], '', '', '');
  1092. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1093. $body .= $this->LE;
  1094. $body .= $this->AttachAll();
  1095. break;
  1096. case 'alt_attachments':
  1097. $body .= sprintf("--%s%s", $this->boundary[1], $this->LE);
  1098. $body .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
  1099. $body .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
  1100. $body .= $this->EncodeString($this->AltBody, $this->Encoding);
  1101. $body .= $this->LE.$this->LE;
  1102. $body .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
  1103. $body .= $this->EncodeString($this->Body, $this->Encoding);
  1104. $body .= $this->LE.$this->LE;
  1105. $body .= $this->EndBoundary($this->boundary[2]);
  1106. $body .= $this->AttachAll();
  1107. break;
  1108. }
  1109. if ($this->IsError()) {
  1110. $body = '';
  1111. } elseif ($this->sign_key_file) {
  1112. try {
  1113. $file = tempnam('', 'mail');
  1114. file_put_contents($file, $body); //TODO check this worked
  1115. $signed = tempnam("", "signed");
  1116. if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_cert_file, array("file://".$this->sign_key_file, $this->sign_key_pass), NULL)) {
  1117. @unlink($file);
  1118. @unlink($signed);
  1119. $body = file_get_contents($signed);
  1120. } else {
  1121. @unlink($file);
  1122. @unlink($signed);
  1123. throw new phpmailerException($this->Lang("signing").openssl_error_string());
  1124. }
  1125. } catch (phpmailerException $e) {
  1126. $body = '';
  1127. if ($this->exceptions) {
  1128. throw $e;
  1129. }
  1130. }
  1131. }
  1132. return $body;
  1133. }
  1134. /**
  1135. * Returns the start of a message boundary.
  1136. * @access private
  1137. */
  1138. private function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  1139. $result = '';
  1140. if($charSet == '') {
  1141. $charSet = $this->CharSet;
  1142. }
  1143. if($contentType == '') {
  1144. $contentType = $this->ContentType;
  1145. }
  1146. if($encoding == '') {
  1147. $encoding = $this->Encoding;
  1148. }
  1149. $result .= $this->TextLine('--' . $boundary);
  1150. $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
  1151. $result .= $this->LE;
  1152. $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
  1153. $result .= $this->LE;
  1154. return $result;
  1155. }
  1156. /**
  1157. * Returns the end of a message boundary.
  1158. * @access private
  1159. */
  1160. private function EndBoundary($boundary) {
  1161. return $this->LE . '--' . $boundary . '--' . $this->LE;
  1162. }
  1163. /**
  1164. * Sets the message type.
  1165. * @access private
  1166. * @return void
  1167. */
  1168. private function SetMessageType() {
  1169. if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
  1170. $this->message_type = 'plain';
  1171. } else {
  1172. if(count($this->attachment) > 0) {
  1173. $this->message_type = 'attachments';
  1174. }
  1175. if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
  1176. $this->message_type = 'alt';
  1177. }
  1178. if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
  1179. $this->message_type = 'alt_attachments';
  1180. }
  1181. }
  1182. }
  1183. /**
  1184. * Returns a formatted header line.
  1185. * @access public
  1186. * @return string
  1187. */
  1188. public function HeaderLine($name, $value) {
  1189. return $name . ': ' . $value . $this->LE;
  1190. }
  1191. /**
  1192. * Returns a formatted mail line.
  1193. * @access public
  1194. * @return string
  1195. */
  1196. public function TextLine($value) {
  1197. return $value . $this->LE;
  1198. }
  1199. /////////////////////////////////////////////////
  1200. // CLASS METHODS, ATTACHMENTS
  1201. /////////////////////////////////////////////////
  1202. /**
  1203. * Adds an attachment from a path on the filesystem.
  1204. * Returns false if the file could not be found
  1205. * or accessed.
  1206. * @param string $path Path to the attachment.
  1207. * @param string $name Overrides the attachment name.
  1208. * @param string $encoding File encoding (see $Encoding).
  1209. * @param string $type File extension (MIME) type.
  1210. * @return bool
  1211. */
  1212. public function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1213. try {
  1214. if ( !@is_file($path) ) {
  1215. throw new phpmailerException($this->Lang('file_access') . $path, self::STOP_CONTINUE);
  1216. }
  1217. $filename = basename($path);
  1218. if ( $name == '' ) {
  1219. $name = $filename;
  1220. }
  1221. $this->attachment[] = array(
  1222. 0 => $path,
  1223. 1 => $filename,
  1224. 2 => $name,
  1225. 3 => $encoding,
  1226. 4 => $type,
  1227. 5 => false, // isStringAttachment
  1228. 6 => 'attachment',
  1229. 7 => 0
  1230. );
  1231. } catch (phpmailerException $e) {
  1232. $this->SetError($e->getMessage());
  1233. if ($this->exceptions) {
  1234. throw $e;
  1235. }
  1236. echo $e->getMessage()."\n";
  1237. if ( $e->getCode() == self::STOP_CRITICAL ) {
  1238. return false;
  1239. }
  1240. }
  1241. return true;
  1242. }
  1243. /**
  1244. * Return the current array of attachments
  1245. * @return array
  1246. */
  1247. public function GetAttachments() {
  1248. return $this->attachment;
  1249. }
  1250. /**
  1251. * Attaches all fs, string, and binary attachments to the message.
  1252. * Returns an empty string on failure.
  1253. * @access private
  1254. * @return string
  1255. */
  1256. private function AttachAll() {
  1257. // Return text of body
  1258. $mime = array();
  1259. $cidUniq = array();
  1260. $incl = array();
  1261. // Add all attachments
  1262. foreach ($this->attachment as $attachment) {
  1263. // Check for string attachment
  1264. $bString = $attachment[5];
  1265. if ($bString) {
  1266. $string = $attachment[0];
  1267. } else {
  1268. $path = $attachment[0];
  1269. }
  1270. if (in_array($attachment[0], $incl)) { continue; }
  1271. $filename = $attachment[1];
  1272. $name = $attachment[2];
  1273. $encoding = $attachment[3];
  1274. $type = $attachment[4];
  1275. $disposition = $attachment[6];
  1276. $cid = $attachment[7];
  1277. $incl[] = $attachment[0];
  1278. if ( $disposition == 'inline' && isset($cidUniq[$cid]) ) { continue; }
  1279. $cidUniq[$cid] = true;
  1280. $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
  1281. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $this->EncodeHeader($this->SecureHeader($name)), $this->LE);
  1282. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  1283. if($disposition == 'inline') {
  1284. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  1285. }
  1286. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $this->EncodeHeader($this->SecureHeader($name)), $this->LE.$this->LE);
  1287. // Encode as string attachment
  1288. if($bString) {
  1289. $mime[] = $this->EncodeString($string, $encoding);
  1290. if($this->IsError()) {
  1291. return '';
  1292. }
  1293. $mime[] = $this->LE.$this->LE;
  1294. } else {
  1295. $mime[] = $this->EncodeFile($path, $encoding);
  1296. if($this->IsError()) {
  1297. return '';
  1298. }
  1299. $mime[] = $this->LE.$this->LE;
  1300. }
  1301. }
  1302. $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
  1303. return join('', $mime);
  1304. }
  1305. /**
  1306. * Encodes attachment in requested format.
  1307. * Returns an empty string on failure.
  1308. * @param string $path The full path to the file
  1309. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
  1310. * @see EncodeFile()
  1311. * @access private
  1312. * @return string
  1313. */
  1314. private function EncodeFile($path, $encoding = 'base64') {
  1315. try {
  1316. if (!is_readable($path)) {
  1317. throw new phpmailerException($this->Lang('file_open') . $path, self::STOP_CONTINUE);
  1318. }
  1319. if (function_exists('get_magic_quotes')) {
  1320. function get_magic_quotes() {
  1321. return false;
  1322. }
  1323. }
  1324. if (PHP_VERSION < 6) {
  1325. $magic_quotes = get_magic_quotes_runtime();
  1326. set_magic_quotes_runtime(0);
  1327. }
  1328. $file_buffer = file_get_contents($path);
  1329. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  1330. if (PHP_VERSION < 6) { set_magic_quotes_runtime($magic_quotes); }
  1331. return $file_buffer;
  1332. } catch (Exception $e) {
  1333. $this->SetError($e->getMessage());
  1334. return '';
  1335. }
  1336. }
  1337. /**
  1338. * Encodes string to requested format.
  1339. * Returns an empty string on failure.
  1340. * @param string $str The text to encode
  1341. * @param string $encoding The encoding to use; one of 'base64', '7bit', '8bit', 'binary', 'quoted-printable'
  1342. * @access public
  1343. * @return string
  1344. */
  1345. public function EncodeString ($str, $encoding = 'base64') {
  1346. $encoded = '';
  1347. switch(strtolower($encoding)) {
  1348. case 'base64':
  1349. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  1350. break;
  1351. case '7bit':
  1352. case '8bit':
  1353. $encoded = $this->FixEOL($str);
  1354. //Make sure it ends with a line break
  1355. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1356. $encoded .= $this->LE;
  1357. break;
  1358. case 'binary':
  1359. $encoded = $str;
  1360. break;
  1361. case 'quoted-printable':
  1362. $encoded = $this->EncodeQP($str);
  1363. break;
  1364. default:
  1365. $this->SetError($this->Lang('encoding') . $encoding);
  1366. break;
  1367. }
  1368. return $encoded;
  1369. }
  1370. /**
  1371. * Encode a header string to best (shortest) of Q, B, quoted or none.
  1372. * @access public
  1373. * @return string
  1374. */
  1375. public function EncodeHeader($str, $position = 'text') {
  1376. $x = 0;
  1377. switch (strtolower($position)) {
  1378. case 'phrase':
  1379. if (!preg_match('/[\200-\377]/', $str)) {
  1380. // Can't use addslashes as we don't know what value has magic_quotes_sybase
  1381. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1382. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1383. return ($encoded);
  1384. } else {
  1385. return ("\"$encoded\"");
  1386. }
  1387. }
  1388. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1389. break;
  1390. case 'comment':
  1391. $x = preg_match_all('/[()"]/', $str, $matches);
  1392. // Fall-through
  1393. case 'text':
  1394. default:
  1395. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1396. break;
  1397. }
  1398. if ($x == 0) {
  1399. return ($str);
  1400. }
  1401. $maxlen = 75 - 7 - strlen($this->CharSet);
  1402. // Try to select the encoding which should produce the shortest output
  1403. if (strlen($str)/3 < $x) {
  1404. $encoding = 'B';
  1405. if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
  1406. // Use a custom function which correctly encodes and wraps long
  1407. // multibyte strings without breaking lines within a character
  1408. $encoded = $this->Base64EncodeWrapMB($str);
  1409. } else {
  1410. $encoded = base64_encode($str);
  1411. $maxlen -= $maxlen % 4;
  1412. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1413. }
  1414. } el

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