class.phpmailer.php 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896
  1. <?php
  2. /*~ class.phpmailer.php
  3. .---------------------------------------------------------------------------.
  4. | Software: PHPMailer - PHP email class |
  5. | Version: 2.0.2 |
  6. | Contact: via sourceforge.net support pages (also www.codeworxtech.com) |
  7. | Info: http://phpmailer.sourceforge.net |
  8. | Support: http://sourceforge.net/projects/phpmailer/ |
  9. | ------------------------------------------------------------------------- |
  10. | Author: Andy Prevost (project admininistrator) |
  11. | Author: Brent R. Matzelle (original founder) |
  12. | Copyright (c) 2004-2007, Andy Prevost. All Rights Reserved. |
  13. | Copyright (c) 2001-2003, Brent R. Matzelle |
  14. | ------------------------------------------------------------------------- |
  15. | License: Distributed under the Lesser General Public License (LGPL) |
  16. | http://www.gnu.org/copyleft/lesser.html |
  17. | This program is distributed in the hope that it will be useful - WITHOUT |
  18. | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
  19. | FITNESS FOR A PARTICULAR PURPOSE. |
  20. | ------------------------------------------------------------------------- |
  21. | We offer a number of paid services (www.codeworxtech.com): |
  22. | - Web Hosting on highly optimized fast and secure servers |
  23. | - Technology Consulting |
  24. | - Oursourcing (highly qualified programmers and graphic designers) |
  25. '---------------------------------------------------------------------------'
  26. /**
  27. * PHPMailer - PHP email transport class
  28. * @package PHPMailer
  29. * @author Andy Prevost
  30. * @copyright 2004 - 2008 Andy Prevost
  31. */
  32. class PHPMailer {
  33. /////////////////////////////////////////////////
  34. // PROPERTIES, PUBLIC
  35. /////////////////////////////////////////////////
  36. /**
  37. * Email priority (1 = High, 3 = Normal, 5 = low).
  38. * @var int
  39. */
  40. var $Priority = 3;
  41. /**
  42. * Sets the CharSet of the message.
  43. * @var string
  44. */
  45. var $CharSet = 'iso-8859-1';
  46. /**
  47. * Sets the Content-type of the message.
  48. * @var string
  49. */
  50. var $ContentType = 'text/plain';
  51. /**
  52. * Sets the Encoding of the message. Options for this are "8bit",
  53. * "7bit", "binary", "base64", and "quoted-printable".
  54. * @var string
  55. */
  56. var $Encoding = '8bit';
  57. /**
  58. * Holds the most recent mailer error message.
  59. * @var string
  60. */
  61. var $ErrorInfo = '';
  62. /**
  63. * Sets the From email address for the message.
  64. * @var string
  65. */
  66. var $From = 'root@localhost';
  67. /**
  68. * Sets the From name of the message.
  69. * @var string
  70. */
  71. var $FromName = 'Root User';
  72. /**
  73. * Sets the Sender email (Return-Path) of the message. If not empty,
  74. * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode.
  75. * @var string
  76. */
  77. var $Sender = '';
  78. /**
  79. * Sets the Subject of the message.
  80. * @var string
  81. */
  82. var $Subject = '';
  83. /**
  84. * Sets the Body of the message. This can be either an HTML or text body.
  85. * If HTML then run IsHTML(true).
  86. * @var string
  87. */
  88. var $Body = '';
  89. /**
  90. * Sets the text-only body of the message. This automatically sets the
  91. * email to multipart/alternative. This body can be read by mail
  92. * clients that do not have HTML email capability such as mutt. Clients
  93. * that can read HTML will view the normal Body.
  94. * @var string
  95. */
  96. var $AltBody = '';
  97. /**
  98. * Sets word wrapping on the body of the message to a given number of
  99. * characters.
  100. * @var int
  101. */
  102. var $WordWrap = 0;
  103. /**
  104. * Method to send mail: ("mail", "sendmail", or "smtp").
  105. * @var string
  106. */
  107. var $Mailer = 'mail';
  108. /**
  109. * Sets the path of the sendmail program.
  110. * @var string
  111. */
  112. var $Sendmail = '/usr/sbin/sendmail';
  113. /**
  114. * Path to PHPMailer plugins. This is now only useful if the SMTP class
  115. * is in a different directory than the PHP include path.
  116. * @var string
  117. */
  118. var $PluginDir = '';
  119. /**
  120. * Holds PHPMailer version.
  121. * @var string
  122. */
  123. var $Version = "2.0.2";
  124. /**
  125. * Sets the email address that a reading confirmation will be sent.
  126. * @var string
  127. */
  128. var $ConfirmReadingTo = '';
  129. /**
  130. * Sets the hostname to use in Message-Id and Received headers
  131. * and as default HELO string. If empty, the value returned
  132. * by SERVER_NAME is used or 'localhost.localdomain'.
  133. * @var string
  134. */
  135. var $Hostname = '';
  136. /**
  137. * Sets the message ID to be used in the Message-Id header.
  138. * If empty, a unique id will be generated.
  139. * @var string
  140. */
  141. var $MessageID = '';
  142. /////////////////////////////////////////////////
  143. // PROPERTIES FOR SMTP
  144. /////////////////////////////////////////////////
  145. /**
  146. * Sets the SMTP hosts. All hosts must be separated by a
  147. * semicolon. You can also specify a different port
  148. * for each host by using this format: [hostname:port]
  149. * (e.g. "smtp1.example.com:25;smtp2.example.com").
  150. * Hosts will be tried in order.
  151. * @var string
  152. */
  153. var $Host = 'localhost';
  154. /**
  155. * Sets the default SMTP server port.
  156. * @var int
  157. */
  158. var $Port = 25;
  159. /**
  160. * Sets the SMTP HELO of the message (Default is $Hostname).
  161. * @var string
  162. */
  163. var $Helo = '';
  164. /**
  165. * Sets connection prefix.
  166. * Options are "", "ssl" or "tls"
  167. * @var string
  168. */
  169. var $SMTPSecure = "";
  170. /**
  171. * Sets SMTP authentication. Utilizes the Username and Password variables.
  172. * @var bool
  173. */
  174. var $SMTPAuth = false;
  175. /**
  176. * Sets SMTP username.
  177. * @var string
  178. */
  179. var $Username = '';
  180. /**
  181. * Sets SMTP password.
  182. * @var string
  183. */
  184. var $Password = '';
  185. /**
  186. * Sets the SMTP server timeout in seconds. This function will not
  187. * work with the win32 version.
  188. * @var int
  189. */
  190. var $Timeout = 10;
  191. /**
  192. * Sets SMTP class debugging on or off.
  193. * @var bool
  194. */
  195. var $SMTPDebug = false;
  196. /**
  197. * Prevents the SMTP connection from being closed after each mail
  198. * sending. If this is set to true then to close the connection
  199. * requires an explicit call to SmtpClose().
  200. * @var bool
  201. */
  202. var $SMTPKeepAlive = false;
  203. /**
  204. * Provides the ability to have the TO field process individual
  205. * emails, instead of sending to entire TO addresses
  206. * @var bool
  207. */
  208. var $SingleTo = false;
  209. /////////////////////////////////////////////////
  210. // PROPERTIES, PRIVATE
  211. /////////////////////////////////////////////////
  212. var $smtp = NULL;
  213. var $to = array();
  214. var $cc = array();
  215. var $bcc = array();
  216. var $ReplyTo = array();
  217. var $attachment = array();
  218. var $CustomHeader = array();
  219. var $message_type = '';
  220. var $boundary = array();
  221. var $language = array();
  222. var $error_count = 0;
  223. var $LE = "\n";
  224. var $sign_key_file = "";
  225. var $sign_key_pass = "";
  226. /////////////////////////////////////////////////
  227. // METHODS, VARIABLES
  228. /////////////////////////////////////////////////
  229. /**
  230. * Sets message type to HTML.
  231. * @param bool $bool
  232. * @return void
  233. */
  234. function IsHTML($bool) {
  235. if($bool == true) {
  236. $this->ContentType = 'text/html';
  237. } else {
  238. $this->ContentType = 'text/plain';
  239. }
  240. }
  241. /**
  242. * Sets Mailer to send message using SMTP.
  243. * @return void
  244. */
  245. function IsSMTP() {
  246. $this->Mailer = 'smtp';
  247. }
  248. /**
  249. * Sets Mailer to send message using PHP mail() function.
  250. * @return void
  251. */
  252. function IsMail() {
  253. $this->Mailer = 'mail';
  254. }
  255. /**
  256. * Sets Mailer to send message using the $Sendmail program.
  257. * @return void
  258. */
  259. function IsSendmail() {
  260. $this->Mailer = 'sendmail';
  261. }
  262. /**
  263. * Sets Mailer to send message using the qmail MTA.
  264. * @return void
  265. */
  266. function IsQmail() {
  267. $this->Sendmail = '/var/qmail/bin/sendmail';
  268. $this->Mailer = 'sendmail';
  269. }
  270. /////////////////////////////////////////////////
  271. // METHODS, RECIPIENTS
  272. /////////////////////////////////////////////////
  273. /**
  274. * Adds a "To" address.
  275. * @param string $address
  276. * @param string $name
  277. * @return void
  278. */
  279. function AddAddress($address, $name = '') {
  280. $cur = count($this->to);
  281. $this->to[$cur][0] = trim($address);
  282. $this->to[$cur][1] = $name;
  283. }
  284. /**
  285. * Adds a "Cc" address. Note: this function works
  286. * with the SMTP mailer on win32, not with the "mail"
  287. * mailer.
  288. * @param string $address
  289. * @param string $name
  290. * @return void
  291. */
  292. function AddCC($address, $name = '') {
  293. $cur = count($this->cc);
  294. $this->cc[$cur][0] = trim($address);
  295. $this->cc[$cur][1] = $name;
  296. }
  297. /**
  298. * Adds a "Bcc" address. Note: this function works
  299. * with the SMTP mailer on win32, not with the "mail"
  300. * mailer.
  301. * @param string $address
  302. * @param string $name
  303. * @return void
  304. */
  305. function AddBCC($address, $name = '') {
  306. $cur = count($this->bcc);
  307. $this->bcc[$cur][0] = trim($address);
  308. $this->bcc[$cur][1] = $name;
  309. }
  310. /**
  311. * Adds a "Reply-To" address.
  312. * @param string $address
  313. * @param string $name
  314. * @return void
  315. */
  316. function AddReplyTo($address, $name = '') {
  317. $cur = count($this->ReplyTo);
  318. $this->ReplyTo[$cur][0] = trim($address);
  319. $this->ReplyTo[$cur][1] = $name;
  320. }
  321. /////////////////////////////////////////////////
  322. // METHODS, MAIL SENDING
  323. /////////////////////////////////////////////////
  324. /**
  325. * Creates message and assigns Mailer. If the message is
  326. * not sent successfully then it returns false. Use the ErrorInfo
  327. * variable to view description of the error.
  328. * @return bool
  329. */
  330. function Send() {
  331. $header = '';
  332. $body = '';
  333. $result = true;
  334. if((count($this->to) + count($this->cc) + count($this->bcc)) < 1) {
  335. $this->SetError($this->Lang('provide_address'));
  336. return false;
  337. }
  338. /* Set whether the message is multipart/alternative */
  339. if(!empty($this->AltBody)) {
  340. $this->ContentType = 'multipart/alternative';
  341. }
  342. $this->error_count = 0; // reset errors
  343. $this->SetMessageType();
  344. $header .= $this->CreateHeader();
  345. $body = $this->CreateBody();
  346. if($body == '') {
  347. return false;
  348. }
  349. /* Choose the mailer */
  350. switch($this->Mailer) {
  351. case 'sendmail':
  352. $result = $this->SendmailSend($header, $body);
  353. break;
  354. case 'smtp':
  355. $result = $this->SmtpSend($header, $body);
  356. break;
  357. case 'mail':
  358. $result = $this->MailSend($header, $body);
  359. break;
  360. default:
  361. $result = $this->MailSend($header, $body);
  362. break;
  363. //$this->SetError($this->Mailer . $this->Lang('mailer_not_supported'));
  364. //$result = false;
  365. //break;
  366. }
  367. return $result;
  368. }
  369. /**
  370. * Sends mail using the $Sendmail program.
  371. * @access private
  372. * @return bool
  373. */
  374. function SendmailSend($header, $body) {
  375. if ($this->Sender != '') {
  376. $sendmail = sprintf("%s -oi -f %s -t", escapeshellcmd($this->Sendmail), escapeshellarg($this->Sender));
  377. } else {
  378. $sendmail = sprintf("%s -oi -t", escapeshellcmd($this->Sendmail));
  379. }
  380. if(!@$mail = popen($sendmail, 'w')) {
  381. $this->SetError($this->Lang('execute') . $this->Sendmail);
  382. return false;
  383. }
  384. fputs($mail, $header);
  385. fputs($mail, $body);
  386. $result = pclose($mail);
  387. if (version_compare(phpversion(), '4.2.3') == -1) {
  388. $result = $result >> 8 & 0xFF;
  389. }
  390. if($result != 0) {
  391. $this->SetError($this->Lang('execute') . $this->Sendmail);
  392. return false;
  393. }
  394. return true;
  395. }
  396. /**
  397. * Sends mail using the PHP mail() function.
  398. * @access private
  399. * @return bool
  400. */
  401. function MailSend($header, $body) {
  402. $to = '';
  403. for($i = 0; $i < count($this->to); $i++) {
  404. if($i != 0) { $to .= ', '; }
  405. $to .= $this->AddrFormat($this->to[$i]);
  406. }
  407. $toArr = split(',', $to);
  408. $params = sprintf("-oi -f %s", $this->Sender);
  409. if ($this->Sender != '' && strlen(ini_get('safe_mode')) < 1) {
  410. $old_from = ini_get('sendmail_from');
  411. ini_set('sendmail_from', $this->Sender);
  412. if ($this->SingleTo === true && count($toArr) > 1) {
  413. foreach ($toArr as $key => $val) {
  414. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  415. }
  416. } else {
  417. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  418. }
  419. } else {
  420. if ($this->SingleTo === true && count($toArr) > 1) {
  421. foreach ($toArr as $key => $val) {
  422. $rt = @mail($val, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header, $params);
  423. }
  424. } else {
  425. $rt = @mail($to, $this->EncodeHeader($this->SecureHeader($this->Subject)), $body, $header);
  426. }
  427. }
  428. if (isset($old_from)) {
  429. ini_set('sendmail_from', $old_from);
  430. }
  431. if(!$rt) {
  432. $this->SetError($this->Lang('instantiate'));
  433. return false;
  434. }
  435. return true;
  436. }
  437. /**
  438. * Sends mail via SMTP using PhpSMTP (Author:
  439. * Chris Ryan). Returns bool. Returns false if there is a
  440. * bad MAIL FROM, RCPT, or DATA input.
  441. * @access private
  442. * @return bool
  443. */
  444. function SmtpSend($header, $body) {
  445. include_once($this->PluginDir . 'class.smtp.php');
  446. $error = '';
  447. $bad_rcpt = array();
  448. if(!$this->SmtpConnect()) {
  449. return false;
  450. }
  451. $smtp_from = ($this->Sender == '') ? $this->From : $this->Sender;
  452. if(!$this->smtp->Mail($smtp_from)) {
  453. $error = $this->Lang('from_failed') . $smtp_from;
  454. $this->SetError($error);
  455. $this->smtp->Reset();
  456. return false;
  457. }
  458. /* Attempt to send attach all recipients */
  459. for($i = 0; $i < count($this->to); $i++) {
  460. if(!$this->smtp->Recipient($this->to[$i][0])) {
  461. $bad_rcpt[] = $this->to[$i][0];
  462. }
  463. }
  464. for($i = 0; $i < count($this->cc); $i++) {
  465. if(!$this->smtp->Recipient($this->cc[$i][0])) {
  466. $bad_rcpt[] = $this->cc[$i][0];
  467. }
  468. }
  469. for($i = 0; $i < count($this->bcc); $i++) {
  470. if(!$this->smtp->Recipient($this->bcc[$i][0])) {
  471. $bad_rcpt[] = $this->bcc[$i][0];
  472. }
  473. }
  474. if(count($bad_rcpt) > 0) { // Create error message
  475. for($i = 0; $i < count($bad_rcpt); $i++) {
  476. if($i != 0) {
  477. $error .= ', ';
  478. }
  479. $error .= $bad_rcpt[$i];
  480. }
  481. $error = $this->Lang('recipients_failed') . $error;
  482. $this->SetError($error);
  483. $this->smtp->Reset();
  484. return false;
  485. }
  486. if(!$this->smtp->Data($header . $body)) {
  487. $this->SetError($this->Lang('data_not_accepted'));
  488. $this->smtp->Reset();
  489. return false;
  490. }
  491. if($this->SMTPKeepAlive == true) {
  492. $this->smtp->Reset();
  493. } else {
  494. $this->SmtpClose();
  495. }
  496. return true;
  497. }
  498. /**
  499. * Initiates a connection to an SMTP server. Returns false if the
  500. * operation failed.
  501. * @access private
  502. * @return bool
  503. */
  504. function SmtpConnect() {
  505. if($this->smtp == NULL) {
  506. $this->smtp = new SMTP();
  507. }
  508. $this->smtp->do_debug = $this->SMTPDebug;
  509. $hosts = explode(';', $this->Host);
  510. $index = 0;
  511. $connection = ($this->smtp->Connected());
  512. /* Retry while there is no connection */
  513. while($index < count($hosts) && $connection == false) {
  514. $hostinfo = array();
  515. if(preg_match('/^(.+):([0-9]+)$/', $hosts[$index], $hostinfo)) {
  516. $host = $hostinfo[1];
  517. $port = $hostinfo[2];
  518. } else {
  519. $host = $hosts[$index];
  520. $port = $this->Port;
  521. }
  522. if($this->smtp->Connect(((!empty($this->SMTPSecure))?$this->SMTPSecure.'://':'').$host, $port, $this->Timeout)) {
  523. if ($this->Helo != '') {
  524. $this->smtp->Hello($this->Helo);
  525. } else {
  526. $this->smtp->Hello($this->ServerHostname());
  527. }
  528. $connection = true;
  529. if($this->SMTPAuth) {
  530. if(!$this->smtp->Authenticate($this->Username, $this->Password)) {
  531. $this->SetError($this->Lang('authenticate'));
  532. $this->smtp->Reset();
  533. $connection = false;
  534. }
  535. }
  536. }
  537. $index++;
  538. }
  539. if(!$connection) {
  540. $this->SetError($this->Lang('connect_host'));
  541. }
  542. return $connection;
  543. }
  544. /**
  545. * Closes the active SMTP session if one exists.
  546. * @return void
  547. */
  548. function SmtpClose() {
  549. if($this->smtp != NULL) {
  550. if($this->smtp->Connected()) {
  551. $this->smtp->Quit();
  552. $this->smtp->Close();
  553. }
  554. }
  555. }
  556. /**
  557. * Sets the language for all class error messages. Returns false
  558. * if it cannot load the language file. The default language type
  559. * is English.
  560. * @param string $lang_type Type of language (e.g. Portuguese: "br")
  561. * @param string $lang_path Path to the language file directory
  562. * @access public
  563. * @return bool
  564. */
  565. function SetLanguage($lang_type, $lang_path = 'language/') {
  566. if(file_exists($lang_path.'phpmailer.lang-'.$lang_type.'.php')) {
  567. include($lang_path.'phpmailer.lang-'.$lang_type.'.php');
  568. } elseif (file_exists($lang_path.'phpmailer.lang-en.php')) {
  569. include($lang_path.'phpmailer.lang-en.php');
  570. } else {
  571. $this->SetError('Could not load language file');
  572. return false;
  573. }
  574. $this->language = $PHPMAILER_LANG;
  575. return true;
  576. }
  577. /////////////////////////////////////////////////
  578. // METHODS, MESSAGE CREATION
  579. /////////////////////////////////////////////////
  580. /**
  581. * Creates recipient headers.
  582. * @access private
  583. * @return string
  584. */
  585. function AddrAppend($type, $addr) {
  586. $addr_str = $type . ': ';
  587. $addr_str .= $this->AddrFormat($addr[0]);
  588. if(count($addr) > 1) {
  589. for($i = 1; $i < count($addr); $i++) {
  590. $addr_str .= ', ' . $this->AddrFormat($addr[$i]);
  591. }
  592. }
  593. $addr_str .= $this->LE;
  594. return $addr_str;
  595. }
  596. /**
  597. * Formats an address correctly.
  598. * @access private
  599. * @return string
  600. */
  601. function AddrFormat($addr) {
  602. if(empty($addr[1])) {
  603. $formatted = $this->SecureHeader($addr[0]);
  604. } else {
  605. $formatted = $this->EncodeHeader($this->SecureHeader($addr[1]), 'phrase') . " <" . $this->SecureHeader($addr[0]) . ">";
  606. }
  607. return $formatted;
  608. }
  609. /**
  610. * Wraps message for use with mailers that do not
  611. * automatically perform wrapping and for quoted-printable.
  612. * Original written by philippe.
  613. * @access private
  614. * @return string
  615. */
  616. function WrapText($message, $length, $qp_mode = false) {
  617. $soft_break = ($qp_mode) ? sprintf(" =%s", $this->LE) : $this->LE;
  618. // If utf-8 encoding is used, we will need to make sure we don't
  619. // split multibyte characters when we wrap
  620. $is_utf8 = (strtolower($this->CharSet) == "utf-8");
  621. $message = $this->FixEOL($message);
  622. if (substr($message, -1) == $this->LE) {
  623. $message = substr($message, 0, -1);
  624. }
  625. $line = explode($this->LE, $message);
  626. $message = '';
  627. for ($i=0 ;$i < count($line); $i++) {
  628. $line_part = explode(' ', $line[$i]);
  629. $buf = '';
  630. for ($e = 0; $e<count($line_part); $e++) {
  631. $word = $line_part[$e];
  632. if ($qp_mode and (strlen($word) > $length)) {
  633. $space_left = $length - strlen($buf) - 1;
  634. if ($e != 0) {
  635. if ($space_left > 20) {
  636. $len = $space_left;
  637. if ($is_utf8) {
  638. $len = $this->UTF8CharBoundary($word, $len);
  639. } elseif (substr($word, $len - 1, 1) == "=") {
  640. $len--;
  641. } elseif (substr($word, $len - 2, 1) == "=") {
  642. $len -= 2;
  643. }
  644. $part = substr($word, 0, $len);
  645. $word = substr($word, $len);
  646. $buf .= ' ' . $part;
  647. $message .= $buf . sprintf("=%s", $this->LE);
  648. } else {
  649. $message .= $buf . $soft_break;
  650. }
  651. $buf = '';
  652. }
  653. while (strlen($word) > 0) {
  654. $len = $length;
  655. if ($is_utf8) {
  656. $len = $this->UTF8CharBoundary($word, $len);
  657. } elseif (substr($word, $len - 1, 1) == "=") {
  658. $len--;
  659. } elseif (substr($word, $len - 2, 1) == "=") {
  660. $len -= 2;
  661. }
  662. $part = substr($word, 0, $len);
  663. $word = substr($word, $len);
  664. if (strlen($word) > 0) {
  665. $message .= $part . sprintf("=%s", $this->LE);
  666. } else {
  667. $buf = $part;
  668. }
  669. }
  670. } else {
  671. $buf_o = $buf;
  672. $buf .= ($e == 0) ? $word : (' ' . $word);
  673. if (strlen($buf) > $length and $buf_o != '') {
  674. $message .= $buf_o . $soft_break;
  675. $buf = $word;
  676. }
  677. }
  678. }
  679. $message .= $buf . $this->LE;
  680. }
  681. return $message;
  682. }
  683. /**
  684. * Finds last character boundary prior to maxLength in a utf-8
  685. * quoted (printable) encoded string.
  686. * Original written by Colin Brown.
  687. * @access private
  688. * @param string $encodedText utf-8 QP text
  689. * @param int $maxLength find last character boundary prior to this length
  690. * @return int
  691. */
  692. function UTF8CharBoundary($encodedText, $maxLength) {
  693. $foundSplitPos = false;
  694. $lookBack = 3;
  695. while (!$foundSplitPos) {
  696. $lastChunk = substr($encodedText, $maxLength - $lookBack, $lookBack);
  697. $encodedCharPos = strpos($lastChunk, "=");
  698. if ($encodedCharPos !== false) {
  699. // Found start of encoded character byte within $lookBack block.
  700. // Check the encoded byte value (the 2 chars after the '=')
  701. $hex = substr($encodedText, $maxLength - $lookBack + $encodedCharPos + 1, 2);
  702. $dec = hexdec($hex);
  703. if ($dec < 128) { // Single byte character.
  704. // If the encoded char was found at pos 0, it will fit
  705. // otherwise reduce maxLength to start of the encoded char
  706. $maxLength = ($encodedCharPos == 0) ? $maxLength :
  707. $maxLength - ($lookBack - $encodedCharPos);
  708. $foundSplitPos = true;
  709. } elseif ($dec >= 192) { // First byte of a multi byte character
  710. // Reduce maxLength to split at start of character
  711. $maxLength = $maxLength - ($lookBack - $encodedCharPos);
  712. $foundSplitPos = true;
  713. } elseif ($dec < 192) { // Middle byte of a multi byte character, look further back
  714. $lookBack += 3;
  715. }
  716. } else {
  717. // No encoded character found
  718. $foundSplitPos = true;
  719. }
  720. }
  721. return $maxLength;
  722. }
  723. /**
  724. * Set the body wrapping.
  725. * @access private
  726. * @return void
  727. */
  728. function SetWordWrap() {
  729. if($this->WordWrap < 1) {
  730. return;
  731. }
  732. switch($this->message_type) {
  733. case 'alt':
  734. /* fall through */
  735. case 'alt_attachments':
  736. $this->AltBody = $this->WrapText($this->AltBody, $this->WordWrap);
  737. break;
  738. default:
  739. $this->Body = $this->WrapText($this->Body, $this->WordWrap);
  740. break;
  741. }
  742. }
  743. /**
  744. * Assembles message header.
  745. * @access private
  746. * @return string
  747. */
  748. function CreateHeader() {
  749. $result = '';
  750. /* Set the boundaries */
  751. $uniq_id = md5(uniqid(time()));
  752. $this->boundary[1] = 'b1_' . $uniq_id;
  753. $this->boundary[2] = 'b2_' . $uniq_id;
  754. $result .= $this->HeaderLine('Date', $this->RFCDate());
  755. if($this->Sender == '') {
  756. $result .= $this->HeaderLine('Return-Path', trim($this->From));
  757. } else {
  758. $result .= $this->HeaderLine('Return-Path', trim($this->Sender));
  759. }
  760. /* To be created automatically by mail() */
  761. if($this->Mailer != 'mail') {
  762. if(count($this->to) > 0) {
  763. $result .= $this->AddrAppend('To', $this->to);
  764. } elseif (count($this->cc) == 0) {
  765. $result .= $this->HeaderLine('To', 'undisclosed-recipients:;');
  766. }
  767. if(count($this->cc) > 0) {
  768. $result .= $this->AddrAppend('Cc', $this->cc);
  769. }
  770. }
  771. $from = array();
  772. $from[0][0] = trim($this->From);
  773. $from[0][1] = $this->FromName;
  774. $result .= $this->AddrAppend('From', $from);
  775. /* sendmail and mail() extract Cc from the header before sending */
  776. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->cc) > 0)) {
  777. $result .= $this->AddrAppend('Cc', $this->cc);
  778. }
  779. /* sendmail and mail() extract Bcc from the header before sending */
  780. if((($this->Mailer == 'sendmail') || ($this->Mailer == 'mail')) && (count($this->bcc) > 0)) {
  781. $result .= $this->AddrAppend('Bcc', $this->bcc);
  782. }
  783. if(count($this->ReplyTo) > 0) {
  784. $result .= $this->AddrAppend('Reply-To', $this->ReplyTo);
  785. }
  786. /* mail() sets the subject itself */
  787. if($this->Mailer != 'mail') {
  788. $result .= $this->HeaderLine('Subject', $this->EncodeHeader($this->SecureHeader($this->Subject)));
  789. }
  790. if($this->MessageID != '') {
  791. $result .= $this->HeaderLine('Message-ID',$this->MessageID);
  792. } else {
  793. $result .= sprintf("Message-ID: <%s@%s>%s", $uniq_id, $this->ServerHostname(), $this->LE);
  794. }
  795. $result .= $this->HeaderLine('X-Priority', $this->Priority);
  796. $result .= $this->HeaderLine('X-Mailer', 'PHPMailer (phpmailer.sourceforge.net) [version ' . $this->Version . ']');
  797. if($this->ConfirmReadingTo != '') {
  798. $result .= $this->HeaderLine('Disposition-Notification-To', '<' . trim($this->ConfirmReadingTo) . '>');
  799. }
  800. // Add custom headers
  801. for($index = 0; $index < count($this->CustomHeader); $index++) {
  802. $result .= $this->HeaderLine(trim($this->CustomHeader[$index][0]), $this->EncodeHeader(trim($this->CustomHeader[$index][1])));
  803. }
  804. if (!$this->sign_key_file) {
  805. $result .= $this->HeaderLine('MIME-Version', '1.0');
  806. $result .= $this->GetMailMIME();
  807. }
  808. return $result;
  809. }
  810. /**
  811. * Returns the message MIME.
  812. * @access private
  813. * @return string
  814. */
  815. function GetMailMIME() {
  816. $result = '';
  817. switch($this->message_type) {
  818. case 'plain':
  819. $result .= $this->HeaderLine('Content-Transfer-Encoding', $this->Encoding);
  820. $result .= sprintf("Content-Type: %s; charset=\"%s\"", $this->ContentType, $this->CharSet);
  821. break;
  822. case 'attachments':
  823. /* fall through */
  824. case 'alt_attachments':
  825. if($this->InlineImageExists()){
  826. $result .= sprintf("Content-Type: %s;%s\ttype=\"text/html\";%s\tboundary=\"%s\"%s", 'multipart/related', $this->LE, $this->LE, $this->boundary[1], $this->LE);
  827. } else {
  828. $result .= $this->HeaderLine('Content-Type', 'multipart/mixed;');
  829. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  830. }
  831. break;
  832. case 'alt':
  833. $result .= $this->HeaderLine('Content-Type', 'multipart/alternative;');
  834. $result .= $this->TextLine("\tboundary=\"" . $this->boundary[1] . '"');
  835. break;
  836. }
  837. if($this->Mailer != 'mail') {
  838. $result .= $this->LE.$this->LE;
  839. }
  840. return $result;
  841. }
  842. /**
  843. * Assembles the message body. Returns an empty string on failure.
  844. * @access private
  845. * @return string
  846. */
  847. function CreateBody() {
  848. $result = '';
  849. if ($this->sign_key_file) {
  850. $result .= $this->GetMailMIME();
  851. }
  852. $this->SetWordWrap();
  853. switch($this->message_type) {
  854. case 'alt':
  855. $result .= $this->GetBoundary($this->boundary[1], '', 'text/plain', '');
  856. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  857. $result .= $this->LE.$this->LE;
  858. $result .= $this->GetBoundary($this->boundary[1], '', 'text/html', '');
  859. $result .= $this->EncodeString($this->Body, $this->Encoding);
  860. $result .= $this->LE.$this->LE;
  861. $result .= $this->EndBoundary($this->boundary[1]);
  862. break;
  863. case 'plain':
  864. $result .= $this->EncodeString($this->Body, $this->Encoding);
  865. break;
  866. case 'attachments':
  867. $result .= $this->GetBoundary($this->boundary[1], '', '', '');
  868. $result .= $this->EncodeString($this->Body, $this->Encoding);
  869. $result .= $this->LE;
  870. $result .= $this->AttachAll();
  871. break;
  872. case 'alt_attachments':
  873. $result .= sprintf("--%s%s", $this->boundary[1], $this->LE);
  874. $result .= sprintf("Content-Type: %s;%s" . "\tboundary=\"%s\"%s", 'multipart/alternative', $this->LE, $this->boundary[2], $this->LE.$this->LE);
  875. $result .= $this->GetBoundary($this->boundary[2], '', 'text/plain', '') . $this->LE; // Create text body
  876. $result .= $this->EncodeString($this->AltBody, $this->Encoding);
  877. $result .= $this->LE.$this->LE;
  878. $result .= $this->GetBoundary($this->boundary[2], '', 'text/html', '') . $this->LE; // Create the HTML body
  879. $result .= $this->EncodeString($this->Body, $this->Encoding);
  880. $result .= $this->LE.$this->LE;
  881. $result .= $this->EndBoundary($this->boundary[2]);
  882. $result .= $this->AttachAll();
  883. break;
  884. }
  885. if($this->IsError()) {
  886. $result = '';
  887. } else if ($this->sign_key_file) {
  888. $file = tempnam("", "mail");
  889. $fp = fopen($file, "w");
  890. fwrite($fp, $result);
  891. fclose($fp);
  892. $signed = tempnam("", "signed");
  893. if (@openssl_pkcs7_sign($file, $signed, "file://".$this->sign_key_file, array("file://".$this->sign_key_file, $this->sign_key_pass), null)) {
  894. $fp = fopen($signed, "r");
  895. $result = fread($fp, filesize($this->sign_key_file));
  896. fclose($fp);
  897. } else {
  898. $this->SetError($this->Lang("signing").openssl_error_string());
  899. $result = '';
  900. }
  901. unlink($file);
  902. unlink($signed);
  903. }
  904. return $result;
  905. }
  906. /**
  907. * Returns the start of a message boundary.
  908. * @access private
  909. */
  910. function GetBoundary($boundary, $charSet, $contentType, $encoding) {
  911. $result = '';
  912. if($charSet == '') {
  913. $charSet = $this->CharSet;
  914. }
  915. if($contentType == '') {
  916. $contentType = $this->ContentType;
  917. }
  918. if($encoding == '') {
  919. $encoding = $this->Encoding;
  920. }
  921. $result .= $this->TextLine('--' . $boundary);
  922. $result .= sprintf("Content-Type: %s; charset = \"%s\"", $contentType, $charSet);
  923. $result .= $this->LE;
  924. $result .= $this->HeaderLine('Content-Transfer-Encoding', $encoding);
  925. $result .= $this->LE;
  926. return $result;
  927. }
  928. /**
  929. * Returns the end of a message boundary.
  930. * @access private
  931. */
  932. function EndBoundary($boundary) {
  933. return $this->LE . '--' . $boundary . '--' . $this->LE;
  934. }
  935. /**
  936. * Sets the message type.
  937. * @access private
  938. * @return void
  939. */
  940. function SetMessageType() {
  941. if(count($this->attachment) < 1 && strlen($this->AltBody) < 1) {
  942. $this->message_type = 'plain';
  943. } else {
  944. if(count($this->attachment) > 0) {
  945. $this->message_type = 'attachments';
  946. }
  947. if(strlen($this->AltBody) > 0 && count($this->attachment) < 1) {
  948. $this->message_type = 'alt';
  949. }
  950. if(strlen($this->AltBody) > 0 && count($this->attachment) > 0) {
  951. $this->message_type = 'alt_attachments';
  952. }
  953. }
  954. }
  955. /* Returns a formatted header line.
  956. * @access private
  957. * @return string
  958. */
  959. function HeaderLine($name, $value) {
  960. return $name . ': ' . $value . $this->LE;
  961. }
  962. /**
  963. * Returns a formatted mail line.
  964. * @access private
  965. * @return string
  966. */
  967. function TextLine($value) {
  968. return $value . $this->LE;
  969. }
  970. /////////////////////////////////////////////////
  971. // CLASS METHODS, ATTACHMENTS
  972. /////////////////////////////////////////////////
  973. /**
  974. * Adds an attachment from a path on the filesystem.
  975. * Returns false if the file could not be found
  976. * or accessed.
  977. * @param string $path Path to the attachment.
  978. * @param string $name Overrides the attachment name.
  979. * @param string $encoding File encoding (see $Encoding).
  980. * @param string $type File extension (MIME) type.
  981. * @return bool
  982. */
  983. function AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  984. if(!@is_file($path)) {
  985. $this->SetError($this->Lang('file_access') . $path);
  986. return false;
  987. }
  988. $filename = basename($path);
  989. if($name == '') {
  990. $name = $filename;
  991. }
  992. $cur = count($this->attachment);
  993. $this->attachment[$cur][0] = $path;
  994. $this->attachment[$cur][1] = $filename;
  995. $this->attachment[$cur][2] = $name;
  996. $this->attachment[$cur][3] = $encoding;
  997. $this->attachment[$cur][4] = $type;
  998. $this->attachment[$cur][5] = false; // isStringAttachment
  999. $this->attachment[$cur][6] = 'attachment';
  1000. $this->attachment[$cur][7] = 0;
  1001. return true;
  1002. }
  1003. /**
  1004. * Attaches all fs, string, and binary attachments to the message.
  1005. * Returns an empty string on failure.
  1006. * @access private
  1007. * @return string
  1008. */
  1009. function AttachAll() {
  1010. /* Return text of body */
  1011. $mime = array();
  1012. /* Add all attachments */
  1013. for($i = 0; $i < count($this->attachment); $i++) {
  1014. /* Check for string attachment */
  1015. $bString = $this->attachment[$i][5];
  1016. if ($bString) {
  1017. $string = $this->attachment[$i][0];
  1018. } else {
  1019. $path = $this->attachment[$i][0];
  1020. }
  1021. $filename = $this->attachment[$i][1];
  1022. $name = $this->attachment[$i][2];
  1023. $encoding = $this->attachment[$i][3];
  1024. $type = $this->attachment[$i][4];
  1025. $disposition = $this->attachment[$i][6];
  1026. $cid = $this->attachment[$i][7];
  1027. $mime[] = sprintf("--%s%s", $this->boundary[1], $this->LE);
  1028. $mime[] = sprintf("Content-Type: %s; name=\"%s\"%s", $type, $name, $this->LE);
  1029. $mime[] = sprintf("Content-Transfer-Encoding: %s%s", $encoding, $this->LE);
  1030. if($disposition == 'inline') {
  1031. $mime[] = sprintf("Content-ID: <%s>%s", $cid, $this->LE);
  1032. }
  1033. $mime[] = sprintf("Content-Disposition: %s; filename=\"%s\"%s", $disposition, $name, $this->LE.$this->LE);
  1034. /* Encode as string attachment */
  1035. if($bString) {
  1036. $mime[] = $this->EncodeString($string, $encoding);
  1037. if($this->IsError()) {
  1038. return '';
  1039. }
  1040. $mime[] = $this->LE.$this->LE;
  1041. } else {
  1042. $mime[] = $this->EncodeFile($path, $encoding);
  1043. if($this->IsError()) {
  1044. return '';
  1045. }
  1046. $mime[] = $this->LE.$this->LE;
  1047. }
  1048. }
  1049. $mime[] = sprintf("--%s--%s", $this->boundary[1], $this->LE);
  1050. return join('', $mime);
  1051. }
  1052. /**
  1053. * Encodes attachment in requested format. Returns an
  1054. * empty string on failure.
  1055. * @access private
  1056. * @return string
  1057. */
  1058. function EncodeFile ($path, $encoding = 'base64') {
  1059. if(!@$fd = fopen($path, 'rb')) {
  1060. $this->SetError($this->Lang('file_open') . $path);
  1061. return '';
  1062. }
  1063. $magic_quotes = get_magic_quotes_runtime();
  1064. set_magic_quotes_runtime(0);
  1065. $file_buffer = fread($fd, filesize($path));
  1066. $file_buffer = $this->EncodeString($file_buffer, $encoding);
  1067. fclose($fd);
  1068. set_magic_quotes_runtime($magic_quotes);
  1069. return $file_buffer;
  1070. }
  1071. /**
  1072. * Encodes string to requested format. Returns an
  1073. * empty string on failure.
  1074. * @access private
  1075. * @return string
  1076. */
  1077. function EncodeString ($str, $encoding = 'base64') {
  1078. $encoded = '';
  1079. switch(strtolower($encoding)) {
  1080. case 'base64':
  1081. /* chunk_split is found in PHP >= 3.0.6 */
  1082. $encoded = chunk_split(base64_encode($str), 76, $this->LE);
  1083. break;
  1084. case '7bit':
  1085. case '8bit':
  1086. $encoded = $this->FixEOL($str);
  1087. if (substr($encoded, -(strlen($this->LE))) != $this->LE)
  1088. $encoded .= $this->LE;
  1089. break;
  1090. case 'binary':
  1091. $encoded = $str;
  1092. break;
  1093. case 'quoted-printable':
  1094. $encoded = $this->EncodeQP($str);
  1095. break;
  1096. default:
  1097. $this->SetError($this->Lang('encoding') . $encoding);
  1098. break;
  1099. }
  1100. return $encoded;
  1101. }
  1102. /**
  1103. * Encode a header string to best of Q, B, quoted or none.
  1104. * @access private
  1105. * @return string
  1106. */
  1107. function EncodeHeader ($str, $position = 'text') {
  1108. $x = 0;
  1109. switch (strtolower($position)) {
  1110. case 'phrase':
  1111. if (!preg_match('/[\200-\377]/', $str)) {
  1112. /* Can't use addslashes as we don't know what value has magic_quotes_sybase. */
  1113. $encoded = addcslashes($str, "\0..\37\177\\\"");
  1114. if (($str == $encoded) && !preg_match('/[^A-Za-z0-9!#$%&\'*+\/=?^_`{|}~ -]/', $str)) {
  1115. return ($encoded);
  1116. } else {
  1117. return ("\"$encoded\"");
  1118. }
  1119. }
  1120. $x = preg_match_all('/[^\040\041\043-\133\135-\176]/', $str, $matches);
  1121. break;
  1122. case 'comment':
  1123. $x = preg_match_all('/[()"]/', $str, $matches);
  1124. /* Fall-through */
  1125. case 'text':
  1126. default:
  1127. $x += preg_match_all('/[\000-\010\013\014\016-\037\177-\377]/', $str, $matches);
  1128. break;
  1129. }
  1130. if ($x == 0) {
  1131. return ($str);
  1132. }
  1133. $maxlen = 75 - 7 - strlen($this->CharSet);
  1134. /* Try to select the encoding which should produce the shortest output */
  1135. if (strlen($str)/3 < $x) {
  1136. $encoding = 'B';
  1137. if (function_exists('mb_strlen') && $this->HasMultiBytes($str)) {
  1138. // Use a custom function which correctly encodes and wraps long
  1139. // multibyte strings without breaking lines within a character
  1140. $encoded = $this->Base64EncodeWrapMB($str);
  1141. } else {
  1142. $encoded = base64_encode($str);
  1143. $maxlen -= $maxlen % 4;
  1144. $encoded = trim(chunk_split($encoded, $maxlen, "\n"));
  1145. }
  1146. } else {
  1147. $encoding = 'Q';
  1148. $encoded = $this->EncodeQ($str, $position);
  1149. $encoded = $this->WrapText($encoded, $maxlen, true);
  1150. $encoded = str_replace('='.$this->LE, "\n", trim($encoded));
  1151. }
  1152. $encoded = preg_replace('/^(.*)$/m', " =?".$this->CharSet."?$encoding?\\1?=", $encoded);
  1153. $encoded = trim(str_replace("\n", $this->LE, $encoded));
  1154. return $encoded;
  1155. }
  1156. /**
  1157. * Checks if a string contains multibyte characters.
  1158. * @access private
  1159. * @param string $str multi-byte text to wrap encode
  1160. * @return bool
  1161. */
  1162. function HasMultiBytes($str) {
  1163. if (function_exists('mb_strlen')) {
  1164. return (strlen($str) > mb_strlen($str, $this->CharSet));
  1165. } else { // Assume no multibytes (we can't handle without mbstring functions anyway)
  1166. return False;
  1167. }
  1168. }
  1169. /**
  1170. * Correctly encodes and wraps long multibyte strings for mail headers
  1171. * without breaking lines within a character.
  1172. * Adapted from a function by paravoid at http://uk.php.net/manual/en/function.mb-encode-mimeheader.php
  1173. * @access private
  1174. * @param string $str multi-byte text to wrap encode
  1175. * @return string
  1176. */
  1177. function Base64EncodeWrapMB($str) {
  1178. $start = "=?".$this->CharSet."?B?";
  1179. $end = "?=";
  1180. $encoded = "";
  1181. $mb_length = mb_strlen($str, $this->CharSet);
  1182. // Each line must have length <= 75, including $start and $end
  1183. $length = 75 - strlen($start) - strlen($end);
  1184. // Average multi-byte ratio
  1185. $ratio = $mb_length / strlen($str);
  1186. // Base64 has a 4:3 ratio
  1187. $offset = $avgLength = floor($length * $ratio * .75);
  1188. for ($i = 0; $i < $mb_length; $i += $offset) {
  1189. $lookBack = 0;
  1190. do {
  1191. $offset = $avgLength - $lookBack;
  1192. $chunk = mb_substr($str, $i, $offset, $this->CharSet);
  1193. $chunk = base64_encode($chunk);
  1194. $lookBack++;
  1195. }
  1196. while (strlen($chunk) > $length);
  1197. $encoded .= $chunk . $this->LE;
  1198. }
  1199. // Chomp the last linefeed
  1200. $encoded = substr($encoded, 0, -strlen($this->LE));
  1201. return $encoded;
  1202. }
  1203. /**
  1204. * Encode string to quoted-printable.
  1205. * @access private
  1206. * @return string
  1207. */
  1208. function EncodeQP( $input = '', $line_max = 76, $space_conv = false ) {
  1209. $hex = array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
  1210. $lines = preg_split('/(?:\r\n|\r|\n)/', $input);
  1211. $eol = "\r\n";
  1212. $escape = '=';
  1213. $output = '';
  1214. while( list(, $line) = each($lines) ) {
  1215. $linlen = strlen($line);
  1216. $newline = '';
  1217. for($i = 0; $i < $linlen; $i++) {
  1218. $c = substr( $line, $i, 1 );
  1219. $dec = ord( $c );
  1220. if ( ( $i == 0 ) && ( $dec == 46 ) ) { // convert first point in the line into =2E
  1221. $c = '=2E';
  1222. }
  1223. if ( $dec == 32 ) {
  1224. if ( $i == ( $linlen - 1 ) ) { // convert space at eol only
  1225. $c = '=20';
  1226. } else if ( $space_conv ) {
  1227. $c = '=20';
  1228. }
  1229. } elseif ( ($dec == 61) || ($dec < 32 ) || ($dec > 126) ) { // always encode "\t", which is *not* required
  1230. $h2 = floor($dec/16);
  1231. $h1 = floor($dec%16);
  1232. $c = $escape.$hex[$h2].$hex[$h1];
  1233. }
  1234. if ( (strlen($newline) + strlen($c)) >= $line_max ) { // CRLF is not counted
  1235. $output .= $newline.$escape.$eol; // soft line break; " =\r\n" is okay
  1236. $newline = '';
  1237. // check if newline first character will be point or not
  1238. if ( $dec == 46 ) {
  1239. $c = '=2E';
  1240. }
  1241. }
  1242. $newline .= $c;
  1243. } // end of for
  1244. $output .= $newline.$eol;
  1245. } // end of while
  1246. return trim($output);
  1247. }
  1248. /**
  1249. * Encode string to q encoding.
  1250. * @access private
  1251. * @return string
  1252. */
  1253. function EncodeQ ($str, $position = 'text') {
  1254. /* There should not be any EOL in the string */
  1255. $encoded = preg_replace("[\r\n]", '', $str);
  1256. switch (strtolower($position)) {
  1257. case 'phrase':
  1258. $encoded = preg_replace("/([^A-Za-z0-9!*+\/ -])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1259. break;
  1260. case 'comment':
  1261. $encoded = preg_replace("/([\(\)\"])/e", "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1262. case 'text':
  1263. default:
  1264. /* Replace every high ascii, control =, ? and _ characters */
  1265. $encoded = preg_replace('/([\000-\011\013\014\016-\037\075\077\137\177-\377])/e',
  1266. "'='.sprintf('%02X', ord('\\1'))", $encoded);
  1267. break;
  1268. }
  1269. /* Replace every spaces to _ (more readable than =20) */
  1270. $encoded = str_replace(' ', '_', $encoded);
  1271. return $encoded;
  1272. }
  1273. /**
  1274. * Adds a string or binary attachment (non-filesystem) to the list.
  1275. * This method can be used to attach ascii or binary data,
  1276. * such as a BLOB record from a database.
  1277. * @param string $string String attachment data.
  1278. * @param string $filename Name of the attachment.
  1279. * @param string $encoding File encoding (see $Encoding).
  1280. * @param string $type File extension (MIME) type.
  1281. * @return void
  1282. */
  1283. function AddStringAttachment($string, $filename, $encoding = 'base64', $type = 'application/octet-stream') {
  1284. /* Append to $attachment array */
  1285. $cur = count($this->attachment);
  1286. $this->attachment[$cur][0] = $string;
  1287. $this->attachment[$cur][1] = $filename;
  1288. $this->attachment[$cur][2] = $filename;
  1289. $this->attachment[$cur][3] = $encoding;
  1290. $this->attachment[$cur][4] = $type;
  1291. $this->attachment[$cur][5] = true; // isString
  1292. $this->attachment[$cur][6] = 'attachment';
  1293. $this->attachment[$cur][7] = 0;
  1294. }
  1295. /**
  1296. * Adds an embedded attachment. This can include images, sounds, and
  1297. * just about any other document. Make sure to set the $type to an
  1298. * image type. For JPEG images use "image/jpeg" and for GIF images
  1299. * use "image/gif".
  1300. * @param string $path Path to the attachment.
  1301. * @param string $cid Content ID of the attachment. Use this to identify
  1302. * the Id for accessing the image in an HTML form.
  1303. * @param string $name Overrides the attachment name.
  1304. * @param string $encoding File encoding (see $Encoding).
  1305. * @param string $type File extension (MIME) type.
  1306. * @return bool
  1307. */
  1308. function AddEmbeddedImage($path, $cid, $name = '', $encoding = 'base64', $type = 'application/octet-stream') {
  1309. if(!@is_file($path)) {
  1310. $this->SetError($this->Lang('file_access') . $path);
  1311. return false;
  1312. }
  1313. $filename = basename($path);
  1314. if($name == '') {
  1315. $name = $filename;
  1316. }
  1317. /* Append to $attachment array */
  1318. $cur = count($this->attachment);
  1319. $this->attachment[$cur][0] = $path;
  1320. $this->attachment[$cur][1] = $filename;
  1321. $this->attachment[$cur][2] = $name;
  1322. $this->attachment[$cur][3] = $encoding;
  1323. $this->attachment[$cur][4] = $type;
  1324. $this->attachment[$cur][5] = false;
  1325. $this->attachment[$cur][6] = 'inline';
  1326. $this->attachment[$cur][7] = $cid;
  1327. return true;
  1328. }
  1329. /**
  1330. * Returns true if an inline attachment is present.
  1331. * @access private
  1332. * @return bool
  1333. */
  1334. function InlineImageExists() {
  1335. $result = false;
  1336. for($i = 0; $i < count($this->attachment); $i++) {
  1337. if($this->attachment[$i][6] == 'inline') {
  1338. $result = true;
  1339. break;
  1340. }
  1341. }
  1342. return $result;
  1343. }
  1344. /////////////////////////////////////////////////
  1345. // CLASS METHODS, MESSAGE RESET
  1346. /////////////////////////////////////////////////
  1347. /**
  1348. * Clears all recipients assigned in the TO array. Returns void.
  1349. * @return void
  1350. */
  1351. function ClearAddresses() {
  1352. $this->to = array();
  1353. }
  1354. /**
  1355. * Clears all recipients assigned in the CC array. Returns void.
  1356. * @return void
  1357. */
  1358. function ClearCCs() {
  1359. $this->cc = array();
  1360. }
  1361. /**
  1362. * Clears all recipients assigned in the BCC array. Returns void.
  1363. * @return void
  1364. */
  1365. function ClearBCCs() {
  1366. $this->bcc = array();
  1367. }
  1368. /**
  1369. * Clears all recipients assigned in the ReplyTo array. Returns void.
  1370. * @return void
  1371. */
  1372. function ClearReplyTos() {
  1373. $this->ReplyTo = array();
  1374. }
  1375. /**
  1376. * Clears all recipients assigned in the TO, CC and BCC
  1377. * array. Returns void.
  1378. * @return void
  1379. */
  1380. function ClearAllRecipients() {
  1381. $this->to = array();
  1382. $this->cc = array();
  1383. $this->bcc = array();
  1384. }
  1385. /**
  1386. * Clears all previously set filesystem, string, and binary
  1387. * attachments. Returns void.
  1388. * @return void
  1389. */
  1390. function ClearAttachments() {
  1391. $this->attachment = array();
  1392. }
  1393. /**
  1394. * Clears all custom headers. Returns void.
  1395. * @return void
  1396. */
  1397. function ClearCustomHeaders() {
  1398. $this->CustomHeader = array();
  1399. }
  1400. /////////////////////////////////////////////////
  1401. // CLASS METHODS, MISCELLANEOUS
  1402. /////////////////////////////////////////////////
  1403. /**
  1404. * Adds the error message to the error container.
  1405. * Returns void.
  1406. * @access private
  1407. * @return void
  1408. */
  1409. function SetError($msg) {
  1410. $this->error_count++;
  1411. $this->ErrorInfo = $msg;
  1412. }
  1413. /**
  1414. * Returns the proper RFC 822 formatted date.
  1415. * @access private
  1416. * @return string
  1417. */
  1418. function RFCDate() {
  1419. $tz = date('Z');
  1420. $tzs = ($tz < 0) ? '-' : '+';
  1421. $tz = abs($tz);
  1422. $tz = (int)($tz/3600)*100 + ($tz%3600)/60;
  1423. $result = sprintf("%s %s%04d", date('D, j M Y H:i:s'), $tzs, $tz);
  1424. return $result;
  1425. }
  1426. /**
  1427. * Returns the appropriate server variable. Should work with both
  1428. * PHP 4.1.0+ as well as older versions. Returns an empty string
  1429. * if nothing is found.
  1430. * @access private
  1431. * @return mixed
  1432. */
  1433. function ServerVar($varName) {
  1434. global $HTTP_SERVER_VARS;
  1435. global $HTTP_ENV_VARS;
  1436. if(!isset($_SERVER)) {
  1437. $_SERVER = $HTTP_SERVER_VARS;
  1438. if(!isset($_SERVER['REMOTE_ADDR'])) {
  1439. $_SERVER = $HTTP_ENV_VARS; // must be Apache
  1440. }
  1441. }
  1442. if(isset($_SERVER[$varName])) {
  1443. return $_SERVER[$varName];
  1444. } else {
  1445. return '';
  1446. }
  1447. }
  1448. /**
  1449. * Returns the server hostname or 'localhost.localdomain' if unknown.
  1450. * @access private
  1451. * @return string
  1452. */
  1453. function ServerHostname() {
  1454. if ($this->Hostname != '') {
  1455. $result = $this->Hostname;
  1456. } elseif ($this->ServerVar('SERVER_NAME') != '') {
  1457. $result = $this->ServerVar('SERVER_NAME');
  1458. } else {
  1459. $result = 'localhost.localdomain';
  1460. }
  1461. return $result;
  1462. }
  1463. /**
  1464. * Returns a message in the appropriate language.
  1465. * @access private
  1466. * @return string
  1467. */
  1468. function Lang($key) {
  1469. if(count($this->language) < 1) {
  1470. $this->SetLanguage('en'); // set the default language
  1471. }
  1472. if(isset($this->language[$key])) {
  1473. return $this->language[$key];
  1474. } else {
  1475. return 'Language string failed to load: ' . $key;
  1476. }
  1477. }
  1478. /**
  1479. * Returns true if an error occurred.
  1480. * @return bool
  1481. */
  1482. function IsError() {
  1483. return ($this->error_count > 0);
  1484. }
  1485. /**
  1486. * Changes every end of line from CR or LF to CRLF.
  1487. * @access private
  1488. * @return string
  1489. */
  1490. function FixEOL($str) {
  1491. $str = str_replace("\r\n", "\n", $str);
  1492. $str = str_replace("\r", "\n", $str);
  1493. $str = str_replace("\n", $this->LE, $str);
  1494. return $str;
  1495. }
  1496. /**
  1497. * Adds a custom header.
  1498. * @return void
  1499. */
  1500. function AddCustomHeader($custom_header) {
  1501. $this->CustomHeader[] = explode(':', $custom_header, 2);
  1502. }
  1503. /**
  1504. * Evaluates the message and returns modifications for inline images and backgrounds
  1505. * @access public
  1506. * @return $message
  1507. */
  1508. function MsgHTML($message,$basedir='') {
  1509. preg_match_all("/(src|background)=\"(.*)\"/Ui", $message, $images);
  1510. if(isset($images[2])) {
  1511. foreach($images[2] as $i => $url) {
  1512. // do not change urls for absolute images (thanks to corvuscorax)
  1513. if (!preg_match('/^[A-z][A-z]*:\/\//',$url)) {
  1514. $filename = basename($url);
  1515. $directory = dirname($url);
  1516. ($directory == '.')?$directory='':'';
  1517. $cid = 'cid:' . md5($filename);
  1518. $fileParts = split("\.", $filename);
  1519. $ext = $fileParts[1];
  1520. $mimeType = $this->_mime_types($ext);
  1521. if ( strlen($basedir) > 1 && substr($basedir,-1) != '/') { $basedir .= '/'; }
  1522. if ( strlen($directory) > 1 && substr($basedir,-1) != '/') { $directory .= '/'; }
  1523. $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64', $mimeType);
  1524. if ( $this->AddEmbeddedImage($basedir.$directory.$filename, md5($filename), $filename, 'base64',$mimeType) ) {
  1525. $message = preg_replace("/".$images[1][$i]."=\"".preg_quote($url, '/')."\"/Ui", $images[1][$i]."=\"".$cid."\"", $message);
  1526. }
  1527. }
  1528. }
  1529. }
  1530. $this->IsHTML(true);
  1531. $this->Body = $message;
  1532. $textMsg = trim(strip_tags(preg_replace('/<(head|title|style|script)[^>]*>.*?<\/\\1>/s','',$message)));
  1533. if ( !empty($textMsg) && empty($this->AltBody) ) {
  1534. $this->AltBody = $textMsg;
  1535. }
  1536. if ( empty($this->AltBody) ) {
  1537. $this->AltBody = 'To view this email message, open the email in with HTML compatibility!' . "\n\n";
  1538. }
  1539. }
  1540. /**
  1541. * Gets the mime type of the embedded or inline image
  1542. * @access private
  1543. * @return mime type of ext
  1544. */
  1545. function _mime_types($ext = '') {
  1546. $mimes = array(
  1547. 'hqx' => 'application/mac-binhex40',
  1548. 'cpt' => 'application/mac-compactpro',
  1549. 'doc' => 'application/msword',
  1550. 'bin' => 'application/macbinary',
  1551. 'dms' => 'application/octet-stream',
  1552. 'lha' => 'application/octet-stream',
  1553. 'lzh' => 'application/octet-stream',
  1554. 'exe' => 'application/octet-stream',
  1555. 'class' => 'application/octet-stream',
  1556. 'psd' => 'application/octet-stream',
  1557. 'so' => 'application/octet-stream',
  1558. 'sea' => 'application/octet-stream',
  1559. 'dll' => 'application/octet-stream',
  1560. 'oda' => 'application/oda',
  1561. 'pdf' => 'application/pdf',
  1562. 'ai' => 'application/postscript',
  1563. 'eps' => 'application/postscript',
  1564. 'ps' => 'application/postscript',
  1565. 'smi' => 'application/smil',
  1566. 'smil' => 'application/smil',
  1567. 'mif' => 'application/vnd.mif',
  1568. 'xls' => 'application/vnd.ms-excel',
  1569. 'ppt' => 'application/vnd.ms-powerpoint',
  1570. 'wbxml' => 'application/vnd.wap.wbxml',
  1571. 'wmlc' => 'application/vnd.wap.wmlc',
  1572. 'dcr' => 'application/x-director',
  1573. 'dir' => 'application/x-director',
  1574. 'dxr' => 'application/x-director',
  1575. 'dvi' => 'application/x-dvi',
  1576. 'gtar' => 'application/x-gtar',
  1577. 'php' => 'application/x-httpd-php',
  1578. 'php4' => 'application/x-httpd-php',
  1579. 'php3' => 'application/x-httpd-php',
  1580. 'phtml' => 'application/x-httpd-php',
  1581. 'phps' => 'application/x-httpd-php-source',
  1582. 'js' => 'application/x-javascript',
  1583. 'swf' => 'application/x-shockwave-flash',
  1584. 'sit' => 'application/x-stuffit',
  1585. 'tar' => 'application/x-tar',
  1586. 'tgz' => 'application/x-tar',
  1587. 'xhtml' => 'application/xhtml+xml',
  1588. 'xht' => 'application/xhtml+xml',
  1589. 'zip' => 'application/zip',
  1590. 'mid' => 'audio/midi',
  1591. 'midi' => 'audio/midi',
  1592. 'mpga' => 'audio/mpeg',
  1593. 'mp2' => 'audio/mpeg',
  1594. 'mp3' => 'audio/mpeg',
  1595. 'aif' => 'audio/x-aiff',
  1596. 'aiff' => 'audio/x-aiff',
  1597. 'aifc' => 'audio/x-aiff',
  1598. 'ram' => 'audio/x-pn-realaudio',
  1599. 'rm' => 'audio/x-pn-realaudio',
  1600. 'rpm' => 'audio/x-pn-realaudio-plugin',
  1601. 'ra' => 'audio/x-realaudio',
  1602. 'rv' => 'video/vnd.rn-realvideo',
  1603. 'wav' => 'audio/x-wav',
  1604. 'bmp' => 'image/bmp',
  1605. 'gif' => 'image/gif',
  1606. 'jpeg' => 'image/jpeg',
  1607. 'jpg' => 'image/jpeg',
  1608. 'jpe' => 'image/jpeg',
  1609. 'png' => 'image/png',
  1610. 'tiff' => 'image/tiff',
  1611. 'tif' => 'image/tiff',
  1612. 'css' => 'text/css',
  1613. 'html' => 'text/html',
  1614. 'htm' => 'text/html',
  1615. 'shtml' => 'text/html',
  1616. 'txt' => 'text/plain',
  1617. 'text' => 'text/plain',
  1618. 'log' => 'text/plain',
  1619. 'rtx' => 'text/richtext',
  1620. 'rtf' => 'text/rtf',
  1621. 'xml' => 'text/xml',
  1622. 'xsl' => 'text/xml',
  1623. 'mpeg' => 'video/mpeg',
  1624. 'mpg' => 'video/mpeg',
  1625. 'mpe' => 'video/mpeg',
  1626. 'qt' => 'video/quicktime',
  1627. 'mov' => 'video/quicktime',
  1628. 'avi' => 'video/x-msvideo',
  1629. 'movie' => 'video/x-sgi-movie',
  1630. 'doc' => 'application/msword',
  1631. 'word' => 'application/msword',
  1632. 'xl' => 'application/excel',
  1633. 'eml' => 'message/rfc822'
  1634. );
  1635. return ( ! isset($mimes[strtolower($ext)])) ? 'application/octet-stream' : $mimes[strtolower($ext)];
  1636. }
  1637. /**
  1638. * Set (or reset) Class Objects (variables)
  1639. *
  1640. * Usage Example:
  1641. * $page->set('X-Priority', '3');
  1642. *
  1643. * @access public
  1644. * @param string $name Parameter Name
  1645. * @param mixed $value Parameter Value
  1646. * NOTE: will not work with arrays, there are no arrays to set/reset
  1647. */
  1648. function set ( $name, $value = '' ) {
  1649. if ( isset($this->$name) ) {
  1650. $this->$name = $value;
  1651. } else {
  1652. $this->SetError('Cannot set or reset variable ' . $name);
  1653. return false;
  1654. }
  1655. }
  1656. /**
  1657. * Read a file from a supplied filename and return it.
  1658. *
  1659. * @access public
  1660. * @param string $filename Parameter File Name
  1661. */
  1662. function getFile($filename) {
  1663. $return = '';
  1664. if ($fp = fopen($filename, 'rb')) {
  1665. while (!feof($fp)) {
  1666. $return .= fread($fp, 1024);
  1667. }
  1668. fclose($fp);
  1669. return $return;
  1670. } else {
  1671. return false;
  1672. }
  1673. }
  1674. /**
  1675. * Strips newlines to prevent header injection.
  1676. * @access private
  1677. * @param string $str String
  1678. * @return string
  1679. */
  1680. function SecureHeader($str) {
  1681. $str = trim($str);
  1682. $str = str_replace("\r", "", $str);
  1683. $str = str_replace("\n", "", $str);
  1684. return $str;
  1685. }
  1686. /**
  1687. * Set the private key file and password to sign the message.
  1688. *
  1689. * @access public
  1690. * @param string $key_filename Parameter File Name
  1691. * @param string $key_pass Password for private key
  1692. */
  1693. function Sign($key_filename, $key_pass) {
  1694. $this->sign_key_file = $key_filename;
  1695. $this->sign_key_pass = $key_pass;
  1696. }
  1697. }
  1698. ?>