Email.php 55 KB

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