Email.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author EllisLab Dev Team
  9. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc.
  10. * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
  11. * @license http://codeigniter.com/user_guide/license.html
  12. * @link http://codeigniter.com
  13. * @since Version 1.0
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * CodeIgniter Email Class
  19. *
  20. * Permits email to be sent using Mail, Sendmail, or SMTP.
  21. *
  22. * @package CodeIgniter
  23. * @subpackage Libraries
  24. * @category Libraries
  25. * @author EllisLab Dev Team
  26. * @link http://codeigniter.com/user_guide/libraries/email.html
  27. */
  28. class CI_Email {
  29. var $useragent = "CodeIgniter";
  30. var $mailpath = "/usr/sbin/sendmail"; // Sendmail path
  31. var $protocol = "mail"; // mail/sendmail/smtp
  32. var $smtp_host = ""; // SMTP Server. Example: mail.earthlink.net
  33. var $smtp_user = ""; // SMTP Username
  34. var $smtp_pass = ""; // SMTP Password
  35. var $smtp_port = "25"; // SMTP Port
  36. var $smtp_timeout = 5; // SMTP Timeout in seconds
  37. var $smtp_crypto = ""; // SMTP Encryption. Can be null, tls or ssl.
  38. var $wordwrap = TRUE; // TRUE/FALSE Turns word-wrap on/off
  39. var $wrapchars = "76"; // Number of characters to wrap at.
  40. var $mailtype = "text"; // text/html Defines email formatting
  41. var $charset = "utf-8"; // Default char set: iso-8859-1 or us-ascii
  42. var $multipart = "mixed"; // "mixed" (in the body) or "related" (separate)
  43. var $alt_message = ''; // Alternative message for HTML emails
  44. var $validate = FALSE; // TRUE/FALSE. Enables email validation
  45. var $priority = "3"; // Default priority (1 - 5)
  46. var $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
  47. var $crlf = "\n"; // The RFC 2045 compliant CRLF for quoted-printable is "\r\n". Apparently some servers,
  48. // even on the receiving end think they need to muck with CRLFs, so using "\n", while
  49. // distasteful, is the only thing that seems to work for all environments.
  50. var $send_multipart = TRUE; // TRUE/FALSE - Yahoo does not like multipart alternative, so this is an override. Set to FALSE for Yahoo.
  51. var $bcc_batch_mode = FALSE; // TRUE/FALSE Turns on/off Bcc batch feature
  52. var $bcc_batch_size = 200; // If bcc_batch_mode = TRUE, sets max number of Bccs in each batch
  53. var $_safe_mode = FALSE;
  54. var $_subject = "";
  55. var $_body = "";
  56. var $_finalbody = "";
  57. var $_alt_boundary = "";
  58. var $_atc_boundary = "";
  59. var $_header_str = "";
  60. var $_smtp_connect = "";
  61. var $_encoding = "8bit";
  62. var $_IP = FALSE;
  63. var $_smtp_auth = FALSE;
  64. var $_replyto_flag = FALSE;
  65. var $_debug_msg = array();
  66. var $_recipients = array();
  67. var $_cc_array = array();
  68. var $_bcc_array = array();
  69. var $_headers = array();
  70. var $_attach_name = array();
  71. var $_attach_type = array();
  72. var $_attach_disp = array();
  73. var $_protocols = array('mail', 'sendmail', 'smtp');
  74. var $_base_charsets = array('us-ascii', 'iso-2022-'); // 7-bit charsets (excluding language suffix)
  75. var $_bit_depths = array('7bit', '8bit');
  76. var $_priorities = array('1 (Highest)', '2 (High)', '3 (Normal)', '4 (Low)', '5 (Lowest)');
  77. /**
  78. * Constructor - Sets Email Preferences
  79. *
  80. * The constructor can be passed an array of config values
  81. */
  82. public function __construct($config = array())
  83. {
  84. if (count($config) > 0)
  85. {
  86. $this->initialize($config);
  87. }
  88. else
  89. {
  90. $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
  91. $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
  92. }
  93. log_message('debug', "Email Class Initialized");
  94. }
  95. // --------------------------------------------------------------------
  96. /**
  97. * Initialize preferences
  98. *
  99. * @access public
  100. * @param array
  101. * @return void
  102. */
  103. public function initialize($config = array())
  104. {
  105. foreach ($config as $key => $val)
  106. {
  107. if (isset($this->$key))
  108. {
  109. $method = 'set_'.$key;
  110. if (method_exists($this, $method))
  111. {
  112. $this->$method($val);
  113. }
  114. else
  115. {
  116. $this->$key = $val;
  117. }
  118. }
  119. }
  120. $this->clear();
  121. $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
  122. $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
  123. return $this;
  124. }
  125. // --------------------------------------------------------------------
  126. /**
  127. * Initialize the Email Data
  128. *
  129. * @access public
  130. * @return void
  131. */
  132. public function clear($clear_attachments = FALSE)
  133. {
  134. $this->_subject = "";
  135. $this->_body = "";
  136. $this->_finalbody = "";
  137. $this->_header_str = "";
  138. $this->_replyto_flag = FALSE;
  139. $this->_recipients = array();
  140. $this->_cc_array = array();
  141. $this->_bcc_array = array();
  142. $this->_headers = array();
  143. $this->_debug_msg = array();
  144. $this->_set_header('User-Agent', $this->useragent);
  145. $this->_set_header('Date', $this->_set_date());
  146. if ($clear_attachments !== FALSE)
  147. {
  148. $this->_attach_name = array();
  149. $this->_attach_type = array();
  150. $this->_attach_disp = array();
  151. }
  152. return $this;
  153. }
  154. // --------------------------------------------------------------------
  155. /**
  156. * Set FROM
  157. *
  158. * @access public
  159. * @param string
  160. * @param string
  161. * @return void
  162. */
  163. public function from($from, $name = '')
  164. {
  165. if (preg_match( '/\<(.*)\>/', $from, $match))
  166. {
  167. $from = $match['1'];
  168. }
  169. if ($this->validate)
  170. {
  171. $this->validate_email($this->_str_to_array($from));
  172. }
  173. // prepare the display name
  174. if ($name != '')
  175. {
  176. // only use Q encoding if there are characters that would require it
  177. if ( ! preg_match('/[\200-\377]/', $name))
  178. {
  179. // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
  180. $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
  181. }
  182. else
  183. {
  184. $name = $this->_prep_q_encoding($name, TRUE);
  185. }
  186. }
  187. $this->_set_header('From', $name.' <'.$from.'>');
  188. $this->_set_header('Return-Path', '<'.$from.'>');
  189. return $this;
  190. }
  191. // --------------------------------------------------------------------
  192. /**
  193. * Set Reply-to
  194. *
  195. * @access public
  196. * @param string
  197. * @param string
  198. * @return void
  199. */
  200. public function reply_to($replyto, $name = '')
  201. {
  202. if (preg_match( '/\<(.*)\>/', $replyto, $match))
  203. {
  204. $replyto = $match['1'];
  205. }
  206. if ($this->validate)
  207. {
  208. $this->validate_email($this->_str_to_array($replyto));
  209. }
  210. if ($name == '')
  211. {
  212. $name = $replyto;
  213. }
  214. if (strncmp($name, '"', 1) != 0)
  215. {
  216. $name = '"'.$name.'"';
  217. }
  218. $this->_set_header('Reply-To', $name.' <'.$replyto.'>');
  219. $this->_replyto_flag = TRUE;
  220. return $this;
  221. }
  222. // --------------------------------------------------------------------
  223. /**
  224. * Set Recipients
  225. *
  226. * @access public
  227. * @param string
  228. * @return void
  229. */
  230. public function to($to)
  231. {
  232. $to = $this->_str_to_array($to);
  233. $to = $this->clean_email($to);
  234. if ($this->validate)
  235. {
  236. $this->validate_email($to);
  237. }
  238. if ($this->_get_protocol() != 'mail')
  239. {
  240. $this->_set_header('To', implode(", ", $to));
  241. }
  242. switch ($this->_get_protocol())
  243. {
  244. case 'smtp' :
  245. $this->_recipients = $to;
  246. break;
  247. case 'sendmail' :
  248. case 'mail' :
  249. $this->_recipients = implode(", ", $to);
  250. break;
  251. }
  252. return $this;
  253. }
  254. // --------------------------------------------------------------------
  255. /**
  256. * Set CC
  257. *
  258. * @access public
  259. * @param string
  260. * @return void
  261. */
  262. public function cc($cc)
  263. {
  264. $cc = $this->_str_to_array($cc);
  265. $cc = $this->clean_email($cc);
  266. if ($this->validate)
  267. {
  268. $this->validate_email($cc);
  269. }
  270. $this->_set_header('Cc', implode(", ", $cc));
  271. if ($this->_get_protocol() == "smtp")
  272. {
  273. $this->_cc_array = $cc;
  274. }
  275. return $this;
  276. }
  277. // --------------------------------------------------------------------
  278. /**
  279. * Set BCC
  280. *
  281. * @access public
  282. * @param string
  283. * @param string
  284. * @return void
  285. */
  286. public function bcc($bcc, $limit = '')
  287. {
  288. if ($limit != '' && is_numeric($limit))
  289. {
  290. $this->bcc_batch_mode = TRUE;
  291. $this->bcc_batch_size = $limit;
  292. }
  293. $bcc = $this->_str_to_array($bcc);
  294. $bcc = $this->clean_email($bcc);
  295. if ($this->validate)
  296. {
  297. $this->validate_email($bcc);
  298. }
  299. if (($this->_get_protocol() == "smtp") OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
  300. {
  301. $this->_bcc_array = $bcc;
  302. }
  303. else
  304. {
  305. $this->_set_header('Bcc', implode(", ", $bcc));
  306. }
  307. return $this;
  308. }
  309. // --------------------------------------------------------------------
  310. /**
  311. * Set Email Subject
  312. *
  313. * @access public
  314. * @param string
  315. * @return void
  316. */
  317. public function subject($subject)
  318. {
  319. $subject = $this->_prep_q_encoding($subject);
  320. $this->_set_header('Subject', $subject);
  321. return $this;
  322. }
  323. // --------------------------------------------------------------------
  324. /**
  325. * Set Body
  326. *
  327. * @access public
  328. * @param string
  329. * @return void
  330. */
  331. public function message($body)
  332. {
  333. $this->_body = rtrim(str_replace("\r", "", $body));
  334. /* strip slashes only if magic quotes is ON
  335. if we do it with magic quotes OFF, it strips real, user-inputted chars.
  336. NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
  337. it will probably not exist in future versions at all.
  338. */
  339. if ( ! is_php('5.4') && get_magic_quotes_gpc())
  340. {
  341. $this->_body = stripslashes($this->_body);
  342. }
  343. return $this;
  344. }
  345. // --------------------------------------------------------------------
  346. /**
  347. * Assign file attachments
  348. *
  349. * @access public
  350. * @param string
  351. * @return void
  352. */
  353. public function attach($filename, $disposition = 'attachment')
  354. {
  355. $this->_attach_name[] = $filename;
  356. $this->_attach_type[] = $this->_mime_types(pathinfo($filename, PATHINFO_EXTENSION));
  357. $this->_attach_disp[] = $disposition; // Can also be 'inline' Not sure if it matters
  358. return $this;
  359. }
  360. // --------------------------------------------------------------------
  361. /**
  362. * Add a Header Item
  363. *
  364. * @access protected
  365. * @param string
  366. * @param string
  367. * @return void
  368. */
  369. protected function _set_header($header, $value)
  370. {
  371. $this->_headers[$header] = $value;
  372. }
  373. // --------------------------------------------------------------------
  374. /**
  375. * Convert a String to an Array
  376. *
  377. * @access protected
  378. * @param string
  379. * @return array
  380. */
  381. protected function _str_to_array($email)
  382. {
  383. if ( ! is_array($email))
  384. {
  385. if (strpos($email, ',') !== FALSE)
  386. {
  387. $email = preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY);
  388. }
  389. else
  390. {
  391. $email = trim($email);
  392. settype($email, "array");
  393. }
  394. }
  395. return $email;
  396. }
  397. // --------------------------------------------------------------------
  398. /**
  399. * Set Multipart Value
  400. *
  401. * @access public
  402. * @param string
  403. * @return void
  404. */
  405. public function set_alt_message($str = '')
  406. {
  407. $this->alt_message = $str;
  408. return $this;
  409. }
  410. // --------------------------------------------------------------------
  411. /**
  412. * Set Mailtype
  413. *
  414. * @access public
  415. * @param string
  416. * @return void
  417. */
  418. public function set_mailtype($type = 'text')
  419. {
  420. $this->mailtype = ($type == 'html') ? 'html' : 'text';
  421. return $this;
  422. }
  423. // --------------------------------------------------------------------
  424. /**
  425. * Set Wordwrap
  426. *
  427. * @access public
  428. * @param string
  429. * @return void
  430. */
  431. public function set_wordwrap($wordwrap = TRUE)
  432. {
  433. $this->wordwrap = ($wordwrap === FALSE) ? FALSE : TRUE;
  434. return $this;
  435. }
  436. // --------------------------------------------------------------------
  437. /**
  438. * Set Protocol
  439. *
  440. * @access public
  441. * @param string
  442. * @return void
  443. */
  444. public function set_protocol($protocol = 'mail')
  445. {
  446. $this->protocol = ( ! in_array($protocol, $this->_protocols, TRUE)) ? 'mail' : strtolower($protocol);
  447. return $this;
  448. }
  449. // --------------------------------------------------------------------
  450. /**
  451. * Set Priority
  452. *
  453. * @access public
  454. * @param integer
  455. * @return void
  456. */
  457. public function set_priority($n = 3)
  458. {
  459. if ( ! is_numeric($n))
  460. {
  461. $this->priority = 3;
  462. return;
  463. }
  464. if ($n < 1 OR $n > 5)
  465. {
  466. $this->priority = 3;
  467. return;
  468. }
  469. $this->priority = $n;
  470. return $this;
  471. }
  472. // --------------------------------------------------------------------
  473. /**
  474. * Set Newline Character
  475. *
  476. * @access public
  477. * @param string
  478. * @return void
  479. */
  480. public function set_newline($newline = "\n")
  481. {
  482. if ($newline != "\n" AND $newline != "\r\n" AND $newline != "\r")
  483. {
  484. $this->newline = "\n";
  485. return;
  486. }
  487. $this->newline = $newline;
  488. return $this;
  489. }
  490. // --------------------------------------------------------------------
  491. /**
  492. * Set CRLF
  493. *
  494. * @access public
  495. * @param string
  496. * @return void
  497. */
  498. public function set_crlf($crlf = "\n")
  499. {
  500. if ($crlf != "\n" AND $crlf != "\r\n" AND $crlf != "\r")
  501. {
  502. $this->crlf = "\n";
  503. return;
  504. }
  505. $this->crlf = $crlf;
  506. return $this;
  507. }
  508. // --------------------------------------------------------------------
  509. /**
  510. * Set Message Boundary
  511. *
  512. * @access protected
  513. * @return void
  514. */
  515. protected function _set_boundaries()
  516. {
  517. $this->_alt_boundary = "B_ALT_".uniqid(''); // multipart/alternative
  518. $this->_atc_boundary = "B_ATC_".uniqid(''); // attachment boundary
  519. }
  520. // --------------------------------------------------------------------
  521. /**
  522. * Get the Message ID
  523. *
  524. * @access protected
  525. * @return string
  526. */
  527. protected function _get_message_id()
  528. {
  529. $from = $this->_headers['Return-Path'];
  530. $from = str_replace(">", "", $from);
  531. $from = str_replace("<", "", $from);
  532. return "<".uniqid('').strstr($from, '@').">";
  533. }
  534. // --------------------------------------------------------------------
  535. /**
  536. * Get Mail Protocol
  537. *
  538. * @access protected
  539. * @param bool
  540. * @return string
  541. */
  542. protected function _get_protocol($return = TRUE)
  543. {
  544. $this->protocol = strtolower($this->protocol);
  545. $this->protocol = ( ! in_array($this->protocol, $this->_protocols, TRUE)) ? 'mail' : $this->protocol;
  546. if ($return == TRUE)
  547. {
  548. return $this->protocol;
  549. }
  550. }
  551. // --------------------------------------------------------------------
  552. /**
  553. * Get Mail Encoding
  554. *
  555. * @access protected
  556. * @param bool
  557. * @return string
  558. */
  559. protected function _get_encoding($return = TRUE)
  560. {
  561. $this->_encoding = ( ! in_array($this->_encoding, $this->_bit_depths)) ? '8bit' : $this->_encoding;
  562. foreach ($this->_base_charsets as $charset)
  563. {
  564. if (strncmp($charset, $this->charset, strlen($charset)) == 0)
  565. {
  566. $this->_encoding = '7bit';
  567. }
  568. }
  569. if ($return == TRUE)
  570. {
  571. return $this->_encoding;
  572. }
  573. }
  574. // --------------------------------------------------------------------
  575. /**
  576. * Get content type (text/html/attachment)
  577. *
  578. * @access protected
  579. * @return string
  580. */
  581. protected function _get_content_type()
  582. {
  583. if ($this->mailtype == 'html' && count($this->_attach_name) == 0)
  584. {
  585. return 'html';
  586. }
  587. elseif ($this->mailtype == 'html' && count($this->_attach_name) > 0)
  588. {
  589. return 'html-attach';
  590. }
  591. elseif ($this->mailtype == 'text' && count($this->_attach_name) > 0)
  592. {
  593. return 'plain-attach';
  594. }
  595. else
  596. {
  597. return 'plain';
  598. }
  599. }
  600. // --------------------------------------------------------------------
  601. /**
  602. * Set RFC 822 Date
  603. *
  604. * @access protected
  605. * @return string
  606. */
  607. protected function _set_date()
  608. {
  609. $timezone = date("Z");
  610. $operator = (strncmp($timezone, '-', 1) == 0) ? '-' : '+';
  611. $timezone = abs($timezone);
  612. $timezone = floor($timezone/3600) * 100 + ($timezone % 3600 ) / 60;
  613. return sprintf("%s %s%04d", date("D, j M Y H:i:s"), $operator, $timezone);
  614. }
  615. // --------------------------------------------------------------------
  616. /**
  617. * Mime message
  618. *
  619. * @access protected
  620. * @return string
  621. */
  622. protected function _get_mime_message()
  623. {
  624. return "This is a multi-part message in MIME format.".$this->newline."Your email application may not support this format.";
  625. }
  626. // --------------------------------------------------------------------
  627. /**
  628. * Validate Email Address
  629. *
  630. * @access public
  631. * @param string
  632. * @return bool
  633. */
  634. public function validate_email($email)
  635. {
  636. if ( ! is_array($email))
  637. {
  638. $this->_set_error_message('lang:email_must_be_array');
  639. return FALSE;
  640. }
  641. foreach ($email as $val)
  642. {
  643. if ( ! $this->valid_email($val))
  644. {
  645. $this->_set_error_message('lang:email_invalid_address', $val);
  646. return FALSE;
  647. }
  648. }
  649. return TRUE;
  650. }
  651. // --------------------------------------------------------------------
  652. /**
  653. * Email Validation
  654. *
  655. * @access public
  656. * @param string
  657. * @return bool
  658. */
  659. public function valid_email($address)
  660. {
  661. return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $address)) ? FALSE : TRUE;
  662. }
  663. // --------------------------------------------------------------------
  664. /**
  665. * Clean Extended Email Address: Joe Smith <joe@smith.com>
  666. *
  667. * @access public
  668. * @param string
  669. * @return string
  670. */
  671. public function clean_email($email)
  672. {
  673. if ( ! is_array($email))
  674. {
  675. if (preg_match('/\<(.*)\>/', $email, $match))
  676. {
  677. return $match['1'];
  678. }
  679. else
  680. {
  681. return $email;
  682. }
  683. }
  684. $clean_email = array();
  685. foreach ($email as $addy)
  686. {
  687. if (preg_match( '/\<(.*)\>/', $addy, $match))
  688. {
  689. $clean_email[] = $match['1'];
  690. }
  691. else
  692. {
  693. $clean_email[] = $addy;
  694. }
  695. }
  696. return $clean_email;
  697. }
  698. // --------------------------------------------------------------------
  699. /**
  700. * Build alternative plain text message
  701. *
  702. * This public function provides the raw message for use
  703. * in plain-text headers of HTML-formatted emails.
  704. * If the user hasn't specified his own alternative message
  705. * it creates one by stripping the HTML
  706. *
  707. * @access protected
  708. * @return string
  709. */
  710. protected function _get_alt_message()
  711. {
  712. if ($this->alt_message != "")
  713. {
  714. return $this->word_wrap($this->alt_message, '76');
  715. }
  716. if (preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match))
  717. {
  718. $body = $match['1'];
  719. }
  720. else
  721. {
  722. $body = $this->_body;
  723. }
  724. $body = trim(strip_tags($body));
  725. $body = preg_replace( '#<!--(.*)--\>#', "", $body);
  726. $body = str_replace("\t", "", $body);
  727. for ($i = 20; $i >= 3; $i--)
  728. {
  729. $n = "";
  730. for ($x = 1; $x <= $i; $x ++)
  731. {
  732. $n .= "\n";
  733. }
  734. $body = str_replace($n, "\n\n", $body);
  735. }
  736. return $this->word_wrap($body, '76');
  737. }
  738. // --------------------------------------------------------------------
  739. /**
  740. * Word Wrap
  741. *
  742. * @access public
  743. * @param string
  744. * @param integer
  745. * @return string
  746. */
  747. public function word_wrap($str, $charlim = '')
  748. {
  749. // Se the character limit
  750. if ($charlim == '')
  751. {
  752. $charlim = ($this->wrapchars == "") ? "76" : $this->wrapchars;
  753. }
  754. // Reduce multiple spaces
  755. $str = preg_replace("| +|", " ", $str);
  756. // Standardize newlines
  757. if (strpos($str, "\r") !== FALSE)
  758. {
  759. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  760. }
  761. // If the current word is surrounded by {unwrap} tags we'll
  762. // strip the entire chunk and replace it with a marker.
  763. $unwrap = array();
  764. if (preg_match_all("|(\{unwrap\}.+?\{/unwrap\})|s", $str, $matches))
  765. {
  766. for ($i = 0; $i < count($matches['0']); $i++)
  767. {
  768. $unwrap[] = $matches['1'][$i];
  769. $str = str_replace($matches['1'][$i], "{{unwrapped".$i."}}", $str);
  770. }
  771. }
  772. // Use PHP's native public function to do the initial wordwrap.
  773. // We set the cut flag to FALSE so that any individual words that are
  774. // too long get left alone. In the next step we'll deal with them.
  775. $str = wordwrap($str, $charlim, "\n", FALSE);
  776. // Split the string into individual lines of text and cycle through them
  777. $output = "";
  778. foreach (explode("\n", $str) as $line)
  779. {
  780. // Is the line within the allowed character count?
  781. // If so we'll join it to the output and continue
  782. if (strlen($line) <= $charlim)
  783. {
  784. $output .= $line.$this->newline;
  785. continue;
  786. }
  787. $temp = '';
  788. while ((strlen($line)) > $charlim)
  789. {
  790. // If the over-length word is a URL we won't wrap it
  791. if (preg_match("!\[url.+\]|://|wwww.!", $line))
  792. {
  793. break;
  794. }
  795. // Trim the word down
  796. $temp .= substr($line, 0, $charlim-1);
  797. $line = substr($line, $charlim-1);
  798. }
  799. // If $temp contains data it means we had to split up an over-length
  800. // word into smaller chunks so we'll add it back to our current line
  801. if ($temp != '')
  802. {
  803. $output .= $temp.$this->newline.$line;
  804. }
  805. else
  806. {
  807. $output .= $line;
  808. }
  809. $output .= $this->newline;
  810. }
  811. // Put our markers back
  812. if (count($unwrap) > 0)
  813. {
  814. foreach ($unwrap as $key => $val)
  815. {
  816. $output = str_replace("{{unwrapped".$key."}}", $val, $output);
  817. }
  818. }
  819. return $output;
  820. }
  821. // --------------------------------------------------------------------
  822. /**
  823. * Build final headers
  824. *
  825. * @access protected
  826. * @param string
  827. * @return string
  828. */
  829. protected function _build_headers()
  830. {
  831. $this->_set_header('X-Sender', $this->clean_email($this->_headers['From']));
  832. $this->_set_header('X-Mailer', $this->useragent);
  833. $this->_set_header('X-Priority', $this->_priorities[$this->priority - 1]);
  834. $this->_set_header('Message-ID', $this->_get_message_id());
  835. $this->_set_header('Mime-Version', '1.0');
  836. }
  837. // --------------------------------------------------------------------
  838. /**
  839. * Write Headers as a string
  840. *
  841. * @access protected
  842. * @return void
  843. */
  844. protected function _write_headers()
  845. {
  846. if ($this->protocol == 'mail')
  847. {
  848. $this->_subject = $this->_headers['Subject'];
  849. unset($this->_headers['Subject']);
  850. }
  851. reset($this->_headers);
  852. $this->_header_str = "";
  853. foreach ($this->_headers as $key => $val)
  854. {
  855. $val = trim($val);
  856. if ($val != "")
  857. {
  858. $this->_header_str .= $key.": ".$val.$this->newline;
  859. }
  860. }
  861. if ($this->_get_protocol() == 'mail')
  862. {
  863. $this->_header_str = rtrim($this->_header_str);
  864. }
  865. }
  866. // --------------------------------------------------------------------
  867. /**
  868. * Build Final Body and attachments
  869. *
  870. * @access protected
  871. * @return void
  872. */
  873. protected function _build_message()
  874. {
  875. if ($this->wordwrap === TRUE AND $this->mailtype != 'html')
  876. {
  877. $this->_body = $this->word_wrap($this->_body);
  878. }
  879. $this->_set_boundaries();
  880. $this->_write_headers();
  881. $hdr = ($this->_get_protocol() == 'mail') ? $this->newline : '';
  882. $body = '';
  883. switch ($this->_get_content_type())
  884. {
  885. case 'plain' :
  886. $hdr .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  887. $hdr .= "Content-Transfer-Encoding: " . $this->_get_encoding();
  888. if ($this->_get_protocol() == 'mail')
  889. {
  890. $this->_header_str .= rtrim($hdr);
  891. $this->_finalbody = $this->_body;
  892. }
  893. else
  894. {
  895. $this->_finalbody = $hdr . $this->newline . $this->newline . $this->_body;
  896. }
  897. return;
  898. break;
  899. case 'html' :
  900. if ($this->send_multipart === FALSE)
  901. {
  902. $hdr .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
  903. $hdr .= "Content-Transfer-Encoding: quoted-printable";
  904. }
  905. else
  906. {
  907. $hdr .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline . $this->newline;
  908. $body .= $this->_get_mime_message() . $this->newline . $this->newline;
  909. $body .= "--" . $this->_alt_boundary . $this->newline;
  910. $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  911. $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
  912. $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
  913. $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
  914. $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
  915. }
  916. $this->_finalbody = $body . $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
  917. if ($this->_get_protocol() == 'mail')
  918. {
  919. $this->_header_str .= rtrim($hdr);
  920. }
  921. else
  922. {
  923. $this->_finalbody = $hdr . $this->_finalbody;
  924. }
  925. if ($this->send_multipart !== FALSE)
  926. {
  927. $this->_finalbody .= "--" . $this->_alt_boundary . "--";
  928. }
  929. return;
  930. break;
  931. case 'plain-attach' :
  932. $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
  933. if ($this->_get_protocol() == 'mail')
  934. {
  935. $this->_header_str .= rtrim($hdr);
  936. }
  937. $body .= $this->_get_mime_message() . $this->newline . $this->newline;
  938. $body .= "--" . $this->_atc_boundary . $this->newline;
  939. $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  940. $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
  941. $body .= $this->_body . $this->newline . $this->newline;
  942. break;
  943. case 'html-attach' :
  944. $hdr .= "Content-Type: multipart/".$this->multipart."; boundary=\"" . $this->_atc_boundary."\"" . $this->newline . $this->newline;
  945. if ($this->_get_protocol() == 'mail')
  946. {
  947. $this->_header_str .= rtrim($hdr);
  948. }
  949. $body .= $this->_get_mime_message() . $this->newline . $this->newline;
  950. $body .= "--" . $this->_atc_boundary . $this->newline;
  951. $body .= "Content-Type: multipart/alternative; boundary=\"" . $this->_alt_boundary . "\"" . $this->newline .$this->newline;
  952. $body .= "--" . $this->_alt_boundary . $this->newline;
  953. $body .= "Content-Type: text/plain; charset=" . $this->charset . $this->newline;
  954. $body .= "Content-Transfer-Encoding: " . $this->_get_encoding() . $this->newline . $this->newline;
  955. $body .= $this->_get_alt_message() . $this->newline . $this->newline . "--" . $this->_alt_boundary . $this->newline;
  956. $body .= "Content-Type: text/html; charset=" . $this->charset . $this->newline;
  957. $body .= "Content-Transfer-Encoding: quoted-printable" . $this->newline . $this->newline;
  958. $body .= $this->_prep_quoted_printable($this->_body) . $this->newline . $this->newline;
  959. $body .= "--" . $this->_alt_boundary . "--" . $this->newline . $this->newline;
  960. break;
  961. }
  962. $attachment = array();
  963. $z = 0;
  964. for ($i=0; $i < count($this->_attach_name); $i++)
  965. {
  966. $filename = $this->_attach_name[$i];
  967. $basename = basename($filename);
  968. $ctype = $this->_attach_type[$i];
  969. if ( ! file_exists($filename))
  970. {
  971. $this->_set_error_message('lang:email_attachment_missing', $filename);
  972. return FALSE;
  973. }
  974. $h = "--".$this->_atc_boundary.$this->newline;
  975. $h .= "Content-type: ".$ctype."; ";
  976. $h .= "name=\"".$basename."\"".$this->newline;
  977. $h .= "Content-Disposition: ".$this->_attach_disp[$i].";".$this->newline;
  978. $h .= "Content-Transfer-Encoding: base64".$this->newline;
  979. $attachment[$z++] = $h;
  980. $file = filesize($filename) +1;
  981. if ( ! $fp = fopen($filename, FOPEN_READ))
  982. {
  983. $this->_set_error_message('lang:email_attachment_unreadable', $filename);
  984. return FALSE;
  985. }
  986. $attachment[$z++] = chunk_split(base64_encode(fread($fp, $file)));
  987. fclose($fp);
  988. }
  989. $body .= implode($this->newline, $attachment).$this->newline."--".$this->_atc_boundary."--";
  990. if ($this->_get_protocol() == 'mail')
  991. {
  992. $this->_finalbody = $body;
  993. }
  994. else
  995. {
  996. $this->_finalbody = $hdr . $body;
  997. }
  998. return;
  999. }
  1000. // --------------------------------------------------------------------
  1001. /**
  1002. * Prep Quoted Printable
  1003. *
  1004. * Prepares string for Quoted-Printable Content-Transfer-Encoding
  1005. * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
  1006. *
  1007. * @access protected
  1008. * @param string
  1009. * @param integer
  1010. * @return string
  1011. */
  1012. protected function _prep_quoted_printable($str, $charlim = '')
  1013. {
  1014. // Set the character limit
  1015. // Don't allow over 76, as that will make servers and MUAs barf
  1016. // all over quoted-printable data
  1017. if ($charlim == '' OR $charlim > '76')
  1018. {
  1019. $charlim = '76';
  1020. }
  1021. // Reduce multiple spaces
  1022. $str = preg_replace("| +|", " ", $str);
  1023. // kill nulls
  1024. $str = preg_replace('/\x00+/', '', $str);
  1025. // Standardize newlines
  1026. if (strpos($str, "\r") !== FALSE)
  1027. {
  1028. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  1029. }
  1030. // We are intentionally wrapping so mail servers will encode characters
  1031. // properly and MUAs will behave, so {unwrap} must go!
  1032. $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
  1033. // Break into an array of lines
  1034. $lines = explode("\n", $str);
  1035. $escape = '=';
  1036. $output = '';
  1037. foreach ($lines as $line)
  1038. {
  1039. $length = strlen($line);
  1040. $temp = '';
  1041. // Loop through each character in the line to add soft-wrap
  1042. // characters at the end of a line " =\r\n" and add the newly
  1043. // processed line(s) to the output (see comment on $crlf class property)
  1044. for ($i = 0; $i < $length; $i++)
  1045. {
  1046. // Grab the next character
  1047. $char = substr($line, $i, 1);
  1048. $ascii = ord($char);
  1049. // Convert spaces and tabs but only if it's the end of the line
  1050. if ($i == ($length - 1))
  1051. {
  1052. $char = ($ascii == '32' OR $ascii == '9') ? $escape.sprintf('%02s', dechex($ascii)) : $char;
  1053. }
  1054. // encode = signs
  1055. if ($ascii == '61')
  1056. {
  1057. $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
  1058. }
  1059. // If we're at the character limit, add the line to the output,
  1060. // reset our temp variable, and keep on chuggin'
  1061. if ((strlen($temp) + strlen($char)) >= $charlim)
  1062. {
  1063. $output .= $temp.$escape.$this->crlf;
  1064. $temp = '';
  1065. }
  1066. // Add the character to our temporary line
  1067. $temp .= $char;
  1068. }
  1069. // Add our completed line to the output
  1070. $output .= $temp.$this->crlf;
  1071. }
  1072. // get rid of extra CRLF tacked onto the end
  1073. $output = substr($output, 0, strlen($this->crlf) * -1);
  1074. return $output;
  1075. }
  1076. // --------------------------------------------------------------------
  1077. /**
  1078. * Prep Q Encoding
  1079. *
  1080. * Performs "Q Encoding" on a string for use in email headers. It's related
  1081. * but not identical to quoted-printable, so it has its own method
  1082. *
  1083. * @access public
  1084. * @param str
  1085. * @param bool // set to TRUE for processing From: headers
  1086. * @return str
  1087. */
  1088. protected function _prep_q_encoding($str, $from = FALSE)
  1089. {
  1090. $str = str_replace(array("\r", "\n"), array('', ''), $str);
  1091. // Line length must not exceed 76 characters, so we adjust for
  1092. // a space, 7 extra characters =??Q??=, and the charset that we will add to each line
  1093. $limit = 75 - 7 - strlen($this->charset);
  1094. // these special characters must be converted too
  1095. $convert = array('_', '=', '?');
  1096. if ($from === TRUE)
  1097. {
  1098. $convert[] = ',';
  1099. $convert[] = ';';
  1100. }
  1101. $output = '';
  1102. $temp = '';
  1103. for ($i = 0, $length = strlen($str); $i < $length; $i++)
  1104. {
  1105. // Grab the next character
  1106. $char = substr($str, $i, 1);
  1107. $ascii = ord($char);
  1108. // convert ALL non-printable ASCII characters and our specials
  1109. if ($ascii < 32 OR $ascii > 126 OR in_array($char, $convert))
  1110. {
  1111. $char = '='.dechex($ascii);
  1112. }
  1113. // handle regular spaces a bit more compactly than =20
  1114. if ($ascii == 32)
  1115. {
  1116. $char = '_';
  1117. }
  1118. // If we're at the character limit, add the line to the output,
  1119. // reset our temp variable, and keep on chuggin'
  1120. if ((strlen($temp) + strlen($char)) >= $limit)
  1121. {
  1122. $output .= $temp.$this->crlf;
  1123. $temp = '';
  1124. }
  1125. // Add the character to our temporary line
  1126. $temp .= $char;
  1127. }
  1128. $str = $output.$temp;
  1129. // wrap each line with the shebang, charset, and transfer encoding
  1130. // the preceding space on successive lines is required for header "folding"
  1131. $str = trim(preg_replace('/^(.*)$/m', ' =?'.$this->charset.'?Q?$1?=', $str));
  1132. return $str;
  1133. }
  1134. // --------------------------------------------------------------------
  1135. /**
  1136. * Send Email
  1137. *
  1138. * @access public
  1139. * @return bool
  1140. */
  1141. public function send()
  1142. {
  1143. if ($this->_replyto_flag == FALSE)
  1144. {
  1145. $this->reply_to($this->_headers['From']);
  1146. }
  1147. if (( ! isset($this->_recipients) AND ! isset($this->_headers['To'])) AND
  1148. ( ! isset($this->_bcc_array) AND ! isset($this->_headers['Bcc'])) AND
  1149. ( ! isset($this->_headers['Cc'])))
  1150. {
  1151. $this->_set_error_message('lang:email_no_recipients');
  1152. return FALSE;
  1153. }
  1154. $this->_build_headers();
  1155. if ($this->bcc_batch_mode AND count($this->_bcc_array) > 0)
  1156. {
  1157. if (count($this->_bcc_array) > $this->bcc_batch_size)
  1158. return $this->batch_bcc_send();
  1159. }
  1160. $this->_build_message();
  1161. if ( ! $this->_spool_email())
  1162. {
  1163. return FALSE;
  1164. }
  1165. else
  1166. {
  1167. return TRUE;
  1168. }
  1169. }
  1170. // --------------------------------------------------------------------
  1171. /**
  1172. * Batch Bcc Send. Sends groups of BCCs in batches
  1173. *
  1174. * @access public
  1175. * @return bool
  1176. */
  1177. public function batch_bcc_send()
  1178. {
  1179. $float = $this->bcc_batch_size -1;
  1180. $set = "";
  1181. $chunk = array();
  1182. for ($i = 0; $i < count($this->_bcc_array); $i++)
  1183. {
  1184. if (isset($this->_bcc_array[$i]))
  1185. {
  1186. $set .= ", ".$this->_bcc_array[$i];
  1187. }
  1188. if ($i == $float)
  1189. {
  1190. $chunk[] = substr($set, 1);
  1191. $float = $float + $this->bcc_batch_size;
  1192. $set = "";
  1193. }
  1194. if ($i == count($this->_bcc_array)-1)
  1195. {
  1196. $chunk[] = substr($set, 1);
  1197. }
  1198. }
  1199. for ($i = 0; $i < count($chunk); $i++)
  1200. {
  1201. unset($this->_headers['Bcc']);
  1202. unset($bcc);
  1203. $bcc = $this->_str_to_array($chunk[$i]);
  1204. $bcc = $this->clean_email($bcc);
  1205. if ($this->protocol != 'smtp')
  1206. {
  1207. $this->_set_header('Bcc', implode(", ", $bcc));
  1208. }
  1209. else
  1210. {
  1211. $this->_bcc_array = $bcc;
  1212. }
  1213. $this->_build_message();
  1214. $this->_spool_email();
  1215. }
  1216. }
  1217. // --------------------------------------------------------------------
  1218. /**
  1219. * Unwrap special elements
  1220. *
  1221. * @access protected
  1222. * @return void
  1223. */
  1224. protected function _unwrap_specials()
  1225. {
  1226. $this->_finalbody = preg_replace_callback("/\{unwrap\}(.*?)\{\/unwrap\}/si", array($this, '_remove_nl_callback'), $this->_finalbody);
  1227. }
  1228. // --------------------------------------------------------------------
  1229. /**
  1230. * Strip line-breaks via callback
  1231. *
  1232. * @access protected
  1233. * @return string
  1234. */
  1235. protected function _remove_nl_callback($matches)
  1236. {
  1237. if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
  1238. {
  1239. $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
  1240. }
  1241. return $matches[1];
  1242. }
  1243. // --------------------------------------------------------------------
  1244. /**
  1245. * Spool mail to the mail server
  1246. *
  1247. * @access protected
  1248. * @return bool
  1249. */
  1250. protected function _spool_email()
  1251. {
  1252. $this->_unwrap_specials();
  1253. switch ($this->_get_protocol())
  1254. {
  1255. case 'mail' :
  1256. if ( ! $this->_send_with_mail())
  1257. {
  1258. $this->_set_error_message('lang:email_send_failure_phpmail');
  1259. return FALSE;
  1260. }
  1261. break;
  1262. case 'sendmail' :
  1263. if ( ! $this->_send_with_sendmail())
  1264. {
  1265. $this->_set_error_message('lang:email_send_failure_sendmail');
  1266. return FALSE;
  1267. }
  1268. break;
  1269. case 'smtp' :
  1270. if ( ! $this->_send_with_smtp())
  1271. {
  1272. $this->_set_error_message('lang:email_send_failure_smtp');
  1273. return FALSE;
  1274. }
  1275. break;
  1276. }
  1277. $this->_set_error_message('lang:email_sent', $this->_get_protocol());
  1278. return TRUE;
  1279. }
  1280. // --------------------------------------------------------------------
  1281. /**
  1282. * Send using mail()
  1283. *
  1284. * @access protected
  1285. * @return bool
  1286. */
  1287. protected function _send_with_mail()
  1288. {
  1289. if ($this->_safe_mode == TRUE)
  1290. {
  1291. if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str))
  1292. {
  1293. return FALSE;
  1294. }
  1295. else
  1296. {
  1297. return TRUE;
  1298. }
  1299. }
  1300. else
  1301. {
  1302. // most documentation of sendmail using the "-f" flag lacks a space after it, however
  1303. // we've encountered servers that seem to require it to be in place.
  1304. if ( ! mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, "-f ".$this->clean_email($this->_headers['From'])))
  1305. {
  1306. return FALSE;
  1307. }
  1308. else
  1309. {
  1310. return TRUE;
  1311. }
  1312. }
  1313. }
  1314. // --------------------------------------------------------------------
  1315. /**
  1316. * Send using Sendmail
  1317. *
  1318. * @access protected
  1319. * @return bool
  1320. */
  1321. protected function _send_with_sendmail()
  1322. {
  1323. $fp = @popen($this->mailpath . " -oi -f ".$this->clean_email($this->_headers['From'])." -t", 'w');
  1324. if ($fp === FALSE OR $fp === NULL)
  1325. {
  1326. // server probably has popen disabled, so nothing we can do to get a verbose error.
  1327. return FALSE;
  1328. }
  1329. fputs($fp, $this->_header_str);
  1330. fputs($fp, $this->_finalbody);
  1331. $status = pclose($fp);
  1332. if (version_compare(PHP_VERSION, '4.2.3') == -1)
  1333. {
  1334. $status = $status >> 8 & 0xFF;
  1335. }
  1336. if ($status != 0)
  1337. {
  1338. $this->_set_error_message('lang:email_exit_status', $status);
  1339. $this->_set_error_message('lang:email_no_socket');
  1340. return FALSE;
  1341. }
  1342. return TRUE;
  1343. }
  1344. // --------------------------------------------------------------------
  1345. /**
  1346. * Send using SMTP
  1347. *
  1348. * @access protected
  1349. * @return bool
  1350. */
  1351. protected function _send_with_smtp()
  1352. {
  1353. if ($this->smtp_host == '')
  1354. {
  1355. $this->_set_error_message('lang:email_no_hostname');
  1356. return FALSE;
  1357. }
  1358. $this->_smtp_connect();
  1359. $this->_smtp_authenticate();
  1360. $this->_send_command('from', $this->clean_email($this->_headers['From']));
  1361. foreach ($this->_recipients as $val)
  1362. {
  1363. $this->_send_command('to', $val);
  1364. }
  1365. if (count($this->_cc_array) > 0)
  1366. {
  1367. foreach ($this->_cc_array as $val)
  1368. {
  1369. if ($val != "")
  1370. {
  1371. $this->_send_command('to', $val);
  1372. }
  1373. }
  1374. }
  1375. if (count($this->_bcc_array) > 0)
  1376. {
  1377. foreach ($this->_bcc_array as $val)
  1378. {
  1379. if ($val != "")
  1380. {
  1381. $this->_send_command('to', $val);
  1382. }
  1383. }
  1384. }
  1385. $this->_send_command('data');
  1386. // perform dot transformation on any lines that begin with a dot
  1387. $this->_send_data($this->_header_str . preg_replace('/^\./m', '..$1', $this->_finalbody));
  1388. $this->_send_data('.');
  1389. $reply = $this->_get_smtp_data();
  1390. $this->_set_error_message($reply);
  1391. if (strncmp($reply, '250', 3) != 0)
  1392. {
  1393. $this->_set_error_message('lang:email_smtp_error', $reply);
  1394. return FALSE;
  1395. }
  1396. $this->_send_command('quit');
  1397. return TRUE;
  1398. }
  1399. // --------------------------------------------------------------------
  1400. /**
  1401. * SMTP Connect
  1402. *
  1403. * @access protected
  1404. * @param string
  1405. * @return string
  1406. */
  1407. protected function _smtp_connect()
  1408. {
  1409. $ssl = NULL;
  1410. if ($this->smtp_crypto == 'ssl')
  1411. $ssl = 'ssl://';
  1412. $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
  1413. $this->smtp_port,
  1414. $errno,
  1415. $errstr,
  1416. $this->smtp_timeout);
  1417. if ( ! is_resource($this->_smtp_connect))
  1418. {
  1419. $this->_set_error_message('lang:email_smtp_error', $errno." ".$errstr);
  1420. return FALSE;
  1421. }
  1422. $this->_set_error_message($this->_get_smtp_data());
  1423. if ($this->smtp_crypto == 'tls')
  1424. {
  1425. $this->_send_command('hello');
  1426. $this->_send_command('starttls');
  1427. stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
  1428. }
  1429. return $this->_send_command('hello');
  1430. }
  1431. // --------------------------------------------------------------------
  1432. /**
  1433. * Send SMTP command
  1434. *
  1435. * @access protected
  1436. * @param string
  1437. * @param string
  1438. * @return string
  1439. */
  1440. protected function _send_command($cmd, $data = '')
  1441. {
  1442. switch ($cmd)
  1443. {
  1444. case 'hello' :
  1445. if ($this->_smtp_auth OR $this->_get_encoding() == '8bit')
  1446. $this->_send_data('EHLO '.$this->_get_hostname());
  1447. else
  1448. $this->_send_data('HELO '.$this->_get_hostname());
  1449. $resp = 250;
  1450. break;
  1451. case 'starttls' :
  1452. $this->_send_data('STARTTLS');
  1453. $resp = 220;
  1454. break;
  1455. case 'from' :
  1456. $this->_send_data('MAIL FROM:<'.$data.'>');
  1457. $resp = 250;
  1458. break;
  1459. case 'to' :
  1460. $this->_send_data('RCPT TO:<'.$data.'>');
  1461. $resp = 250;
  1462. break;
  1463. case 'data' :
  1464. $this->_send_data('DATA');
  1465. $resp = 354;
  1466. break;
  1467. case 'quit' :
  1468. $this->_send_data('QUIT');
  1469. $resp = 221;
  1470. break;
  1471. }
  1472. $reply = $this->_get_smtp_data();
  1473. $this->_debug_msg[] = "<pre>".$cmd.": ".$reply."</pre>";
  1474. if (substr($reply, 0, 3) != $resp)
  1475. {
  1476. $this->_set_error_message('lang:email_smtp_error', $reply);
  1477. return FALSE;
  1478. }
  1479. if ($cmd == 'quit')
  1480. {
  1481. fclose($this->_smtp_connect);
  1482. }
  1483. return TRUE;
  1484. }
  1485. // --------------------------------------------------------------------
  1486. /**
  1487. * SMTP Authenticate
  1488. *
  1489. * @access protected
  1490. * @return bool
  1491. */
  1492. protected function _smtp_authenticate()
  1493. {
  1494. if ( ! $this->_smtp_auth)
  1495. {
  1496. return TRUE;
  1497. }
  1498. if ($this->smtp_user == "" AND $this->smtp_pass == "")
  1499. {
  1500. $this->_set_error_message('lang:email_no_smtp_unpw');
  1501. return FALSE;
  1502. }
  1503. $this->_send_data('AUTH LOGIN');
  1504. $reply = $this->_get_smtp_data();
  1505. if (strncmp($reply, '334', 3) != 0)
  1506. {
  1507. $this->_set_error_message('lang:email_failed_smtp_login', $reply);
  1508. return FALSE;
  1509. }
  1510. $this->_send_data(base64_encode($this->smtp_user));
  1511. $reply = $this->_get_smtp_data();
  1512. if (strncmp($reply, '334', 3) != 0)
  1513. {
  1514. $this->_set_error_message('lang:email_smtp_auth_un', $reply);
  1515. return FALSE;
  1516. }
  1517. $this->_send_data(base64_encode($this->smtp_pass));
  1518. $reply = $this->_get_smtp_data();
  1519. if (strncmp($reply, '235', 3) != 0)
  1520. {
  1521. $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
  1522. return FALSE;
  1523. }
  1524. return TRUE;
  1525. }
  1526. // --------------------------------------------------------------------
  1527. /**
  1528. * Send SMTP data
  1529. *
  1530. * @access protected
  1531. * @return bool
  1532. */
  1533. protected function _send_data($data)
  1534. {
  1535. if ( ! fwrite($this->_smtp_connect, $data . $this->newline))
  1536. {
  1537. $this->_set_error_message('lang:email_smtp_data_failure', $data);
  1538. return FALSE;
  1539. }
  1540. else
  1541. {
  1542. return TRUE;
  1543. }
  1544. }
  1545. // --------------------------------------------------------------------
  1546. /**
  1547. * Get SMTP data
  1548. *
  1549. * @access protected
  1550. * @return string
  1551. */
  1552. protected function _get_smtp_data()
  1553. {
  1554. $data = "";
  1555. while ($str = fgets($this->_smtp_connect, 512))
  1556. {
  1557. $data .= $str;
  1558. if (substr($str, 3, 1) == " ")
  1559. {
  1560. break;
  1561. }
  1562. }
  1563. return $data;
  1564. }
  1565. // --------------------------------------------------------------------
  1566. /**
  1567. * Get Hostname
  1568. *
  1569. * @access protected
  1570. * @return string
  1571. */
  1572. protected function _get_hostname()
  1573. {
  1574. return (isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : 'localhost.localdomain';
  1575. }
  1576. // --------------------------------------------------------------------
  1577. /**
  1578. * Get IP
  1579. *
  1580. * @access protected
  1581. * @return string
  1582. */
  1583. protected function _get_ip()
  1584. {
  1585. if ($this->_IP !== FALSE)
  1586. {
  1587. return $this->_IP;
  1588. }
  1589. $cip = (isset($_SERVER['HTTP_CLIENT_IP']) AND $_SERVER['HTTP_CLIENT_IP'] != "") ? $_SERVER['HTTP_CLIENT_IP'] : FALSE;
  1590. $rip = (isset($_SERVER['REMOTE_ADDR']) AND $_SERVER['REMOTE_ADDR'] != "") ? $_SERVER['REMOTE_ADDR'] : FALSE;
  1591. $fip = (isset($_SERVER['HTTP_X_FORWARDED_FOR']) AND $_SERVER['HTTP_X_FORWARDED_FOR'] != "") ? $_SERVER['HTTP_X_FORWARDED_FOR'] : FALSE;
  1592. if ($cip && $rip) $this->_IP = $cip;
  1593. elseif ($rip) $this->_IP = $rip;
  1594. elseif ($cip) $this->_IP = $cip;
  1595. elseif ($fip) $this->_IP = $fip;
  1596. if (strpos($this->_IP, ',') !== FALSE)
  1597. {
  1598. $x = explode(',', $this->_IP);
  1599. $this->_IP = end($x);
  1600. }
  1601. if ( ! preg_match( "/^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$/", $this->_IP))
  1602. {
  1603. $this->_IP = '0.0.0.0';
  1604. }
  1605. unset($cip);
  1606. unset($rip);
  1607. unset($fip);
  1608. return $this->_IP;
  1609. }
  1610. // --------------------------------------------------------------------
  1611. /**
  1612. * Get Debug Message
  1613. *
  1614. * @access public
  1615. * @return string
  1616. */
  1617. public function print_debugger()
  1618. {
  1619. $msg = '';
  1620. if (count($this->_debug_msg) > 0)
  1621. {
  1622. foreach ($this->_debug_msg as $val)
  1623. {
  1624. $msg .= $val;
  1625. }
  1626. }
  1627. $msg .= "<pre>".htmlspecialchars($this->_header_str)."\n".htmlspecialchars($this->_subject)."\n".htmlspecialchars($this->_finalbody).'</pre>';
  1628. return $msg;
  1629. }
  1630. // --------------------------------------------------------------------
  1631. /**
  1632. * Set Message
  1633. *
  1634. * @access protected
  1635. * @param string
  1636. * @return string
  1637. */
  1638. protected function _set_error_message($msg, $val = '')
  1639. {
  1640. $CI =& get_instance();
  1641. $CI->lang->load('email');
  1642. if (substr($msg, 0, 5) != 'lang:' || FALSE === ($line = $CI->lang->line(substr($msg, 5))))
  1643. {
  1644. $this->_debug_msg[] = str_replace('%s', $val, $msg)."<br />";
  1645. }
  1646. else
  1647. {
  1648. $this->_debug_msg[] = str_replace('%s', $val, $line)."<br />";
  1649. }
  1650. }
  1651. // --------------------------------------------------------------------
  1652. /**
  1653. * Mime Types
  1654. *
  1655. * @access protected
  1656. * @param string
  1657. * @return string
  1658. */
  1659. protected function _mime_types($ext = "")
  1660. {
  1661. $mimes = array( 'hqx' => 'application/mac-binhex40',
  1662. 'cpt' => 'application/mac-compactpro',
  1663. 'doc' => 'application/msword',
  1664. 'bin' => 'application/macbinary',
  1665. 'dms' => 'application/octet-stream',
  1666. 'lha' => 'application/octet-stream',
  1667. 'lzh' => 'application/octet-stream',
  1668. 'exe' => 'application/octet-stream',
  1669. 'class' => 'application/octet-stream',
  1670. 'psd' => 'application/octet-stream',
  1671. 'so' => 'application/octet-stream',
  1672. 'sea' => 'application/octet-stream',
  1673. 'dll' => 'application/octet-stream',
  1674. 'oda' => 'application/oda',
  1675. 'pdf' => 'application/pdf',
  1676. 'ai' => 'application/postscript',
  1677. 'eps' => 'application/postscript',
  1678. 'ps' => 'application/postscript',
  1679. 'smi' => 'application/smil',
  1680. 'smil' => 'application/smil',
  1681. 'mif' => 'application/vnd.mif',
  1682. 'xls' => 'application/vnd.ms-excel',
  1683. 'ppt' => 'application/vnd.ms-powerpoint',
  1684. 'wbxml' => 'application/vnd.wap.wbxml',
  1685. 'wmlc' => 'application/vnd.wap.wmlc',
  1686. 'dcr' => 'application/x-director',
  1687. 'dir' => 'application/x-director',
  1688. 'dxr' => 'application/x-director',
  1689. 'dvi' => 'application/x-dvi',
  1690. 'gtar' => 'application/x-gtar',
  1691. 'php' => 'application/x-httpd-php',
  1692. 'php4' => 'application/x-httpd-php',
  1693. 'php3' => 'application/x-httpd-php',
  1694. 'phtml' => 'application/x-httpd-php',
  1695. 'phps' => 'application/x-httpd-php-source',
  1696. 'js' => 'application/x-javascript',
  1697. 'swf' => 'application/x-shockwave-flash',
  1698. 'sit' => 'application/x-stuffit',
  1699. 'tar' => 'application/x-tar',
  1700. 'tgz' => 'application/x-tar',
  1701. 'xhtml' => 'application/xhtml+xml',
  1702. 'xht' => 'application/xhtml+xml',
  1703. 'zip' => 'application/zip',
  1704. 'mid' => 'audio/midi',
  1705. 'midi' => 'audio/midi',
  1706. 'mpga' => 'audio/mpeg',
  1707. 'mp2' => 'audio/mpeg',
  1708. 'mp3' => 'audio/mpeg',
  1709. 'aif' => 'audio/x-aiff',
  1710. 'aiff' => 'audio/x-aiff',
  1711. 'aifc' => 'audio/x-aiff',
  1712. 'ram' => 'audio/x-pn-realaudio',
  1713. 'rm' => 'audio/x-pn-realaudio',
  1714. 'rpm' => 'audio/x-pn-realaudio-plugin',
  1715. 'ra' => 'audio/x-realaudio',
  1716. 'rv' => 'video/vnd.rn-realvideo',
  1717. 'wav' => 'audio/x-wav',
  1718. 'bmp' => 'image/bmp',
  1719. 'gif' => 'image/gif',
  1720. 'jpeg' => 'image/jpeg',
  1721. 'jpg' => 'image/jpeg',
  1722. 'jpe' => 'image/jpeg',
  1723. 'png' => 'image/png',
  1724. 'tiff' => 'image/tiff',
  1725. 'tif' => 'image/tiff',
  1726. 'css' => 'text/css',
  1727. 'html' => 'text/html',
  1728. 'htm' => 'text/html',
  1729. 'shtml' => 'text/html',
  1730. 'txt' => 'text/plain',
  1731. 'text' => 'text/plain',
  1732. 'log' => 'text/plain',
  1733. 'rtx' => 'text/richtext',
  1734. 'rtf' => 'text/rtf',
  1735. 'xml' => 'text/xml',
  1736. 'xsl' => 'text/xml',
  1737. 'mpeg' => 'video/mpeg',
  1738. 'mpg' => 'video/mpeg',
  1739. 'mpe' => 'video/mpeg',
  1740. 'qt' => 'video/quicktime',
  1741. 'mov' => 'video/quicktime',
  1742. 'avi' => 'video/x-msvideo',
  1743. 'movie' => 'video/x-sgi-movie',
  1744. 'doc' => 'application/msword',
  1745. 'word' => 'application/msword',
  1746. 'xl' => 'application/excel',
  1747. 'eml' => 'message/rfc822'
  1748. );
  1749. return ( ! isset($mimes[strtolower($ext)])) ? "application/x-unknown-content-type" : $mimes[strtolower($ext)];
  1750. }
  1751. }
  1752. // END CI_Email class
  1753. /* End of file Email.php */
  1754. /* Location: ./system/libraries/Email.php */