Email.php 44 KB

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