Email.php 59 KB

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