Email.php 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  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\Http\Client\FormDataPart;
  22. use Cake\Log\Log;
  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;
  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|null
  200. */
  201. protected $_transport;
  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|null
  213. */
  214. public $headerCharset;
  215. /**
  216. * The application wide charset, used to encode headers and body
  217. *
  218. * @var string|null
  219. */
  220. protected $_appCharset;
  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|null
  233. */
  234. protected $_boundary;
  235. /**
  236. * Contains the optional priority of the email.
  237. *
  238. * @var int|null
  239. */
  240. protected $_priority;
  241. /**
  242. * An array mapping url schemes to fully qualified Transport class names
  243. *
  244. * @var array
  245. */
  246. protected static $_dsnClassMap = [
  247. 'debug' => 'Cake\Mailer\Transport\DebugTransport',
  248. 'mail' => 'Cake\Mailer\Transport\MailTransport',
  249. 'smtp' => 'Cake\Mailer\Transport\SmtpTransport',
  250. ];
  251. /**
  252. * Configuration profiles for transports.
  253. *
  254. * @var array
  255. */
  256. protected static $_transportConfig = [];
  257. /**
  258. * A copy of the configuration profile for this
  259. * instance. This copy can be modified with Email::profile().
  260. *
  261. * @var array
  262. */
  263. protected $_profile = [];
  264. /**
  265. * 8Bit character sets
  266. *
  267. * @var array
  268. */
  269. protected $_charset8bit = ['UTF-8', 'SHIFT_JIS'];
  270. /**
  271. * Define Content-Type charset name
  272. *
  273. * @var array
  274. */
  275. protected $_contentTypeCharset = [
  276. 'ISO-2022-JP-MS' => 'ISO-2022-JP'
  277. ];
  278. /**
  279. * Regex for email validation
  280. *
  281. * If null, filter_var() will be used. Use the emailPattern() method
  282. * to set a custom pattern.'
  283. *
  284. * @var string
  285. */
  286. protected $_emailPattern = self::EMAIL_PATTERN;
  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. ->setClassName('Cake\View\View')
  304. ->setTemplate('')
  305. ->setLayout('default')
  306. ->setHelpers(['Html']);
  307. if ($config === null) {
  308. $config = static::getConfig('default');
  309. }
  310. if ($config) {
  311. $this->setProfile($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. * Sets "from" address.
  328. *
  329. * @param string|array $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 $this
  333. * @throws \InvalidArgumentException
  334. */
  335. public function setFrom($email, $name = null)
  336. {
  337. return $this->_setEmailSingle('_from', $email, $name, 'From requires only 1 email address.');
  338. }
  339. /**
  340. * Gets "from" address.
  341. *
  342. * @return array
  343. */
  344. public function getFrom()
  345. {
  346. return $this->_from;
  347. }
  348. /**
  349. * From
  350. *
  351. * @deprecated 3.4.0 Use setFrom()/getFrom() instead.
  352. * @param string|array|null $email Null to get, String with email,
  353. * Array with email as key, name as value or email as value (without name)
  354. * @param string|null $name Name
  355. * @return array|$this
  356. * @throws \InvalidArgumentException
  357. */
  358. public function from($email = null, $name = null)
  359. {
  360. if ($email === null) {
  361. return $this->getFrom();
  362. }
  363. return $this->setFrom($email, $name);
  364. }
  365. /**
  366. * Sets "sender" address.
  367. *
  368. * @param string|array $email String with email,
  369. * Array with email as key, name as value or email as value (without name)
  370. * @param string|null $name Name
  371. * @return $this
  372. * @throws \InvalidArgumentException
  373. */
  374. public function setSender($email, $name = null)
  375. {
  376. return $this->_setEmailSingle('_sender', $email, $name, 'Sender requires only 1 email address.');
  377. }
  378. /**
  379. * Gets "sender" address.
  380. *
  381. * @return array
  382. */
  383. public function getSender()
  384. {
  385. return $this->_sender;
  386. }
  387. /**
  388. * Sender
  389. *
  390. * @deprecated 3.4.0 Use setSender()/getSender() instead.
  391. * @param string|array|null $email Null to get, String with email,
  392. * Array with email as key, name as value or email as value (without name)
  393. * @param string|null $name Name
  394. * @return array|$this
  395. * @throws \InvalidArgumentException
  396. */
  397. public function sender($email = null, $name = null)
  398. {
  399. if ($email === null) {
  400. return $this->getSender();
  401. }
  402. return $this->setSender($email, $name);
  403. }
  404. /**
  405. * Sets "Reply-To" address.
  406. *
  407. * @param string|array $email String with email,
  408. * Array with email as key, name as value or email as value (without name)
  409. * @param string|null $name Name
  410. * @return $this
  411. * @throws \InvalidArgumentException
  412. */
  413. public function setReplyTo($email, $name = null)
  414. {
  415. return $this->_setEmailSingle('_replyTo', $email, $name, 'Reply-To requires only 1 email address.');
  416. }
  417. /**
  418. * Gets "Reply-To" address.
  419. *
  420. * @return array
  421. */
  422. public function getReplyTo()
  423. {
  424. return $this->_replyTo;
  425. }
  426. /**
  427. * Reply-To
  428. *
  429. * @deprecated 3.4.0 Use setReplyTo()/getReplyTo() instead.
  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. * @throws \InvalidArgumentException
  435. */
  436. public function replyTo($email = null, $name = null)
  437. {
  438. if ($email === null) {
  439. return $this->getReplyTo();
  440. }
  441. return $this->setReplyTo($email, $name);
  442. }
  443. /**
  444. * Sets Read Receipt (Disposition-Notification-To header).
  445. *
  446. * @param string|array $email String with email,
  447. * Array with email as key, name as value or email as value (without name)
  448. * @param string|null $name Name
  449. * @return $this
  450. * @throws \InvalidArgumentException
  451. */
  452. public function setReadReceipt($email, $name = null)
  453. {
  454. return $this->_setEmailSingle('_readReceipt', $email, $name, 'Disposition-Notification-To requires only 1 email address.');
  455. }
  456. /**
  457. * Gets Read Receipt (Disposition-Notification-To header).
  458. *
  459. * @return array
  460. */
  461. public function getReadReceipt()
  462. {
  463. return $this->_readReceipt;
  464. }
  465. /**
  466. * Read Receipt (Disposition-Notification-To header)
  467. *
  468. * @deprecated 3.4.0 Use setReadReceipt()/getReadReceipt() instead.
  469. * @param string|array|null $email Null to get, String with email,
  470. * Array with email as key, name as value or email as value (without name)
  471. * @param string|null $name Name
  472. * @return array|$this
  473. * @throws \InvalidArgumentException
  474. */
  475. public function readReceipt($email = null, $name = null)
  476. {
  477. if ($email === null) {
  478. return $this->getReadReceipt();
  479. }
  480. return $this->setReadReceipt($email, $name);
  481. }
  482. /**
  483. * Return Path
  484. *
  485. * @param string|array $email String with email,
  486. * Array with email as key, name as value or email as value (without name)
  487. * @param string|null $name Name
  488. * @return $this
  489. * @throws \InvalidArgumentException
  490. */
  491. public function setReturnPath($email, $name = null)
  492. {
  493. return $this->_setEmailSingle('_returnPath', $email, $name, 'Return-Path requires only 1 email address.');
  494. }
  495. /**
  496. * Gets return path.
  497. *
  498. * @return array
  499. */
  500. public function getReturnPath()
  501. {
  502. return $this->_returnPath;
  503. }
  504. /**
  505. * Return Path
  506. *
  507. * @deprecated 3.4.0 Use setReturnPath()/getReturnPath() instead.
  508. * @param string|array|null $email Null to get, String with email,
  509. * Array with email as key, name as value or email as value (without name)
  510. * @param string|null $name Name
  511. * @return array|$this
  512. * @throws \InvalidArgumentException
  513. */
  514. public function returnPath($email = null, $name = null)
  515. {
  516. if ($email === null) {
  517. return $this->getReturnPath();
  518. }
  519. return $this->setReturnPath($email, $name);
  520. }
  521. /**
  522. * Sets "to" address.
  523. *
  524. * @param string|array $email String with email,
  525. * Array with email as key, name as value or email as value (without name)
  526. * @param string|null $name Name
  527. * @return $this
  528. */
  529. public function setTo($email, $name = null)
  530. {
  531. return $this->_setEmail('_to', $email, $name);
  532. }
  533. /**
  534. * Gets "to" address
  535. *
  536. * @return array
  537. */
  538. public function getTo()
  539. {
  540. return $this->_to;
  541. }
  542. /**
  543. * To
  544. *
  545. * @deprecated 3.4.0 Use setTo()/getTo() instead.
  546. * @param string|array|null $email Null to get, String with email,
  547. * Array with email as key, name as value or email as value (without name)
  548. * @param string|null $name Name
  549. * @return array|$this
  550. */
  551. public function to($email = null, $name = null)
  552. {
  553. if ($email === null) {
  554. return $this->getTo();
  555. }
  556. return $this->setTo($email, $name);
  557. }
  558. /**
  559. * Add To
  560. *
  561. * @param string|array $email Null to get, String with email,
  562. * Array with email as key, name as value or email as value (without name)
  563. * @param string|null $name Name
  564. * @return $this
  565. */
  566. public function addTo($email, $name = null)
  567. {
  568. return $this->_addEmail('_to', $email, $name);
  569. }
  570. /**
  571. * Sets "cc" address.
  572. *
  573. * @param string|array $email String with email,
  574. * Array with email as key, name as value or email as value (without name)
  575. * @param string|null $name Name
  576. * @return $this
  577. */
  578. public function setCc($email, $name = null)
  579. {
  580. return $this->_setEmail('_cc', $email, $name);
  581. }
  582. /**
  583. * Gets "cc" address.
  584. *
  585. * @return array
  586. */
  587. public function getCc()
  588. {
  589. return $this->_cc;
  590. }
  591. /**
  592. * Cc
  593. *
  594. * @deprecated 3.4.0 Use setCc()/getCc() instead.
  595. * @param string|array|null $email Null to get, String with email,
  596. * Array with email as key, name as value or email as value (without name)
  597. * @param string|null $name Name
  598. * @return array|$this
  599. */
  600. public function cc($email = null, $name = null)
  601. {
  602. if ($email === null) {
  603. return $this->getCc();
  604. }
  605. return $this->setCc($email, $name);
  606. }
  607. /**
  608. * Add Cc
  609. *
  610. * @param string|array $email Null to get, String with email,
  611. * Array with email as key, name as value or email as value (without name)
  612. * @param string|null $name Name
  613. * @return $this
  614. */
  615. public function addCc($email, $name = null)
  616. {
  617. return $this->_addEmail('_cc', $email, $name);
  618. }
  619. /**
  620. * Sets "bcc" address.
  621. *
  622. * @param string|array $email String with email,
  623. * Array with email as key, name as value or email as value (without name)
  624. * @param string|null $name Name
  625. * @return $this
  626. */
  627. public function setBcc($email, $name = null)
  628. {
  629. return $this->_setEmail('_bcc', $email, $name);
  630. }
  631. /**
  632. * Gets "bcc" address.
  633. *
  634. * @return array
  635. */
  636. public function getBcc()
  637. {
  638. return $this->_bcc;
  639. }
  640. /**
  641. * Bcc
  642. *
  643. * @deprecated 3.4.0 Use setBcc()/getBcc() instead.
  644. * @param string|array|null $email Null to get, String with email,
  645. * Array with email as key, name as value or email as value (without name)
  646. * @param string|null $name Name
  647. * @return array|$this
  648. */
  649. public function bcc($email = null, $name = null)
  650. {
  651. if ($email === null) {
  652. return $this->getBcc();
  653. }
  654. return $this->setBcc($email, $name);
  655. }
  656. /**
  657. * Add Bcc
  658. *
  659. * @param string|array $email Null to get, String with email,
  660. * Array with email as key, name as value or email as value (without name)
  661. * @param string|null $name Name
  662. * @return $this
  663. */
  664. public function addBcc($email, $name = null)
  665. {
  666. return $this->_addEmail('_bcc', $email, $name);
  667. }
  668. /**
  669. * Charset setter.
  670. *
  671. * @param string|null $charset Character set.
  672. * @return $this
  673. */
  674. public function setCharset($charset)
  675. {
  676. $this->charset = $charset;
  677. if (!$this->headerCharset) {
  678. $this->headerCharset = $charset;
  679. }
  680. return $this;
  681. }
  682. /**
  683. * Charset getter.
  684. *
  685. * @return string Charset
  686. */
  687. public function getCharset()
  688. {
  689. return $this->charset;
  690. }
  691. /**
  692. * Charset setter/getter
  693. *
  694. * @deprecated 3.4.0 Use setCharset()/getCharset() instead.
  695. * @param string|null $charset Character set.
  696. * @return string Charset
  697. */
  698. public function charset($charset = null)
  699. {
  700. if ($charset === null) {
  701. return $this->getCharset();
  702. }
  703. $this->setCharset($charset);
  704. return $this->charset;
  705. }
  706. /**
  707. * HeaderCharset setter.
  708. *
  709. * @param string|null $charset Character set.
  710. * @return $this
  711. */
  712. public function setHeaderCharset($charset)
  713. {
  714. $this->headerCharset = $charset;
  715. return $this;
  716. }
  717. /**
  718. * HeaderCharset getter.
  719. *
  720. * @return string Charset
  721. */
  722. public function getHeaderCharset()
  723. {
  724. return $this->headerCharset;
  725. }
  726. /**
  727. * HeaderCharset setter/getter
  728. *
  729. * @deprecated 3.4.0 Use setHeaderCharset()/getHeaderCharset() instead.
  730. * @param string|null $charset Character set.
  731. * @return string Charset
  732. */
  733. public function headerCharset($charset = null)
  734. {
  735. if ($charset === null) {
  736. return $this->getHeaderCharset();
  737. }
  738. $this->setHeaderCharset($charset);
  739. return $this->headerCharset;
  740. }
  741. /**
  742. * EmailPattern setter/getter
  743. *
  744. * @param string|null $regex The pattern to use for email address validation,
  745. * null to unset the pattern and make use of filter_var() instead.
  746. * @return $this
  747. */
  748. public function setEmailPattern($regex)
  749. {
  750. $this->_emailPattern = $regex;
  751. return $this;
  752. }
  753. /**
  754. * EmailPattern setter/getter
  755. *
  756. * @return string
  757. */
  758. public function getEmailPattern()
  759. {
  760. return $this->_emailPattern;
  761. }
  762. /**
  763. * EmailPattern setter/getter
  764. *
  765. * @deprecated 3.4.0 Use setEmailPattern()/getEmailPattern() instead.
  766. * @param string|bool|null $regex The pattern to use for email address validation,
  767. * null to unset the pattern and make use of filter_var() instead, false or
  768. * nothing to return the current value
  769. * @return string|$this
  770. */
  771. public function emailPattern($regex = false)
  772. {
  773. if ($regex === false) {
  774. return $this->getEmailPattern();
  775. }
  776. return $this->setEmailPattern($regex);
  777. }
  778. /**
  779. * Set email
  780. *
  781. * @param string $varName Property name
  782. * @param string|array $email String with email,
  783. * Array with email as key, name as value or email as value (without name)
  784. * @param string $name Name
  785. * @return $this
  786. * @throws \InvalidArgumentException
  787. */
  788. protected function _setEmail($varName, $email, $name)
  789. {
  790. if (!is_array($email)) {
  791. $this->_validateEmail($email);
  792. if ($name === null) {
  793. $name = $email;
  794. }
  795. $this->{$varName} = [$email => $name];
  796. return $this;
  797. }
  798. $list = [];
  799. foreach ($email as $key => $value) {
  800. if (is_int($key)) {
  801. $key = $value;
  802. }
  803. $this->_validateEmail($key);
  804. $list[$key] = $value;
  805. }
  806. $this->{$varName} = $list;
  807. return $this;
  808. }
  809. /**
  810. * Validate email address
  811. *
  812. * @param string $email Email address to validate
  813. * @return void
  814. * @throws \InvalidArgumentException If email address does not validate
  815. */
  816. protected function _validateEmail($email)
  817. {
  818. if ($this->_emailPattern === null) {
  819. if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
  820. return;
  821. }
  822. } elseif (preg_match($this->_emailPattern, $email)) {
  823. return;
  824. }
  825. throw new InvalidArgumentException(sprintf('Invalid email: "%s"', $email));
  826. }
  827. /**
  828. * Set only 1 email
  829. *
  830. * @param string $varName Property name
  831. * @param string|array $email String with email,
  832. * Array with email as key, name as value or email as value (without name)
  833. * @param string $name Name
  834. * @param string $throwMessage Exception message
  835. * @return $this
  836. * @throws \InvalidArgumentException
  837. */
  838. protected function _setEmailSingle($varName, $email, $name, $throwMessage)
  839. {
  840. $current = $this->{$varName};
  841. $this->_setEmail($varName, $email, $name);
  842. if (count($this->{$varName}) !== 1) {
  843. $this->{$varName} = $current;
  844. throw new InvalidArgumentException($throwMessage);
  845. }
  846. return $this;
  847. }
  848. /**
  849. * Add email
  850. *
  851. * @param string $varName Property name
  852. * @param string|array $email String with email,
  853. * Array with email as key, name as value or email as value (without name)
  854. * @param string $name Name
  855. * @return $this
  856. * @throws \InvalidArgumentException
  857. */
  858. protected function _addEmail($varName, $email, $name)
  859. {
  860. if (!is_array($email)) {
  861. $this->_validateEmail($email);
  862. if ($name === null) {
  863. $name = $email;
  864. }
  865. $this->{$varName}[$email] = $name;
  866. return $this;
  867. }
  868. $list = [];
  869. foreach ($email as $key => $value) {
  870. if (is_int($key)) {
  871. $key = $value;
  872. }
  873. $this->_validateEmail($key);
  874. $list[$key] = $value;
  875. }
  876. $this->{$varName} = array_merge($this->{$varName}, $list);
  877. return $this;
  878. }
  879. /**
  880. * Sets subject.
  881. *
  882. * @param string $subject Subject string.
  883. * @return $this
  884. */
  885. public function setSubject($subject)
  886. {
  887. $this->_subject = $this->_encode((string)$subject);
  888. return $this;
  889. }
  890. /**
  891. * Gets subject.
  892. *
  893. * @return string
  894. */
  895. public function getSubject()
  896. {
  897. return $this->_subject;
  898. }
  899. /**
  900. * Get/Set Subject.
  901. *
  902. * @deprecated 3.4.0 Use setSubject()/getSubject() instead.
  903. * @param string|null $subject Subject string.
  904. * @return string|$this
  905. */
  906. public function subject($subject = null)
  907. {
  908. if ($subject === null) {
  909. return $this->getSubject();
  910. }
  911. return $this->setSubject($subject);
  912. }
  913. /**
  914. * Get original subject without encoding
  915. *
  916. * @return string Original subject
  917. */
  918. public function getOriginalSubject()
  919. {
  920. return $this->_decode($this->_subject);
  921. }
  922. /**
  923. * Sets headers for the message
  924. *
  925. * @param array $headers Associative array containing headers to be set.
  926. * @return $this
  927. */
  928. public function setHeaders(array $headers)
  929. {
  930. $this->_headers = $headers;
  931. return $this;
  932. }
  933. /**
  934. * Add header for the message
  935. *
  936. * @param array $headers Headers to set.
  937. * @return $this
  938. */
  939. public function addHeaders(array $headers)
  940. {
  941. $this->_headers = array_merge($this->_headers, $headers);
  942. return $this;
  943. }
  944. /**
  945. * Get list of headers
  946. *
  947. * ### Includes:
  948. *
  949. * - `from`
  950. * - `replyTo`
  951. * - `readReceipt`
  952. * - `returnPath`
  953. * - `to`
  954. * - `cc`
  955. * - `bcc`
  956. * - `subject`
  957. *
  958. * @param array $include List of headers.
  959. * @return array
  960. */
  961. public function getHeaders(array $include = [])
  962. {
  963. if ($include == array_values($include)) {
  964. $include = array_fill_keys($include, true);
  965. }
  966. $defaults = array_fill_keys(
  967. [
  968. 'from', 'sender', 'replyTo', 'readReceipt', 'returnPath',
  969. 'to', 'cc', 'bcc', 'subject'],
  970. false
  971. );
  972. $include += $defaults;
  973. $headers = [];
  974. $relation = [
  975. 'from' => 'From',
  976. 'replyTo' => 'Reply-To',
  977. 'readReceipt' => 'Disposition-Notification-To',
  978. 'returnPath' => 'Return-Path'
  979. ];
  980. foreach ($relation as $var => $header) {
  981. if ($include[$var]) {
  982. $var = '_' . $var;
  983. $headers[$header] = current($this->_formatAddress($this->{$var}));
  984. }
  985. }
  986. if ($include['sender']) {
  987. if (key($this->_sender) === key($this->_from)) {
  988. $headers['Sender'] = '';
  989. } else {
  990. $headers['Sender'] = current($this->_formatAddress($this->_sender));
  991. }
  992. }
  993. foreach (['to', 'cc', 'bcc'] as $var) {
  994. if ($include[$var]) {
  995. $classVar = '_' . $var;
  996. $headers[ucfirst($var)] = implode(', ', $this->_formatAddress($this->{$classVar}));
  997. }
  998. }
  999. $headers += $this->_headers;
  1000. if (!isset($headers['Date'])) {
  1001. $headers['Date'] = date(DATE_RFC2822);
  1002. }
  1003. if ($this->_messageId !== false) {
  1004. if ($this->_messageId === true) {
  1005. $headers['Message-ID'] = '<' . str_replace('-', '', Text::uuid()) . '@' . $this->_domain . '>';
  1006. } else {
  1007. $headers['Message-ID'] = $this->_messageId;
  1008. }
  1009. }
  1010. if ($this->_priority) {
  1011. $headers['X-Priority'] = $this->_priority;
  1012. }
  1013. if ($include['subject']) {
  1014. $headers['Subject'] = $this->_subject;
  1015. }
  1016. $headers['MIME-Version'] = '1.0';
  1017. if ($this->_attachments) {
  1018. $headers['Content-Type'] = 'multipart/mixed; boundary="' . $this->_boundary . '"';
  1019. } elseif ($this->_emailFormat === 'both') {
  1020. $headers['Content-Type'] = 'multipart/alternative; boundary="' . $this->_boundary . '"';
  1021. } elseif ($this->_emailFormat === 'text') {
  1022. $headers['Content-Type'] = 'text/plain; charset=' . $this->_getContentTypeCharset();
  1023. } elseif ($this->_emailFormat === 'html') {
  1024. $headers['Content-Type'] = 'text/html; charset=' . $this->_getContentTypeCharset();
  1025. }
  1026. $headers['Content-Transfer-Encoding'] = $this->_getContentTransferEncoding();
  1027. return $headers;
  1028. }
  1029. /**
  1030. * Format addresses
  1031. *
  1032. * If the address contains non alphanumeric/whitespace characters, it will
  1033. * be quoted as characters like `:` and `,` are known to cause issues
  1034. * in address header fields.
  1035. *
  1036. * @param array $address Addresses to format.
  1037. * @return array
  1038. */
  1039. protected function _formatAddress($address)
  1040. {
  1041. $return = [];
  1042. foreach ($address as $email => $alias) {
  1043. if ($email === $alias) {
  1044. $return[] = $email;
  1045. } else {
  1046. $encoded = $this->_encode($alias);
  1047. if ($encoded === $alias && preg_match('/[^a-z0-9 ]/i', $encoded)) {
  1048. $encoded = '"' . str_replace('"', '\"', $encoded) . '"';
  1049. }
  1050. $return[] = sprintf('%s <%s>', $encoded, $email);
  1051. }
  1052. }
  1053. return $return;
  1054. }
  1055. /**
  1056. * Sets template.
  1057. *
  1058. * @param string|null $template Template name or null to not use.
  1059. * @return $this
  1060. */
  1061. public function setTemplate($template)
  1062. {
  1063. $this->viewBuilder()->setTemplate($template ?: '');
  1064. return $this;
  1065. }
  1066. /**
  1067. * Gets template.
  1068. *
  1069. * @return string
  1070. */
  1071. public function getTemplate()
  1072. {
  1073. return $this->viewBuilder()->getTemplate();
  1074. }
  1075. /**
  1076. * Sets layout.
  1077. *
  1078. * @param string|null $layout Layout name or null to not use
  1079. * @return $this
  1080. */
  1081. public function setLayout($layout)
  1082. {
  1083. $this->viewBuilder()->setLayout($layout ?: false);
  1084. return $this;
  1085. }
  1086. /**
  1087. * Gets layout.
  1088. *
  1089. * @return string
  1090. */
  1091. public function getLayout()
  1092. {
  1093. return $this->viewBuilder()->getLayout();
  1094. }
  1095. /**
  1096. * Template and layout
  1097. *
  1098. * @deprecated 3.4.0 Use setTemplate()/getTemplate() and setLayout()/getLayout() instead.
  1099. * @param bool|string $template Template name or null to not use
  1100. * @param bool|string $layout Layout name or null to not use
  1101. * @return array|$this
  1102. */
  1103. public function template($template = false, $layout = false)
  1104. {
  1105. if ($template === false) {
  1106. return [
  1107. 'template' => $this->getTemplate(),
  1108. 'layout' => $this->getLayout()
  1109. ];
  1110. }
  1111. $this->setTemplate($template);
  1112. if ($layout !== false) {
  1113. $this->setLayout($layout);
  1114. }
  1115. return $this;
  1116. }
  1117. /**
  1118. * Sets view class for render.
  1119. *
  1120. * @param string $viewClass View class name.
  1121. * @return $this
  1122. */
  1123. public function setViewRenderer($viewClass)
  1124. {
  1125. $this->viewBuilder()->setClassName($viewClass);
  1126. return $this;
  1127. }
  1128. /**
  1129. * Gets view class for render.
  1130. *
  1131. * @return string
  1132. */
  1133. public function getViewRenderer()
  1134. {
  1135. return $this->viewBuilder()->getClassName();
  1136. }
  1137. /**
  1138. * View class for render
  1139. *
  1140. * @deprecated 3.4.0 Use setViewRenderer()/getViewRenderer() instead.
  1141. * @param string|null $viewClass View class name.
  1142. * @return string|$this
  1143. */
  1144. public function viewRender($viewClass = null)
  1145. {
  1146. if ($viewClass === null) {
  1147. return $this->getViewRenderer();
  1148. }
  1149. $this->setViewRenderer($viewClass);
  1150. return $this;
  1151. }
  1152. /**
  1153. * Sets variables to be set on render.
  1154. *
  1155. * @param array $viewVars Variables to set for view.
  1156. * @return $this
  1157. */
  1158. public function setViewVars($viewVars)
  1159. {
  1160. $this->set((array)$viewVars);
  1161. return $this;
  1162. }
  1163. /**
  1164. * Gets variables to be set on render.
  1165. *
  1166. * @return array
  1167. */
  1168. public function getViewVars()
  1169. {
  1170. return $this->viewVars;
  1171. }
  1172. /**
  1173. * Variables to be set on render
  1174. *
  1175. * @deprecated 3.4.0 Use setViewVars()/getViewVars() instead.
  1176. * @param array|null $viewVars Variables to set for view.
  1177. * @return array|$this
  1178. */
  1179. public function viewVars($viewVars = null)
  1180. {
  1181. if ($viewVars === null) {
  1182. return $this->getViewVars();
  1183. }
  1184. return $this->setViewVars($viewVars);
  1185. }
  1186. /**
  1187. * Sets theme to use when rendering.
  1188. *
  1189. * @param string $theme Theme name.
  1190. * @return $this
  1191. */
  1192. public function setTheme($theme)
  1193. {
  1194. $this->viewBuilder()->setTheme($theme);
  1195. return $this;
  1196. }
  1197. /**
  1198. * Gets theme to use when rendering.
  1199. *
  1200. * @return string
  1201. */
  1202. public function getTheme()
  1203. {
  1204. return $this->viewBuilder()->getTheme();
  1205. }
  1206. /**
  1207. * Theme to use when rendering
  1208. *
  1209. * @deprecated 3.4.0 Use setTheme()/getTheme() instead.
  1210. * @param string|null $theme Theme name.
  1211. * @return string|$this
  1212. */
  1213. public function theme($theme = null)
  1214. {
  1215. if ($theme === null) {
  1216. return $this->getTheme();
  1217. }
  1218. return $this->setTheme($theme);
  1219. }
  1220. /**
  1221. * Sets helpers to be used when rendering.
  1222. *
  1223. * @param array $helpers Helpers list.
  1224. * @return $this
  1225. */
  1226. public function setHelpers(array $helpers)
  1227. {
  1228. $this->viewBuilder()->setHelpers($helpers, false);
  1229. return $this;
  1230. }
  1231. /**
  1232. * Gets helpers to be used when rendering.
  1233. *
  1234. * @return array
  1235. */
  1236. public function getHelpers()
  1237. {
  1238. return $this->viewBuilder()->getHelpers();
  1239. }
  1240. /**
  1241. * Helpers to be used in render
  1242. *
  1243. * @deprecated 3.4.0 Use setHelpers()/getHelpers() instead.
  1244. * @param array|null $helpers Helpers list.
  1245. * @return array|$this
  1246. */
  1247. public function helpers($helpers = null)
  1248. {
  1249. if ($helpers === null) {
  1250. return $this->getHelpers();
  1251. }
  1252. return $this->setHelpers((array)$helpers);
  1253. }
  1254. /**
  1255. * Sets email format.
  1256. *
  1257. * @param string $format Formatting string.
  1258. * @return $this
  1259. * @throws \InvalidArgumentException
  1260. */
  1261. public function setEmailFormat($format)
  1262. {
  1263. if (!in_array($format, $this->_emailFormatAvailable)) {
  1264. throw new InvalidArgumentException('Format not available.');
  1265. }
  1266. $this->_emailFormat = $format;
  1267. return $this;
  1268. }
  1269. /**
  1270. * Gets email format.
  1271. *
  1272. * @return string
  1273. */
  1274. public function getEmailFormat()
  1275. {
  1276. return $this->_emailFormat;
  1277. }
  1278. /**
  1279. * Email format
  1280. *
  1281. * @deprecated 3.4.0 Use setEmailFormat()/getEmailFormat() instead.
  1282. * @param string|null $format Formatting string.
  1283. * @return string|$this
  1284. * @throws \InvalidArgumentException
  1285. */
  1286. public function emailFormat($format = null)
  1287. {
  1288. if ($format === null) {
  1289. return $this->getEmailFormat();
  1290. }
  1291. return $this->setEmailFormat($format);
  1292. }
  1293. /**
  1294. * Sets the transport.
  1295. *
  1296. * When setting the transport you can either use the name
  1297. * of a configured transport or supply a constructed transport.
  1298. *
  1299. * @param string|\Cake\Mailer\AbstractTransport $name Either the name of a configured
  1300. * transport, or a transport instance.
  1301. * @return $this
  1302. * @throws \LogicException When the chosen transport lacks a send method.
  1303. * @throws \InvalidArgumentException When $name is neither a string nor an object.
  1304. */
  1305. public function setTransport($name)
  1306. {
  1307. if (is_string($name)) {
  1308. $transport = $this->_constructTransport($name);
  1309. } elseif (is_object($name)) {
  1310. $transport = $name;
  1311. } else {
  1312. throw new InvalidArgumentException(
  1313. sprintf('The value passed for the "$name" argument must be either a string, or an object, %s given.', gettype($name))
  1314. );
  1315. }
  1316. if (!method_exists($transport, 'send')) {
  1317. throw new LogicException(sprintf('The "%s" do not have send method.', get_class($transport)));
  1318. }
  1319. $this->_transport = $transport;
  1320. return $this;
  1321. }
  1322. /**
  1323. * Gets the transport.
  1324. *
  1325. * @return \Cake\Mailer\AbstractTransport
  1326. */
  1327. public function getTransport()
  1328. {
  1329. return $this->_transport;
  1330. }
  1331. /**
  1332. * Get/set the transport.
  1333. *
  1334. * When setting the transport you can either use the name
  1335. * of a configured transport or supply a constructed transport.
  1336. *
  1337. * @deprecated 3.4.0 Use setTransport()/getTransport() instead.
  1338. * @param string|\Cake\Mailer\AbstractTransport|null $name Either the name of a configured
  1339. * transport, or a transport instance.
  1340. * @return \Cake\Mailer\AbstractTransport|$this
  1341. * @throws \LogicException When the chosen transport lacks a send method.
  1342. * @throws \InvalidArgumentException When $name is neither a string nor an object.
  1343. */
  1344. public function transport($name = null)
  1345. {
  1346. if ($name === null) {
  1347. return $this->getTransport();
  1348. }
  1349. return $this->setTransport($name);
  1350. }
  1351. /**
  1352. * Build a transport instance from configuration data.
  1353. *
  1354. * @param string $name The transport configuration name to build.
  1355. * @return \Cake\Mailer\AbstractTransport
  1356. * @throws \InvalidArgumentException When transport configuration is missing or invalid.
  1357. */
  1358. protected function _constructTransport($name)
  1359. {
  1360. if (!isset(static::$_transportConfig[$name])) {
  1361. throw new InvalidArgumentException(sprintf('Transport config "%s" is missing.', $name));
  1362. }
  1363. if (!isset(static::$_transportConfig[$name]['className'])) {
  1364. throw new InvalidArgumentException(
  1365. sprintf('Transport config "%s" is invalid, the required `className` option is missing', $name)
  1366. );
  1367. }
  1368. $config = static::$_transportConfig[$name];
  1369. if (is_object($config['className'])) {
  1370. return $config['className'];
  1371. }
  1372. $className = App::className($config['className'], 'Mailer/Transport', 'Transport');
  1373. if (!$className) {
  1374. $className = App::className($config['className'], 'Network/Email', 'Transport');
  1375. if ($className) {
  1376. trigger_error(
  1377. 'Transports in "Network/Email" are deprecated, use "Mailer/Transport" instead.',
  1378. E_USER_DEPRECATED
  1379. );
  1380. }
  1381. }
  1382. if (!$className) {
  1383. throw new InvalidArgumentException(sprintf('Transport class "%s" not found.', $config['className']));
  1384. }
  1385. if (!method_exists($className, 'send')) {
  1386. throw new InvalidArgumentException(sprintf('The "%s" does not have a send() method.', $className));
  1387. }
  1388. unset($config['className']);
  1389. return new $className($config);
  1390. }
  1391. /**
  1392. * Sets message ID.
  1393. *
  1394. * @param bool|string $message True to generate a new Message-ID, False to ignore (not send in email), String to set as Message-ID.
  1395. * @return $this
  1396. * @throws \InvalidArgumentException
  1397. */
  1398. public function setMessageId($message)
  1399. {
  1400. if (is_bool($message)) {
  1401. $this->_messageId = $message;
  1402. } else {
  1403. if (!preg_match('/^\<.+@.+\>$/', $message)) {
  1404. throw new InvalidArgumentException('Invalid format to Message-ID. The text should be something like "<uuid@server.com>"');
  1405. }
  1406. $this->_messageId = $message;
  1407. }
  1408. return $this;
  1409. }
  1410. /**
  1411. * Gets message ID.
  1412. *
  1413. * @return bool|string
  1414. */
  1415. public function getMessageId()
  1416. {
  1417. return $this->_messageId;
  1418. }
  1419. /**
  1420. * Message-ID
  1421. *
  1422. * @deprecated 3.4.0 Use setMessageId()/getMessageId() instead.
  1423. * @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
  1424. * @return bool|string|$this
  1425. * @throws \InvalidArgumentException
  1426. */
  1427. public function messageId($message = null)
  1428. {
  1429. if ($message === null) {
  1430. return $this->getMessageId();
  1431. }
  1432. return $this->setMessageId($message);
  1433. }
  1434. /**
  1435. * Sets domain.
  1436. *
  1437. * Domain as top level (the part after @).
  1438. *
  1439. * @param string $domain Manually set the domain for CLI mailing.
  1440. * @return $this
  1441. */
  1442. public function setDomain($domain)
  1443. {
  1444. $this->_domain = $domain;
  1445. return $this;
  1446. }
  1447. /**
  1448. * Gets domain.
  1449. *
  1450. * @return string
  1451. */
  1452. public function getDomain()
  1453. {
  1454. return $this->_domain;
  1455. }
  1456. /**
  1457. * Domain as top level (the part after @)
  1458. *
  1459. * @deprecated 3.4.0 Use setDomain()/getDomain() instead.
  1460. * @param string|null $domain Manually set the domain for CLI mailing
  1461. * @return string|$this
  1462. */
  1463. public function domain($domain = null)
  1464. {
  1465. if ($domain === null) {
  1466. return $this->getDomain();
  1467. }
  1468. return $this->setDomain($domain);
  1469. }
  1470. /**
  1471. * Add attachments to the email message
  1472. *
  1473. * Attachments can be defined in a few forms depending on how much control you need:
  1474. *
  1475. * Attach a single file:
  1476. *
  1477. * ```
  1478. * $email->attachments('path/to/file');
  1479. * ```
  1480. *
  1481. * Attach a file with a different filename:
  1482. *
  1483. * ```
  1484. * $email->attachments(['custom_name.txt' => 'path/to/file.txt']);
  1485. * ```
  1486. *
  1487. * Attach a file and specify additional properties:
  1488. *
  1489. * ```
  1490. * $email->attachments(['custom_name.png' => [
  1491. * 'file' => 'path/to/file',
  1492. * 'mimetype' => 'image/png',
  1493. * 'contentId' => 'abc123',
  1494. * 'contentDisposition' => false
  1495. * ]
  1496. * ]);
  1497. * ```
  1498. *
  1499. * Attach a file from string and specify additional properties:
  1500. *
  1501. * ```
  1502. * $email->attachments(['custom_name.png' => [
  1503. * 'data' => file_get_contents('path/to/file'),
  1504. * 'mimetype' => 'image/png'
  1505. * ]
  1506. * ]);
  1507. * ```
  1508. *
  1509. * The `contentId` key allows you to specify an inline attachment. In your email text, you
  1510. * can use `<img src="cid:abc123" />` to display the image inline.
  1511. *
  1512. * The `contentDisposition` key allows you to disable the `Content-Disposition` header, this can improve
  1513. * attachment compatibility with outlook email clients.
  1514. *
  1515. * @param string|array $attachments String with the filename or array with filenames
  1516. * @return $this
  1517. * @throws \InvalidArgumentException
  1518. */
  1519. public function setAttachments($attachments)
  1520. {
  1521. $attach = [];
  1522. foreach ((array)$attachments as $name => $fileInfo) {
  1523. if (!is_array($fileInfo)) {
  1524. $fileInfo = ['file' => $fileInfo];
  1525. }
  1526. if (!isset($fileInfo['file'])) {
  1527. if (!isset($fileInfo['data'])) {
  1528. throw new InvalidArgumentException('No file or data specified.');
  1529. }
  1530. if (is_int($name)) {
  1531. throw new InvalidArgumentException('No filename specified.');
  1532. }
  1533. $fileInfo['data'] = chunk_split(base64_encode($fileInfo['data']), 76, "\r\n");
  1534. } else {
  1535. $fileName = $fileInfo['file'];
  1536. $fileInfo['file'] = realpath($fileInfo['file']);
  1537. if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
  1538. throw new InvalidArgumentException(sprintf('File not found: "%s"', $fileName));
  1539. }
  1540. if (is_int($name)) {
  1541. $name = basename($fileInfo['file']);
  1542. }
  1543. }
  1544. if (!isset($fileInfo['mimetype']) && function_exists('mime_content_type')) {
  1545. $fileInfo['mimetype'] = mime_content_type($fileInfo['file']);
  1546. }
  1547. if (!isset($fileInfo['mimetype'])) {
  1548. $fileInfo['mimetype'] = 'application/octet-stream';
  1549. }
  1550. $attach[$name] = $fileInfo;
  1551. }
  1552. $this->_attachments = $attach;
  1553. return $this;
  1554. }
  1555. /**
  1556. * Gets attachments to the email message.
  1557. *
  1558. * @return array Array of attachments.
  1559. */
  1560. public function getAttachments()
  1561. {
  1562. return $this->_attachments;
  1563. }
  1564. /**
  1565. * Add attachments to the email message
  1566. *
  1567. * Attachments can be defined in a few forms depending on how much control you need:
  1568. *
  1569. * Attach a single file:
  1570. *
  1571. * ```
  1572. * $email->attachments('path/to/file');
  1573. * ```
  1574. *
  1575. * Attach a file with a different filename:
  1576. *
  1577. * ```
  1578. * $email->attachments(['custom_name.txt' => 'path/to/file.txt']);
  1579. * ```
  1580. *
  1581. * Attach a file and specify additional properties:
  1582. *
  1583. * ```
  1584. * $email->attachments(['custom_name.png' => [
  1585. * 'file' => 'path/to/file',
  1586. * 'mimetype' => 'image/png',
  1587. * 'contentId' => 'abc123',
  1588. * 'contentDisposition' => false
  1589. * ]
  1590. * ]);
  1591. * ```
  1592. *
  1593. * Attach a file from string and specify additional properties:
  1594. *
  1595. * ```
  1596. * $email->attachments(['custom_name.png' => [
  1597. * 'data' => file_get_contents('path/to/file'),
  1598. * 'mimetype' => 'image/png'
  1599. * ]
  1600. * ]);
  1601. * ```
  1602. *
  1603. * The `contentId` key allows you to specify an inline attachment. In your email text, you
  1604. * can use `<img src="cid:abc123" />` to display the image inline.
  1605. *
  1606. * The `contentDisposition` key allows you to disable the `Content-Disposition` header, this can improve
  1607. * attachment compatibility with outlook email clients.
  1608. *
  1609. * @deprecated 3.4.0 Use setAttachments()/getAttachments() instead.
  1610. * @param string|array|null $attachments String with the filename or array with filenames
  1611. * @return array|$this Either the array of attachments when getting or $this when setting.
  1612. * @throws \InvalidArgumentException
  1613. */
  1614. public function attachments($attachments = null)
  1615. {
  1616. if ($attachments === null) {
  1617. return $this->getAttachments();
  1618. }
  1619. return $this->setAttachments($attachments);
  1620. }
  1621. /**
  1622. * Add attachments
  1623. *
  1624. * @param string|array $attachments String with the filename or array with filenames
  1625. * @return $this
  1626. * @throws \InvalidArgumentException
  1627. * @see \Cake\Mailer\Email::attachments()
  1628. */
  1629. public function addAttachments($attachments)
  1630. {
  1631. $current = $this->_attachments;
  1632. $this->setAttachments($attachments);
  1633. $this->_attachments = array_merge($current, $this->_attachments);
  1634. return $this;
  1635. }
  1636. /**
  1637. * Get generated message (used by transport classes)
  1638. *
  1639. * @param string|null $type Use MESSAGE_* constants or null to return the full message as array
  1640. * @return string|array String if have type, array if type is null
  1641. */
  1642. public function message($type = null)
  1643. {
  1644. switch ($type) {
  1645. case static::MESSAGE_HTML:
  1646. return $this->_htmlMessage;
  1647. case static::MESSAGE_TEXT:
  1648. return $this->_textMessage;
  1649. }
  1650. return $this->_message;
  1651. }
  1652. /**
  1653. * Sets priority.
  1654. *
  1655. * @param int|null $priority 1 (highest) to 5 (lowest)
  1656. * @return $this
  1657. */
  1658. public function setPriority($priority)
  1659. {
  1660. $this->_priority = $priority;
  1661. return $this;
  1662. }
  1663. /**
  1664. * Gets priority.
  1665. *
  1666. * @return int
  1667. */
  1668. public function getPriority()
  1669. {
  1670. return $this->_priority;
  1671. }
  1672. /**
  1673. * Sets transport configuration.
  1674. *
  1675. * Use this method to define transports to use in delivery profiles.
  1676. * Once defined you cannot edit the configurations, and must use
  1677. * Email::dropTransport() to flush the configuration first.
  1678. *
  1679. * When using an array of configuration data a new transport
  1680. * will be constructed for each message sent. When using a Closure, the
  1681. * closure will be evaluated for each message.
  1682. *
  1683. * The `className` is used to define the class to use for a transport.
  1684. * It can either be a short name, or a fully qualified class name
  1685. *
  1686. * @param string|array $key The configuration name to write. Or
  1687. * an array of multiple transports to set.
  1688. * @param array|\Cake\Mailer\AbstractTransport|null $config Either an array of configuration
  1689. * data, or a transport instance. Null when using key as array.
  1690. * @return void
  1691. * @throws \BadMethodCallException When modifying an existing configuration.
  1692. */
  1693. public static function setConfigTransport($key, $config = null)
  1694. {
  1695. if (is_array($key)) {
  1696. foreach ($key as $name => $settings) {
  1697. static::setConfigTransport($name, $settings);
  1698. }
  1699. return;
  1700. }
  1701. if (isset(static::$_transportConfig[$key])) {
  1702. throw new BadMethodCallException(sprintf('Cannot modify an existing config "%s"', $key));
  1703. }
  1704. if (is_object($config)) {
  1705. $config = ['className' => $config];
  1706. }
  1707. if (isset($config['url'])) {
  1708. $parsed = static::parseDsn($config['url']);
  1709. unset($config['url']);
  1710. $config = $parsed + $config;
  1711. }
  1712. static::$_transportConfig[$key] = $config;
  1713. }
  1714. /**
  1715. * Gets current transport configuration.
  1716. *
  1717. * @param string $key The configuration name to read.
  1718. * @return array|null Transport config.
  1719. */
  1720. public static function getConfigTransport($key)
  1721. {
  1722. return isset(static::$_transportConfig[$key]) ? static::$_transportConfig[$key] : null;
  1723. }
  1724. /**
  1725. * Add or read transport configuration.
  1726. *
  1727. * Use this method to define transports to use in delivery profiles.
  1728. * Once defined you cannot edit the configurations, and must use
  1729. * Email::dropTransport() to flush the configuration first.
  1730. *
  1731. * When using an array of configuration data a new transport
  1732. * will be constructed for each message sent. When using a Closure, the
  1733. * closure will be evaluated for each message.
  1734. *
  1735. * The `className` is used to define the class to use for a transport.
  1736. * It can either be a short name, or a fully qualified classname
  1737. *
  1738. * @deprecated 3.4.0 Use setConfigTransport()/getConfigTransport() instead.
  1739. * @param string|array $key The configuration name to read/write. Or
  1740. * an array of multiple transports to set.
  1741. * @param array|\Cake\Mailer\AbstractTransport|null $config Either an array of configuration
  1742. * data, or a transport instance.
  1743. * @return array|null Either null when setting or an array of data when reading.
  1744. * @throws \BadMethodCallException When modifying an existing configuration.
  1745. */
  1746. public static function configTransport($key, $config = null)
  1747. {
  1748. if ($config === null && is_string($key)) {
  1749. return static::getConfigTransport($key);
  1750. }
  1751. if ($config === null && is_array($key)) {
  1752. static::setConfigTransport($key);
  1753. return null;
  1754. }
  1755. static::setConfigTransport($key, $config);
  1756. }
  1757. /**
  1758. * Returns an array containing the named transport configurations
  1759. *
  1760. * @return array Array of configurations.
  1761. */
  1762. public static function configuredTransport()
  1763. {
  1764. return array_keys(static::$_transportConfig);
  1765. }
  1766. /**
  1767. * Delete transport configuration.
  1768. *
  1769. * @param string $key The transport name to remove.
  1770. * @return void
  1771. */
  1772. public static function dropTransport($key)
  1773. {
  1774. unset(static::$_transportConfig[$key]);
  1775. }
  1776. /**
  1777. * Sets the configuration profile to use for this instance.
  1778. *
  1779. * @param string|array $config String with configuration name, or
  1780. * an array with config.
  1781. * @return $this
  1782. */
  1783. public function setProfile($config)
  1784. {
  1785. if (!is_array($config)) {
  1786. $config = (string)$config;
  1787. }
  1788. $this->_applyConfig($config);
  1789. return $this;
  1790. }
  1791. /**
  1792. * Gets the configuration profile to use for this instance.
  1793. *
  1794. * @return string|array
  1795. */
  1796. public function getProfile()
  1797. {
  1798. return $this->_profile;
  1799. }
  1800. /**
  1801. * Get/Set the configuration profile to use for this instance.
  1802. *
  1803. * @deprecated 3.4.0 Use setProfile()/getProfile() instead.
  1804. * @param null|string|array $config String with configuration name, or
  1805. * an array with config or null to return current config.
  1806. * @return string|array|$this
  1807. */
  1808. public function profile($config = null)
  1809. {
  1810. if ($config === null) {
  1811. return $this->getProfile();
  1812. }
  1813. return $this->setProfile($config);
  1814. }
  1815. /**
  1816. * Send an email using the specified content, template and layout
  1817. *
  1818. * @param string|array|null $content String with message or array with messages
  1819. * @return array
  1820. * @throws \BadMethodCallException
  1821. */
  1822. public function send($content = null)
  1823. {
  1824. if (empty($this->_from)) {
  1825. throw new BadMethodCallException('From is not specified.');
  1826. }
  1827. if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
  1828. throw new BadMethodCallException('You need specify one destination on to, cc or bcc.');
  1829. }
  1830. if (is_array($content)) {
  1831. $content = implode("\n", $content) . "\n";
  1832. }
  1833. $this->_message = $this->_render($this->_wrap($content));
  1834. $transport = $this->getTransport();
  1835. if (!$transport) {
  1836. $msg = 'Cannot send email, transport was not defined. Did you call transport() or define ' .
  1837. ' a transport in the set profile?';
  1838. throw new BadMethodCallException($msg);
  1839. }
  1840. $contents = $transport->send($this);
  1841. $this->_logDelivery($contents);
  1842. return $contents;
  1843. }
  1844. /**
  1845. * Log the email message delivery.
  1846. *
  1847. * @param array $contents The content with 'headers' and 'message' keys.
  1848. * @return void
  1849. */
  1850. protected function _logDelivery($contents)
  1851. {
  1852. if (empty($this->_profile['log'])) {
  1853. return;
  1854. }
  1855. $config = [
  1856. 'level' => 'debug',
  1857. 'scope' => 'email'
  1858. ];
  1859. if ($this->_profile['log'] !== true) {
  1860. if (!is_array($this->_profile['log'])) {
  1861. $this->_profile['log'] = ['level' => $this->_profile['log']];
  1862. }
  1863. $config = $this->_profile['log'] + $config;
  1864. }
  1865. Log::write(
  1866. $config['level'],
  1867. PHP_EOL . $contents['headers'] . PHP_EOL . PHP_EOL . $contents['message'],
  1868. $config['scope']
  1869. );
  1870. }
  1871. /**
  1872. * Static method to fast create an instance of \Cake\Mailer\Email
  1873. *
  1874. * @param string|array|null $to Address to send (see Cake\Mailer\Email::to()). If null, will try to use 'to' from transport config
  1875. * @param string|null $subject String of subject or null to use 'subject' from transport config
  1876. * @param string|array|null $message String with message or array with variables to be used in render
  1877. * @param string|array $transportConfig String to use config from EmailConfig or array with configs
  1878. * @param bool $send Send the email or just return the instance pre-configured
  1879. * @return static Instance of Cake\Mailer\Email
  1880. * @throws \InvalidArgumentException
  1881. */
  1882. public static function deliver($to = null, $subject = null, $message = null, $transportConfig = 'default', $send = true)
  1883. {
  1884. $class = __CLASS__;
  1885. if (is_array($transportConfig) && !isset($transportConfig['transport'])) {
  1886. $transportConfig['transport'] = 'default';
  1887. }
  1888. /* @var \Cake\Mailer\Email $instance */
  1889. $instance = new $class($transportConfig);
  1890. if ($to !== null) {
  1891. $instance->setTo($to);
  1892. }
  1893. if ($subject !== null) {
  1894. $instance->setSubject($subject);
  1895. }
  1896. if (is_array($message)) {
  1897. $instance->setViewVars($message);
  1898. $message = null;
  1899. } elseif ($message === null && array_key_exists('message', $config = $instance->getProfile())) {
  1900. $message = $config['message'];
  1901. }
  1902. if ($send === true) {
  1903. $instance->send($message);
  1904. }
  1905. return $instance;
  1906. }
  1907. /**
  1908. * Apply the config to an instance
  1909. *
  1910. * @param string|array $config Configuration options.
  1911. * @return void
  1912. * @throws \InvalidArgumentException When using a configuration that doesn't exist.
  1913. */
  1914. protected function _applyConfig($config)
  1915. {
  1916. if (is_string($config)) {
  1917. $name = $config;
  1918. $config = static::getConfig($name);
  1919. if (empty($config)) {
  1920. throw new InvalidArgumentException(sprintf('Unknown email configuration "%s".', $name));
  1921. }
  1922. unset($name);
  1923. }
  1924. $this->_profile = array_merge($this->_profile, $config);
  1925. $simpleMethods = [
  1926. 'from', 'sender', 'to', 'replyTo', 'readReceipt', 'returnPath',
  1927. 'cc', 'bcc', 'messageId', 'domain', 'subject', 'attachments',
  1928. 'transport', 'emailFormat', 'emailPattern', 'charset', 'headerCharset'
  1929. ];
  1930. foreach ($simpleMethods as $method) {
  1931. if (isset($config[$method])) {
  1932. $this->$method($config[$method]);
  1933. }
  1934. }
  1935. if (empty($this->headerCharset)) {
  1936. $this->headerCharset = $this->charset;
  1937. }
  1938. if (isset($config['headers'])) {
  1939. $this->setHeaders($config['headers']);
  1940. }
  1941. $viewBuilderMethods = [
  1942. 'template', 'layout', 'theme'
  1943. ];
  1944. foreach ($viewBuilderMethods as $method) {
  1945. if (array_key_exists($method, $config)) {
  1946. $this->viewBuilder()->$method($config[$method]);
  1947. }
  1948. }
  1949. if (array_key_exists('helpers', $config)) {
  1950. $this->viewBuilder()->setHelpers($config['helpers'], false);
  1951. }
  1952. if (array_key_exists('viewRender', $config)) {
  1953. $this->viewBuilder()->setClassName($config['viewRender']);
  1954. }
  1955. if (array_key_exists('viewVars', $config)) {
  1956. $this->set($config['viewVars']);
  1957. }
  1958. }
  1959. /**
  1960. * Reset all the internal variables to be able to send out a new email.
  1961. *
  1962. * @return $this
  1963. */
  1964. public function reset()
  1965. {
  1966. $this->_to = [];
  1967. $this->_from = [];
  1968. $this->_sender = [];
  1969. $this->_replyTo = [];
  1970. $this->_readReceipt = [];
  1971. $this->_returnPath = [];
  1972. $this->_cc = [];
  1973. $this->_bcc = [];
  1974. $this->_messageId = true;
  1975. $this->_subject = '';
  1976. $this->_headers = [];
  1977. $this->_textMessage = '';
  1978. $this->_htmlMessage = '';
  1979. $this->_message = '';
  1980. $this->_emailFormat = 'text';
  1981. $this->_transport = null;
  1982. $this->_priority = null;
  1983. $this->charset = 'utf-8';
  1984. $this->headerCharset = null;
  1985. $this->_attachments = [];
  1986. $this->_profile = [];
  1987. $this->_emailPattern = self::EMAIL_PATTERN;
  1988. $this->viewBuilder()->setLayout('default');
  1989. $this->viewBuilder()->setTemplate('');
  1990. $this->viewBuilder()->setClassName('Cake\View\View');
  1991. $this->viewVars = [];
  1992. $this->viewBuilder()->setTheme(false);
  1993. $this->viewBuilder()->setHelpers(['Html'], false);
  1994. return $this;
  1995. }
  1996. /**
  1997. * Encode the specified string using the current charset
  1998. *
  1999. * @param string $text String to encode
  2000. * @return string Encoded string
  2001. */
  2002. protected function _encode($text)
  2003. {
  2004. $restore = mb_internal_encoding();
  2005. mb_internal_encoding($this->_appCharset);
  2006. if (empty($this->headerCharset)) {
  2007. $this->headerCharset = $this->charset;
  2008. }
  2009. $return = mb_encode_mimeheader($text, $this->headerCharset, 'B');
  2010. mb_internal_encoding($restore);
  2011. return $return;
  2012. }
  2013. /**
  2014. * Decode the specified string
  2015. *
  2016. * @param string $text String to decode
  2017. * @return string Decoded string
  2018. */
  2019. protected function _decode($text)
  2020. {
  2021. $restore = mb_internal_encoding();
  2022. mb_internal_encoding($this->_appCharset);
  2023. $return = mb_decode_mimeheader($text);
  2024. mb_internal_encoding($restore);
  2025. return $return;
  2026. }
  2027. /**
  2028. * Translates a string for one charset to another if the App.encoding value
  2029. * differs and the mb_convert_encoding function exists
  2030. *
  2031. * @param string $text The text to be converted
  2032. * @param string $charset the target encoding
  2033. * @return string
  2034. */
  2035. protected function _encodeString($text, $charset)
  2036. {
  2037. if ($this->_appCharset === $charset) {
  2038. return $text;
  2039. }
  2040. return mb_convert_encoding($text, $charset, $this->_appCharset);
  2041. }
  2042. /**
  2043. * Wrap the message to follow the RFC 2822 - 2.1.1
  2044. *
  2045. * @param string $message Message to wrap
  2046. * @param int $wrapLength The line length
  2047. * @return array Wrapped message
  2048. */
  2049. protected function _wrap($message, $wrapLength = Email::LINE_LENGTH_MUST)
  2050. {
  2051. if (strlen($message) === 0) {
  2052. return [''];
  2053. }
  2054. $message = str_replace(["\r\n", "\r"], "\n", $message);
  2055. $lines = explode("\n", $message);
  2056. $formatted = [];
  2057. $cut = ($wrapLength == Email::LINE_LENGTH_MUST);
  2058. foreach ($lines as $line) {
  2059. if (empty($line) && $line !== '0') {
  2060. $formatted[] = '';
  2061. continue;
  2062. }
  2063. if (strlen($line) < $wrapLength) {
  2064. $formatted[] = $line;
  2065. continue;
  2066. }
  2067. if (!preg_match('/<[a-z]+.*>/i', $line)) {
  2068. $formatted = array_merge(
  2069. $formatted,
  2070. explode("\n", wordwrap($line, $wrapLength, "\n", $cut))
  2071. );
  2072. continue;
  2073. }
  2074. $tagOpen = false;
  2075. $tmpLine = $tag = '';
  2076. $tmpLineLength = 0;
  2077. for ($i = 0, $count = strlen($line); $i < $count; $i++) {
  2078. $char = $line[$i];
  2079. if ($tagOpen) {
  2080. $tag .= $char;
  2081. if ($char === '>') {
  2082. $tagLength = strlen($tag);
  2083. if ($tagLength + $tmpLineLength < $wrapLength) {
  2084. $tmpLine .= $tag;
  2085. $tmpLineLength += $tagLength;
  2086. } else {
  2087. if ($tmpLineLength > 0) {
  2088. $formatted = array_merge(
  2089. $formatted,
  2090. explode("\n", wordwrap(trim($tmpLine), $wrapLength, "\n", $cut))
  2091. );
  2092. $tmpLine = '';
  2093. $tmpLineLength = 0;
  2094. }
  2095. if ($tagLength > $wrapLength) {
  2096. $formatted[] = $tag;
  2097. } else {
  2098. $tmpLine = $tag;
  2099. $tmpLineLength = $tagLength;
  2100. }
  2101. }
  2102. $tag = '';
  2103. $tagOpen = false;
  2104. }
  2105. continue;
  2106. }
  2107. if ($char === '<') {
  2108. $tagOpen = true;
  2109. $tag = '<';
  2110. continue;
  2111. }
  2112. if ($char === ' ' && $tmpLineLength >= $wrapLength) {
  2113. $formatted[] = $tmpLine;
  2114. $tmpLineLength = 0;
  2115. continue;
  2116. }
  2117. $tmpLine .= $char;
  2118. $tmpLineLength++;
  2119. if ($tmpLineLength === $wrapLength) {
  2120. $nextChar = $line[$i + 1];
  2121. if ($nextChar === ' ' || $nextChar === '<') {
  2122. $formatted[] = trim($tmpLine);
  2123. $tmpLine = '';
  2124. $tmpLineLength = 0;
  2125. if ($nextChar === ' ') {
  2126. $i++;
  2127. }
  2128. } else {
  2129. $lastSpace = strrpos($tmpLine, ' ');
  2130. if ($lastSpace === false) {
  2131. continue;
  2132. }
  2133. $formatted[] = trim(substr($tmpLine, 0, $lastSpace));
  2134. $tmpLine = substr($tmpLine, $lastSpace + 1);
  2135. $tmpLineLength = strlen($tmpLine);
  2136. }
  2137. }
  2138. }
  2139. if (!empty($tmpLine)) {
  2140. $formatted[] = $tmpLine;
  2141. }
  2142. }
  2143. $formatted[] = '';
  2144. return $formatted;
  2145. }
  2146. /**
  2147. * Create unique boundary identifier
  2148. *
  2149. * @return void
  2150. */
  2151. protected function _createBoundary()
  2152. {
  2153. if ($this->_attachments || $this->_emailFormat === 'both') {
  2154. $this->_boundary = md5(Security::randomBytes(16));
  2155. }
  2156. }
  2157. /**
  2158. * Attach non-embedded files by adding file contents inside boundaries.
  2159. *
  2160. * @param string|null $boundary Boundary to use. If null, will default to $this->_boundary
  2161. * @return array An array of lines to add to the message
  2162. */
  2163. protected function _attachFiles($boundary = null)
  2164. {
  2165. if ($boundary === null) {
  2166. $boundary = $this->_boundary;
  2167. }
  2168. $msg = [];
  2169. foreach ($this->_attachments as $filename => $fileInfo) {
  2170. if (!empty($fileInfo['contentId'])) {
  2171. continue;
  2172. }
  2173. $data = isset($fileInfo['data']) ? $fileInfo['data'] : $this->_readFile($fileInfo['file']);
  2174. $hasDisposition = (
  2175. !isset($fileInfo['contentDisposition']) ||
  2176. $fileInfo['contentDisposition']
  2177. );
  2178. $part = new FormDataPart(false, $data, false);
  2179. if ($hasDisposition) {
  2180. $part->disposition('attachment');
  2181. $part->filename($filename);
  2182. }
  2183. $part->transferEncoding('base64');
  2184. $part->type($fileInfo['mimetype']);
  2185. $msg[] = '--' . $boundary;
  2186. $msg[] = (string)$part;
  2187. $msg[] = '';
  2188. }
  2189. return $msg;
  2190. }
  2191. /**
  2192. * Read the file contents and return a base64 version of the file contents.
  2193. *
  2194. * @param string $path The absolute path to the file to read.
  2195. * @return string File contents in base64 encoding
  2196. */
  2197. protected function _readFile($path)
  2198. {
  2199. $File = new File($path);
  2200. return chunk_split(base64_encode($File->read()));
  2201. }
  2202. /**
  2203. * Attach inline/embedded files to the message.
  2204. *
  2205. * @param string|null $boundary Boundary to use. If null, will default to $this->_boundary
  2206. * @return array An array of lines to add to the message
  2207. */
  2208. protected function _attachInlineFiles($boundary = null)
  2209. {
  2210. if ($boundary === null) {
  2211. $boundary = $this->_boundary;
  2212. }
  2213. $msg = [];
  2214. foreach ($this->_attachments as $filename => $fileInfo) {
  2215. if (empty($fileInfo['contentId'])) {
  2216. continue;
  2217. }
  2218. $data = isset($fileInfo['data']) ? $fileInfo['data'] : $this->_readFile($fileInfo['file']);
  2219. $msg[] = '--' . $boundary;
  2220. $part = new FormDataPart(false, $data, 'inline');
  2221. $part->type($fileInfo['mimetype']);
  2222. $part->transferEncoding('base64');
  2223. $part->contentId($fileInfo['contentId']);
  2224. $part->filename($filename);
  2225. $msg[] = (string)$part;
  2226. $msg[] = '';
  2227. }
  2228. return $msg;
  2229. }
  2230. /**
  2231. * Render the body of the email.
  2232. *
  2233. * @param array $content Content to render
  2234. * @return array Email body ready to be sent
  2235. */
  2236. protected function _render($content)
  2237. {
  2238. $this->_textMessage = $this->_htmlMessage = '';
  2239. $content = implode("\n", $content);
  2240. $rendered = $this->_renderTemplates($content);
  2241. $this->_createBoundary();
  2242. $msg = [];
  2243. $contentIds = array_filter((array)Hash::extract($this->_attachments, '{s}.contentId'));
  2244. $hasInlineAttachments = count($contentIds) > 0;
  2245. $hasAttachments = !empty($this->_attachments);
  2246. $hasMultipleTypes = count($rendered) > 1;
  2247. $multiPart = ($hasAttachments || $hasMultipleTypes);
  2248. $boundary = $relBoundary = $textBoundary = $this->_boundary;
  2249. if ($hasInlineAttachments) {
  2250. $msg[] = '--' . $boundary;
  2251. $msg[] = 'Content-Type: multipart/related; boundary="rel-' . $boundary . '"';
  2252. $msg[] = '';
  2253. $relBoundary = $textBoundary = 'rel-' . $boundary;
  2254. }
  2255. if ($hasMultipleTypes && $hasAttachments) {
  2256. $msg[] = '--' . $relBoundary;
  2257. $msg[] = 'Content-Type: multipart/alternative; boundary="alt-' . $boundary . '"';
  2258. $msg[] = '';
  2259. $textBoundary = 'alt-' . $boundary;
  2260. }
  2261. if (isset($rendered['text'])) {
  2262. if ($multiPart) {
  2263. $msg[] = '--' . $textBoundary;
  2264. $msg[] = 'Content-Type: text/plain; charset=' . $this->_getContentTypeCharset();
  2265. $msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
  2266. $msg[] = '';
  2267. }
  2268. $this->_textMessage = $rendered['text'];
  2269. $content = explode("\n", $this->_textMessage);
  2270. $msg = array_merge($msg, $content);
  2271. $msg[] = '';
  2272. }
  2273. if (isset($rendered['html'])) {
  2274. if ($multiPart) {
  2275. $msg[] = '--' . $textBoundary;
  2276. $msg[] = 'Content-Type: text/html; charset=' . $this->_getContentTypeCharset();
  2277. $msg[] = 'Content-Transfer-Encoding: ' . $this->_getContentTransferEncoding();
  2278. $msg[] = '';
  2279. }
  2280. $this->_htmlMessage = $rendered['html'];
  2281. $content = explode("\n", $this->_htmlMessage);
  2282. $msg = array_merge($msg, $content);
  2283. $msg[] = '';
  2284. }
  2285. if ($textBoundary !== $relBoundary) {
  2286. $msg[] = '--' . $textBoundary . '--';
  2287. $msg[] = '';
  2288. }
  2289. if ($hasInlineAttachments) {
  2290. $attachments = $this->_attachInlineFiles($relBoundary);
  2291. $msg = array_merge($msg, $attachments);
  2292. $msg[] = '';
  2293. $msg[] = '--' . $relBoundary . '--';
  2294. $msg[] = '';
  2295. }
  2296. if ($hasAttachments) {
  2297. $attachments = $this->_attachFiles($boundary);
  2298. $msg = array_merge($msg, $attachments);
  2299. }
  2300. if ($hasAttachments || $hasMultipleTypes) {
  2301. $msg[] = '';
  2302. $msg[] = '--' . $boundary . '--';
  2303. $msg[] = '';
  2304. }
  2305. return $msg;
  2306. }
  2307. /**
  2308. * Gets the text body types that are in this email message
  2309. *
  2310. * @return array Array of types. Valid types are 'text' and 'html'
  2311. */
  2312. protected function _getTypes()
  2313. {
  2314. $types = [$this->_emailFormat];
  2315. if ($this->_emailFormat === 'both') {
  2316. $types = ['html', 'text'];
  2317. }
  2318. return $types;
  2319. }
  2320. /**
  2321. * Build and set all the view properties needed to render the templated emails.
  2322. * If there is no template set, the $content will be returned in a hash
  2323. * of the text content types for the email.
  2324. *
  2325. * @param string $content The content passed in from send() in most cases.
  2326. * @return array The rendered content with html and text keys.
  2327. */
  2328. protected function _renderTemplates($content)
  2329. {
  2330. $types = $this->_getTypes();
  2331. $rendered = [];
  2332. $template = $this->viewBuilder()->getTemplate();
  2333. if (empty($template)) {
  2334. foreach ($types as $type) {
  2335. $rendered[$type] = $this->_encodeString($content, $this->charset);
  2336. }
  2337. return $rendered;
  2338. }
  2339. $View = $this->createView();
  2340. list($templatePlugin) = pluginSplit($View->template());
  2341. list($layoutPlugin) = pluginSplit($View->layout());
  2342. if ($templatePlugin) {
  2343. $View->plugin = $templatePlugin;
  2344. } elseif ($layoutPlugin) {
  2345. $View->plugin = $layoutPlugin;
  2346. }
  2347. if ($View->get('content') === null) {
  2348. $View->set('content', $content);
  2349. }
  2350. foreach ($types as $type) {
  2351. $View->hasRendered = false;
  2352. $View->templatePath('Email' . DIRECTORY_SEPARATOR . $type);
  2353. $View->layoutPath('Email' . DIRECTORY_SEPARATOR . $type);
  2354. $render = $View->render();
  2355. $render = str_replace(["\r\n", "\r"], "\n", $render);
  2356. $rendered[$type] = $this->_encodeString($render, $this->charset);
  2357. }
  2358. foreach ($rendered as $type => $content) {
  2359. $rendered[$type] = $this->_wrap($content);
  2360. $rendered[$type] = implode("\n", $rendered[$type]);
  2361. $rendered[$type] = rtrim($rendered[$type], "\n");
  2362. }
  2363. return $rendered;
  2364. }
  2365. /**
  2366. * Return the Content-Transfer Encoding value based on the set charset
  2367. *
  2368. * @return string
  2369. */
  2370. protected function _getContentTransferEncoding()
  2371. {
  2372. $charset = strtoupper($this->charset);
  2373. if (in_array($charset, $this->_charset8bit)) {
  2374. return '8bit';
  2375. }
  2376. return '7bit';
  2377. }
  2378. /**
  2379. * Return charset value for Content-Type.
  2380. *
  2381. * Checks fallback/compatibility types which include workarounds
  2382. * for legacy japanese character sets.
  2383. *
  2384. * @return string
  2385. */
  2386. protected function _getContentTypeCharset()
  2387. {
  2388. $charset = strtoupper($this->charset);
  2389. if (array_key_exists($charset, $this->_contentTypeCharset)) {
  2390. return strtoupper($this->_contentTypeCharset[$charset]);
  2391. }
  2392. return strtoupper($this->charset);
  2393. }
  2394. /**
  2395. * Serializes the email object to a value that can be natively serialized and re-used
  2396. * to clone this email instance.
  2397. *
  2398. * It has certain limitations for viewVars that are good to know:
  2399. *
  2400. * - ORM\Query executed and stored as resultset
  2401. * - SimpleXmlElements stored as associative array
  2402. * - Exceptions stored as strings
  2403. * - Resources, \Closure and \PDO are not supported.
  2404. *
  2405. * @return array Serializable array of configuration properties.
  2406. * @throws \Exception When a view var object can not be properly serialized.
  2407. */
  2408. public function jsonSerialize()
  2409. {
  2410. $properties = [
  2411. '_to', '_from', '_sender', '_replyTo', '_cc', '_bcc', '_subject',
  2412. '_returnPath', '_readReceipt', '_emailFormat', '_emailPattern', '_domain',
  2413. '_attachments', '_messageId', '_headers', '_appCharset', 'viewVars', 'charset', 'headerCharset'
  2414. ];
  2415. $array = ['viewConfig' => $this->viewBuilder()->jsonSerialize()];
  2416. foreach ($properties as $property) {
  2417. $array[$property] = $this->{$property};
  2418. }
  2419. array_walk($array['_attachments'], function (&$item, $key) {
  2420. if (!empty($item['file'])) {
  2421. $item['data'] = $this->_readFile($item['file']);
  2422. unset($item['file']);
  2423. }
  2424. });
  2425. array_walk_recursive($array['viewVars'], [$this, '_checkViewVars']);
  2426. return array_filter($array, function ($i) {
  2427. return !is_array($i) && strlen($i) || !empty($i);
  2428. });
  2429. }
  2430. /**
  2431. * Iterates through hash to clean up and normalize.
  2432. *
  2433. * @param mixed $item Reference to the view var value.
  2434. * @param string $key View var key.
  2435. * @return void
  2436. */
  2437. protected function _checkViewVars(&$item, $key)
  2438. {
  2439. if ($item instanceof Exception) {
  2440. $item = (string)$item;
  2441. }
  2442. if (is_resource($item) ||
  2443. $item instanceof Closure ||
  2444. $item instanceof PDO
  2445. ) {
  2446. throw new RuntimeException(sprintf(
  2447. 'Failed serializing the `%s` %s in the `%s` view var',
  2448. is_resource($item) ? get_resource_type($item) : get_class($item),
  2449. is_resource($item) ? 'resource' : 'object',
  2450. $key
  2451. ));
  2452. }
  2453. }
  2454. /**
  2455. * Configures an email instance object from serialized config.
  2456. *
  2457. * @param array $config Email configuration array.
  2458. * @return $this Configured email instance.
  2459. */
  2460. public function createFromArray($config)
  2461. {
  2462. if (isset($config['viewConfig'])) {
  2463. $this->viewBuilder()->createFromArray($config['viewConfig']);
  2464. unset($config['viewConfig']);
  2465. }
  2466. foreach ($config as $property => $value) {
  2467. $this->{$property} = $value;
  2468. }
  2469. return $this;
  2470. }
  2471. /**
  2472. * Serializes the Email object.
  2473. *
  2474. * @return string
  2475. */
  2476. public function serialize()
  2477. {
  2478. $array = $this->jsonSerialize();
  2479. array_walk_recursive($array, function (&$item, $key) {
  2480. if ($item instanceof SimpleXmlElement) {
  2481. $item = json_decode(json_encode((array)$item), true);
  2482. }
  2483. });
  2484. return serialize($array);
  2485. }
  2486. /**
  2487. * Unserializes the Email object.
  2488. *
  2489. * @param string $data Serialized string.
  2490. * @return static Configured email instance.
  2491. */
  2492. public function unserialize($data)
  2493. {
  2494. return $this->createFromArray(unserialize($data));
  2495. }
  2496. }