Email.php 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2017, British Columbia Institute of Technology
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining a copy
  12. * of this software and associated documentation files (the "Software"), to deal
  13. * in the Software without restriction, including without limitation the rights
  14. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  15. * copies of the Software, and to permit persons to whom the Software is
  16. * furnished to do so, subject to the following conditions:
  17. *
  18. * The above copyright notice and this permission notice shall be included in
  19. * all copies or substantial portions of the Software.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  25. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  26. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  27. * THE SOFTWARE.
  28. *
  29. * @package CodeIgniter
  30. * @author EllisLab Dev Team
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2017, British Columbia Institute of Technology (http://bcit.ca/)
  33. * @license http://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. /**
  40. * CodeIgniter Email Class
  41. *
  42. * Permits email to be sent using Mail, Sendmail, or SMTP.
  43. *
  44. * @package CodeIgniter
  45. * @subpackage Libraries
  46. * @category Libraries
  47. * @author EllisLab Dev Team
  48. * @link https://codeigniter.com/user_guide/libraries/email.html
  49. */
  50. class CI_Email {
  51. /**
  52. * Used as the User-Agent and X-Mailer headers' value.
  53. *
  54. * @var string
  55. */
  56. public $useragent = 'CodeIgniter';
  57. /**
  58. * Path to the Sendmail binary.
  59. *
  60. * @var string
  61. */
  62. public $mailpath = '/usr/sbin/sendmail'; // Sendmail path
  63. /**
  64. * Which method to use for sending e-mails.
  65. *
  66. * @var string 'mail', 'sendmail' or 'smtp'
  67. */
  68. public $protocol = 'mail'; // mail/sendmail/smtp
  69. /**
  70. * STMP Server host
  71. *
  72. * @var string
  73. */
  74. public $smtp_host = '';
  75. /**
  76. * SMTP Username
  77. *
  78. * @var string
  79. */
  80. public $smtp_user = '';
  81. /**
  82. * SMTP Password
  83. *
  84. * @var string
  85. */
  86. public $smtp_pass = '';
  87. /**
  88. * SMTP Server port
  89. *
  90. * @var int
  91. */
  92. public $smtp_port = 25;
  93. /**
  94. * SMTP connection timeout in seconds
  95. *
  96. * @var int
  97. */
  98. public $smtp_timeout = 5;
  99. /**
  100. * SMTP persistent connection
  101. *
  102. * @var bool
  103. */
  104. public $smtp_keepalive = FALSE;
  105. /**
  106. * SMTP Encryption
  107. *
  108. * @var string empty, 'tls' or 'ssl'
  109. */
  110. public $smtp_crypto = '';
  111. /**
  112. * Whether to apply word-wrapping to the message body.
  113. *
  114. * @var bool
  115. */
  116. public $wordwrap = TRUE;
  117. /**
  118. * Number of characters to wrap at.
  119. *
  120. * @see CI_Email::$wordwrap
  121. * @var int
  122. */
  123. public $wrapchars = 76;
  124. /**
  125. * Message format.
  126. *
  127. * @var string 'text' or 'html'
  128. */
  129. public $mailtype = 'text';
  130. /**
  131. * Character set (default: utf-8)
  132. *
  133. * @var string
  134. */
  135. public $charset = 'UTF-8';
  136. /**
  137. * Alternative message (for HTML messages only)
  138. *
  139. * @var string
  140. */
  141. public $alt_message = '';
  142. /**
  143. * Whether to validate e-mail addresses.
  144. *
  145. * @var bool
  146. */
  147. public $validate = FALSE;
  148. /**
  149. * X-Priority header value.
  150. *
  151. * @var int 1-5
  152. */
  153. public $priority = 3; // Default priority (1 - 5)
  154. /**
  155. * Newline character sequence.
  156. * Use "\r\n" to comply with RFC 822.
  157. *
  158. * @link http://www.ietf.org/rfc/rfc822.txt
  159. * @var string "\r\n" or "\n"
  160. */
  161. public $newline = "\n"; // Default newline. "\r\n" or "\n" (Use "\r\n" to comply with RFC 822)
  162. /**
  163. * CRLF character sequence
  164. *
  165. * RFC 2045 specifies that for 'quoted-printable' encoding,
  166. * "\r\n" must be used. However, it appears that some servers
  167. * (even on the receiving end) don't handle it properly and
  168. * switching to "\n", while improper, is the only solution
  169. * that seems to work for all environments.
  170. *
  171. * @link http://www.ietf.org/rfc/rfc822.txt
  172. * @var string
  173. */
  174. public $crlf = "\n";
  175. /**
  176. * Whether to use Delivery Status Notification.
  177. *
  178. * @var bool
  179. */
  180. public $dsn = FALSE;
  181. /**
  182. * Whether to send multipart alternatives.
  183. * Yahoo! doesn't seem to like these.
  184. *
  185. * @var bool
  186. */
  187. public $send_multipart = TRUE;
  188. /**
  189. * Whether to send messages to BCC recipients in batches.
  190. *
  191. * @var bool
  192. */
  193. public $bcc_batch_mode = FALSE;
  194. /**
  195. * BCC Batch max number size.
  196. *
  197. * @see CI_Email::$bcc_batch_mode
  198. * @var int
  199. */
  200. public $bcc_batch_size = 200;
  201. // --------------------------------------------------------------------
  202. /**
  203. * Whether PHP is running in safe mode. Initialized by the class constructor.
  204. *
  205. * @var bool
  206. */
  207. protected $_safe_mode = FALSE;
  208. /**
  209. * Subject header
  210. *
  211. * @var string
  212. */
  213. protected $_subject = '';
  214. /**
  215. * Message body
  216. *
  217. * @var string
  218. */
  219. protected $_body = '';
  220. /**
  221. * Final message body to be sent.
  222. *
  223. * @var string
  224. */
  225. protected $_finalbody = '';
  226. /**
  227. * Final headers to send
  228. *
  229. * @var string
  230. */
  231. protected $_header_str = '';
  232. /**
  233. * SMTP Connection socket placeholder
  234. *
  235. * @var resource
  236. */
  237. protected $_smtp_connect = '';
  238. /**
  239. * Mail encoding
  240. *
  241. * @var string '8bit' or '7bit'
  242. */
  243. protected $_encoding = '8bit';
  244. /**
  245. * Whether to perform SMTP authentication
  246. *
  247. * @var bool
  248. */
  249. protected $_smtp_auth = FALSE;
  250. /**
  251. * Whether to send a Reply-To header
  252. *
  253. * @var bool
  254. */
  255. protected $_replyto_flag = FALSE;
  256. /**
  257. * Debug messages
  258. *
  259. * @see CI_Email::print_debugger()
  260. * @var string
  261. */
  262. protected $_debug_msg = array();
  263. /**
  264. * Recipients
  265. *
  266. * @var string[]
  267. */
  268. protected $_recipients = array();
  269. /**
  270. * CC Recipients
  271. *
  272. * @var string[]
  273. */
  274. protected $_cc_array = array();
  275. /**
  276. * BCC Recipients
  277. *
  278. * @var string[]
  279. */
  280. protected $_bcc_array = array();
  281. /**
  282. * Message headers
  283. *
  284. * @var string[]
  285. */
  286. protected $_headers = array();
  287. /**
  288. * Attachment data
  289. *
  290. * @var array
  291. */
  292. protected $_attachments = array();
  293. /**
  294. * Valid $protocol values
  295. *
  296. * @see CI_Email::$protocol
  297. * @var string[]
  298. */
  299. protected $_protocols = array('mail', 'sendmail', 'smtp');
  300. /**
  301. * Base charsets
  302. *
  303. * Character sets valid for 7-bit encoding,
  304. * excluding language suffix.
  305. *
  306. * @var string[]
  307. */
  308. protected $_base_charsets = array('us-ascii', 'iso-2022-');
  309. /**
  310. * Bit depths
  311. *
  312. * Valid mail encodings
  313. *
  314. * @see CI_Email::$_encoding
  315. * @var string[]
  316. */
  317. protected $_bit_depths = array('7bit', '8bit');
  318. /**
  319. * $priority translations
  320. *
  321. * Actual values to send with the X-Priority header
  322. *
  323. * @var string[]
  324. */
  325. protected $_priorities = array(
  326. 1 => '1 (Highest)',
  327. 2 => '2 (High)',
  328. 3 => '3 (Normal)',
  329. 4 => '4 (Low)',
  330. 5 => '5 (Lowest)'
  331. );
  332. /**
  333. * mbstring.func_overload flag
  334. *
  335. * @var bool
  336. */
  337. protected static $func_overload;
  338. // --------------------------------------------------------------------
  339. /**
  340. * Constructor - Sets Email Preferences
  341. *
  342. * The constructor can be passed an array of config values
  343. *
  344. * @param array $config = array()
  345. * @return void
  346. */
  347. public function __construct(array $config = array())
  348. {
  349. $this->charset = config_item('charset');
  350. $this->initialize($config);
  351. $this->_safe_mode = ( ! is_php('5.4') && ini_get('safe_mode'));
  352. isset(self::$func_overload) OR self::$func_overload = (extension_loaded('mbstring') && ini_get('mbstring.func_overload'));
  353. log_message('info', 'Email Class Initialized');
  354. }
  355. // --------------------------------------------------------------------
  356. /**
  357. * Initialize preferences
  358. *
  359. * @param array $config
  360. * @return CI_Email
  361. */
  362. public function initialize(array $config = array())
  363. {
  364. $this->clear();
  365. foreach ($config as $key => $val)
  366. {
  367. if (isset($this->$key))
  368. {
  369. $method = 'set_'.$key;
  370. if (method_exists($this, $method))
  371. {
  372. $this->$method($val);
  373. }
  374. else
  375. {
  376. $this->$key = $val;
  377. }
  378. }
  379. }
  380. $this->charset = strtoupper($this->charset);
  381. $this->_smtp_auth = isset($this->smtp_user[0], $this->smtp_pass[0]);
  382. return $this;
  383. }
  384. // --------------------------------------------------------------------
  385. /**
  386. * Initialize the Email Data
  387. *
  388. * @param bool
  389. * @return CI_Email
  390. */
  391. public function clear($clear_attachments = FALSE)
  392. {
  393. $this->_subject = '';
  394. $this->_body = '';
  395. $this->_finalbody = '';
  396. $this->_header_str = '';
  397. $this->_replyto_flag = FALSE;
  398. $this->_recipients = array();
  399. $this->_cc_array = array();
  400. $this->_bcc_array = array();
  401. $this->_headers = array();
  402. $this->_debug_msg = array();
  403. $this->set_header('Date', $this->_set_date());
  404. if ($clear_attachments !== FALSE)
  405. {
  406. $this->_attachments = array();
  407. }
  408. return $this;
  409. }
  410. // --------------------------------------------------------------------
  411. /**
  412. * Set FROM
  413. *
  414. * @param string $from
  415. * @param string $name
  416. * @param string $return_path = NULL Return-Path
  417. * @return CI_Email
  418. */
  419. public function from($from, $name = '', $return_path = NULL)
  420. {
  421. if (preg_match('/\<(.*)\>/', $from, $match))
  422. {
  423. $from = $match[1];
  424. }
  425. if ($this->validate)
  426. {
  427. $this->validate_email($this->_str_to_array($from));
  428. if ($return_path)
  429. {
  430. $this->validate_email($this->_str_to_array($return_path));
  431. }
  432. }
  433. // prepare the display name
  434. if ($name !== '')
  435. {
  436. // only use Q encoding if there are characters that would require it
  437. if ( ! preg_match('/[\200-\377]/', $name))
  438. {
  439. // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
  440. $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
  441. }
  442. else
  443. {
  444. $name = $this->_prep_q_encoding($name);
  445. }
  446. }
  447. $this->set_header('From', $name.' <'.$from.'>');
  448. isset($return_path) OR $return_path = $from;
  449. $this->set_header('Return-Path', '<'.$return_path.'>');
  450. return $this;
  451. }
  452. // --------------------------------------------------------------------
  453. /**
  454. * Set Reply-to
  455. *
  456. * @param string
  457. * @param string
  458. * @return CI_Email
  459. */
  460. public function reply_to($replyto, $name = '')
  461. {
  462. if (preg_match('/\<(.*)\>/', $replyto, $match))
  463. {
  464. $replyto = $match[1];
  465. }
  466. if ($this->validate)
  467. {
  468. $this->validate_email($this->_str_to_array($replyto));
  469. }
  470. if ($name !== '')
  471. {
  472. // only use Q encoding if there are characters that would require it
  473. if ( ! preg_match('/[\200-\377]/', $name))
  474. {
  475. // add slashes for non-printing characters, slashes, and double quotes, and surround it in double quotes
  476. $name = '"'.addcslashes($name, "\0..\37\177'\"\\").'"';
  477. }
  478. else
  479. {
  480. $name = $this->_prep_q_encoding($name);
  481. }
  482. }
  483. $this->set_header('Reply-To', $name.' <'.$replyto.'>');
  484. $this->_replyto_flag = TRUE;
  485. return $this;
  486. }
  487. // --------------------------------------------------------------------
  488. /**
  489. * Set Recipients
  490. *
  491. * @param string
  492. * @return CI_Email
  493. */
  494. public function to($to)
  495. {
  496. $to = $this->_str_to_array($to);
  497. $to = $this->clean_email($to);
  498. if ($this->validate)
  499. {
  500. $this->validate_email($to);
  501. }
  502. if ($this->_get_protocol() !== 'mail')
  503. {
  504. $this->set_header('To', implode(', ', $to));
  505. }
  506. $this->_recipients = $to;
  507. return $this;
  508. }
  509. // --------------------------------------------------------------------
  510. /**
  511. * Set CC
  512. *
  513. * @param string
  514. * @return CI_Email
  515. */
  516. public function cc($cc)
  517. {
  518. $cc = $this->clean_email($this->_str_to_array($cc));
  519. if ($this->validate)
  520. {
  521. $this->validate_email($cc);
  522. }
  523. $this->set_header('Cc', implode(', ', $cc));
  524. if ($this->_get_protocol() === 'smtp')
  525. {
  526. $this->_cc_array = $cc;
  527. }
  528. return $this;
  529. }
  530. // --------------------------------------------------------------------
  531. /**
  532. * Set BCC
  533. *
  534. * @param string
  535. * @param string
  536. * @return CI_Email
  537. */
  538. public function bcc($bcc, $limit = '')
  539. {
  540. if ($limit !== '' && is_numeric($limit))
  541. {
  542. $this->bcc_batch_mode = TRUE;
  543. $this->bcc_batch_size = $limit;
  544. }
  545. $bcc = $this->clean_email($this->_str_to_array($bcc));
  546. if ($this->validate)
  547. {
  548. $this->validate_email($bcc);
  549. }
  550. if ($this->_get_protocol() === 'smtp' OR ($this->bcc_batch_mode && count($bcc) > $this->bcc_batch_size))
  551. {
  552. $this->_bcc_array = $bcc;
  553. }
  554. else
  555. {
  556. $this->set_header('Bcc', implode(', ', $bcc));
  557. }
  558. return $this;
  559. }
  560. // --------------------------------------------------------------------
  561. /**
  562. * Set Email Subject
  563. *
  564. * @param string
  565. * @return CI_Email
  566. */
  567. public function subject($subject)
  568. {
  569. $subject = $this->_prep_q_encoding($subject);
  570. $this->set_header('Subject', $subject);
  571. return $this;
  572. }
  573. // --------------------------------------------------------------------
  574. /**
  575. * Set Body
  576. *
  577. * @param string
  578. * @return CI_Email
  579. */
  580. public function message($body)
  581. {
  582. $this->_body = rtrim(str_replace("\r", '', $body));
  583. /* strip slashes only if magic quotes is ON
  584. if we do it with magic quotes OFF, it strips real, user-inputted chars.
  585. NOTE: In PHP 5.4 get_magic_quotes_gpc() will always return 0 and
  586. it will probably not exist in future versions at all.
  587. */
  588. if ( ! is_php('5.4') && get_magic_quotes_gpc())
  589. {
  590. $this->_body = stripslashes($this->_body);
  591. }
  592. return $this;
  593. }
  594. // --------------------------------------------------------------------
  595. /**
  596. * Assign file attachments
  597. *
  598. * @param string $file Can be local path, URL or buffered content
  599. * @param string $disposition = 'attachment'
  600. * @param string $newname = NULL
  601. * @param string $mime = ''
  602. * @return CI_Email
  603. */
  604. public function attach($file, $disposition = '', $newname = NULL, $mime = '')
  605. {
  606. if ($mime === '')
  607. {
  608. if (strpos($file, '://') === FALSE && ! file_exists($file))
  609. {
  610. $this->_set_error_message('lang:email_attachment_missing', $file);
  611. return FALSE;
  612. }
  613. if ( ! $fp = @fopen($file, 'rb'))
  614. {
  615. $this->_set_error_message('lang:email_attachment_unreadable', $file);
  616. return FALSE;
  617. }
  618. $file_content = stream_get_contents($fp);
  619. $mime = $this->_mime_types(pathinfo($file, PATHINFO_EXTENSION));
  620. fclose($fp);
  621. }
  622. else
  623. {
  624. $file_content =& $file; // buffered file
  625. }
  626. $this->_attachments[] = array(
  627. 'name' => array($file, $newname),
  628. 'disposition' => empty($disposition) ? 'attachment' : $disposition, // Can also be 'inline' Not sure if it matters
  629. 'type' => $mime,
  630. 'content' => chunk_split(base64_encode($file_content)),
  631. 'multipart' => 'mixed'
  632. );
  633. return $this;
  634. }
  635. // --------------------------------------------------------------------
  636. /**
  637. * Set and return attachment Content-ID
  638. *
  639. * Useful for attached inline pictures
  640. *
  641. * @param string $filename
  642. * @return string
  643. */
  644. public function attachment_cid($filename)
  645. {
  646. for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
  647. {
  648. if ($this->_attachments[$i]['name'][0] === $filename)
  649. {
  650. $this->_attachments[$i]['multipart'] = 'related';
  651. $this->_attachments[$i]['cid'] = uniqid(basename($this->_attachments[$i]['name'][0]).'@');
  652. return $this->_attachments[$i]['cid'];
  653. }
  654. }
  655. return FALSE;
  656. }
  657. // --------------------------------------------------------------------
  658. /**
  659. * Add a Header Item
  660. *
  661. * @param string
  662. * @param string
  663. * @return CI_Email
  664. */
  665. public function set_header($header, $value)
  666. {
  667. $this->_headers[$header] = str_replace(array("\n", "\r"), '', $value);
  668. return $this;
  669. }
  670. // --------------------------------------------------------------------
  671. /**
  672. * Convert a String to an Array
  673. *
  674. * @param string
  675. * @return array
  676. */
  677. protected function _str_to_array($email)
  678. {
  679. if ( ! is_array($email))
  680. {
  681. return (strpos($email, ',') !== FALSE)
  682. ? preg_split('/[\s,]/', $email, -1, PREG_SPLIT_NO_EMPTY)
  683. : (array) trim($email);
  684. }
  685. return $email;
  686. }
  687. // --------------------------------------------------------------------
  688. /**
  689. * Set Multipart Value
  690. *
  691. * @param string
  692. * @return CI_Email
  693. */
  694. public function set_alt_message($str)
  695. {
  696. $this->alt_message = (string) $str;
  697. return $this;
  698. }
  699. // --------------------------------------------------------------------
  700. /**
  701. * Set Mailtype
  702. *
  703. * @param string
  704. * @return CI_Email
  705. */
  706. public function set_mailtype($type = 'text')
  707. {
  708. $this->mailtype = ($type === 'html') ? 'html' : 'text';
  709. return $this;
  710. }
  711. // --------------------------------------------------------------------
  712. /**
  713. * Set Wordwrap
  714. *
  715. * @param bool
  716. * @return CI_Email
  717. */
  718. public function set_wordwrap($wordwrap = TRUE)
  719. {
  720. $this->wordwrap = (bool) $wordwrap;
  721. return $this;
  722. }
  723. // --------------------------------------------------------------------
  724. /**
  725. * Set Protocol
  726. *
  727. * @param string
  728. * @return CI_Email
  729. */
  730. public function set_protocol($protocol = 'mail')
  731. {
  732. $this->protocol = in_array($protocol, $this->_protocols, TRUE) ? strtolower($protocol) : 'mail';
  733. return $this;
  734. }
  735. // --------------------------------------------------------------------
  736. /**
  737. * Set Priority
  738. *
  739. * @param int
  740. * @return CI_Email
  741. */
  742. public function set_priority($n = 3)
  743. {
  744. $this->priority = preg_match('/^[1-5]$/', $n) ? (int) $n : 3;
  745. return $this;
  746. }
  747. // --------------------------------------------------------------------
  748. /**
  749. * Set Newline Character
  750. *
  751. * @param string
  752. * @return CI_Email
  753. */
  754. public function set_newline($newline = "\n")
  755. {
  756. $this->newline = in_array($newline, array("\n", "\r\n", "\r")) ? $newline : "\n";
  757. return $this;
  758. }
  759. // --------------------------------------------------------------------
  760. /**
  761. * Set CRLF
  762. *
  763. * @param string
  764. * @return CI_Email
  765. */
  766. public function set_crlf($crlf = "\n")
  767. {
  768. $this->crlf = ($crlf !== "\n" && $crlf !== "\r\n" && $crlf !== "\r") ? "\n" : $crlf;
  769. return $this;
  770. }
  771. // --------------------------------------------------------------------
  772. /**
  773. * Get the Message ID
  774. *
  775. * @return string
  776. */
  777. protected function _get_message_id()
  778. {
  779. $from = str_replace(array('>', '<'), '', $this->_headers['Return-Path']);
  780. return '<'.uniqid('').strstr($from, '@').'>';
  781. }
  782. // --------------------------------------------------------------------
  783. /**
  784. * Get Mail Protocol
  785. *
  786. * @param bool
  787. * @return mixed
  788. */
  789. protected function _get_protocol($return = TRUE)
  790. {
  791. $this->protocol = strtolower($this->protocol);
  792. in_array($this->protocol, $this->_protocols, TRUE) OR $this->protocol = 'mail';
  793. if ($return === TRUE)
  794. {
  795. return $this->protocol;
  796. }
  797. }
  798. // --------------------------------------------------------------------
  799. /**
  800. * Get Mail Encoding
  801. *
  802. * @param bool
  803. * @return string
  804. */
  805. protected function _get_encoding($return = TRUE)
  806. {
  807. in_array($this->_encoding, $this->_bit_depths) OR $this->_encoding = '8bit';
  808. foreach ($this->_base_charsets as $charset)
  809. {
  810. if (strpos($charset, $this->charset) === 0)
  811. {
  812. $this->_encoding = '7bit';
  813. }
  814. }
  815. if ($return === TRUE)
  816. {
  817. return $this->_encoding;
  818. }
  819. }
  820. // --------------------------------------------------------------------
  821. /**
  822. * Get content type (text/html/attachment)
  823. *
  824. * @return string
  825. */
  826. protected function _get_content_type()
  827. {
  828. if ($this->mailtype === 'html')
  829. {
  830. return empty($this->_attachments) ? 'html' : 'html-attach';
  831. }
  832. elseif ($this->mailtype === 'text' && ! empty($this->_attachments))
  833. {
  834. return 'plain-attach';
  835. }
  836. else
  837. {
  838. return 'plain';
  839. }
  840. }
  841. // --------------------------------------------------------------------
  842. /**
  843. * Set RFC 822 Date
  844. *
  845. * @return string
  846. */
  847. protected function _set_date()
  848. {
  849. $timezone = date('Z');
  850. $operator = ($timezone[0] === '-') ? '-' : '+';
  851. $timezone = abs($timezone);
  852. $timezone = floor($timezone/3600) * 100 + ($timezone % 3600) / 60;
  853. return sprintf('%s %s%04d', date('D, j M Y H:i:s'), $operator, $timezone);
  854. }
  855. // --------------------------------------------------------------------
  856. /**
  857. * Mime message
  858. *
  859. * @return string
  860. */
  861. protected function _get_mime_message()
  862. {
  863. return 'This is a multi-part message in MIME format.'.$this->newline.'Your email application may not support this format.';
  864. }
  865. // --------------------------------------------------------------------
  866. /**
  867. * Validate Email Address
  868. *
  869. * @param string
  870. * @return bool
  871. */
  872. public function validate_email($email)
  873. {
  874. if ( ! is_array($email))
  875. {
  876. $this->_set_error_message('lang:email_must_be_array');
  877. return FALSE;
  878. }
  879. foreach ($email as $val)
  880. {
  881. if ( ! $this->valid_email($val))
  882. {
  883. $this->_set_error_message('lang:email_invalid_address', $val);
  884. return FALSE;
  885. }
  886. }
  887. return TRUE;
  888. }
  889. // --------------------------------------------------------------------
  890. /**
  891. * Email Validation
  892. *
  893. * @param string
  894. * @return bool
  895. */
  896. public function valid_email($email)
  897. {
  898. if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
  899. {
  900. $email = self::substr($email, 0, ++$atpos).idn_to_ascii(self::substr($email, $atpos));
  901. }
  902. return (bool) filter_var($email, FILTER_VALIDATE_EMAIL);
  903. }
  904. // --------------------------------------------------------------------
  905. /**
  906. * Clean Extended Email Address: Joe Smith <joe@smith.com>
  907. *
  908. * @param string
  909. * @return string
  910. */
  911. public function clean_email($email)
  912. {
  913. if ( ! is_array($email))
  914. {
  915. return preg_match('/\<(.*)\>/', $email, $match) ? $match[1] : $email;
  916. }
  917. $clean_email = array();
  918. foreach ($email as $addy)
  919. {
  920. $clean_email[] = preg_match('/\<(.*)\>/', $addy, $match) ? $match[1] : $addy;
  921. }
  922. return $clean_email;
  923. }
  924. // --------------------------------------------------------------------
  925. /**
  926. * Build alternative plain text message
  927. *
  928. * Provides the raw message for use in plain-text headers of
  929. * HTML-formatted emails.
  930. * If the user hasn't specified his own alternative message
  931. * it creates one by stripping the HTML
  932. *
  933. * @return string
  934. */
  935. protected function _get_alt_message()
  936. {
  937. if ( ! empty($this->alt_message))
  938. {
  939. return ($this->wordwrap)
  940. ? $this->word_wrap($this->alt_message, 76)
  941. : $this->alt_message;
  942. }
  943. $body = preg_match('/\<body.*?\>(.*)\<\/body\>/si', $this->_body, $match) ? $match[1] : $this->_body;
  944. $body = str_replace("\t", '', preg_replace('#<!--(.*)--\>#', '', trim(strip_tags($body))));
  945. for ($i = 20; $i >= 3; $i--)
  946. {
  947. $body = str_replace(str_repeat("\n", $i), "\n\n", $body);
  948. }
  949. // Reduce multiple spaces
  950. $body = preg_replace('| +|', ' ', $body);
  951. return ($this->wordwrap)
  952. ? $this->word_wrap($body, 76)
  953. : $body;
  954. }
  955. // --------------------------------------------------------------------
  956. /**
  957. * Word Wrap
  958. *
  959. * @param string
  960. * @param int line-length limit
  961. * @return string
  962. */
  963. public function word_wrap($str, $charlim = NULL)
  964. {
  965. // Set the character limit, if not already present
  966. if (empty($charlim))
  967. {
  968. $charlim = empty($this->wrapchars) ? 76 : $this->wrapchars;
  969. }
  970. // Standardize newlines
  971. if (strpos($str, "\r") !== FALSE)
  972. {
  973. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  974. }
  975. // Reduce multiple spaces at end of line
  976. $str = preg_replace('| +\n|', "\n", $str);
  977. // If the current word is surrounded by {unwrap} tags we'll
  978. // strip the entire chunk and replace it with a marker.
  979. $unwrap = array();
  980. if (preg_match_all('|\{unwrap\}(.+?)\{/unwrap\}|s', $str, $matches))
  981. {
  982. for ($i = 0, $c = count($matches[0]); $i < $c; $i++)
  983. {
  984. $unwrap[] = $matches[1][$i];
  985. $str = str_replace($matches[0][$i], '{{unwrapped'.$i.'}}', $str);
  986. }
  987. }
  988. // Use PHP's native function to do the initial wordwrap.
  989. // We set the cut flag to FALSE so that any individual words that are
  990. // too long get left alone. In the next step we'll deal with them.
  991. $str = wordwrap($str, $charlim, "\n", FALSE);
  992. // Split the string into individual lines of text and cycle through them
  993. $output = '';
  994. foreach (explode("\n", $str) as $line)
  995. {
  996. // Is the line within the allowed character count?
  997. // If so we'll join it to the output and continue
  998. if (self::strlen($line) <= $charlim)
  999. {
  1000. $output .= $line.$this->newline;
  1001. continue;
  1002. }
  1003. $temp = '';
  1004. do
  1005. {
  1006. // If the over-length word is a URL we won't wrap it
  1007. if (preg_match('!\[url.+\]|://|www\.!', $line))
  1008. {
  1009. break;
  1010. }
  1011. // Trim the word down
  1012. $temp .= self::substr($line, 0, $charlim - 1);
  1013. $line = self::substr($line, $charlim - 1);
  1014. }
  1015. while (self::strlen($line) > $charlim);
  1016. // If $temp contains data it means we had to split up an over-length
  1017. // word into smaller chunks so we'll add it back to our current line
  1018. if ($temp !== '')
  1019. {
  1020. $output .= $temp.$this->newline;
  1021. }
  1022. $output .= $line.$this->newline;
  1023. }
  1024. // Put our markers back
  1025. if (count($unwrap) > 0)
  1026. {
  1027. foreach ($unwrap as $key => $val)
  1028. {
  1029. $output = str_replace('{{unwrapped'.$key.'}}', $val, $output);
  1030. }
  1031. }
  1032. return $output;
  1033. }
  1034. // --------------------------------------------------------------------
  1035. /**
  1036. * Build final headers
  1037. *
  1038. * @return void
  1039. */
  1040. protected function _build_headers()
  1041. {
  1042. $this->set_header('User-Agent', $this->useragent);
  1043. $this->set_header('X-Sender', $this->clean_email($this->_headers['From']));
  1044. $this->set_header('X-Mailer', $this->useragent);
  1045. $this->set_header('X-Priority', $this->_priorities[$this->priority]);
  1046. $this->set_header('Message-ID', $this->_get_message_id());
  1047. $this->set_header('Mime-Version', '1.0');
  1048. }
  1049. // --------------------------------------------------------------------
  1050. /**
  1051. * Write Headers as a string
  1052. *
  1053. * @return void
  1054. */
  1055. protected function _write_headers()
  1056. {
  1057. if ($this->protocol === 'mail')
  1058. {
  1059. if (isset($this->_headers['Subject']))
  1060. {
  1061. $this->_subject = $this->_headers['Subject'];
  1062. unset($this->_headers['Subject']);
  1063. }
  1064. }
  1065. reset($this->_headers);
  1066. $this->_header_str = '';
  1067. foreach ($this->_headers as $key => $val)
  1068. {
  1069. $val = trim($val);
  1070. if ($val !== '')
  1071. {
  1072. $this->_header_str .= $key.': '.$val.$this->newline;
  1073. }
  1074. }
  1075. if ($this->_get_protocol() === 'mail')
  1076. {
  1077. $this->_header_str = rtrim($this->_header_str);
  1078. }
  1079. }
  1080. // --------------------------------------------------------------------
  1081. /**
  1082. * Build Final Body and attachments
  1083. *
  1084. * @return bool
  1085. */
  1086. protected function _build_message()
  1087. {
  1088. if ($this->wordwrap === TRUE && $this->mailtype !== 'html')
  1089. {
  1090. $this->_body = $this->word_wrap($this->_body);
  1091. }
  1092. $this->_write_headers();
  1093. $hdr = ($this->_get_protocol() === 'mail') ? $this->newline : '';
  1094. $body = '';
  1095. switch ($this->_get_content_type())
  1096. {
  1097. case 'plain':
  1098. $hdr .= 'Content-Type: text/plain; charset='.$this->charset.$this->newline
  1099. .'Content-Transfer-Encoding: '.$this->_get_encoding();
  1100. if ($this->_get_protocol() === 'mail')
  1101. {
  1102. $this->_header_str .= $hdr;
  1103. $this->_finalbody = $this->_body;
  1104. }
  1105. else
  1106. {
  1107. $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_body;
  1108. }
  1109. return;
  1110. case 'html':
  1111. if ($this->send_multipart === FALSE)
  1112. {
  1113. $hdr .= 'Content-Type: text/html; charset='.$this->charset.$this->newline
  1114. .'Content-Transfer-Encoding: quoted-printable';
  1115. }
  1116. else
  1117. {
  1118. $boundary = uniqid('B_ALT_');
  1119. $hdr .= 'Content-Type: multipart/alternative; boundary="'.$boundary.'"';
  1120. $body .= $this->_get_mime_message().$this->newline.$this->newline
  1121. .'--'.$boundary.$this->newline
  1122. .'Content-Type: text/plain; charset='.$this->charset.$this->newline
  1123. .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
  1124. .$this->_get_alt_message().$this->newline.$this->newline
  1125. .'--'.$boundary.$this->newline
  1126. .'Content-Type: text/html; charset='.$this->charset.$this->newline
  1127. .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline;
  1128. }
  1129. $this->_finalbody = $body.$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline;
  1130. if ($this->_get_protocol() === 'mail')
  1131. {
  1132. $this->_header_str .= $hdr;
  1133. }
  1134. else
  1135. {
  1136. $this->_finalbody = $hdr.$this->newline.$this->newline.$this->_finalbody;
  1137. }
  1138. if ($this->send_multipart !== FALSE)
  1139. {
  1140. $this->_finalbody .= '--'.$boundary.'--';
  1141. }
  1142. return;
  1143. case 'plain-attach':
  1144. $boundary = uniqid('B_ATC_');
  1145. $hdr .= 'Content-Type: multipart/mixed; boundary="'.$boundary.'"';
  1146. if ($this->_get_protocol() === 'mail')
  1147. {
  1148. $this->_header_str .= $hdr;
  1149. }
  1150. $body .= $this->_get_mime_message().$this->newline
  1151. .$this->newline
  1152. .'--'.$boundary.$this->newline
  1153. .'Content-Type: text/plain; charset='.$this->charset.$this->newline
  1154. .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline
  1155. .$this->newline
  1156. .$this->_body.$this->newline.$this->newline;
  1157. $this->_append_attachments($body, $boundary);
  1158. break;
  1159. case 'html-attach':
  1160. $alt_boundary = uniqid('B_ALT_');
  1161. $last_boundary = NULL;
  1162. if ($this->_attachments_have_multipart('mixed'))
  1163. {
  1164. $atc_boundary = uniqid('B_ATC_');
  1165. $hdr .= 'Content-Type: multipart/mixed; boundary="'.$atc_boundary.'"';
  1166. $last_boundary = $atc_boundary;
  1167. }
  1168. if ($this->_attachments_have_multipart('related'))
  1169. {
  1170. $rel_boundary = uniqid('B_REL_');
  1171. $rel_boundary_header = 'Content-Type: multipart/related; boundary="'.$rel_boundary.'"';
  1172. if (isset($last_boundary))
  1173. {
  1174. $body .= '--'.$last_boundary.$this->newline.$rel_boundary_header;
  1175. }
  1176. else
  1177. {
  1178. $hdr .= $rel_boundary_header;
  1179. }
  1180. $last_boundary = $rel_boundary;
  1181. }
  1182. if ($this->_get_protocol() === 'mail')
  1183. {
  1184. $this->_header_str .= $hdr;
  1185. }
  1186. self::strlen($body) && $body .= $this->newline.$this->newline;
  1187. $body .= $this->_get_mime_message().$this->newline.$this->newline
  1188. .'--'.$last_boundary.$this->newline
  1189. .'Content-Type: multipart/alternative; boundary="'.$alt_boundary.'"'.$this->newline.$this->newline
  1190. .'--'.$alt_boundary.$this->newline
  1191. .'Content-Type: text/plain; charset='.$this->charset.$this->newline
  1192. .'Content-Transfer-Encoding: '.$this->_get_encoding().$this->newline.$this->newline
  1193. .$this->_get_alt_message().$this->newline.$this->newline
  1194. .'--'.$alt_boundary.$this->newline
  1195. .'Content-Type: text/html; charset='.$this->charset.$this->newline
  1196. .'Content-Transfer-Encoding: quoted-printable'.$this->newline.$this->newline
  1197. .$this->_prep_quoted_printable($this->_body).$this->newline.$this->newline
  1198. .'--'.$alt_boundary.'--'.$this->newline.$this->newline;
  1199. if ( ! empty($rel_boundary))
  1200. {
  1201. $body .= $this->newline.$this->newline;
  1202. $this->_append_attachments($body, $rel_boundary, 'related');
  1203. }
  1204. // multipart/mixed attachments
  1205. if ( ! empty($atc_boundary))
  1206. {
  1207. $body .= $this->newline.$this->newline;
  1208. $this->_append_attachments($body, $atc_boundary, 'mixed');
  1209. }
  1210. break;
  1211. }
  1212. $this->_finalbody = ($this->_get_protocol() === 'mail')
  1213. ? $body
  1214. : $hdr.$this->newline.$this->newline.$body;
  1215. return TRUE;
  1216. }
  1217. // --------------------------------------------------------------------
  1218. protected function _attachments_have_multipart($type)
  1219. {
  1220. foreach ($this->_attachments as &$attachment)
  1221. {
  1222. if ($attachment['multipart'] === $type)
  1223. {
  1224. return TRUE;
  1225. }
  1226. }
  1227. return FALSE;
  1228. }
  1229. // --------------------------------------------------------------------
  1230. /**
  1231. * Prepares attachment string
  1232. *
  1233. * @param string $body Message body to append to
  1234. * @param string $boundary Multipart boundary
  1235. * @param string $multipart When provided, only attachments of this type will be processed
  1236. * @return string
  1237. */
  1238. protected function _append_attachments(&$body, $boundary, $multipart = null)
  1239. {
  1240. for ($i = 0, $c = count($this->_attachments); $i < $c; $i++)
  1241. {
  1242. if (isset($multipart) && $this->_attachments[$i]['multipart'] !== $multipart)
  1243. {
  1244. continue;
  1245. }
  1246. $name = isset($this->_attachments[$i]['name'][1])
  1247. ? $this->_attachments[$i]['name'][1]
  1248. : basename($this->_attachments[$i]['name'][0]);
  1249. $body .= '--'.$boundary.$this->newline
  1250. .'Content-Type: '.$this->_attachments[$i]['type'].'; name="'.$name.'"'.$this->newline
  1251. .'Content-Disposition: '.$this->_attachments[$i]['disposition'].';'.$this->newline
  1252. .'Content-Transfer-Encoding: base64'.$this->newline
  1253. .(empty($this->_attachments[$i]['cid']) ? '' : 'Content-ID: <'.$this->_attachments[$i]['cid'].'>'.$this->newline)
  1254. .$this->newline
  1255. .$this->_attachments[$i]['content'].$this->newline;
  1256. }
  1257. // $name won't be set if no attachments were appended,
  1258. // and therefore a boundary wouldn't be necessary
  1259. empty($name) OR $body .= '--'.$boundary.'--';
  1260. }
  1261. // --------------------------------------------------------------------
  1262. /**
  1263. * Prep Quoted Printable
  1264. *
  1265. * Prepares string for Quoted-Printable Content-Transfer-Encoding
  1266. * Refer to RFC 2045 http://www.ietf.org/rfc/rfc2045.txt
  1267. *
  1268. * @param string
  1269. * @return string
  1270. */
  1271. protected function _prep_quoted_printable($str)
  1272. {
  1273. // ASCII code numbers for "safe" characters that can always be
  1274. // used literally, without encoding, as described in RFC 2049.
  1275. // http://www.ietf.org/rfc/rfc2049.txt
  1276. static $ascii_safe_chars = array(
  1277. // ' ( ) + , - . / : = ?
  1278. 39, 40, 41, 43, 44, 45, 46, 47, 58, 61, 63,
  1279. // numbers
  1280. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  1281. // upper-case letters
  1282. 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90,
  1283. // lower-case letters
  1284. 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122
  1285. );
  1286. // We are intentionally wrapping so mail servers will encode characters
  1287. // properly and MUAs will behave, so {unwrap} must go!
  1288. $str = str_replace(array('{unwrap}', '{/unwrap}'), '', $str);
  1289. // RFC 2045 specifies CRLF as "\r\n".
  1290. // However, many developers choose to override that and violate
  1291. // the RFC rules due to (apparently) a bug in MS Exchange,
  1292. // which only works with "\n".
  1293. if ($this->crlf === "\r\n")
  1294. {
  1295. return quoted_printable_encode($str);
  1296. }
  1297. // Reduce multiple spaces & remove nulls
  1298. $str = preg_replace(array('| +|', '/\x00+/'), array(' ', ''), $str);
  1299. // Standardize newlines
  1300. if (strpos($str, "\r") !== FALSE)
  1301. {
  1302. $str = str_replace(array("\r\n", "\r"), "\n", $str);
  1303. }
  1304. $escape = '=';
  1305. $output = '';
  1306. foreach (explode("\n", $str) as $line)
  1307. {
  1308. $length = self::strlen($line);
  1309. $temp = '';
  1310. // Loop through each character in the line to add soft-wrap
  1311. // characters at the end of a line " =\r\n" and add the newly
  1312. // processed line(s) to the output (see comment on $crlf class property)
  1313. for ($i = 0; $i < $length; $i++)
  1314. {
  1315. // Grab the next character
  1316. $char = $line[$i];
  1317. $ascii = ord($char);
  1318. // Convert spaces and tabs but only if it's the end of the line
  1319. if ($ascii === 32 OR $ascii === 9)
  1320. {
  1321. if ($i === ($length - 1))
  1322. {
  1323. $char = $escape.sprintf('%02s', dechex($ascii));
  1324. }
  1325. }
  1326. // DO NOT move this below the $ascii_safe_chars line!
  1327. //
  1328. // = (equals) signs are allowed by RFC2049, but must be encoded
  1329. // as they are the encoding delimiter!
  1330. elseif ($ascii === 61)
  1331. {
  1332. $char = $escape.strtoupper(sprintf('%02s', dechex($ascii))); // =3D
  1333. }
  1334. elseif ( ! in_array($ascii, $ascii_safe_chars, TRUE))
  1335. {
  1336. $char = $escape.strtoupper(sprintf('%02s', dechex($ascii)));
  1337. }
  1338. // If we're at the character limit, add the line to the output,
  1339. // reset our temp variable, and keep on chuggin'
  1340. if ((self::strlen($temp) + self::strlen($char)) >= 76)
  1341. {
  1342. $output .= $temp.$escape.$this->crlf;
  1343. $temp = '';
  1344. }
  1345. // Add the character to our temporary line
  1346. $temp .= $char;
  1347. }
  1348. // Add our completed line to the output
  1349. $output .= $temp.$this->crlf;
  1350. }
  1351. // get rid of extra CRLF tacked onto the end
  1352. return self::substr($output, 0, self::strlen($this->crlf) * -1);
  1353. }
  1354. // --------------------------------------------------------------------
  1355. /**
  1356. * Prep Q Encoding
  1357. *
  1358. * Performs "Q Encoding" on a string for use in email headers.
  1359. * It's related but not identical to quoted-printable, so it has its
  1360. * own method.
  1361. *
  1362. * @param string
  1363. * @return string
  1364. */
  1365. protected function _prep_q_encoding($str)
  1366. {
  1367. $str = str_replace(array("\r", "\n"), '', $str);
  1368. if ($this->charset === 'UTF-8')
  1369. {
  1370. // Note: We used to have mb_encode_mimeheader() as the first choice
  1371. // here, but it turned out to be buggy and unreliable. DO NOT
  1372. // re-add it! -- Narf
  1373. if (ICONV_ENABLED === TRUE)
  1374. {
  1375. $output = @iconv_mime_encode('', $str,
  1376. array(
  1377. 'scheme' => 'Q',
  1378. 'line-length' => 76,
  1379. 'input-charset' => $this->charset,
  1380. 'output-charset' => $this->charset,
  1381. 'line-break-chars' => $this->crlf
  1382. )
  1383. );
  1384. // There are reports that iconv_mime_encode() might fail and return FALSE
  1385. if ($output !== FALSE)
  1386. {
  1387. // iconv_mime_encode() will always put a header field name.
  1388. // We've passed it an empty one, but it still prepends our
  1389. // encoded string with ': ', so we need to strip it.
  1390. return self::substr($output, 2);
  1391. }
  1392. $chars = iconv_strlen($str, 'UTF-8');
  1393. }
  1394. elseif (MB_ENABLED === TRUE)
  1395. {
  1396. $chars = mb_strlen($str, 'UTF-8');
  1397. }
  1398. }
  1399. // We might already have this set for UTF-8
  1400. isset($chars) OR $chars = self::strlen($str);
  1401. $output = '=?'.$this->charset.'?Q?';
  1402. for ($i = 0, $length = self::strlen($output); $i < $chars; $i++)
  1403. {
  1404. $chr = ($this->charset === 'UTF-8' && ICONV_ENABLED === TRUE)
  1405. ? '='.implode('=', str_split(strtoupper(bin2hex(iconv_substr($str, $i, 1, $this->charset))), 2))
  1406. : '='.strtoupper(bin2hex($str[$i]));
  1407. // RFC 2045 sets a limit of 76 characters per line.
  1408. // We'll append ?= to the end of each line though.
  1409. if ($length + ($l = self::strlen($chr)) > 74)
  1410. {
  1411. $output .= '?='.$this->crlf // EOL
  1412. .' =?'.$this->charset.'?Q?'.$chr; // New line
  1413. $length = 6 + self::strlen($this->charset) + $l; // Reset the length for the new line
  1414. }
  1415. else
  1416. {
  1417. $output .= $chr;
  1418. $length += $l;
  1419. }
  1420. }
  1421. // End the header
  1422. return $output.'?=';
  1423. }
  1424. // --------------------------------------------------------------------
  1425. /**
  1426. * Send Email
  1427. *
  1428. * @param bool $auto_clear = TRUE
  1429. * @return bool
  1430. */
  1431. public function send($auto_clear = TRUE)
  1432. {
  1433. if ( ! isset($this->_headers['From']))
  1434. {
  1435. $this->_set_error_message('lang:email_no_from');
  1436. return FALSE;
  1437. }
  1438. if ($this->_replyto_flag === FALSE)
  1439. {
  1440. $this->reply_to($this->_headers['From']);
  1441. }
  1442. if ( ! isset($this->_recipients) && ! isset($this->_headers['To'])
  1443. && ! isset($this->_bcc_array) && ! isset($this->_headers['Bcc'])
  1444. && ! isset($this->_headers['Cc']))
  1445. {
  1446. $this->_set_error_message('lang:email_no_recipients');
  1447. return FALSE;
  1448. }
  1449. $this->_build_headers();
  1450. if ($this->bcc_batch_mode && count($this->_bcc_array) > $this->bcc_batch_size)
  1451. {
  1452. $result = $this->batch_bcc_send();
  1453. if ($result && $auto_clear)
  1454. {
  1455. $this->clear();
  1456. }
  1457. return $result;
  1458. }
  1459. if ($this->_build_message() === FALSE)
  1460. {
  1461. return FALSE;
  1462. }
  1463. $result = $this->_spool_email();
  1464. if ($result && $auto_clear)
  1465. {
  1466. $this->clear();
  1467. }
  1468. return $result;
  1469. }
  1470. // --------------------------------------------------------------------
  1471. /**
  1472. * Batch Bcc Send. Sends groups of BCCs in batches
  1473. *
  1474. * @return void
  1475. */
  1476. public function batch_bcc_send()
  1477. {
  1478. $float = $this->bcc_batch_size - 1;
  1479. $set = '';
  1480. $chunk = array();
  1481. for ($i = 0, $c = count($this->_bcc_array); $i < $c; $i++)
  1482. {
  1483. if (isset($this->_bcc_array[$i]))
  1484. {
  1485. $set .= ', '.$this->_bcc_array[$i];
  1486. }
  1487. if ($i === $float)
  1488. {
  1489. $chunk[] = self::substr($set, 1);
  1490. $float += $this->bcc_batch_size;
  1491. $set = '';
  1492. }
  1493. if ($i === $c-1)
  1494. {
  1495. $chunk[] = self::substr($set, 1);
  1496. }
  1497. }
  1498. for ($i = 0, $c = count($chunk); $i < $c; $i++)
  1499. {
  1500. unset($this->_headers['Bcc']);
  1501. $bcc = $this->clean_email($this->_str_to_array($chunk[$i]));
  1502. if ($this->protocol !== 'smtp')
  1503. {
  1504. $this->set_header('Bcc', implode(', ', $bcc));
  1505. }
  1506. else
  1507. {
  1508. $this->_bcc_array = $bcc;
  1509. }
  1510. if ($this->_build_message() === FALSE)
  1511. {
  1512. return FALSE;
  1513. }
  1514. $this->_spool_email();
  1515. }
  1516. }
  1517. // --------------------------------------------------------------------
  1518. /**
  1519. * Unwrap special elements
  1520. *
  1521. * @return void
  1522. */
  1523. protected function _unwrap_specials()
  1524. {
  1525. $this->_finalbody = preg_replace_callback('/\{unwrap\}(.*?)\{\/unwrap\}/si', array($this, '_remove_nl_callback'), $this->_finalbody);
  1526. }
  1527. // --------------------------------------------------------------------
  1528. /**
  1529. * Strip line-breaks via callback
  1530. *
  1531. * @param string $matches
  1532. * @return string
  1533. */
  1534. protected function _remove_nl_callback($matches)
  1535. {
  1536. if (strpos($matches[1], "\r") !== FALSE OR strpos($matches[1], "\n") !== FALSE)
  1537. {
  1538. $matches[1] = str_replace(array("\r\n", "\r", "\n"), '', $matches[1]);
  1539. }
  1540. return $matches[1];
  1541. }
  1542. // --------------------------------------------------------------------
  1543. /**
  1544. * Spool mail to the mail server
  1545. *
  1546. * @return bool
  1547. */
  1548. protected function _spool_email()
  1549. {
  1550. $this->_unwrap_specials();
  1551. $method = '_send_with_'.$this->_get_protocol();
  1552. if ( ! $this->$method())
  1553. {
  1554. $this->_set_error_message('lang:email_send_failure_'.($this->_get_protocol() === 'mail' ? 'phpmail' : $this->_get_protocol()));
  1555. return FALSE;
  1556. }
  1557. $this->_set_error_message('lang:email_sent', $this->_get_protocol());
  1558. return TRUE;
  1559. }
  1560. // --------------------------------------------------------------------
  1561. /**
  1562. * Validate email for shell
  1563. *
  1564. * Applies stricter, shell-safe validation to email addresses.
  1565. * Introduced to prevent RCE via sendmail's -f option.
  1566. *
  1567. * @see https://github.com/bcit-ci/CodeIgniter/issues/4963
  1568. * @see https://gist.github.com/Zenexer/40d02da5e07f151adeaeeaa11af9ab36
  1569. * @license https://creativecommons.org/publicdomain/zero/1.0/ CC0 1.0, Public Domain
  1570. *
  1571. * Credits for the base concept go to Paul Buonopane <paul@namepros.com>
  1572. *
  1573. * @param string $email
  1574. * @return bool
  1575. */
  1576. protected function _validate_email_for_shell(&$email)
  1577. {
  1578. if (function_exists('idn_to_ascii') && $atpos = strpos($email, '@'))
  1579. {
  1580. $email = self::substr($email, 0, ++$atpos).idn_to_ascii(self::substr($email, $atpos),IDNA_NONTRANSITIONAL_TO_ASCII,INTL_IDNA_VARIANT_UTS46);
  1581. }
  1582. return (filter_var($email, FILTER_VALIDATE_EMAIL) === $email && preg_match('#\A[a-z0-9._+-]+@[a-z0-9.-]{1,253}\z#i', $email));
  1583. }
  1584. // --------------------------------------------------------------------
  1585. /**
  1586. * Send using mail()
  1587. *
  1588. * @return bool
  1589. */
  1590. protected function _send_with_mail()
  1591. {
  1592. if (is_array($this->_recipients))
  1593. {
  1594. $this->_recipients = implode(', ', $this->_recipients);
  1595. }
  1596. // _validate_email_for_shell() below accepts by reference,
  1597. // so this needs to be assigned to a variable
  1598. $from = $this->clean_email($this->_headers['Return-Path']);
  1599. if ($this->_safe_mode === TRUE || ! $this->_validate_email_for_shell($from))
  1600. {
  1601. return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str);
  1602. }
  1603. else
  1604. {
  1605. // most documentation of sendmail using the "-f" flag lacks a space after it, however
  1606. // we've encountered servers that seem to require it to be in place.
  1607. return mail($this->_recipients, $this->_subject, $this->_finalbody, $this->_header_str, '-f '.$from);
  1608. }
  1609. }
  1610. // --------------------------------------------------------------------
  1611. /**
  1612. * Send using Sendmail
  1613. *
  1614. * @return bool
  1615. */
  1616. protected function _send_with_sendmail()
  1617. {
  1618. // _validate_email_for_shell() below accepts by reference,
  1619. // so this needs to be assigned to a variable
  1620. $from = $this->clean_email($this->_headers['From']);
  1621. if ($this->_validate_email_for_shell($from))
  1622. {
  1623. $from = '-f '.$from;
  1624. }
  1625. else
  1626. {
  1627. $from = '';
  1628. }
  1629. // is popen() enabled?
  1630. if ( ! function_usable('popen') OR FALSE === ($fp = @popen($this->mailpath.' -oi '.$from.' -t', 'w')))
  1631. {
  1632. // server probably has popen disabled, so nothing we can do to get a verbose error.
  1633. return FALSE;
  1634. }
  1635. fputs($fp, $this->_header_str);
  1636. fputs($fp, $this->_finalbody);
  1637. $status = pclose($fp);
  1638. if ($status !== 0)
  1639. {
  1640. $this->_set_error_message('lang:email_exit_status', $status);
  1641. $this->_set_error_message('lang:email_no_socket');
  1642. return FALSE;
  1643. }
  1644. return TRUE;
  1645. }
  1646. // --------------------------------------------------------------------
  1647. /**
  1648. * Send using SMTP
  1649. *
  1650. * @return bool
  1651. */
  1652. protected function _send_with_smtp()
  1653. {
  1654. if ($this->smtp_host === '')
  1655. {
  1656. $this->_set_error_message('lang:email_no_hostname');
  1657. return FALSE;
  1658. }
  1659. if ( ! $this->_smtp_connect() OR ! $this->_smtp_authenticate())
  1660. {
  1661. return FALSE;
  1662. }
  1663. if ( ! $this->_send_command('from', $this->clean_email($this->_headers['From'])))
  1664. {
  1665. $this->_smtp_end();
  1666. return FALSE;
  1667. }
  1668. foreach ($this->_recipients as $val)
  1669. {
  1670. if ( ! $this->_send_command('to', $val))
  1671. {
  1672. $this->_smtp_end();
  1673. return FALSE;
  1674. }
  1675. }
  1676. if (count($this->_cc_array) > 0)
  1677. {
  1678. foreach ($this->_cc_array as $val)
  1679. {
  1680. if ($val !== '' && ! $this->_send_command('to', $val))
  1681. {
  1682. $this->_smtp_end();
  1683. return FALSE;
  1684. }
  1685. }
  1686. }
  1687. if (count($this->_bcc_array) > 0)
  1688. {
  1689. foreach ($this->_bcc_array as $val)
  1690. {
  1691. if ($val !== '' && ! $this->_send_command('to', $val))
  1692. {
  1693. $this->_smtp_end();
  1694. return FALSE;
  1695. }
  1696. }
  1697. }
  1698. if ( ! $this->_send_command('data'))
  1699. {
  1700. $this->_smtp_end();
  1701. return FALSE;
  1702. }
  1703. // perform dot transformation on any lines that begin with a dot
  1704. $this->_send_data($this->_header_str.preg_replace('/^\./m', '..$1', $this->_finalbody));
  1705. $this->_send_data('.');
  1706. $reply = $this->_get_smtp_data();
  1707. $this->_set_error_message($reply);
  1708. $this->_smtp_end();
  1709. if (strpos($reply, '250') !== 0)
  1710. {
  1711. $this->_set_error_message('lang:email_smtp_error', $reply);
  1712. return FALSE;
  1713. }
  1714. return TRUE;
  1715. }
  1716. // --------------------------------------------------------------------
  1717. /**
  1718. * SMTP End
  1719. *
  1720. * Shortcut to send RSET or QUIT depending on keep-alive
  1721. *
  1722. * @return void
  1723. */
  1724. protected function _smtp_end()
  1725. {
  1726. ($this->smtp_keepalive)
  1727. ? $this->_send_command('reset')
  1728. : $this->_send_command('quit');
  1729. }
  1730. // --------------------------------------------------------------------
  1731. /**
  1732. * SMTP Connect
  1733. *
  1734. * @return string
  1735. */
  1736. protected function _smtp_connect()
  1737. {
  1738. if (is_resource($this->_smtp_connect))
  1739. {
  1740. return TRUE;
  1741. }
  1742. $ssl = ($this->smtp_crypto === 'ssl') ? 'ssl://' : '';
  1743. $this->_smtp_connect = fsockopen($ssl.$this->smtp_host,
  1744. $this->smtp_port,
  1745. $errno,
  1746. $errstr,
  1747. $this->smtp_timeout);
  1748. if ( ! is_resource($this->_smtp_connect))
  1749. {
  1750. $this->_set_error_message('lang:email_smtp_error', $errno.' '.$errstr);
  1751. return FALSE;
  1752. }
  1753. stream_set_timeout($this->_smtp_connect, $this->smtp_timeout);
  1754. $this->_set_error_message($this->_get_smtp_data());
  1755. if ($this->smtp_crypto === 'tls')
  1756. {
  1757. $this->_send_command('hello');
  1758. $this->_send_command('starttls');
  1759. $crypto = stream_socket_enable_crypto($this->_smtp_connect, TRUE, STREAM_CRYPTO_METHOD_TLS_CLIENT);
  1760. if ($crypto !== TRUE)
  1761. {
  1762. $this->_set_error_message('lang:email_smtp_error', $this->_get_smtp_data());
  1763. return FALSE;
  1764. }
  1765. }
  1766. return $this->_send_command('hello');
  1767. }
  1768. // --------------------------------------------------------------------
  1769. /**
  1770. * Send SMTP command
  1771. *
  1772. * @param string
  1773. * @param string
  1774. * @return bool
  1775. */
  1776. protected function _send_command($cmd, $data = '')
  1777. {
  1778. switch ($cmd)
  1779. {
  1780. case 'hello' :
  1781. if ($this->_smtp_auth OR $this->_get_encoding() === '8bit')
  1782. {
  1783. $this->_send_data('EHLO '.$this->_get_hostname());
  1784. }
  1785. else
  1786. {
  1787. $this->_send_data('HELO '.$this->_get_hostname());
  1788. }
  1789. $resp = 250;
  1790. break;
  1791. case 'starttls' :
  1792. $this->_send_data('STARTTLS');
  1793. $resp = 220;
  1794. break;
  1795. case 'from' :
  1796. $this->_send_data('MAIL FROM:<'.$data.'>');
  1797. $resp = 250;
  1798. break;
  1799. case 'to' :
  1800. if ($this->dsn)
  1801. {
  1802. $this->_send_data('RCPT TO:<'.$data.'> NOTIFY=SUCCESS,DELAY,FAILURE ORCPT=rfc822;'.$data);
  1803. }
  1804. else
  1805. {
  1806. $this->_send_data('RCPT TO:<'.$data.'>');
  1807. }
  1808. $resp = 250;
  1809. break;
  1810. case 'data' :
  1811. $this->_send_data('DATA');
  1812. $resp = 354;
  1813. break;
  1814. case 'reset':
  1815. $this->_send_data('RSET');
  1816. $resp = 250;
  1817. break;
  1818. case 'quit' :
  1819. $this->_send_data('QUIT');
  1820. $resp = 221;
  1821. break;
  1822. }
  1823. $reply = $this->_get_smtp_data();
  1824. $this->_debug_msg[] = '<pre>'.$cmd.': '.$reply.'</pre>';
  1825. if ((int) self::substr($reply, 0, 3) !== $resp)
  1826. {
  1827. $this->_set_error_message('lang:email_smtp_error', $reply);
  1828. return FALSE;
  1829. }
  1830. if ($cmd === 'quit')
  1831. {
  1832. fclose($this->_smtp_connect);
  1833. }
  1834. return TRUE;
  1835. }
  1836. // --------------------------------------------------------------------
  1837. /**
  1838. * SMTP Authenticate
  1839. *
  1840. * @return bool
  1841. */
  1842. protected function _smtp_authenticate()
  1843. {
  1844. if ( ! $this->_smtp_auth)
  1845. {
  1846. return TRUE;
  1847. }
  1848. if ($this->smtp_user === '' && $this->smtp_pass === '')
  1849. {
  1850. $this->_set_error_message('lang:email_no_smtp_unpw');
  1851. return FALSE;
  1852. }
  1853. $this->_send_data('AUTH LOGIN');
  1854. $reply = $this->_get_smtp_data();
  1855. if (strpos($reply, '503') === 0) // Already authenticated
  1856. {
  1857. return TRUE;
  1858. }
  1859. elseif (strpos($reply, '334') !== 0)
  1860. {
  1861. $this->_set_error_message('lang:email_failed_smtp_login', $reply);
  1862. return FALSE;
  1863. }
  1864. $this->_send_data(base64_encode($this->smtp_user));
  1865. $reply = $this->_get_smtp_data();
  1866. if (strpos($reply, '334') !== 0)
  1867. {
  1868. $this->_set_error_message('lang:email_smtp_auth_un', $reply);
  1869. return FALSE;
  1870. }
  1871. $this->_send_data(base64_encode($this->smtp_pass));
  1872. $reply = $this->_get_smtp_data();
  1873. if (strpos($reply, '235') !== 0)
  1874. {
  1875. $this->_set_error_message('lang:email_smtp_auth_pw', $reply);
  1876. return FALSE;
  1877. }
  1878. if ($this->smtp_keepalive)
  1879. {
  1880. $this->_smtp_auth = FALSE;
  1881. }
  1882. return TRUE;
  1883. }
  1884. // --------------------------------------------------------------------
  1885. /**
  1886. * Send SMTP data
  1887. *
  1888. * @param string $data
  1889. * @return bool
  1890. */
  1891. protected function _send_data($data)
  1892. {
  1893. $data .= $this->newline;
  1894. for ($written = $timestamp = 0, $length = self::strlen($data); $written < $length; $written += $result)
  1895. {
  1896. if (($result = fwrite($this->_smtp_connect, self::substr($data, $written))) === FALSE)
  1897. {
  1898. break;
  1899. }
  1900. // See https://bugs.php.net/bug.php?id=39598 and http://php.net/manual/en/function.fwrite.php#96951
  1901. elseif ($result === 0)
  1902. {
  1903. if ($timestamp === 0)
  1904. {
  1905. $timestamp = time();
  1906. }
  1907. elseif ($timestamp < (time() - $this->smtp_timeout))
  1908. {
  1909. $result = FALSE;
  1910. break;
  1911. }
  1912. usleep(250000);
  1913. continue;
  1914. }
  1915. else
  1916. {
  1917. $timestamp = 0;
  1918. }
  1919. }
  1920. if ($result === FALSE)
  1921. {
  1922. $this->_set_error_message('lang:email_smtp_data_failure', $data);
  1923. return FALSE;
  1924. }
  1925. return TRUE;
  1926. }
  1927. // --------------------------------------------------------------------
  1928. /**
  1929. * Get SMTP data
  1930. *
  1931. * @return string
  1932. */
  1933. protected function _get_smtp_data()
  1934. {
  1935. $data = '';
  1936. while ($str = fgets($this->_smtp_connect, 512))
  1937. {
  1938. $data .= $str;
  1939. if ($str[3] === ' ')
  1940. {
  1941. break;
  1942. }
  1943. }
  1944. return $data;
  1945. }
  1946. // --------------------------------------------------------------------
  1947. /**
  1948. * Get Hostname
  1949. *
  1950. * There are only two legal types of hostname - either a fully
  1951. * qualified domain name (eg: "mail.example.com") or an IP literal
  1952. * (eg: "[1.2.3.4]").
  1953. *
  1954. * @link https://tools.ietf.org/html/rfc5321#section-2.3.5
  1955. * @link http://cbl.abuseat.org/namingproblems.html
  1956. * @return string
  1957. */
  1958. protected function _get_hostname()
  1959. {
  1960. if (isset($_SERVER['SERVER_NAME']))
  1961. {
  1962. return $_SERVER['SERVER_NAME'];
  1963. }
  1964. return isset($_SERVER['SERVER_ADDR']) ? '['.$_SERVER['SERVER_ADDR'].']' : '[127.0.0.1]';
  1965. }
  1966. // --------------------------------------------------------------------
  1967. /**
  1968. * Get Debug Message
  1969. *
  1970. * @param array $include List of raw data chunks to include in the output
  1971. * Valid options are: 'headers', 'subject', 'body'
  1972. * @return string
  1973. */
  1974. public function print_debugger($include = array('headers', 'subject', 'body'))
  1975. {
  1976. $msg = '';
  1977. if (count($this->_debug_msg) > 0)
  1978. {
  1979. foreach ($this->_debug_msg as $val)
  1980. {
  1981. $msg .= $val;
  1982. }
  1983. }
  1984. // Determine which parts of our raw data needs to be printed
  1985. $raw_data = '';
  1986. is_array($include) OR $include = array($include);
  1987. if (in_array('headers', $include, TRUE))
  1988. {
  1989. $raw_data = htmlspecialchars($this->_header_str)."\n";
  1990. }
  1991. if (in_array('subject', $include, TRUE))
  1992. {
  1993. $raw_data .= htmlspecialchars($this->_subject)."\n";
  1994. }
  1995. if (in_array('body', $include, TRUE))
  1996. {
  1997. $raw_data .= htmlspecialchars($this->_finalbody);
  1998. }
  1999. return $msg.($raw_data === '' ? '' : '<pre>'.$raw_data.'</pre>');
  2000. }
  2001. // --------------------------------------------------------------------
  2002. /**
  2003. * Set Message
  2004. *
  2005. * @param string $msg
  2006. * @param string $val = ''
  2007. * @return void
  2008. */
  2009. protected function _set_error_message($msg, $val = '')
  2010. {
  2011. $CI =& get_instance();
  2012. $CI->lang->load('email');
  2013. if (sscanf($msg, 'lang:%s', $line) !== 1 OR FALSE === ($line = $CI->lang->line($line)))
  2014. {
  2015. $this->_debug_msg[] = str_replace('%s', $val, $msg).'<br />';
  2016. }
  2017. else
  2018. {
  2019. $this->_debug_msg[] = str_replace('%s', $val, $line).'<br />';
  2020. }
  2021. }
  2022. // --------------------------------------------------------------------
  2023. /**
  2024. * Mime Types
  2025. *
  2026. * @param string
  2027. * @return string
  2028. */
  2029. protected function _mime_types($ext = '')
  2030. {
  2031. $ext = strtolower($ext);
  2032. $mimes =& get_mimes();
  2033. if (isset($mimes[$ext]))
  2034. {
  2035. return is_array($mimes[$ext])
  2036. ? current($mimes[$ext])
  2037. : $mimes[$ext];
  2038. }
  2039. return 'application/x-unknown-content-type';
  2040. }
  2041. // --------------------------------------------------------------------
  2042. /**
  2043. * Destructor
  2044. *
  2045. * @return void
  2046. */
  2047. public function __destruct()
  2048. {
  2049. is_resource($this->_smtp_connect) && $this->_send_command('quit');
  2050. }
  2051. // --------------------------------------------------------------------
  2052. /**
  2053. * Byte-safe strlen()
  2054. *
  2055. * @param string $str
  2056. * @return int
  2057. */
  2058. protected static function strlen($str)
  2059. {
  2060. return (self::$func_overload)
  2061. ? mb_strlen($str, '8bit')
  2062. : strlen($str);
  2063. }
  2064. // --------------------------------------------------------------------
  2065. /**
  2066. * Byte-safe substr()
  2067. *
  2068. * @param string $str
  2069. * @param int $start
  2070. * @param int $length
  2071. * @return string
  2072. */
  2073. protected static function substr($str, $start, $length = NULL)
  2074. {
  2075. if (self::$func_overload)
  2076. {
  2077. // mb_substr($str, $start, null, '8bit') returns an empty
  2078. // string on PHP 5.3
  2079. isset($length) OR $length = ($start >= 0 ? self::strlen($str) - $start : -$start);
  2080. return mb_substr($str, $start, $length, '8bit');
  2081. }
  2082. return isset($length)
  2083. ? substr($str, $start, $length)
  2084. : substr($str, $start);
  2085. }
  2086. }