CakeEmail.php 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601
  1. <?php
  2. /**
  3. * Cake E-Mail
  4. *
  5. * PHP 5
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * Redistributions of files must retain the above copyright notice.
  12. *
  13. * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
  14. * @link http://cakephp.org CakePHP(tm) Project
  15. * @package Cake.Network.Email
  16. * @since CakePHP(tm) v 2.0.0
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. App::uses('Validation', 'Utility');
  20. App::uses('Multibyte', 'I18n');
  21. App::uses('AbstractTransport', 'Network/Email');
  22. App::uses('String', 'Utility');
  23. App::uses('View', 'View');
  24. App::import('I18n', 'Multibyte');
  25. /**
  26. * Cake e-mail class.
  27. *
  28. * This class is used for handling Internet Message Format based
  29. * based on the standard outlined in http://www.rfc-editor.org/rfc/rfc2822.txt
  30. *
  31. * @package Cake.Network.Email
  32. */
  33. class CakeEmail {
  34. /**
  35. * Default X-Mailer
  36. *
  37. * @constant EMAIL_CLIENT
  38. */
  39. const EMAIL_CLIENT = 'CakePHP Email';
  40. /**
  41. * Line length - no should more - RFC 2822 - 2.1.1
  42. *
  43. * @constant LINE_LENGTH_SHOULD
  44. */
  45. const LINE_LENGTH_SHOULD = 78;
  46. /**
  47. * Line length - no must more - RFC 2822 - 2.1.1
  48. *
  49. * @constant LINE_LENGTH_MUST
  50. */
  51. const LINE_LENGTH_MUST = 998;
  52. /**
  53. * Type of message - HTML
  54. *
  55. * @constant MESSAGE_HTML
  56. */
  57. const MESSAGE_HTML = 'html';
  58. /**
  59. * Type of message - TEXT
  60. *
  61. * @constant MESSAGE_TEXT
  62. */
  63. const MESSAGE_TEXT = 'text';
  64. /**
  65. * Recipient of the email
  66. *
  67. * @var array
  68. */
  69. protected $_to = array();
  70. /**
  71. * The mail which the email is sent from
  72. *
  73. * @var array
  74. */
  75. protected $_from = array();
  76. /**
  77. * The sender email
  78. *
  79. * @var array
  80. */
  81. protected $_sender = array();
  82. /**
  83. * The email the recipient will reply to
  84. *
  85. * @var array
  86. */
  87. protected $_replyTo = array();
  88. /**
  89. * The read receipt email
  90. *
  91. * @var array
  92. */
  93. protected $_readReceipt = array();
  94. /**
  95. * The mail that will be used in case of any errors like
  96. * - Remote mailserver down
  97. * - Remote user has exceeded his quota
  98. * - Unknown user
  99. *
  100. * @var array
  101. */
  102. protected $_returnPath = array();
  103. /**
  104. * Carbon Copy
  105. *
  106. * List of email's that should receive a copy of the email.
  107. * The Recipient WILL be able to see this list
  108. *
  109. * @var array
  110. */
  111. protected $_cc = array();
  112. /**
  113. * Blind Carbon Copy
  114. *
  115. * List of email's that should receive a copy of the email.
  116. * The Recipient WILL NOT be able to see this list
  117. *
  118. * @var array
  119. */
  120. protected $_bcc = array();
  121. /**
  122. * Message ID
  123. *
  124. * @var boolean|string True to generate, False to ignore, String with value
  125. */
  126. protected $_messageId = true;
  127. /**
  128. * Domain for messageId generation.
  129. * Needs to be manually set for CLI mailing as env('HTTP_HOST') is empty
  130. *
  131. * @var string
  132. */
  133. protected $_domain = null;
  134. /**
  135. * The subject of the email
  136. *
  137. * @var string
  138. */
  139. protected $_subject = '';
  140. /**
  141. * Associative array of a user defined headers
  142. * Keys will be prefixed 'X-' as per RFC2822 Section 4.7.5
  143. *
  144. * @var array
  145. */
  146. protected $_headers = array();
  147. /**
  148. * Layout for the View
  149. *
  150. * @var string
  151. */
  152. protected $_layout = 'default';
  153. /**
  154. * Template for the view
  155. *
  156. * @var string
  157. */
  158. protected $_template = '';
  159. /**
  160. * View for render
  161. *
  162. * @var string
  163. */
  164. protected $_viewRender = 'View';
  165. /**
  166. * Vars to sent to render
  167. *
  168. * @var array
  169. */
  170. protected $_viewVars = array();
  171. /**
  172. * Theme for the View
  173. *
  174. * @var array
  175. */
  176. protected $_theme = null;
  177. /**
  178. * Helpers to be used in the render
  179. *
  180. * @var array
  181. */
  182. protected $_helpers = array('Html');
  183. /**
  184. * Text message
  185. *
  186. * @var string
  187. */
  188. protected $_textMessage = '';
  189. /**
  190. * Html message
  191. *
  192. * @var string
  193. */
  194. protected $_htmlMessage = '';
  195. /**
  196. * Final message to send
  197. *
  198. * @var array
  199. */
  200. protected $_message = array();
  201. /**
  202. * Available formats to be sent.
  203. *
  204. * @var array
  205. */
  206. protected $_emailFormatAvailable = array('text', 'html', 'both');
  207. /**
  208. * What format should the email be sent in
  209. *
  210. * @var string
  211. */
  212. protected $_emailFormat = 'text';
  213. /**
  214. * What method should the email be sent
  215. *
  216. * @var string
  217. */
  218. protected $_transportName = 'Mail';
  219. /**
  220. * Instance of transport class
  221. *
  222. * @var AbstractTransport
  223. */
  224. protected $_transportClass = null;
  225. /**
  226. * Charset the email body is sent in
  227. *
  228. * @var string
  229. */
  230. public $charset = 'utf-8';
  231. /**
  232. * Charset the email header is sent in
  233. * If null, the $charset property will be used as default
  234. *
  235. * @var string
  236. */
  237. public $headerCharset = null;
  238. /**
  239. * The application wide charset, used to encode headers and body
  240. *
  241. * @var string
  242. */
  243. protected $_appCharset = null;
  244. /**
  245. * List of files that should be attached to the email.
  246. *
  247. * Only absolute paths
  248. *
  249. * @var array
  250. */
  251. protected $_attachments = array();
  252. /**
  253. * If set, boundary to use for multipart mime messages
  254. *
  255. * @var string
  256. */
  257. protected $_boundary = null;
  258. /**
  259. * Configuration to transport
  260. *
  261. * @var string|array
  262. */
  263. protected $_config = array();
  264. /**
  265. * 8Bit character sets
  266. *
  267. * @var array
  268. */
  269. protected $_charset8bit = array('UTF-8', 'SHIFT_JIS');
  270. /**
  271. * Define Content-Type charset name
  272. *
  273. * @var array
  274. */
  275. protected $_contentTypeCharset = array(
  276. 'ISO-2022-JP-MS' => 'ISO-2022-JP'
  277. );
  278. /**
  279. * Constructor
  280. * @param array|string $config Array of configs, or string to load configs from email.php
  281. *
  282. */
  283. public function __construct($config = null) {
  284. $this->_appCharset = Configure::read('App.encoding');
  285. if ($this->_appCharset !== null) {
  286. $this->charset = $this->_appCharset;
  287. }
  288. $this->_domain = preg_replace('/\:\d+$/', '', env('HTTP_HOST'));
  289. if (empty($this->_domain)) {
  290. $this->_domain = php_uname('n');
  291. }
  292. if ($config) {
  293. $this->config($config);
  294. }
  295. if (empty($this->headerCharset)) {
  296. $this->headerCharset = $this->charset;
  297. }
  298. }
  299. /**
  300. * From
  301. *
  302. * @param string|array $email
  303. * @param string $name
  304. * @return array|CakeEmail
  305. * @throws SocketException
  306. */
  307. public function from($email = null, $name = null) {
  308. if ($email === null) {
  309. return $this->_from;
  310. }
  311. return $this->_setEmailSingle('_from', $email, $name, __d('cake_dev', 'From requires only 1 email address.'));
  312. }
  313. /**
  314. * Sender
  315. *
  316. * @param string|array $email
  317. * @param string $name
  318. * @return array|CakeEmail
  319. * @throws SocketException
  320. */
  321. public function sender($email = null, $name = null) {
  322. if ($email === null) {
  323. return $this->_sender;
  324. }
  325. return $this->_setEmailSingle('_sender', $email, $name, __d('cake_dev', 'Sender requires only 1 email address.'));
  326. }
  327. /**
  328. * Reply-To
  329. *
  330. * @param string|array $email
  331. * @param string $name
  332. * @return array|CakeEmail
  333. * @throws SocketException
  334. */
  335. public function replyTo($email = null, $name = null) {
  336. if ($email === null) {
  337. return $this->_replyTo;
  338. }
  339. return $this->_setEmailSingle('_replyTo', $email, $name, __d('cake_dev', 'Reply-To requires only 1 email address.'));
  340. }
  341. /**
  342. * Read Receipt (Disposition-Notification-To header)
  343. *
  344. * @param string|array $email
  345. * @param string $name
  346. * @return array|CakeEmail
  347. * @throws SocketException
  348. */
  349. public function readReceipt($email = null, $name = null) {
  350. if ($email === null) {
  351. return $this->_readReceipt;
  352. }
  353. return $this->_setEmailSingle('_readReceipt', $email, $name, __d('cake_dev', 'Disposition-Notification-To requires only 1 email address.'));
  354. }
  355. /**
  356. * Return Path
  357. *
  358. * @param string|array $email
  359. * @param string $name
  360. * @return array|CakeEmail
  361. * @throws SocketException
  362. */
  363. public function returnPath($email = null, $name = null) {
  364. if ($email === null) {
  365. return $this->_returnPath;
  366. }
  367. return $this->_setEmailSingle('_returnPath', $email, $name, __d('cake_dev', 'Return-Path requires only 1 email address.'));
  368. }
  369. /**
  370. * To
  371. *
  372. * @param string|array $email Null to get, String with email, Array with email as key, name as value or email as value (without name)
  373. * @param string $name
  374. * @return array|CakeEmail
  375. */
  376. public function to($email = null, $name = null) {
  377. if ($email === null) {
  378. return $this->_to;
  379. }
  380. return $this->_setEmail('_to', $email, $name);
  381. }
  382. /**
  383. * Add To
  384. *
  385. * @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
  386. * @param string $name
  387. * @return CakeEmail $this
  388. */
  389. public function addTo($email, $name = null) {
  390. return $this->_addEmail('_to', $email, $name);
  391. }
  392. /**
  393. * Cc
  394. *
  395. * @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
  396. * @param string $name
  397. * @return array|CakeEmail
  398. */
  399. public function cc($email = null, $name = null) {
  400. if ($email === null) {
  401. return $this->_cc;
  402. }
  403. return $this->_setEmail('_cc', $email, $name);
  404. }
  405. /**
  406. * Add Cc
  407. *
  408. * @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
  409. * @param string $name
  410. * @return CakeEmail $this
  411. */
  412. public function addCc($email, $name = null) {
  413. return $this->_addEmail('_cc', $email, $name);
  414. }
  415. /**
  416. * Bcc
  417. *
  418. * @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
  419. * @param string $name
  420. * @return array|CakeEmail
  421. */
  422. public function bcc($email = null, $name = null) {
  423. if ($email === null) {
  424. return $this->_bcc;
  425. }
  426. return $this->_setEmail('_bcc', $email, $name);
  427. }
  428. /**
  429. * Add Bcc
  430. *
  431. * @param string|array $email String with email, Array with email as key, name as value or email as value (without name)
  432. * @param string $name
  433. * @return CakeEmail $this
  434. */
  435. public function addBcc($email, $name = null) {
  436. return $this->_addEmail('_bcc', $email, $name);
  437. }
  438. /**
  439. * Charset setter/getter
  440. *
  441. * @param string $charset
  442. * @return string $this->charset
  443. */
  444. public function charset($charset = null) {
  445. if ($charset === null) {
  446. return $this->charset;
  447. }
  448. $this->charset = $charset;
  449. if (empty($this->headerCharset)) {
  450. $this->headerCharset = $charset;
  451. }
  452. return $this->charset;
  453. }
  454. /**
  455. * HeaderCharset setter/getter
  456. *
  457. * @param string $charset
  458. * @return string $this->charset
  459. */
  460. public function headerCharset($charset = null) {
  461. if ($charset === null) {
  462. return $this->headerCharset;
  463. }
  464. return $this->headerCharset = $charset;
  465. }
  466. /**
  467. * Set email
  468. *
  469. * @param string $varName
  470. * @param string|array $email
  471. * @param string $name
  472. * @return CakeEmail $this
  473. * @throws SocketException
  474. */
  475. protected function _setEmail($varName, $email, $name) {
  476. if (!is_array($email)) {
  477. if (!Validation::email($email)) {
  478. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
  479. }
  480. if ($name === null) {
  481. $name = $email;
  482. }
  483. $this->{$varName} = array($email => $name);
  484. return $this;
  485. }
  486. $list = array();
  487. foreach ($email as $key => $value) {
  488. if (is_int($key)) {
  489. $key = $value;
  490. }
  491. if (!Validation::email($key)) {
  492. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
  493. }
  494. $list[$key] = $value;
  495. }
  496. $this->{$varName} = $list;
  497. return $this;
  498. }
  499. /**
  500. * Set only 1 email
  501. *
  502. * @param string $varName
  503. * @param string|array $email
  504. * @param string $name
  505. * @param string $throwMessage
  506. * @return CakeEmail $this
  507. * @throws SocketException
  508. */
  509. protected function _setEmailSingle($varName, $email, $name, $throwMessage) {
  510. $current = $this->{$varName};
  511. $this->_setEmail($varName, $email, $name);
  512. if (count($this->{$varName}) !== 1) {
  513. $this->{$varName} = $current;
  514. throw new SocketException($throwMessage);
  515. }
  516. return $this;
  517. }
  518. /**
  519. * Add email
  520. *
  521. * @param string $varName
  522. * @param string|array $email
  523. * @param string $name
  524. * @return CakeEmail $this
  525. * @throws SocketException
  526. */
  527. protected function _addEmail($varName, $email, $name) {
  528. if (!is_array($email)) {
  529. if (!Validation::email($email)) {
  530. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $email));
  531. }
  532. if ($name === null) {
  533. $name = $email;
  534. }
  535. $this->{$varName}[$email] = $name;
  536. return $this;
  537. }
  538. $list = array();
  539. foreach ($email as $key => $value) {
  540. if (is_int($key)) {
  541. $key = $value;
  542. }
  543. if (!Validation::email($key)) {
  544. throw new SocketException(__d('cake_dev', 'Invalid email: "%s"', $key));
  545. }
  546. $list[$key] = $value;
  547. }
  548. $this->{$varName} = array_merge($this->{$varName}, $list);
  549. return $this;
  550. }
  551. /**
  552. * Get/Set Subject.
  553. *
  554. * @param string $subject
  555. * @return string|CakeEmail
  556. */
  557. public function subject($subject = null) {
  558. if ($subject === null) {
  559. return $this->_subject;
  560. }
  561. $this->_subject = $this->_encode((string)$subject);
  562. return $this;
  563. }
  564. /**
  565. * Sets headers for the message
  566. *
  567. * @param array $headers Associative array containing headers to be set.
  568. * @return CakeEmail $this
  569. * @throws SocketException
  570. */
  571. public function setHeaders($headers) {
  572. if (!is_array($headers)) {
  573. throw new SocketException(__d('cake_dev', '$headers should be an array.'));
  574. }
  575. $this->_headers = $headers;
  576. return $this;
  577. }
  578. /**
  579. * Add header for the message
  580. *
  581. * @param array $headers
  582. * @return object $this
  583. * @throws SocketException
  584. */
  585. public function addHeaders($headers) {
  586. if (!is_array($headers)) {
  587. throw new SocketException(__d('cake_dev', '$headers should be an array.'));
  588. }
  589. $this->_headers = array_merge($this->_headers, $headers);
  590. return $this;
  591. }
  592. /**
  593. * Get list of headers
  594. *
  595. * ### Includes:
  596. *
  597. * - `from`
  598. * - `replyTo`
  599. * - `readReceipt`
  600. * - `returnPath`
  601. * - `to`
  602. * - `cc`
  603. * - `bcc`
  604. * - `subject`
  605. *
  606. * @param array $include
  607. * @return array
  608. */
  609. public function getHeaders($include = array()) {
  610. if ($include == array_values($include)) {
  611. $include = array_fill_keys($include, true);
  612. }
  613. $defaults = array_fill_keys(array('from', 'sender', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc', 'subject'), false);
  614. $include += $defaults;
  615. $headers = array();
  616. $relation = array(
  617. 'from' => 'From',
  618. 'replyTo' => 'Reply-To',
  619. 'readReceipt' => 'Disposition-Notification-To',
  620. 'returnPath' => 'Return-Path'
  621. );
  622. foreach ($relation as $var => $header) {
  623. if ($include[$var]) {
  624. $var = '_' . $var;
  625. $headers[$header] = current($this->_formatAddress($this->{$var}));
  626. }
  627. }
  628. if ($include['sender']) {
  629. if (key($this->_sender) === key($this->_from)) {
  630. $headers['Sender'] = '';
  631. } else {
  632. $headers['Sender'] = current($this->_formatAddress($this->_sender));
  633. }
  634. }
  635. foreach (array('to', 'cc', 'bcc') as $var) {
  636. if ($include[$var]) {
  637. $classVar = '_' . $var;
  638. $headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar}));
  639. }
  640. }
  641. $headers += $this->_headers;
  642. if (!isset($headers['X-Mailer'])) {
  643. $headers['X-Mailer'] = self::EMAIL_CLIENT;
  644. }
  645. if (!isset($headers['Date'])) {
  646. $headers['Date'] = date(DATE_RFC2822);
  647. }
  648. if ($this->_messageId !== false) {
  649. if ($this->_messageId === true) {
  650. $headers['Message-ID'] = '<' . str_replace('-', '', String::UUID()) . '@' . $this->_domain . '>';
  651. } else {
  652. $headers['Message-ID'] = $this->_messageId;
  653. }
  654. }
  655. if ($include['subject']) {
  656. $headers['Subject'] = $this->_subject;
  657. }
  658. $headers['MIME-Version'] = '1.0';
  659. if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
  660. $headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
  661. } elseif ($this->_emailFormat === 'text') {
  662. $headers['Content-Type'] = 'text/plain; charset=' . $this->_getContentTypeCharset();
  663. } elseif ($this->_emailFormat === 'html') {
  664. $headers['Content-Type'] = 'text/html; charset=' . $this->_getContentTypeCharset();
  665. }
  666. $headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
  667. return $headers;
  668. }
  669. /**
  670. * Format addresses
  671. *
  672. * @param array $address
  673. * @return array
  674. */
  675. protected function _formatAddress($address) {
  676. $return = array();
  677. foreach ($address as $email => $alias) {
  678. if ($email === $alias) {
  679. $return[] = $email;
  680. } else {
  681. if (strpos($alias, ',') !== false) {
  682. $alias = '"' . $alias . '"';
  683. }
  684. $return[] = sprintf('%s <%s>', $this->_encode($alias), $email);
  685. }
  686. }
  687. return $return;
  688. }
  689. /**
  690. * Template and layout
  691. *
  692. * @param boolean|string $template Template name or null to not use
  693. * @param boolean|string $layout Layout name or null to not use
  694. * @return array|CakeEmail
  695. */
  696. public function template($template = false, $layout = false) {
  697. if ($template === false) {
  698. return array(
  699. 'template' => $this->_template,
  700. 'layout' => $this->_layout
  701. );
  702. }
  703. $this->_template = $template;
  704. if ($layout !== false) {
  705. $this->_layout = $layout;
  706. }
  707. return $this;
  708. }
  709. /**
  710. * View class for render
  711. *
  712. * @param string $viewClass
  713. * @return string|CakeEmail
  714. */
  715. public function viewRender($viewClass = null) {
  716. if ($viewClass === null) {
  717. return $this->_viewRender;
  718. }
  719. $this->_viewRender = $viewClass;
  720. return $this;
  721. }
  722. /**
  723. * Variables to be set on render
  724. *
  725. * @param array $viewVars
  726. * @return array|CakeEmail
  727. */
  728. public function viewVars($viewVars = null) {
  729. if ($viewVars === null) {
  730. return $this->_viewVars;
  731. }
  732. $this->_viewVars = array_merge($this->_viewVars, (array)$viewVars);
  733. return $this;
  734. }
  735. /**
  736. * Theme to use when rendering
  737. *
  738. * @param string $theme
  739. * @return string|CakeEmail
  740. */
  741. public function theme($theme = null) {
  742. if ($theme === null) {
  743. return $this->_theme;
  744. }
  745. $this->_theme = $theme;
  746. return $this;
  747. }
  748. /**
  749. * Helpers to be used in render
  750. *
  751. * @param array $helpers
  752. * @return array|CakeEmail
  753. */
  754. public function helpers($helpers = null) {
  755. if ($helpers === null) {
  756. return $this->_helpers;
  757. }
  758. $this->_helpers = (array)$helpers;
  759. return $this;
  760. }
  761. /**
  762. * Email format
  763. *
  764. * @param string $format
  765. * @return string|CakeEmail
  766. * @throws SocketException
  767. */
  768. public function emailFormat($format = null) {
  769. if ($format === null) {
  770. return $this->_emailFormat;
  771. }
  772. if (!in_array($format, $this->_emailFormatAvailable)) {
  773. throw new SocketException(__d('cake_dev', 'Format not available.'));
  774. }
  775. $this->_emailFormat = $format;
  776. return $this;
  777. }
  778. /**
  779. * Transport name
  780. *
  781. * @param string $name
  782. * @return string|CakeEmail
  783. */
  784. public function transport($name = null) {
  785. if ($name === null) {
  786. return $this->_transportName;
  787. }
  788. $this->_transportName = (string)$name;
  789. $this->_transportClass = null;
  790. return $this;
  791. }
  792. /**
  793. * Return the transport class
  794. *
  795. * @return CakeEmail
  796. * @throws SocketException
  797. */
  798. public function transportClass() {
  799. if ($this->_transportClass) {
  800. return $this->_transportClass;
  801. }
  802. list($plugin, $transportClassname) = pluginSplit($this->_transportName, true);
  803. $transportClassname .= 'Transport';
  804. App::uses($transportClassname, $plugin . 'Network/Email');
  805. if (!class_exists($transportClassname)) {
  806. throw new SocketException(__d('cake_dev', 'Class "%s" not found.', $transportClassname));
  807. } elseif (!method_exists($transportClassname, 'send')) {
  808. throw new SocketException(__d('cake_dev', 'The "%s" do not have send method.', $transportClassname));
  809. }
  810. return $this->_transportClass = new $transportClassname();
  811. }
  812. /**
  813. * Message-ID
  814. *
  815. * @param boolean|string $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID
  816. * @return boolean|string|CakeEmail
  817. * @throws SocketException
  818. */
  819. public function messageId($message = null) {
  820. if ($message === null) {
  821. return $this->_messageId;
  822. }
  823. if (is_bool($message)) {
  824. $this->_messageId = $message;
  825. } else {
  826. if (!preg_match('/^\<.+@.+\>$/', $message)) {
  827. throw new SocketException(__d('cake_dev', 'Invalid format for Message-ID. The text should be something like "<[email protected]>"'));
  828. }
  829. $this->_messageId = $message;
  830. }
  831. return $this;
  832. }
  833. /**
  834. * Domain as top level (the part after @)
  835. *
  836. * @param string $domain Manually set the domain for CLI mailing
  837. * @return string|CakeEmail
  838. */
  839. public function domain($domain = null) {
  840. if ($domain === null) {
  841. return $this->_domain;
  842. }
  843. $this->_domain = $domain;
  844. return $this;
  845. }
  846. /**
  847. * Add attachments to the email message
  848. *
  849. * Attachments can be defined in a few forms depending on how much control you need:
  850. *
  851. * Attach a single file:
  852. *
  853. * {{{
  854. * $email->attachments('path/to/file');
  855. * }}}
  856. *
  857. * Attach a file with a different filename:
  858. *
  859. * {{{
  860. * $email->attachments(array('custom_name.txt' => 'path/to/file.txt'));
  861. * }}}
  862. *
  863. * Attach a file and specify additional properties:
  864. *
  865. * {{{
  866. * $email->attachments(array('custom_name.png' => array(
  867. * 'file' => 'path/to/file',
  868. * 'mimetype' => 'image/png',
  869. * 'contentId' => 'abc123',
  870. * 'contentDisposition' => false
  871. * ));
  872. * }}}
  873. *
  874. * The `contentId` key allows you to specify an inline attachment. In your email text, you
  875. * can use `<img src="cid:abc123" />` to display the image inline.
  876. *
  877. * The `contentDisposition` key allows you to disable the `Content-Disposition` header, this can improve
  878. * attachment compatibility with outlook email clients.
  879. *
  880. * @param string|array $attachments String with the filename or array with filenames
  881. * @return array|CakeEmail Either the array of attachments when getting or $this when setting.
  882. * @throws SocketException
  883. */
  884. public function attachments($attachments = null) {
  885. if ($attachments === null) {
  886. return $this->_attachments;
  887. }
  888. $attach = array();
  889. foreach ((array)$attachments as $name => $fileInfo) {
  890. if (!is_array($fileInfo)) {
  891. $fileInfo = array('file' => $fileInfo);
  892. }
  893. if (!isset($fileInfo['file'])) {
  894. throw new SocketException(__d('cake_dev', 'File not specified.'));
  895. }
  896. $fileInfo['file'] = realpath($fileInfo['file']);
  897. if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
  898. throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileInfo['file']));
  899. }
  900. if (is_int($name)) {
  901. $name = basename($fileInfo['file']);
  902. }
  903. if (!isset($fileInfo['mimetype'])) {
  904. $fileInfo['mimetype'] = 'application/octet-stream';
  905. }
  906. $attach[$name] = $fileInfo;
  907. }
  908. $this->_attachments = $attach;
  909. return $this;
  910. }
  911. /**
  912. * Add attachments
  913. *
  914. * @param string|array $attachments String with the filename or array with filenames
  915. * @return CakeEmail $this
  916. * @throws SocketException
  917. * @see CakeEmail::attachments()
  918. */
  919. public function addAttachments($attachments) {
  920. $current = $this->_attachments;
  921. $this->attachments($attachments);
  922. $this->_attachments = array_merge($current, $this->_attachments);
  923. return $this;
  924. }
  925. /**
  926. * Get generated message (used by transport classes)
  927. *
  928. * @param string $type Use MESSAGE_* constants or null to return the full message as array
  929. * @return string|array String if have type, array if type is null
  930. */
  931. public function message($type = null) {
  932. switch ($type) {
  933. case self::MESSAGE_HTML:
  934. return $this->_htmlMessage;
  935. case self::MESSAGE_TEXT:
  936. return $this->_textMessage;
  937. }
  938. return $this->_message;
  939. }
  940. /**
  941. * Configuration to use when send email
  942. *
  943. * @param string|array $config String with configuration name (from email.php), array with config or null to return current config
  944. * @return string|array|CakeEmail
  945. */
  946. public function config($config = null) {
  947. if ($config === null) {
  948. return $this->_config;
  949. }
  950. if (!is_array($config)) {
  951. $config = (string)$config;
  952. }
  953. $this->_applyConfig($config);
  954. return $this;
  955. }
  956. /**
  957. * Send an email using the specified content, template and layout
  958. *
  959. * @param string|array $content String with message or array with messages
  960. * @return array
  961. * @throws SocketException
  962. */
  963. public function send($content = null) {
  964. if (empty($this->_from)) {
  965. throw new SocketException(__d('cake_dev', 'From is not specified.'));
  966. }
  967. if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
  968. throw new SocketException(__d('cake_dev', 'You need to specify at least one destination for to, cc or bcc.'));
  969. }
  970. if (is_array($content)) {
  971. $content = implode("\n", $content) . "\n";
  972. }
  973. $this->_textMessage = $this->_htmlMessage = '';
  974. $this->_createBoundary();
  975. $this->_message = $this->_render($this->_wrap($content));
  976. $contents = $this->transportClass()->send($this);
  977. if (!empty($this->_config['log'])) {
  978. $level = LOG_DEBUG;
  979. if ($this->_config['log'] !== true) {
  980. $level = $this->_config['log'];
  981. }
  982. CakeLog::write($level, PHP_EOL . $contents['headers'] . PHP_EOL . $contents['message']);
  983. }
  984. return $contents;
  985. }
  986. /**
  987. * Static method to fast create an instance of CakeEmail
  988. *
  989. * @param string|array $to Address to send (see CakeEmail::to()). If null, will try to use 'to' from transport config
  990. * @param string $subject String of subject or null to use 'subject' from transport config
  991. * @param string|array $message String with message or array with variables to be used in render
  992. * @param string|array $transportConfig String to use config from EmailConfig or array with configs
  993. * @param boolean $send Send the email or just return the instance pre-configured
  994. * @return CakeEmail Instance of CakeEmail
  995. * @throws SocketException
  996. */
  997. public static function deliver($to = null, $subject = null, $message = null, $transportConfig = 'fast', $send = true) {
  998. $class = __CLASS__;
  999. $instance = new $class($transportConfig);
  1000. if ($to !== null) {
  1001. $instance->to($to);
  1002. }
  1003. if ($subject !== null) {
  1004. $instance->subject($subject);
  1005. }
  1006. if (is_array($message)) {
  1007. $instance->viewVars($message);
  1008. $message = null;
  1009. } elseif ($message === null && array_key_exists('message', $config = $instance->config())) {
  1010. $message = $config['message'];
  1011. }
  1012. if ($send === true) {
  1013. $instance->send($message);
  1014. }
  1015. return $instance;
  1016. }
  1017. /**
  1018. * Apply the config to an instance
  1019. *
  1020. * @param array $config
  1021. * @return void
  1022. * @throws ConfigureException When configuration file cannot be found, or is missing
  1023. * the named config.
  1024. */
  1025. protected function _applyConfig($config) {
  1026. if (is_string($config)) {
  1027. if (!class_exists('EmailConfig') && !config('email')) {
  1028. throw new ConfigureException(__d('cake_dev', '%s not found.', APP . 'Config' . DS . 'email.php'));
  1029. }
  1030. $configs = new EmailConfig();
  1031. if (!isset($configs->{$config})) {
  1032. throw new ConfigureException(__d('cake_dev', 'Unknown email configuration "%s".', $config));
  1033. }
  1034. $config = $configs->{$config};
  1035. }
  1036. $this->_config += $config;
  1037. if (!empty($config['charset'])) {
  1038. $this->charset = $config['charset'];
  1039. }
  1040. if (!empty($config['headerCharset'])) {
  1041. $this->headerCharset = $config['headerCharset'];
  1042. }
  1043. if (empty($this->headerCharset)) {
  1044. $this->headerCharset = $this->charset;
  1045. }
  1046. $simpleMethods = array(
  1047. 'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath', 'cc', 'bcc',
  1048. 'messageId', 'domain', 'subject', 'viewRender', 'viewVars', 'attachments',
  1049. 'transport', 'emailFormat', 'theme', 'helpers'
  1050. );
  1051. foreach ($simpleMethods as $method) {
  1052. if (isset($config[$method])) {
  1053. $this->$method($config[$method]);
  1054. unset($config[$method]);
  1055. }
  1056. }
  1057. if (isset($config['headers'])) {
  1058. $this->setHeaders($config['headers']);
  1059. unset($config['headers']);
  1060. }
  1061. if (array_key_exists('template', $config)) {
  1062. $layout = false;
  1063. if (array_key_exists('layout', $config)) {
  1064. $layout = $config['layout'];
  1065. unset($config['layout']);
  1066. }
  1067. $this->template($config['template'], $layout);
  1068. unset($config['template']);
  1069. }
  1070. $this->transportClass()->config($config);
  1071. }
  1072. /**
  1073. * Reset all EmailComponent internal variables to be able to send out a new email.
  1074. *
  1075. * @return CakeEmail $this
  1076. */
  1077. public function reset() {
  1078. $this->_to = array();
  1079. $this->_from = array();
  1080. $this->_sender = array();
  1081. $this->_replyTo = array();
  1082. $this->_readReceipt = array();
  1083. $this->_returnPath = array();
  1084. $this->_cc = array();
  1085. $this->_bcc = array();
  1086. $this->_messageId = true;
  1087. $this->_subject = '';
  1088. $this->_headers = array();
  1089. $this->_layout = 'default';
  1090. $this->_template = '';
  1091. $this->_viewRender = 'View';
  1092. $this->_viewVars = array();
  1093. $this->_theme = null;
  1094. $this->_helpers = array('Html');
  1095. $this->_textMessage = '';
  1096. $this->_htmlMessage = '';
  1097. $this->_message = '';
  1098. $this->_emailFormat = 'text';
  1099. $this->_transportName = 'Mail';
  1100. $this->_transportClass = null;
  1101. $this->charset = 'utf-8';
  1102. $this->headerCharset = null;
  1103. $this->_attachments = array();
  1104. $this->_config = array();
  1105. return $this;
  1106. }
  1107. /**
  1108. * Encode the specified string using the current charset
  1109. *
  1110. * @param string $text String to encode
  1111. * @return string Encoded string
  1112. */
  1113. protected function _encode($text) {
  1114. $internalEncoding = function_exists('mb_internal_encoding');
  1115. if ($internalEncoding) {
  1116. $restore = mb_internal_encoding();
  1117. mb_internal_encoding($this->_appCharset);
  1118. }
  1119. if (empty($this->headerCharset)) {
  1120. $this->headerCharset = $this->charset;
  1121. }
  1122. $return = mb_encode_mimeheader($text, $this->headerCharset, 'B');
  1123. if ($internalEncoding) {
  1124. mb_internal_encoding($restore);
  1125. }
  1126. return $return;
  1127. }
  1128. /**
  1129. * Translates a string for one charset to another if the App.encoding value
  1130. * differs and the mb_convert_encoding function exists
  1131. *
  1132. * @param string $text The text to be converted
  1133. * @param string $charset the target encoding
  1134. * @return string
  1135. */
  1136. protected function _encodeString($text, $charset) {
  1137. if ($this->_appCharset === $charset || !function_exists('mb_convert_encoding')) {
  1138. return $text;
  1139. }
  1140. return mb_convert_encoding($text, $charset, $this->_appCharset);
  1141. }
  1142. /**
  1143. * Wrap the message to follow the RFC 2822 - 2.1.1
  1144. *
  1145. * @param string $message Message to wrap
  1146. * @return array Wrapped message
  1147. */
  1148. protected function _wrap($message, $wrapLength = CakeEmail::LINE_LENGTH_MUST) {
  1149. $message = str_replace(array("\r\n", "\r"), "\n", $message);
  1150. $lines = explode("\n", $message);
  1151. $formatted = array();
  1152. foreach ($lines as $line) {
  1153. if (empty($line)) {
  1154. $formatted[] = '';
  1155. continue;
  1156. }
  1157. if (!preg_match('/\<[a-z]/i', $line)) {
  1158. $formatted = array_merge(
  1159. $formatted,
  1160. explode("\n", wordwrap($line, $wrapLength, "\n"))
  1161. );
  1162. continue;
  1163. }
  1164. $tagOpen = false;
  1165. $tmpLine = $tag = '';
  1166. $tmpLineLength = 0;
  1167. for ($i = 0, $count = strlen($line); $i < $count; $i++) {
  1168. $char = $line[$i];
  1169. if ($tagOpen) {
  1170. $tag .= $char;
  1171. if ($char === '>') {
  1172. $tagLength = strlen($tag);
  1173. if ($tagLength + $tmpLineLength < $wrapLength) {
  1174. $tmpLine .= $tag;
  1175. $tmpLineLength += $tagLength;
  1176. } else {
  1177. if ($tmpLineLength > 0) {
  1178. $formatted[] = trim($tmpLine);
  1179. $tmpLine = '';
  1180. $tmpLineLength = 0;
  1181. }
  1182. if ($tagLength > $wrapLength) {
  1183. $formatted[] = $tag;
  1184. } else {
  1185. $tmpLine = $tag;
  1186. $tmpLineLength = $tagLength;
  1187. }
  1188. }
  1189. $tag = '';
  1190. $tagOpen = false;
  1191. }
  1192. continue;
  1193. }
  1194. if ($char === '<') {
  1195. $tagOpen = true;
  1196. $tag = '<';
  1197. continue;
  1198. }
  1199. if ($char === ' ' && $tmpLineLength >= $wrapLength) {
  1200. $formatted[] = $tmpLine;
  1201. $tmpLineLength = 0;
  1202. continue;
  1203. }
  1204. $tmpLine .= $char;
  1205. $tmpLineLength++;
  1206. if ($tmpLineLength === $wrapLength) {
  1207. $nextChar = $line[$i + 1];
  1208. if ($nextChar === ' ' || $nextChar === '<') {
  1209. $formatted[] = trim($tmpLine);
  1210. $tmpLine = '';
  1211. $tmpLineLength = 0;
  1212. if ($nextChar === ' ') {
  1213. $i++;
  1214. }
  1215. } else {
  1216. $lastSpace = strrpos($tmpLine, ' ');
  1217. if ($lastSpace === false) {
  1218. continue;
  1219. }
  1220. $formatted[] = trim(substr($tmpLine, 0, $lastSpace));
  1221. $tmpLine = substr($tmpLine, $lastSpace + 1);
  1222. $tmpLineLength = strlen($tmpLine);
  1223. }
  1224. }
  1225. }
  1226. if (!empty($tmpLine)) {
  1227. $formatted[] = $tmpLine;
  1228. }
  1229. }
  1230. $formatted[] = '';
  1231. return $formatted;
  1232. }
  1233. /**
  1234. * Create unique boundary identifier
  1235. *
  1236. * @return void
  1237. */
  1238. protected function _createBoundary() {
  1239. if (!empty($this->_attachments) || $this->_emailFormat === 'both') {
  1240. $this->_boundary = md5(uniqid(time()));
  1241. }
  1242. }
  1243. /**
  1244. * Attach non-embedded files by adding file contents inside boundaries.
  1245. *
  1246. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  1247. * @return array An array of lines to add to the message
  1248. */
  1249. protected function _attachFiles($boundary = null) {
  1250. if ($boundary === null) {
  1251. $boundary = $this->_boundary;
  1252. }
  1253. $msg = array();
  1254. foreach ($this->_attachments as $filename => $fileInfo) {
  1255. if (!empty($fileInfo['contentId'])) {
  1256. continue;
  1257. }
  1258. $data = $this->_readFile($fileInfo['file']);
  1259. $msg[] = '--' . $boundary;
  1260. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  1261. $msg[] = 'Content-Transfer-Encoding: base64';
  1262. if (
  1263. !isset($fileInfo['contentDisposition']) ||
  1264. $fileInfo['contentDisposition']
  1265. ) {
  1266. $msg[] = 'Content-Disposition: attachment; filename="' . $filename . '"';
  1267. }
  1268. $msg[] = '';
  1269. $msg[] = $data;
  1270. $msg[] = '';
  1271. }
  1272. return $msg;
  1273. }
  1274. /**
  1275. * Read the file contents and return a base64 version of the file contents.
  1276. *
  1277. * @param string $file The file to read.
  1278. * @return string File contents in base64 encoding
  1279. */
  1280. protected function _readFile($file) {
  1281. $handle = fopen($file, 'rb');
  1282. $data = fread($handle, filesize($file));
  1283. $data = chunk_split(base64_encode($data));
  1284. fclose($handle);
  1285. return $data;
  1286. }
  1287. /**
  1288. * Attach inline/embedded files to the message.
  1289. *
  1290. * @param string $boundary Boundary to use. If null, will default to $this->_boundary
  1291. * @return array An array of lines to add to the message
  1292. */
  1293. protected function _attachInlineFiles($boundary = null) {
  1294. if ($boundary === null) {
  1295. $boundary = $this->_boundary;
  1296. }
  1297. $msg = array();
  1298. foreach ($this->_attachments as $filename => $fileInfo) {
  1299. if (empty($fileInfo['contentId'])) {
  1300. continue;
  1301. }
  1302. $data = $this->_readFile($fileInfo['file']);
  1303. $msg[] = '--' . $boundary;
  1304. $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
  1305. $msg[] = 'Content-Transfer-Encoding: base64';
  1306. $msg[] = 'Content-ID: <' . $fileInfo['contentId'] . '>';
  1307. $msg[] = 'Content-Disposition: inline; filename="' . $filename . '"';
  1308. $msg[] = '';
  1309. $msg[] = $data;
  1310. $msg[] = '';
  1311. }
  1312. return $msg;
  1313. }
  1314. /**
  1315. * Render the body of the email.
  1316. *
  1317. * @param string $content Content to render
  1318. * @return array Email body ready to be sent
  1319. */
  1320. protected function _render($content) {
  1321. $content = implode("\n", $content);
  1322. $rendered = $this->_renderTemplates($content);
  1323. $msg = array();
  1324. $contentIds = array_filter((array)Hash::extract($this->_attachments, '{s}.contentId'));
  1325. $hasInlineAttachments = count($contentIds) > 0;
  1326. $hasAttachments = !empty($this->_attachments);
  1327. $hasMultipleTypes = count($rendered) > 1;
  1328. $boundary = $relBoundary = $textBoundary = $this->_boundary;
  1329. if ($hasInlineAttachments) {
  1330. $msg[] = '--' . $boundary;
  1331. $msg[] = 'Content-Type: multipart/related; boundary="rel-' . $boundary . '"';
  1332. $msg[] = '';
  1333. $relBoundary = $textBoundary = 'rel-' . $boundary;
  1334. }
  1335. if ($hasMultipleTypes) {
  1336. $msg[] = '--' . $relBoundary;
  1337. $msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $boundary . '"';
  1338. $msg[] = '';
  1339. $textBoundary = 'alt-' . $boundary;
  1340. }
  1341. if (isset($rendered['text'])) {
  1342. if ($textBoundary !== $boundary || $hasAttachments) {
  1343. $msg[] = '--' . $textBoundary;
  1344. $msg[] = 'Content-Type: text/plain; charset=' . $this->_getContentTypeCharset();
  1345. $msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
  1346. $msg[] = '';
  1347. }
  1348. $this->_textMessage = $rendered['text'];
  1349. $content = explode("\n", $this->_textMessage);
  1350. $msg = array_merge($msg, $content);
  1351. $msg[] = '';
  1352. }
  1353. if (isset($rendered['html'])) {
  1354. if ($textBoundary !== $boundary || $hasAttachments) {
  1355. $msg[] = '--' . $textBoundary;
  1356. $msg[] = 'Content-Type: text/html; charset=' . $this->_getContentTypeCharset();
  1357. $msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
  1358. $msg[] = '';
  1359. }
  1360. $this->_htmlMessage = $rendered['html'];
  1361. $content = explode("\n", $this->_htmlMessage);
  1362. $msg = array_merge($msg, $content);
  1363. $msg[] = '';
  1364. }
  1365. if ($hasMultipleTypes) {
  1366. $msg[] = '--' . $textBoundary . '--';
  1367. $msg[] = '';
  1368. }
  1369. if ($hasInlineAttachments) {
  1370. $attachments = $this->_attachInlineFiles($relBoundary);
  1371. $msg = array_merge($msg, $attachments);
  1372. $msg[] = '';
  1373. $msg[] = '--' . $relBoundary . '--';
  1374. $msg[] = '';
  1375. }
  1376. if ($hasAttachments) {
  1377. $attachments = $this->_attachFiles($boundary);
  1378. $msg = array_merge($msg, $attachments);
  1379. }
  1380. if ($hasAttachments || $hasMultipleTypes) {
  1381. $msg[] = '';
  1382. $msg[] = '--' . $boundary . '--';
  1383. $msg[] = '';
  1384. }
  1385. return $msg;
  1386. }
  1387. /**
  1388. * Gets the text body types that are in this email message
  1389. *
  1390. * @return array Array of types. Valid types are 'text' and 'html'
  1391. */
  1392. protected function _getTypes() {
  1393. $types = array($this->_emailFormat);
  1394. if ($this->_emailFormat == 'both') {
  1395. $types = array('html', 'text');
  1396. }
  1397. return $types;
  1398. }
  1399. /**
  1400. * Build and set all the view properties needed to render the templated emails.
  1401. * If there is no template set, the $content will be returned in a hash
  1402. * of the text content types for the email.
  1403. *
  1404. * @param string $content The content passed in from send() in most cases.
  1405. * @return array The rendered content with html and text keys.
  1406. */
  1407. protected function _renderTemplates($content) {
  1408. $types = $this->_getTypes();
  1409. $rendered = array();
  1410. if (empty($this->_template)) {
  1411. foreach ($types as $type) {
  1412. $rendered[$type] = $this->_encodeString($content, $this->charset);
  1413. }
  1414. return $rendered;
  1415. }
  1416. $viewClass = $this->_viewRender;
  1417. if ($viewClass !== 'View') {
  1418. list($plugin, $viewClass) = pluginSplit($viewClass, true);
  1419. $viewClass .= 'View';
  1420. App::uses($viewClass, $plugin . 'View');
  1421. }
  1422. $View = new $viewClass(null);
  1423. $View->viewVars = $this->_viewVars;
  1424. $View->helpers = $this->_helpers;
  1425. list($templatePlugin, $template) = pluginSplit($this->_template);
  1426. list($layoutPlugin, $layout) = pluginSplit($this->_layout);
  1427. if ($templatePlugin) {
  1428. $View->plugin = $templatePlugin;
  1429. } elseif ($layoutPlugin) {
  1430. $View->plugin = $layoutPlugin;
  1431. }
  1432. if ($this->_theme) {
  1433. $View->theme = $this->_theme;
  1434. }
  1435. foreach ($types as $type) {
  1436. $View->set('content', $content);
  1437. $View->hasRendered = false;
  1438. $View->viewPath = $View->layoutPath = 'Emails' . DS . $type;
  1439. $render = $View->render($template, $layout);
  1440. $render = str_replace(array("\r\n", "\r"), "\n", $render);
  1441. $rendered[$type] = $this->_encodeString($render, $this->charset);
  1442. }
  1443. return $rendered;
  1444. }
  1445. /**
  1446. * Return the Content-Transfer Encoding value based on the set charset
  1447. *
  1448. * @return void
  1449. */
  1450. protected function _getContentTransferEncoding() {
  1451. $charset = strtoupper($this->charset);
  1452. if (in_array($charset, $this->_charset8bit)) {
  1453. return '8bit';
  1454. }
  1455. return '7bit';
  1456. }
  1457. /**
  1458. * Return charset value for Content-Type.
  1459. *
  1460. * Checks fallback/compatibility types which include workarounds
  1461. * for legacy japanese character sets.
  1462. *
  1463. * @return string
  1464. */
  1465. protected function _getContentTypeCharset() {
  1466. $charset = strtoupper($this->charset);
  1467. if (array_key_exists($charset, $this->_contentTypeCharset)) {
  1468. return strtoupper($this->_contentTypeCharset[$charset]);
  1469. } else {
  1470. return strtoupper($this->charset);
  1471. }
  1472. }
  1473. }