PHPMailer.php 56KB

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