Email.php 60 KB

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