Email.php 81 KB

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