EmailTest.php 109 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130
  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
  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\Test\TestCase\Mailer;
  16. use Cake\Core\Configure;
  17. use Cake\Core\Plugin;
  18. use Cake\Log\Log;
  19. use Cake\Mailer\Email;
  20. use Cake\Mailer\Transport\DebugTransport;
  21. use Cake\TestSuite\TestCase;
  22. use Exception;
  23. use SimpleXmlElement;
  24. /**
  25. * Help to test Email
  26. */
  27. class TestEmail extends Email
  28. {
  29. /**
  30. * Wrap to protected method
  31. *
  32. * @return array
  33. */
  34. public function formatAddress($address)
  35. {
  36. return parent::_formatAddress($address);
  37. }
  38. /**
  39. * Wrap to protected method
  40. *
  41. * @return array
  42. */
  43. public function wrap($text, $length = Email::LINE_LENGTH_MUST)
  44. {
  45. return parent::_wrap($text, $length);
  46. }
  47. /**
  48. * Get the boundary attribute
  49. *
  50. * @return string
  51. */
  52. public function getBoundary()
  53. {
  54. return $this->_boundary;
  55. }
  56. /**
  57. * Encode to protected method
  58. *
  59. * @return string
  60. */
  61. public function encode($text)
  62. {
  63. return $this->_encode($text);
  64. }
  65. /**
  66. * Decode to protected method
  67. *
  68. * @return string
  69. */
  70. public function decode($text)
  71. {
  72. return $this->_decode($text);
  73. }
  74. /**
  75. * Render to protected method
  76. *
  77. * @return array
  78. */
  79. public function render($content)
  80. {
  81. return $this->_render($content);
  82. }
  83. /**
  84. * GetContentTransferEncoding to protected method
  85. *
  86. * @return string
  87. */
  88. public function getContentTransferEncoding()
  89. {
  90. return $this->_getContentTransferEncoding();
  91. }
  92. }
  93. /**
  94. * EmailTest class
  95. */
  96. class EmailTest extends TestCase
  97. {
  98. public $fixtures = ['core.Users'];
  99. /**
  100. * @var \Cake\Mailer\Email
  101. */
  102. protected $Email;
  103. /**
  104. * @var array
  105. */
  106. protected $transports = [];
  107. /**
  108. * setUp
  109. *
  110. * @return void
  111. */
  112. public function setUp()
  113. {
  114. parent::setUp();
  115. $this->Email = new TestEmail();
  116. $this->transports = [
  117. 'debug' => [
  118. 'className' => 'Debug',
  119. ],
  120. 'badClassName' => [
  121. 'className' => 'TestFalse',
  122. ],
  123. ];
  124. $this->deprecated(function () {
  125. Email::setConfigTransport($this->transports);
  126. });
  127. }
  128. /**
  129. * tearDown method
  130. *
  131. * @return void
  132. */
  133. public function tearDown()
  134. {
  135. parent::tearDown();
  136. Log::drop('email');
  137. Email::drop('test');
  138. $this->deprecated(function () {
  139. Email::dropTransport('debug');
  140. Email::dropTransport('badClassName');
  141. Email::dropTransport('test_smtp');
  142. });
  143. }
  144. /**
  145. * testFrom method
  146. *
  147. * @return void
  148. */
  149. public function testFrom()
  150. {
  151. $this->assertSame([], $this->Email->getFrom());
  152. $this->Email->setFrom('cake@cakephp.org');
  153. $expected = ['cake@cakephp.org' => 'cake@cakephp.org'];
  154. $this->assertSame($expected, $this->Email->getFrom());
  155. $this->Email->setFrom(['cake@cakephp.org']);
  156. $this->assertSame($expected, $this->Email->getFrom());
  157. $this->Email->setFrom('cake@cakephp.org', 'CakePHP');
  158. $expected = ['cake@cakephp.org' => 'CakePHP'];
  159. $this->assertSame($expected, $this->Email->getFrom());
  160. $result = $this->Email->setFrom(['cake@cakephp.org' => 'CakePHP']);
  161. $this->assertSame($expected, $this->Email->getFrom());
  162. $this->assertSame($this->Email, $result);
  163. $this->expectException(\InvalidArgumentException::class);
  164. $result = $this->Email->setFrom(['cake@cakephp.org' => 'CakePHP', 'fail@cakephp.org' => 'From can only be one address']);
  165. }
  166. /**
  167. * Test that from addresses using colons work.
  168. *
  169. * @return void
  170. */
  171. public function testFromWithColonsAndQuotes()
  172. {
  173. $address = [
  174. 'info@example.com' => '70:20:00 " Forum',
  175. ];
  176. $this->Email->setFrom($address);
  177. $this->assertEquals($address, $this->Email->getFrom());
  178. $this->Email->setTo('info@example.com')
  179. ->setSubject('Test email')
  180. ->setTransport('debug');
  181. $result = $this->Email->send();
  182. $this->assertContains('From: "70:20:00 \" Forum" <info@example.com>', $result['headers']);
  183. }
  184. /**
  185. * testSender method
  186. *
  187. * @return void
  188. */
  189. public function testSender()
  190. {
  191. $this->Email->reset();
  192. $this->assertSame([], $this->Email->getSender());
  193. $this->Email->setSender('cake@cakephp.org', 'Name');
  194. $expected = ['cake@cakephp.org' => 'Name'];
  195. $this->assertSame($expected, $this->Email->getSender());
  196. $headers = $this->Email->getHeaders(['from' => true, 'sender' => true]);
  197. $this->assertFalse($headers['From']);
  198. $this->assertSame('Name <cake@cakephp.org>', $headers['Sender']);
  199. $this->Email->setFrom('cake@cakephp.org', 'CakePHP');
  200. $headers = $this->Email->getHeaders(['from' => true, 'sender' => true]);
  201. $this->assertSame('CakePHP <cake@cakephp.org>', $headers['From']);
  202. $this->assertSame('', $headers['Sender']);
  203. }
  204. /**
  205. * testTo method
  206. *
  207. * @return void
  208. */
  209. public function testTo()
  210. {
  211. $this->assertSame([], $this->Email->getTo());
  212. $result = $this->Email->setTo('cake@cakephp.org');
  213. $expected = ['cake@cakephp.org' => 'cake@cakephp.org'];
  214. $this->assertSame($expected, $this->Email->getTo());
  215. $this->assertSame($this->Email, $result);
  216. $this->Email->setTo('cake@cakephp.org', 'CakePHP');
  217. $expected = ['cake@cakephp.org' => 'CakePHP'];
  218. $this->assertSame($expected, $this->Email->getTo());
  219. $list = [
  220. 'root@localhost' => 'root',
  221. 'bjørn@hammeröath.com' => 'Bjorn',
  222. 'cake.php@cakephp.org' => 'Cake PHP',
  223. 'cake-php@googlegroups.com' => 'Cake Groups',
  224. 'root@cakephp.org',
  225. ];
  226. $this->Email->setTo($list);
  227. $expected = [
  228. 'root@localhost' => 'root',
  229. 'bjørn@hammeröath.com' => 'Bjorn',
  230. 'cake.php@cakephp.org' => 'Cake PHP',
  231. 'cake-php@googlegroups.com' => 'Cake Groups',
  232. 'root@cakephp.org' => 'root@cakephp.org',
  233. ];
  234. $this->assertSame($expected, $this->Email->getTo());
  235. $this->Email->addTo('jrbasso@cakephp.org');
  236. $this->Email->addTo('mark_story@cakephp.org', 'Mark Story');
  237. $this->Email->addTo('foobar@ætdcadsl.dk');
  238. $result = $this->Email->addTo(['phpnut@cakephp.org' => 'PhpNut', 'jose_zap@cakephp.org']);
  239. $expected = [
  240. 'root@localhost' => 'root',
  241. 'bjørn@hammeröath.com' => 'Bjorn',
  242. 'cake.php@cakephp.org' => 'Cake PHP',
  243. 'cake-php@googlegroups.com' => 'Cake Groups',
  244. 'root@cakephp.org' => 'root@cakephp.org',
  245. 'jrbasso@cakephp.org' => 'jrbasso@cakephp.org',
  246. 'mark_story@cakephp.org' => 'Mark Story',
  247. 'foobar@ætdcadsl.dk' => 'foobar@ætdcadsl.dk',
  248. 'phpnut@cakephp.org' => 'PhpNut',
  249. 'jose_zap@cakephp.org' => 'jose_zap@cakephp.org',
  250. ];
  251. $this->assertSame($expected, $this->Email->getTo());
  252. $this->assertSame($this->Email, $result);
  253. }
  254. /**
  255. * test to address with _ in domain name
  256. *
  257. * @return void
  258. */
  259. public function testToUnderscoreDomain()
  260. {
  261. $result = $this->Email->setTo('cake@cake_php.org');
  262. $expected = ['cake@cake_php.org' => 'cake@cake_php.org'];
  263. $this->assertSame($expected, $this->Email->getTo());
  264. $this->assertSame($this->Email, $result);
  265. }
  266. /**
  267. * Data provider function for testBuildInvalidData
  268. *
  269. * @return array
  270. */
  271. public static function invalidEmails()
  272. {
  273. return [
  274. [1.0],
  275. [''],
  276. ['string'],
  277. ['<tag>'],
  278. [['ok@cakephp.org', 1.0, '', 'string']],
  279. ];
  280. }
  281. /**
  282. * testBuildInvalidData
  283. *
  284. * @dataProvider invalidEmails
  285. * @return void
  286. */
  287. public function testInvalidEmail($value)
  288. {
  289. $this->expectException(\InvalidArgumentException::class);
  290. $this->Email->setTo($value);
  291. }
  292. /**
  293. * testBuildInvalidData
  294. *
  295. * @dataProvider invalidEmails
  296. * @return void
  297. */
  298. public function testInvalidEmailAdd($value)
  299. {
  300. $this->expectException(\InvalidArgumentException::class);
  301. $this->Email->addTo($value);
  302. }
  303. /**
  304. * test emailPattern method
  305. *
  306. * @return void
  307. */
  308. public function testEmailPattern()
  309. {
  310. $regex = '/.+@.+\..+/i';
  311. $this->assertSame($regex, $this->Email->setEmailPattern($regex)->getEmailPattern());
  312. }
  313. /**
  314. * Tests that it is possible to set email regex configuration to a CakeEmail object
  315. *
  316. * @return void
  317. */
  318. public function testConfigEmailPattern()
  319. {
  320. $regex = '/.+@.+\..+/i';
  321. $email = new Email(['emailPattern' => $regex]);
  322. $this->assertSame($regex, $email->getEmailPattern());
  323. }
  324. /**
  325. * Tests that it is possible set custom email validation
  326. *
  327. * @return void
  328. */
  329. public function testCustomEmailValidation()
  330. {
  331. $regex = '/^[\.a-z0-9!#$%&\'*+\/=?^_`{|}~-]+@[-a-z0-9]+(\.[-a-z0-9]+)*\.[a-z]{2,6}$/i';
  332. $this->Email->setEmailPattern($regex)->setTo('pass.@example.com');
  333. $this->assertSame([
  334. 'pass.@example.com' => 'pass.@example.com',
  335. ], $this->Email->getTo());
  336. $this->Email->addTo('pass..old.docomo@example.com');
  337. $this->assertSame([
  338. 'pass.@example.com' => 'pass.@example.com',
  339. 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
  340. ], $this->Email->getTo());
  341. $this->Email->reset();
  342. $emails = [
  343. 'pass.@example.com',
  344. 'pass..old.docomo@example.com',
  345. ];
  346. $additionalEmails = [
  347. '.extend.@example.com',
  348. '.docomo@example.com',
  349. ];
  350. $this->Email->setEmailPattern($regex)->setTo($emails);
  351. $this->assertSame([
  352. 'pass.@example.com' => 'pass.@example.com',
  353. 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
  354. ], $this->Email->getTo());
  355. $this->Email->addTo($additionalEmails);
  356. $this->assertSame([
  357. 'pass.@example.com' => 'pass.@example.com',
  358. 'pass..old.docomo@example.com' => 'pass..old.docomo@example.com',
  359. '.extend.@example.com' => '.extend.@example.com',
  360. '.docomo@example.com' => '.docomo@example.com',
  361. ], $this->Email->getTo());
  362. }
  363. /**
  364. * Tests not found transport class name exception
  365. *
  366. * @return void
  367. */
  368. public function testClassNameException()
  369. {
  370. $this->expectException(\BadMethodCallException::class);
  371. $this->expectExceptionMessage('Mailer transport TestFalse is not available.');
  372. $email = new Email();
  373. $email->setTransport('badClassName');
  374. }
  375. /**
  376. * Tests that it is possible to unset the email pattern and make use of filter_var() instead.
  377. *
  378. * @return void
  379. */
  380. public function testUnsetEmailPattern()
  381. {
  382. $this->expectException(\InvalidArgumentException::class);
  383. $this->expectExceptionMessage('Invalid email set for "to". You passed "fail.@example.com".');
  384. $email = new Email();
  385. $this->assertSame(Email::EMAIL_PATTERN, $email->getEmailPattern());
  386. $email->setEmailPattern(null);
  387. $this->assertNull($email->getEmailPattern());
  388. $email->setTo('pass@example.com');
  389. $email->setTo('fail.@example.com');
  390. }
  391. /**
  392. * Tests that passing an empty string throws an InvalidArgumentException.
  393. *
  394. * @return void
  395. */
  396. public function testEmptyTo()
  397. {
  398. $this->expectException(\InvalidArgumentException::class);
  399. $this->expectExceptionMessage('The email set for "to" is empty.');
  400. $email = new Email();
  401. $email->setTo('');
  402. }
  403. /**
  404. * testFormatAddress method
  405. *
  406. * @return void
  407. */
  408. public function testFormatAddress()
  409. {
  410. $result = $this->Email->formatAddress(['cake@cakephp.org' => 'cake@cakephp.org']);
  411. $expected = ['cake@cakephp.org'];
  412. $this->assertSame($expected, $result);
  413. $result = $this->Email->formatAddress(['cake@cakephp.org' => 'cake@cakephp.org', 'php@cakephp.org' => 'php@cakephp.org']);
  414. $expected = ['cake@cakephp.org', 'php@cakephp.org'];
  415. $this->assertSame($expected, $result);
  416. $result = $this->Email->formatAddress(['cake@cakephp.org' => 'CakePHP', 'php@cakephp.org' => 'Cake']);
  417. $expected = ['CakePHP <cake@cakephp.org>', 'Cake <php@cakephp.org>'];
  418. $this->assertSame($expected, $result);
  419. $result = $this->Email->formatAddress(['me@example.com' => 'Last, First']);
  420. $expected = ['"Last, First" <me@example.com>'];
  421. $this->assertSame($expected, $result);
  422. $result = $this->Email->formatAddress(['me@example.com' => '"Last" First']);
  423. $expected = ['"\"Last\" First" <me@example.com>'];
  424. $this->assertSame($expected, $result);
  425. $result = $this->Email->formatAddress(['me@example.com' => 'Last First']);
  426. $expected = ['Last First <me@example.com>'];
  427. $this->assertSame($expected, $result);
  428. $result = $this->Email->formatAddress(['cake@cakephp.org' => 'ÄÖÜTest']);
  429. $expected = ['=?UTF-8?B?w4TDlsOcVGVzdA==?= <cake@cakephp.org>'];
  430. $this->assertSame($expected, $result);
  431. $result = $this->Email->formatAddress(['cake@cakephp.org' => '日本語Test']);
  432. $expected = ['=?UTF-8?B?5pel5pys6KqeVGVzdA==?= <cake@cakephp.org>'];
  433. $this->assertSame($expected, $result);
  434. }
  435. /**
  436. * testFormatAddressJapanese
  437. *
  438. * @return void
  439. */
  440. public function testFormatAddressJapanese()
  441. {
  442. $this->Email->headerCharset = 'ISO-2022-JP';
  443. $result = $this->Email->formatAddress(['cake@cakephp.org' => '日本語Test']);
  444. $expected = ['=?ISO-2022-JP?B?GyRCRnxLXDhsGyhCVGVzdA==?= <cake@cakephp.org>'];
  445. $this->assertSame($expected, $result);
  446. $result = $this->Email->formatAddress(['cake@cakephp.org' => '寿限無寿限無五劫の擦り切れ海砂利水魚の水行末雲来末風来末食う寝る処に住む処やぶら小路の藪柑子パイポパイポパイポのシューリンガンシューリンガンのグーリンダイグーリンダイのポンポコピーのポンポコナーの長久命の長助']);
  447. $expected = ["=?ISO-2022-JP?B?GyRCPHc4Qkw1PHc4Qkw1OF45ZSROOyQkakBaJGwzJDo9TXg/ZTV7GyhC?=\r\n" .
  448. " =?ISO-2022-JP?B?GyRCJE4/ZTlUS3YxQE1oS3ZJd01oS3Y/KSQmPzIkaz1oJEs9OyRgGyhC?=\r\n" .
  449. " =?ISO-2022-JP?B?GyRCPWgkZCRWJGk+Lk8pJE5pLjQ7O1IlUSUkJV0lUSUkJV0lUSUkGyhC?=\r\n" .
  450. " =?ISO-2022-JP?B?GyRCJV0kTiU3JWUhPCVqJXMlLCVzJTclZSE8JWolcyUsJXMkTiUwGyhC?=\r\n" .
  451. " =?ISO-2022-JP?B?GyRCITwlaiVzJUAlJCUwITwlaiVzJUAlJCROJV0lcyVdJTMlVCE8GyhC?=\r\n" .
  452. ' =?ISO-2022-JP?B?GyRCJE4lXSVzJV0lMyVKITwkTkQ5NVdMPyRORDk9dRsoQg==?= <cake@cakephp.org>'];
  453. $this->assertSame($expected, $result);
  454. }
  455. /**
  456. * testAddresses method
  457. *
  458. * @return void
  459. */
  460. public function testAddresses()
  461. {
  462. $this->Email->reset();
  463. $this->Email->setFrom('cake@cakephp.org', 'CakePHP');
  464. $this->Email->setReplyTo('replyto@cakephp.org', 'ReplyTo CakePHP');
  465. $this->Email->setReadReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP');
  466. $this->Email->setReturnPath('returnpath@cakephp.org', 'ReturnPath CakePHP');
  467. $this->Email->setTo('to@cakephp.org', 'To, CakePHP');
  468. $this->Email->setCc('cc@cakephp.org', 'Cc CakePHP');
  469. $this->Email->setBcc('bcc@cakephp.org', 'Bcc CakePHP');
  470. $this->Email->addTo('to2@cakephp.org', 'To2 CakePHP');
  471. $this->Email->addCc('cc2@cakephp.org', 'Cc2 CakePHP');
  472. $this->Email->addBcc('bcc2@cakephp.org', 'Bcc2 CakePHP');
  473. $this->assertSame($this->Email->getFrom(), ['cake@cakephp.org' => 'CakePHP']);
  474. $this->assertSame($this->Email->getReplyTo(), ['replyto@cakephp.org' => 'ReplyTo CakePHP']);
  475. $this->assertSame($this->Email->getReadReceipt(), ['readreceipt@cakephp.org' => 'ReadReceipt CakePHP']);
  476. $this->assertSame($this->Email->getReturnPath(), ['returnpath@cakephp.org' => 'ReturnPath CakePHP']);
  477. $this->assertSame($this->Email->getTo(), ['to@cakephp.org' => 'To, CakePHP', 'to2@cakephp.org' => 'To2 CakePHP']);
  478. $this->assertSame($this->Email->getCc(), ['cc@cakephp.org' => 'Cc CakePHP', 'cc2@cakephp.org' => 'Cc2 CakePHP']);
  479. $this->assertSame($this->Email->getBcc(), ['bcc@cakephp.org' => 'Bcc CakePHP', 'bcc2@cakephp.org' => 'Bcc2 CakePHP']);
  480. $headers = $this->Email->getHeaders(array_fill_keys(['from', 'replyTo', 'readReceipt', 'returnPath', 'to', 'cc', 'bcc'], true));
  481. $this->assertSame($headers['From'], 'CakePHP <cake@cakephp.org>');
  482. $this->assertSame($headers['Reply-To'], 'ReplyTo CakePHP <replyto@cakephp.org>');
  483. $this->assertSame($headers['Disposition-Notification-To'], 'ReadReceipt CakePHP <readreceipt@cakephp.org>');
  484. $this->assertSame($headers['Return-Path'], 'ReturnPath CakePHP <returnpath@cakephp.org>');
  485. $this->assertSame($headers['To'], '"To, CakePHP" <to@cakephp.org>, To2 CakePHP <to2@cakephp.org>');
  486. $this->assertSame($headers['Cc'], 'Cc CakePHP <cc@cakephp.org>, Cc2 CakePHP <cc2@cakephp.org>');
  487. $this->assertSame($headers['Bcc'], 'Bcc CakePHP <bcc@cakephp.org>, Bcc2 CakePHP <bcc2@cakephp.org>');
  488. $this->Email->setReplyTo(['replyto@cakephp.org' => 'Reply to me', 'replyto2@cakephp.org' => 'Reply to me too']);
  489. $this->assertCount(2, $this->Email->getReplyTo());
  490. $headers = $this->Email->getHeaders(array_fill_keys(['replyTo'], true));
  491. $this->assertSame($headers['Reply-To'], 'Reply to me <replyto@cakephp.org>, Reply to me too <replyto2@cakephp.org>');
  492. }
  493. /**
  494. * test reset addresses method
  495. *
  496. * @return void
  497. */
  498. public function testResetAddresses()
  499. {
  500. $this->Email->reset();
  501. $this->Email
  502. ->setFrom('cake@cakephp.org', 'CakePHP')
  503. ->setReplyTo('replyto@cakephp.org', 'ReplyTo CakePHP')
  504. ->setReadReceipt('readreceipt@cakephp.org', 'ReadReceipt CakePHP')
  505. ->setReturnPath('returnpath@cakephp.org', 'ReturnPath CakePHP')
  506. ->setTo('to@cakephp.org', 'To, CakePHP')
  507. ->setCc('cc@cakephp.org', 'Cc CakePHP')
  508. ->setBcc('bcc@cakephp.org', 'Bcc CakePHP');
  509. $this->assertNotEmpty($this->Email->getFrom());
  510. $this->assertNotEmpty($this->Email->getReplyTo());
  511. $this->assertNotEmpty($this->Email->getReadReceipt());
  512. $this->assertNotEmpty($this->Email->getReturnPath());
  513. $this->assertNotEmpty($this->Email->getTo());
  514. $this->assertNotEmpty($this->Email->getCc());
  515. $this->assertNotEmpty($this->Email->getBcc());
  516. $this->Email
  517. ->setFrom([])
  518. ->setReplyTo([])
  519. ->setReadReceipt([])
  520. ->setReturnPath([])
  521. ->setTo([])
  522. ->setCc([])
  523. ->setBcc([]);
  524. $this->assertEmpty($this->Email->getFrom());
  525. $this->assertEmpty($this->Email->getReplyTo());
  526. $this->assertEmpty($this->Email->getReadReceipt());
  527. $this->assertEmpty($this->Email->getReturnPath());
  528. $this->assertEmpty($this->Email->getTo());
  529. $this->assertEmpty($this->Email->getCc());
  530. $this->assertEmpty($this->Email->getBcc());
  531. }
  532. /**
  533. * testMessageId method
  534. *
  535. * @return void
  536. */
  537. public function testMessageId()
  538. {
  539. $this->Email->setMessageId(true);
  540. $result = $this->Email->getHeaders();
  541. $this->assertArrayHasKey('Message-ID', $result);
  542. $this->Email->setMessageId(false);
  543. $result = $this->Email->getHeaders();
  544. $this->assertArrayNotHasKey('Message-ID', $result);
  545. $result = $this->Email->setMessageId('<my-email@localhost>');
  546. $this->assertSame($this->Email, $result);
  547. $result = $this->Email->getHeaders();
  548. $this->assertSame('<my-email@localhost>', $result['Message-ID']);
  549. $result = $this->Email->getMessageId();
  550. $this->assertSame('<my-email@localhost>', $result);
  551. }
  552. public function testAutoMessageIdIsIdempotent()
  553. {
  554. $headers = $this->Email->getHeaders();
  555. $this->assertArrayHasKey('Message-ID', $headers);
  556. $regeneratedHeaders = $this->Email->getHeaders();
  557. $this->assertSame($headers['Message-ID'], $regeneratedHeaders['Message-ID']);
  558. }
  559. /**
  560. * @return void
  561. */
  562. public function testPriority()
  563. {
  564. $this->Email->setPriority(4);
  565. $this->assertSame(4, $this->Email->getPriority());
  566. $result = $this->Email->getHeaders();
  567. $this->assertArrayHasKey('X-Priority', $result);
  568. }
  569. /**
  570. * testMessageIdInvalid method
  571. *
  572. * @return void
  573. */
  574. public function testMessageIdInvalid()
  575. {
  576. $this->expectException(\InvalidArgumentException::class);
  577. $this->Email->setMessageId('my-email@localhost');
  578. }
  579. /**
  580. * testDomain method
  581. *
  582. * @return void
  583. */
  584. public function testDomain()
  585. {
  586. $result = $this->Email->getDomain();
  587. $expected = env('HTTP_HOST') ? env('HTTP_HOST') : php_uname('n');
  588. $this->assertSame($expected, $result);
  589. $this->Email->setDomain('example.org');
  590. $result = $this->Email->getDomain();
  591. $expected = 'example.org';
  592. $this->assertSame($expected, $result);
  593. }
  594. /**
  595. * testMessageIdWithDomain method
  596. *
  597. * @return void
  598. */
  599. public function testMessageIdWithDomain()
  600. {
  601. $this->Email->setDomain('example.org');
  602. $result = $this->Email->getHeaders();
  603. $expected = '@example.org>';
  604. $this->assertTextContains($expected, $result['Message-ID']);
  605. $_SERVER['HTTP_HOST'] = 'example.org';
  606. $result = $this->Email->getHeaders();
  607. $this->assertTextContains('example.org', $result['Message-ID']);
  608. $_SERVER['HTTP_HOST'] = 'example.org:81';
  609. $result = $this->Email->getHeaders();
  610. $this->assertTextNotContains(':81', $result['Message-ID']);
  611. }
  612. /**
  613. * testSubject method
  614. *
  615. * @return void
  616. */
  617. public function testSubject()
  618. {
  619. $this->Email->setSubject('You have a new message.');
  620. $this->assertSame('You have a new message.', $this->Email->getSubject());
  621. $this->Email->setSubject('You have a new message, I think.');
  622. $this->assertSame($this->Email->getSubject(), 'You have a new message, I think.');
  623. $this->Email->setSubject(1);
  624. $this->assertSame('1', $this->Email->getSubject());
  625. $input = 'هذه رسالة بعنوان طويل مرسل للمستلم';
  626. $this->Email->setSubject($input);
  627. $expected = '=?UTF-8?B?2YfYsNmHINix2LPYp9mE2Kkg2KjYudmG2YjYp9mGINi32YjZitmEINmF2LE=?=' . "\r\n" . ' =?UTF-8?B?2LPZhCDZhNmE2YXYs9iq2YTZhQ==?=';
  628. $this->assertSame($expected, $this->Email->getSubject());
  629. $this->assertSame($input, $this->Email->getOriginalSubject());
  630. }
  631. /**
  632. * testSubjectJapanese
  633. *
  634. * @return void
  635. */
  636. public function testSubjectJapanese()
  637. {
  638. mb_internal_encoding('UTF-8');
  639. $this->Email->headerCharset = 'ISO-2022-JP';
  640. $this->Email->setSubject('日本語のSubjectにも対応するよ');
  641. $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsJE4bKEJTdWJqZWN0GyRCJEskYkJQMX4kOSRrJGgbKEI=?=';
  642. $this->assertSame($expected, $this->Email->getSubject());
  643. $this->Email->setSubject('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
  644. $expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  645. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  646. ' =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=';
  647. $this->assertSame($expected, $this->Email->getSubject());
  648. }
  649. /**
  650. * testHeaders method
  651. *
  652. * @return void
  653. */
  654. public function testHeaders()
  655. {
  656. $this->Email->setMessageId(false);
  657. $this->Email->setHeaders(['X-Something' => 'nice']);
  658. $expected = [
  659. 'X-Something' => 'nice',
  660. 'Date' => date(DATE_RFC2822),
  661. 'MIME-Version' => '1.0',
  662. 'Content-Type' => 'text/plain; charset=UTF-8',
  663. 'Content-Transfer-Encoding' => '8bit',
  664. ];
  665. $this->assertSame($expected, $this->Email->getHeaders());
  666. $this->Email->addHeaders(['X-Something' => 'very nice', 'X-Other' => 'cool']);
  667. $expected = [
  668. 'X-Something' => 'very nice',
  669. 'X-Other' => 'cool',
  670. 'Date' => date(DATE_RFC2822),
  671. 'MIME-Version' => '1.0',
  672. 'Content-Type' => 'text/plain; charset=UTF-8',
  673. 'Content-Transfer-Encoding' => '8bit',
  674. ];
  675. $this->assertSame($expected, $this->Email->getHeaders());
  676. $this->Email->setFrom('cake@cakephp.org');
  677. $this->assertSame($expected, $this->Email->getHeaders());
  678. $expected = [
  679. 'From' => 'cake@cakephp.org',
  680. 'X-Something' => 'very nice',
  681. 'X-Other' => 'cool',
  682. 'Date' => date(DATE_RFC2822),
  683. 'MIME-Version' => '1.0',
  684. 'Content-Type' => 'text/plain; charset=UTF-8',
  685. 'Content-Transfer-Encoding' => '8bit',
  686. ];
  687. $this->assertSame($expected, $this->Email->getHeaders(['from' => true]));
  688. $this->Email->setFrom('cake@cakephp.org', 'CakePHP');
  689. $expected['From'] = 'CakePHP <cake@cakephp.org>';
  690. $this->assertSame($expected, $this->Email->getHeaders(['from' => true]));
  691. $this->Email->setTo(['cake@cakephp.org', 'php@cakephp.org' => 'CakePHP']);
  692. $expected = [
  693. 'From' => 'CakePHP <cake@cakephp.org>',
  694. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  695. 'X-Something' => 'very nice',
  696. 'X-Other' => 'cool',
  697. 'Date' => date(DATE_RFC2822),
  698. 'MIME-Version' => '1.0',
  699. 'Content-Type' => 'text/plain; charset=UTF-8',
  700. 'Content-Transfer-Encoding' => '8bit',
  701. ];
  702. $this->assertSame($expected, $this->Email->getHeaders(['from' => true, 'to' => true]));
  703. $this->Email->charset = 'ISO-2022-JP';
  704. $expected = [
  705. 'From' => 'CakePHP <cake@cakephp.org>',
  706. 'To' => 'cake@cakephp.org, CakePHP <php@cakephp.org>',
  707. 'X-Something' => 'very nice',
  708. 'X-Other' => 'cool',
  709. 'Date' => date(DATE_RFC2822),
  710. 'MIME-Version' => '1.0',
  711. 'Content-Type' => 'text/plain; charset=ISO-2022-JP',
  712. 'Content-Transfer-Encoding' => '7bit',
  713. ];
  714. $this->assertSame($expected, $this->Email->getHeaders(['from' => true, 'to' => true]));
  715. $result = $this->Email->setHeaders([]);
  716. $this->assertInstanceOf('Cake\Mailer\Email', $result);
  717. $this->Email->setHeaders(['o:tag' => ['foo']]);
  718. $this->Email->addHeaders(['o:tag' => ['bar']]);
  719. $result = $this->Email->getHeaders();
  720. $this->assertEquals(['foo', 'bar'], $result['o:tag']);
  721. }
  722. /**
  723. * testTemplate method
  724. *
  725. * @return void
  726. */
  727. public function testTemplate()
  728. {
  729. $this->Email->viewBuilder()->setTemplate('template');
  730. $this->assertSame('template', $this->Email->viewBuilder()->getTemplate());
  731. }
  732. /**
  733. * testLayout method
  734. *
  735. * @return void
  736. */
  737. public function testLayout()
  738. {
  739. $this->Email->viewBuilder()->setLayout('layout');
  740. $this->assertSame('layout', $this->Email->viewBuilder()->getLayout());
  741. }
  742. /**
  743. * testTheme method
  744. *
  745. * @return void
  746. */
  747. public function testTheme()
  748. {
  749. $this->assertNull($this->Email->viewBuilder()->getTheme());
  750. $this->Email->viewBuilder()->setTheme('default');
  751. $expected = 'default';
  752. $this->assertSame($expected, $this->Email->viewBuilder()->getTheme());
  753. }
  754. /**
  755. * testViewVars method
  756. *
  757. * @return void
  758. */
  759. public function testViewVars()
  760. {
  761. $this->assertSame([], $this->Email->getViewVars());
  762. $this->Email->setViewVars(['value' => 12345]);
  763. $this->assertSame(['value' => 12345], $this->Email->getViewVars());
  764. $this->Email->setViewVars(['name' => 'CakePHP']);
  765. $this->assertEquals(['value' => 12345, 'name' => 'CakePHP'], $this->Email->getViewVars());
  766. $this->Email->setViewVars(['value' => 4567]);
  767. $this->assertSame(['value' => 4567, 'name' => 'CakePHP'], $this->Email->getViewVars());
  768. }
  769. /**
  770. * testAttachments method
  771. *
  772. * @return void
  773. */
  774. public function testSetAttachments()
  775. {
  776. $this->Email->setAttachments(CAKE . 'basics.php');
  777. $expected = [
  778. 'basics.php' => [
  779. 'file' => CAKE . 'basics.php',
  780. 'mimetype' => 'text/x-php',
  781. ],
  782. ];
  783. $this->assertSame($expected, $this->Email->getAttachments());
  784. $this->Email->setAttachments([]);
  785. $this->assertSame([], $this->Email->getAttachments());
  786. $this->Email->setAttachments([
  787. ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'],
  788. ]);
  789. $this->Email->addAttachments(CORE_PATH . 'config' . DS . 'bootstrap.php');
  790. $this->Email->addAttachments([CORE_PATH . 'config' . DS . 'bootstrap.php']);
  791. $this->Email->addAttachments([
  792. 'other.txt' => CORE_PATH . 'config' . DS . 'bootstrap.php',
  793. 'license' => CORE_PATH . 'LICENSE',
  794. ]);
  795. $expected = [
  796. 'basics.php' => ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'],
  797. 'bootstrap.php' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'text/x-php'],
  798. 'other.txt' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'text/x-php'],
  799. 'license' => ['file' => CORE_PATH . 'LICENSE', 'mimetype' => 'text/plain'],
  800. ];
  801. $this->assertSame($expected, $this->Email->getAttachments());
  802. $this->expectException(\InvalidArgumentException::class);
  803. $this->Email->setAttachments([['nofile' => CAKE . 'basics.php', 'mimetype' => 'text/plain']]);
  804. }
  805. /**
  806. * Test send() with no template and data string attachment and no mimetype
  807. *
  808. * @return void
  809. */
  810. public function testSetAttachmentDataNoMimetype()
  811. {
  812. $this->Email->setAttachments(['cake.icon.gif' => [
  813. 'data' => 'test',
  814. ]]);
  815. $result = $this->Email->getAttachments();
  816. $expected = [
  817. 'cake.icon.gif' => [
  818. 'data' => base64_encode('test') . "\r\n",
  819. 'mimetype' => 'application/octet-stream',
  820. ],
  821. ];
  822. $this->assertSame($expected, $this->Email->getAttachments());
  823. }
  824. /**
  825. * testTransport method
  826. *
  827. * @return void
  828. */
  829. public function testTransport()
  830. {
  831. $result = $this->Email->setTransport('debug');
  832. $this->assertSame($this->Email, $result);
  833. $result = $this->Email->getTransport();
  834. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  835. $instance = $this->getMockBuilder('Cake\Mailer\Transport\DebugTransport')->getMock();
  836. $this->Email->setTransport($instance);
  837. $this->assertSame($instance, $this->Email->getTransport());
  838. }
  839. /**
  840. * Test that using unknown transports fails.
  841. */
  842. public function testTransportInvalid()
  843. {
  844. $this->expectException(\InvalidArgumentException::class);
  845. $this->expectExceptionMessage('The "Invalid" transport configuration does not exist');
  846. $this->Email->setTransport('Invalid');
  847. }
  848. /**
  849. * Test that using classes with no send method fails.
  850. */
  851. public function testTransportInstanceInvalid()
  852. {
  853. $this->expectException(\LogicException::class);
  854. $this->Email->setTransport(new \StdClass());
  855. }
  856. /**
  857. * Test that using unknown transports fails.
  858. */
  859. public function testTransportTypeInvalid()
  860. {
  861. $this->expectException(\InvalidArgumentException::class);
  862. $this->expectExceptionMessage('The value passed for the "$name" argument must be either a string, or an object, integer given.');
  863. $this->Email->setTransport(123);
  864. }
  865. /**
  866. * Test that using misconfigured transports fails.
  867. */
  868. public function testTransportMissingClassName()
  869. {
  870. $this->expectException(\InvalidArgumentException::class);
  871. $this->expectExceptionMessage('Transport config "debug" is invalid, the required `className` option is missing');
  872. $this->deprecated(function () {
  873. Email::dropTransport('debug');
  874. Email::setConfigTransport('debug', []);
  875. });
  876. $this->Email->setTransport('debug');
  877. }
  878. /**
  879. * Test configuring a transport.
  880. *
  881. * @return void
  882. */
  883. public function testConfigTransport()
  884. {
  885. $settings = [
  886. 'className' => 'Debug',
  887. 'log' => true,
  888. ];
  889. $this->deprecated(function () use ($settings) {
  890. Email::dropTransport('debug');
  891. $result = Email::setConfigTransport('debug', $settings);
  892. $this->assertNull($result, 'No return.');
  893. $result = Email::getConfigTransport('debug');
  894. $this->assertEquals($settings, $result);
  895. });
  896. }
  897. /**
  898. * Test configuring multiple transports.
  899. */
  900. public function testConfigTransportMultiple()
  901. {
  902. $settings = [
  903. 'debug' => [
  904. 'className' => 'Debug',
  905. 'log' => true,
  906. ],
  907. 'test_smtp' => [
  908. 'className' => 'Smtp',
  909. 'username' => 'mark',
  910. 'password' => 'password',
  911. 'host' => 'example.com',
  912. ],
  913. ];
  914. $this->deprecated(function () use ($settings) {
  915. Email::dropTransport('debug');
  916. Email::setConfigTransport($settings);
  917. $this->assertEquals($settings['debug'], Email::getConfigTransport('debug'));
  918. $this->assertEquals($settings['test_smtp'], Email::getConfigTransport('test_smtp'));
  919. });
  920. }
  921. /**
  922. * Test that exceptions are raised when duplicate transports are configured.
  923. */
  924. public function testConfigTransportErrorOnDuplicate()
  925. {
  926. $this->expectException(\BadMethodCallException::class);
  927. $settings = [
  928. 'className' => 'Debug',
  929. 'log' => true,
  930. ];
  931. $this->deprecated(function () use ($settings) {
  932. Email::setConfigTransport('debug', $settings);
  933. Email::setConfigTransport('debug', $settings);
  934. Email::dropTransport('debug');
  935. });
  936. }
  937. /**
  938. * Test configTransport with an instance.
  939. *
  940. * @return void
  941. */
  942. public function testConfigTransportInstance()
  943. {
  944. $this->deprecated(function () {
  945. Email::dropTransport('debug');
  946. $instance = new DebugTransport();
  947. Email::setConfigTransport('debug', $instance);
  948. $this->assertEquals(['className' => $instance], Email::getConfigTransport('debug'));
  949. });
  950. }
  951. /**
  952. * Test enumerating all transport configurations
  953. *
  954. * @return void
  955. */
  956. public function testConfiguredTransport()
  957. {
  958. $this->deprecated(function () {
  959. $result = Email::configuredTransport();
  960. $this->assertInternalType('array', $result, 'Should have config keys');
  961. $this->assertEquals(
  962. array_keys($this->transports),
  963. $result,
  964. 'Loaded transports should be present in enumeration.'
  965. );
  966. });
  967. }
  968. /**
  969. * Test dropping a transport configuration
  970. *
  971. * @return void
  972. */
  973. public function testDropTransport()
  974. {
  975. $this->deprecated(function () {
  976. $result = Email::getConfigTransport('debug');
  977. $this->assertInternalType('array', $result, 'Should have config data');
  978. Email::dropTransport('debug');
  979. $this->assertNull(Email::getConfigTransport('debug'), 'Should not exist.');
  980. });
  981. }
  982. /**
  983. * Test reading/writing configuration profiles.
  984. *
  985. * @return void
  986. */
  987. public function testConfig()
  988. {
  989. $settings = [
  990. 'to' => 'mark@example.com',
  991. 'from' => 'noreply@example.com',
  992. ];
  993. Email::setConfig('test', $settings);
  994. $this->assertEquals($settings, Email::getConfig('test'), 'Should be the same.');
  995. $email = new Email('test');
  996. $this->assertContains($settings['to'], $email->getTo());
  997. }
  998. /**
  999. * Test that exceptions are raised on duplicate config set.
  1000. *
  1001. * @return void
  1002. */
  1003. public function testConfigErrorOnDuplicate()
  1004. {
  1005. $this->expectException(\BadMethodCallException::class);
  1006. $settings = [
  1007. 'to' => 'mark@example.com',
  1008. 'from' => 'noreply@example.com',
  1009. ];
  1010. Email::setConfig('test', $settings);
  1011. Email::setConfig('test', $settings);
  1012. }
  1013. /**
  1014. * test profile method
  1015. *
  1016. * @return void
  1017. */
  1018. public function testProfile()
  1019. {
  1020. $config = ['test' => 'ok', 'test2' => true];
  1021. $this->Email->setProfile($config);
  1022. $this->assertSame($this->Email->getProfile(), $config);
  1023. $config = ['test' => 'test@example.com'];
  1024. $this->Email->setProfile($config);
  1025. $expected = ['test' => 'test@example.com', 'test2' => true];
  1026. $this->assertSame($expected, $this->Email->getProfile());
  1027. }
  1028. /**
  1029. * test that default profile is used by constructor if available.
  1030. *
  1031. * @return void
  1032. */
  1033. public function testDefaultProfile()
  1034. {
  1035. $config = ['test' => 'ok', 'test2' => true];
  1036. Configure::write('Email.default', $config);
  1037. Email::setConfig(Configure::consume('Email'));
  1038. $Email = new Email();
  1039. $this->assertSame($Email->getProfile(), $config);
  1040. Configure::delete('Email');
  1041. Email::drop('default');
  1042. }
  1043. /**
  1044. * Test that using an invalid profile fails.
  1045. */
  1046. public function testProfileInvalid()
  1047. {
  1048. $this->expectException(\InvalidArgumentException::class);
  1049. $this->expectExceptionMessage('Unknown email configuration "derp".');
  1050. $email = new Email();
  1051. $email->setProfile('derp');
  1052. }
  1053. /**
  1054. * testConfigString method
  1055. *
  1056. * @return void
  1057. */
  1058. public function testUseConfigString()
  1059. {
  1060. $config = [
  1061. 'from' => ['some@example.com' => 'My website'],
  1062. 'to' => ['test@example.com' => 'Testname'],
  1063. 'subject' => 'Test mail subject',
  1064. 'transport' => 'debug',
  1065. 'theme' => 'TestTheme',
  1066. 'helpers' => ['Html', 'Form'],
  1067. ];
  1068. Email::setConfig('test', $config);
  1069. $this->Email->setProfile('test');
  1070. $result = $this->Email->getTo();
  1071. $this->assertEquals($config['to'], $result);
  1072. $result = $this->Email->getFrom();
  1073. $this->assertEquals($config['from'], $result);
  1074. $result = $this->Email->getSubject();
  1075. $this->assertEquals($config['subject'], $result);
  1076. $result = $this->Email->viewBuilder()->getTheme();
  1077. $this->assertEquals($config['theme'], $result);
  1078. $result = $this->Email->getTransport();
  1079. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  1080. $result = $this->Email->viewBuilder()->getHelpers();
  1081. $this->assertEquals($config['helpers'], $result);
  1082. }
  1083. /**
  1084. * Calling send() with no parameters should not overwrite the view variables.
  1085. *
  1086. * @return void
  1087. */
  1088. public function testSendWithNoContentDoesNotOverwriteViewVar()
  1089. {
  1090. $this->Email->reset();
  1091. $this->Email->setTransport('debug');
  1092. $this->Email->setFrom('cake@cakephp.org');
  1093. $this->Email->setTo('you@cakephp.org');
  1094. $this->Email->setSubject('My title');
  1095. $this->Email->setEmailFormat('text');
  1096. $this->Email->viewBuilder()->setTemplate('default');
  1097. $this->Email->setViewVars([
  1098. 'content' => 'A message to you',
  1099. ]);
  1100. $result = $this->Email->send();
  1101. $this->assertContains('A message to you', $result['message']);
  1102. }
  1103. /**
  1104. * testSendWithContent method
  1105. *
  1106. * @return void
  1107. */
  1108. public function testSendWithContent()
  1109. {
  1110. $this->Email->reset();
  1111. $this->Email->setTransport('debug');
  1112. $this->Email->setFrom('cake@cakephp.org');
  1113. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1114. $this->Email->setSubject('My title');
  1115. $this->Email->setProfile(['empty']);
  1116. $result = $this->Email->send("Here is my body, with multi lines.\nThis is the second line.\r\n\r\nAnd the last.");
  1117. $expected = ['headers', 'message'];
  1118. $this->assertEquals($expected, array_keys($result));
  1119. $expected = "Here is my body, with multi lines.\r\nThis is the second line.\r\n\r\nAnd the last.\r\n\r\n";
  1120. $this->assertEquals($expected, $result['message']);
  1121. $this->assertContains('Date: ', $result['headers']);
  1122. $this->assertContains('Message-ID: ', $result['headers']);
  1123. $this->assertContains('To: ', $result['headers']);
  1124. $result = $this->Email->send('Other body');
  1125. $expected = "Other body\r\n\r\n";
  1126. $this->assertSame($expected, $result['message']);
  1127. $this->assertContains('Message-ID: ', $result['headers']);
  1128. $this->assertContains('To: ', $result['headers']);
  1129. $this->Email->reset();
  1130. $this->Email->setTransport('debug');
  1131. $this->Email->setFrom('cake@cakephp.org');
  1132. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1133. $this->Email->setSubject('My title');
  1134. $this->Email->setProfile(['empty']);
  1135. $result = $this->Email->send(['Sending content', 'As array']);
  1136. $expected = "Sending content\r\nAs array\r\n\r\n\r\n";
  1137. $this->assertSame($expected, $result['message']);
  1138. }
  1139. /**
  1140. * testSendWithoutFrom method
  1141. *
  1142. * @return void
  1143. */
  1144. public function testSendWithoutFrom()
  1145. {
  1146. $this->expectException(\BadMethodCallException::class);
  1147. $this->Email->setTransport('debug');
  1148. $this->Email->setTo('cake@cakephp.org');
  1149. $this->Email->setSubject('My title');
  1150. $this->Email->setProfile(['empty']);
  1151. $this->Email->send('Forgot to set From');
  1152. }
  1153. /**
  1154. * testSendWithoutTo method
  1155. *
  1156. * @return void
  1157. */
  1158. public function testSendWithoutTo()
  1159. {
  1160. $this->expectException(\BadMethodCallException::class);
  1161. $this->Email->setTransport('debug');
  1162. $this->Email->setFrom('cake@cakephp.org');
  1163. $this->Email->setSubject('My title');
  1164. $this->Email->setProfile(['empty']);
  1165. $this->Email->send('Forgot to set To');
  1166. }
  1167. /**
  1168. * test send without a transport method
  1169. *
  1170. * @return void
  1171. */
  1172. public function testSendWithoutTransport()
  1173. {
  1174. $this->expectException(\BadMethodCallException::class);
  1175. $this->expectExceptionMessage('Cannot send email, transport was not defined.');
  1176. $this->Email->setTo('cake@cakephp.org');
  1177. $this->Email->setFrom('cake@cakephp.org');
  1178. $this->Email->setSubject('My title');
  1179. $this->Email->send('Forgot to set To');
  1180. }
  1181. /**
  1182. * Test send() with no template.
  1183. *
  1184. * @return void
  1185. */
  1186. public function testSendNoTemplateWithAttachments()
  1187. {
  1188. $this->Email->setTransport('debug');
  1189. $this->Email->setFrom('cake@cakephp.org');
  1190. $this->Email->setTo('cake@cakephp.org');
  1191. $this->Email->setSubject('My title');
  1192. $this->Email->setEmailFormat('text');
  1193. $this->Email->setAttachments([CAKE . 'basics.php']);
  1194. $result = $this->Email->send('Hello');
  1195. $boundary = $this->Email->getBoundary();
  1196. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1197. $expected = "--$boundary\r\n" .
  1198. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1199. "Content-Transfer-Encoding: 8bit\r\n" .
  1200. "\r\n" .
  1201. 'Hello' .
  1202. "\r\n" .
  1203. "\r\n" .
  1204. "\r\n" .
  1205. "--$boundary\r\n" .
  1206. "Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
  1207. "Content-Type: text/x-php\r\n" .
  1208. "Content-Transfer-Encoding: base64\r\n" .
  1209. "\r\n";
  1210. $this->assertContains($expected, $result['message']);
  1211. }
  1212. /**
  1213. * Test send() with no template and data string attachment
  1214. *
  1215. * @return void
  1216. */
  1217. public function testSendNoTemplateWithDataStringAttachment()
  1218. {
  1219. $this->Email->setTransport('debug');
  1220. $this->Email->setFrom('cake@cakephp.org');
  1221. $this->Email->setTo('cake@cakephp.org');
  1222. $this->Email->setSubject('My title');
  1223. $this->Email->setEmailFormat('text');
  1224. $data = file_get_contents(TEST_APP . 'webroot/img/cake.power.gif');
  1225. $this->Email->setAttachments(['cake.icon.gif' => [
  1226. 'data' => $data,
  1227. 'mimetype' => 'image/gif',
  1228. ]]);
  1229. $result = $this->Email->send('Hello');
  1230. $boundary = $this->Email->getBoundary();
  1231. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1232. $expected = "--$boundary\r\n" .
  1233. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1234. "Content-Transfer-Encoding: 8bit\r\n" .
  1235. "\r\n" .
  1236. 'Hello' .
  1237. "\r\n" .
  1238. "\r\n" .
  1239. "\r\n" .
  1240. "--$boundary\r\n" .
  1241. "Content-Disposition: attachment; filename=\"cake.icon.gif\"\r\n" .
  1242. "Content-Type: image/gif\r\n" .
  1243. "Content-Transfer-Encoding: base64\r\n\r\n";
  1244. $expected .= chunk_split(base64_encode($data), 76, "\r\n");
  1245. $this->assertContains($expected, $result['message']);
  1246. }
  1247. /**
  1248. * Test send() with no template as both
  1249. *
  1250. * @return void
  1251. */
  1252. public function testSendNoTemplateWithAttachmentsAsBoth()
  1253. {
  1254. $this->Email->setTransport('debug');
  1255. $this->Email->setFrom('cake@cakephp.org');
  1256. $this->Email->setTo('cake@cakephp.org');
  1257. $this->Email->setSubject('My title');
  1258. $this->Email->setEmailFormat('both');
  1259. $this->Email->setAttachments([CORE_PATH . 'VERSION.txt']);
  1260. $result = $this->Email->send('Hello');
  1261. $boundary = $this->Email->getBoundary();
  1262. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1263. $expected = "--$boundary\r\n" .
  1264. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  1265. "\r\n" .
  1266. "--alt-$boundary\r\n" .
  1267. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1268. "Content-Transfer-Encoding: 8bit\r\n" .
  1269. "\r\n" .
  1270. 'Hello' .
  1271. "\r\n" .
  1272. "\r\n" .
  1273. "\r\n" .
  1274. "--alt-$boundary\r\n" .
  1275. "Content-Type: text/html; charset=UTF-8\r\n" .
  1276. "Content-Transfer-Encoding: 8bit\r\n" .
  1277. "\r\n" .
  1278. 'Hello' .
  1279. "\r\n" .
  1280. "\r\n" .
  1281. "\r\n" .
  1282. "--alt-{$boundary}--\r\n" .
  1283. "\r\n" .
  1284. "--$boundary\r\n" .
  1285. "Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n" .
  1286. "Content-Type: text/plain\r\n" .
  1287. "Content-Transfer-Encoding: base64\r\n" .
  1288. "\r\n";
  1289. $this->assertContains($expected, $result['message']);
  1290. }
  1291. /**
  1292. * Test setting inline attachments and messages.
  1293. *
  1294. * @return void
  1295. */
  1296. public function testSendWithInlineAttachments()
  1297. {
  1298. $this->Email->setTransport('debug');
  1299. $this->Email->setFrom('cake@cakephp.org');
  1300. $this->Email->setTo('cake@cakephp.org');
  1301. $this->Email->setSubject('My title');
  1302. $this->Email->setEmailFormat('both');
  1303. $this->Email->setAttachments([
  1304. 'cake.png' => [
  1305. 'file' => CORE_PATH . 'VERSION.txt',
  1306. 'contentId' => 'abc123',
  1307. ],
  1308. ]);
  1309. $result = $this->Email->send('Hello');
  1310. $boundary = $this->Email->getBoundary();
  1311. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1312. $expected = "--$boundary\r\n" .
  1313. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  1314. "\r\n" .
  1315. "--rel-$boundary\r\n" .
  1316. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  1317. "\r\n" .
  1318. "--alt-$boundary\r\n" .
  1319. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1320. "Content-Transfer-Encoding: 8bit\r\n" .
  1321. "\r\n" .
  1322. 'Hello' .
  1323. "\r\n" .
  1324. "\r\n" .
  1325. "\r\n" .
  1326. "--alt-$boundary\r\n" .
  1327. "Content-Type: text/html; charset=UTF-8\r\n" .
  1328. "Content-Transfer-Encoding: 8bit\r\n" .
  1329. "\r\n" .
  1330. 'Hello' .
  1331. "\r\n" .
  1332. "\r\n" .
  1333. "\r\n" .
  1334. "--alt-{$boundary}--\r\n" .
  1335. "\r\n" .
  1336. "--rel-$boundary\r\n" .
  1337. "Content-Disposition: inline; filename=\"cake.png\"\r\n" .
  1338. "Content-Type: text/plain\r\n" .
  1339. "Content-Transfer-Encoding: base64\r\n" .
  1340. "Content-ID: <abc123>\r\n" .
  1341. "\r\n";
  1342. $this->assertContains($expected, $result['message']);
  1343. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  1344. $this->assertContains('--' . $boundary . '--', $result['message']);
  1345. }
  1346. /**
  1347. * Test setting inline attachments and HTML only messages.
  1348. *
  1349. * @return void
  1350. */
  1351. public function testSendWithInlineAttachmentsHtmlOnly()
  1352. {
  1353. $this->Email->setTransport('debug');
  1354. $this->Email->setFrom('cake@cakephp.org');
  1355. $this->Email->setTo('cake@cakephp.org');
  1356. $this->Email->setSubject('My title');
  1357. $this->Email->setEmailFormat('html');
  1358. $this->Email->setAttachments([
  1359. 'cake.png' => [
  1360. 'file' => CORE_PATH . 'VERSION.txt',
  1361. 'contentId' => 'abc123',
  1362. ],
  1363. ]);
  1364. $result = $this->Email->send('Hello');
  1365. $boundary = $this->Email->getBoundary();
  1366. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1367. $expected = "--$boundary\r\n" .
  1368. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  1369. "\r\n" .
  1370. "--rel-$boundary\r\n" .
  1371. "Content-Type: text/html; charset=UTF-8\r\n" .
  1372. "Content-Transfer-Encoding: 8bit\r\n" .
  1373. "\r\n" .
  1374. 'Hello' .
  1375. "\r\n" .
  1376. "\r\n" .
  1377. "\r\n" .
  1378. "--rel-$boundary\r\n" .
  1379. "Content-Disposition: inline; filename=\"cake.png\"\r\n" .
  1380. "Content-Type: text/plain\r\n" .
  1381. "Content-Transfer-Encoding: base64\r\n" .
  1382. "Content-ID: <abc123>\r\n" .
  1383. "\r\n";
  1384. $this->assertContains($expected, $result['message']);
  1385. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  1386. $this->assertContains('--' . $boundary . '--', $result['message']);
  1387. }
  1388. /**
  1389. * Test disabling content-disposition.
  1390. *
  1391. * @return void
  1392. */
  1393. public function testSendWithNoContentDispositionAttachments()
  1394. {
  1395. $this->Email->setTransport('debug');
  1396. $this->Email->setFrom('cake@cakephp.org');
  1397. $this->Email->setTo('cake@cakephp.org');
  1398. $this->Email->setSubject('My title');
  1399. $this->Email->setEmailFormat('text');
  1400. $this->Email->setAttachments([
  1401. 'cake.png' => [
  1402. 'file' => CORE_PATH . 'VERSION.txt',
  1403. 'contentDisposition' => false,
  1404. ],
  1405. ]);
  1406. $result = $this->Email->send('Hello');
  1407. $boundary = $this->Email->getBoundary();
  1408. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1409. $expected = "--$boundary\r\n" .
  1410. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1411. "Content-Transfer-Encoding: 8bit\r\n" .
  1412. "\r\n" .
  1413. 'Hello' .
  1414. "\r\n" .
  1415. "\r\n" .
  1416. "\r\n" .
  1417. "--{$boundary}\r\n" .
  1418. "Content-Type: text/plain\r\n" .
  1419. "Content-Transfer-Encoding: base64\r\n" .
  1420. "\r\n";
  1421. $this->assertContains($expected, $result['message']);
  1422. $this->assertContains('--' . $boundary . '--', $result['message']);
  1423. }
  1424. /**
  1425. * Test an attachment filename with non-ASCII characters.
  1426. *
  1427. * @return void
  1428. */
  1429. public function testSendWithNonAsciiFilenameAttachments()
  1430. {
  1431. $this->Email->setTransport('debug');
  1432. $this->Email->setFrom('cake@cakephp.org');
  1433. $this->Email->setTo('cake@cakephp.org');
  1434. $this->Email->setSubject('My title');
  1435. $this->Email->setEmailFormat('both');
  1436. $this->Email->setAttachments([
  1437. 'gâteau.png' => [
  1438. 'file' => CORE_PATH . 'VERSION.txt',
  1439. 'contentId' => 'abc123',
  1440. ],
  1441. ]);
  1442. $result = $this->Email->send('Hello');
  1443. $boundary = $this->Email->getBoundary();
  1444. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  1445. $expected = "--$boundary\r\n" .
  1446. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  1447. "\r\n" .
  1448. "--rel-$boundary\r\n" .
  1449. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  1450. "\r\n" .
  1451. "--alt-$boundary\r\n" .
  1452. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1453. "Content-Transfer-Encoding: 8bit\r\n" .
  1454. "\r\n" .
  1455. 'Hello' .
  1456. "\r\n" .
  1457. "\r\n" .
  1458. "\r\n" .
  1459. "--alt-$boundary\r\n" .
  1460. "Content-Type: text/html; charset=UTF-8\r\n" .
  1461. "Content-Transfer-Encoding: 8bit\r\n" .
  1462. "\r\n" .
  1463. 'Hello' .
  1464. "\r\n" .
  1465. "\r\n" .
  1466. "\r\n" .
  1467. "--alt-{$boundary}--\r\n" .
  1468. "\r\n" .
  1469. "--rel-$boundary\r\n" .
  1470. "Content-Disposition: inline; filename=\"gateau.png\"; filename*=utf-8''g%C3%A2teau.png\r\n" .
  1471. "Content-Type: text/plain\r\n" .
  1472. "Content-Transfer-Encoding: base64\r\n" .
  1473. "Content-ID: <abc123>\r\n" .
  1474. "\r\n";
  1475. $this->assertContains($expected, $result['message']);
  1476. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  1477. $this->assertContains('--' . $boundary . '--', $result['message']);
  1478. }
  1479. /**
  1480. * testSendWithLog method
  1481. *
  1482. * @return void
  1483. */
  1484. public function testSendWithLog()
  1485. {
  1486. $log = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  1487. ->setMethods(['log'])
  1488. ->setConstructorArgs([['scopes' => 'email']])
  1489. ->getMock();
  1490. $message = 'Logging This';
  1491. $log->expects($this->once())
  1492. ->method('log')
  1493. ->with(
  1494. 'debug',
  1495. $this->logicalAnd(
  1496. $this->stringContains($message),
  1497. $this->stringContains('cake@cakephp.org'),
  1498. $this->stringContains('me@cakephp.org')
  1499. )
  1500. );
  1501. Log::setConfig('email', $log);
  1502. $this->Email->setTransport('debug');
  1503. $this->Email->setTo('me@cakephp.org');
  1504. $this->Email->setFrom('cake@cakephp.org');
  1505. $this->Email->setSubject('My title');
  1506. $this->Email->setProfile(['log' => 'debug']);
  1507. $result = $this->Email->send($message);
  1508. }
  1509. /**
  1510. * testSendWithLogAndScope method
  1511. *
  1512. * @return void
  1513. */
  1514. public function testSendWithLogAndScope()
  1515. {
  1516. $message = 'Logging This';
  1517. $log = $this->getMockBuilder('Cake\Log\Engine\BaseLog')
  1518. ->setMethods(['log'])
  1519. ->setConstructorArgs(['scopes' => ['email']])
  1520. ->getMock();
  1521. $log->expects($this->once())
  1522. ->method('log')
  1523. ->with(
  1524. 'debug',
  1525. $this->logicalAnd(
  1526. $this->stringContains($message),
  1527. $this->stringContains('cake@cakephp.org'),
  1528. $this->stringContains('me@cakephp.org')
  1529. )
  1530. );
  1531. Log::setConfig('email', $log);
  1532. $this->Email->setTransport('debug');
  1533. $this->Email->setTo('me@cakephp.org');
  1534. $this->Email->setFrom('cake@cakephp.org');
  1535. $this->Email->setSubject('My title');
  1536. $this->Email->setProfile(['log' => ['scope' => 'email']]);
  1537. $this->Email->send($message);
  1538. }
  1539. /**
  1540. * testSendRender method
  1541. *
  1542. * @return void
  1543. */
  1544. public function testSendRender()
  1545. {
  1546. $this->Email->reset();
  1547. $this->Email->setTransport('debug');
  1548. $this->Email->setFrom('cake@cakephp.org');
  1549. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1550. $this->Email->setSubject('My title');
  1551. $this->Email->setProfile(['empty']);
  1552. $this->Email->viewBuilder()->setTemplate('default', 'default');
  1553. $result = $this->Email->send();
  1554. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1555. $this->assertContains('Message-ID: ', $result['headers']);
  1556. $this->assertContains('To: ', $result['headers']);
  1557. }
  1558. /**
  1559. * test sending and rendering with no layout
  1560. *
  1561. * @return void
  1562. */
  1563. public function testSendRenderNoLayout()
  1564. {
  1565. $this->Email->reset();
  1566. $this->Email->setTransport('debug');
  1567. $this->Email->setFrom('cake@cakephp.org');
  1568. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1569. $this->Email->setSubject('My title');
  1570. $this->Email->setConfig(['empty']);
  1571. $this->Email->viewBuilder()
  1572. ->setTemplate('default')
  1573. ->setLayout(false);
  1574. $result = $this->Email->send('message body.');
  1575. $this->assertContains('message body.', $result['message']);
  1576. $this->assertNotContains('This email was sent using the CakePHP Framework', $result['message']);
  1577. }
  1578. /**
  1579. * testSendRender both method
  1580. *
  1581. * @return void
  1582. */
  1583. public function testSendRenderBoth()
  1584. {
  1585. $this->Email->reset();
  1586. $this->Email->setTransport('debug');
  1587. $this->Email->setFrom('cake@cakephp.org');
  1588. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1589. $this->Email->setSubject('My title');
  1590. $this->Email->setProfile(['empty']);
  1591. $this->Email->viewBuilder()->setTemplate('default', 'default');
  1592. $this->Email->setEmailFormat('both');
  1593. $result = $this->Email->send();
  1594. $this->assertContains('Message-ID: ', $result['headers']);
  1595. $this->assertContains('To: ', $result['headers']);
  1596. $boundary = $this->Email->getBoundary();
  1597. $this->assertContains('Content-Type: multipart/alternative; boundary="' . $boundary . '"', $result['headers']);
  1598. $expected = "--$boundary\r\n" .
  1599. "Content-Type: text/plain; charset=UTF-8\r\n" .
  1600. "Content-Transfer-Encoding: 8bit\r\n" .
  1601. "\r\n" .
  1602. "\r\n" .
  1603. "\r\n" .
  1604. 'This email was sent using the CakePHP Framework, https://cakephp.org.' .
  1605. "\r\n" .
  1606. "\r\n" .
  1607. "--$boundary\r\n" .
  1608. "Content-Type: text/html; charset=UTF-8\r\n" .
  1609. "Content-Transfer-Encoding: 8bit\r\n" .
  1610. "\r\n" .
  1611. '<!DOCTYPE html';
  1612. $this->assertStringStartsWith($expected, $result['message']);
  1613. $expected = "</html>\r\n" .
  1614. "\r\n" .
  1615. "\r\n" .
  1616. "--$boundary--\r\n";
  1617. $this->assertStringEndsWith($expected, $result['message']);
  1618. }
  1619. /**
  1620. * testSendRender method for ISO-2022-JP
  1621. *
  1622. * @return void
  1623. */
  1624. public function testSendRenderJapanese()
  1625. {
  1626. $this->Email->reset();
  1627. $this->Email->setTransport('debug');
  1628. $this->Email->setFrom('cake@cakephp.org');
  1629. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1630. $this->Email->setSubject('My title');
  1631. $this->Email->setProfile(['empty']);
  1632. $this->Email->viewBuilder()->setTemplate('default');
  1633. $this->Email->viewBuilder()->setLayout('japanese');
  1634. $this->Email->charset = 'ISO-2022-JP';
  1635. $result = $this->Email->send();
  1636. $expected = mb_convert_encoding('CakePHP Framework を使って送信したメールです。 https://cakephp.org.', 'ISO-2022-JP');
  1637. $this->assertContains($expected, $result['message']);
  1638. $this->assertContains('Message-ID: ', $result['headers']);
  1639. $this->assertContains('To: ', $result['headers']);
  1640. }
  1641. /**
  1642. * testSendRenderThemed method
  1643. *
  1644. * @return void
  1645. */
  1646. public function testSendRenderThemed()
  1647. {
  1648. $this->loadPlugins(['TestTheme']);
  1649. $this->Email->reset();
  1650. $this->Email->setTransport('debug');
  1651. $this->Email->setFrom('cake@cakephp.org');
  1652. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1653. $this->Email->setSubject('My title');
  1654. $this->Email->setProfile(['empty']);
  1655. $this->Email->viewBuilder()->setTheme('TestTheme');
  1656. $this->Email->viewBuilder()->setTemplate('themed', 'default');
  1657. $result = $this->Email->send();
  1658. $this->assertContains('In TestTheme', $result['message']);
  1659. $this->assertContains('/test_theme/img/test.jpg', $result['message']);
  1660. $this->assertContains('Message-ID: ', $result['headers']);
  1661. $this->assertContains('To: ', $result['headers']);
  1662. $this->assertContains('/test_theme/img/test.jpg', $result['message']);
  1663. $this->clearPlugins();
  1664. }
  1665. /**
  1666. * testSendRenderWithHTML method and assert line length is kept below the required limit
  1667. *
  1668. * @return void
  1669. */
  1670. public function testSendRenderWithHTML()
  1671. {
  1672. $this->Email->reset();
  1673. $this->Email->setTransport('debug');
  1674. $this->Email->setFrom('cake@cakephp.org');
  1675. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1676. $this->Email->setSubject('My title');
  1677. $this->Email->setProfile(['empty']);
  1678. $this->Email->setEmailFormat('html');
  1679. $this->Email->viewBuilder()->setTemplate('html', 'default');
  1680. $result = $this->Email->send();
  1681. $this->assertTextContains('<h1>HTML Ipsum Presents</h1>', $result['message']);
  1682. $this->assertLineLengths($result['message']);
  1683. }
  1684. /**
  1685. * testSendRenderWithVars method
  1686. *
  1687. * @return void
  1688. */
  1689. public function testSendRenderWithVars()
  1690. {
  1691. $this->Email->reset();
  1692. $this->Email->setTransport('debug');
  1693. $this->Email->setFrom('cake@cakephp.org');
  1694. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1695. $this->Email->setSubject('My title');
  1696. $this->Email->setProfile(['empty']);
  1697. $this->Email->viewBuilder()->setTemplate('custom', 'default');
  1698. $this->Email->setViewVars(['value' => 12345]);
  1699. $result = $this->Email->send();
  1700. $this->assertContains('Here is your value: 12345', $result['message']);
  1701. }
  1702. /**
  1703. * testSendRenderWithVars method for ISO-2022-JP
  1704. *
  1705. * @return void
  1706. */
  1707. public function testSendRenderWithVarsJapanese()
  1708. {
  1709. $this->Email->reset();
  1710. $this->Email->setTransport('debug');
  1711. $this->Email->setFrom('cake@cakephp.org');
  1712. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1713. $this->Email->setSubject('My title');
  1714. $this->Email->setProfile(['empty']);
  1715. $this->Email->viewBuilder()->setTemplate('japanese', 'default');
  1716. $this->Email->setViewVars(['value' => '日本語の差し込み123']);
  1717. $this->Email->charset = 'ISO-2022-JP';
  1718. $result = $this->Email->send();
  1719. $expected = mb_convert_encoding('ここにあなたの設定した値が入ります: 日本語の差し込み123', 'ISO-2022-JP');
  1720. $this->assertContains($expected, $result['message']);
  1721. }
  1722. /**
  1723. * testSendRenderWithHelpers method
  1724. *
  1725. * @return void
  1726. */
  1727. public function testSendRenderWithHelpers()
  1728. {
  1729. $this->Email->reset();
  1730. $this->Email->setTransport('debug');
  1731. $timestamp = time();
  1732. $this->Email->setFrom('cake@cakephp.org');
  1733. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1734. $this->Email->setSubject('My title');
  1735. $this->Email->setProfile(['empty']);
  1736. $this->Email->viewBuilder()
  1737. ->setTemplate('custom_helper')
  1738. ->setLayout('default')
  1739. ->setHelpers(['Time'], false);
  1740. $this->Email->setViewVars(['time' => $timestamp]);
  1741. $result = $this->Email->send();
  1742. $dateTime = new \DateTime();
  1743. $dateTime->setTimestamp($timestamp);
  1744. $this->assertContains('Right now: ' . $dateTime->format($dateTime::ATOM), $result['message']);
  1745. $result = $this->Email->viewBuilder()->getHelpers();
  1746. $this->assertEquals(['Time'], $result);
  1747. }
  1748. /**
  1749. * testSendRenderWithImage method
  1750. *
  1751. * @return void
  1752. */
  1753. public function testSendRenderWithImage()
  1754. {
  1755. $this->Email->reset();
  1756. $this->Email->setTransport('debug');
  1757. $this->Email->setFrom('cake@cakephp.org');
  1758. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1759. $this->Email->setSubject('My title');
  1760. $this->Email->setProfile(['empty']);
  1761. $this->Email->viewBuilder()->setTemplate('image');
  1762. $this->Email->setEmailFormat('html');
  1763. $server = env('SERVER_NAME') ? env('SERVER_NAME') : 'localhost';
  1764. if (env('SERVER_PORT') && env('SERVER_PORT') != 80) {
  1765. $server .= ':' . env('SERVER_PORT');
  1766. }
  1767. $expected = '<img src="http://' . $server . '/img/image.gif" alt="cool image" width="100" height="100"';
  1768. $result = $this->Email->send();
  1769. $this->assertContains($expected, $result['message']);
  1770. }
  1771. /**
  1772. * testSendRenderPlugin method
  1773. *
  1774. * @return void
  1775. */
  1776. public function testSendRenderPlugin()
  1777. {
  1778. $this->loadPlugins(['TestPlugin', 'TestPluginTwo', 'TestTheme']);
  1779. $this->Email->reset();
  1780. $this->Email->setTransport('debug');
  1781. $this->Email->setFrom('cake@cakephp.org');
  1782. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1783. $this->Email->setSubject('My title');
  1784. $this->Email->setProfile(['empty']);
  1785. $this->Email->viewBuilder()
  1786. ->setTemplate('TestPlugin.test_plugin_tpl')
  1787. ->setLayout('default');
  1788. $result = $this->Email->send();
  1789. $this->assertContains('Into TestPlugin.', $result['message']);
  1790. $this->assertContains('This email was sent using the CakePHP Framework', $result['message']);
  1791. $this->Email->viewBuilder()
  1792. ->setTemplate('TestPlugin.test_plugin_tpl')
  1793. ->setLayout('TestPlugin.plug_default');
  1794. $result = $this->Email->send();
  1795. $this->assertContains('Into TestPlugin.', $result['message']);
  1796. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1797. $this->Email->viewBuilder()
  1798. ->setTemplate('TestPlugin.test_plugin_tpl')
  1799. ->setLayout('plug_default');
  1800. $result = $this->Email->send();
  1801. $this->assertContains('Into TestPlugin.', $result['message']);
  1802. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1803. $this->Email->viewBuilder()
  1804. ->setTemplate('TestPlugin.test_plugin_tpl')
  1805. ->setLayout('TestPluginTwo.default');
  1806. $result = $this->Email->send();
  1807. $this->assertContains('Into TestPlugin.', $result['message']);
  1808. $this->assertContains('This email was sent using TestPluginTwo.', $result['message']);
  1809. // test plugin template overridden by theme
  1810. $this->Email->viewBuilder()->setTheme('TestTheme');
  1811. $result = $this->Email->send();
  1812. $this->assertContains('Into TestPlugin. (themed)', $result['message']);
  1813. $this->Email->setViewVars(['value' => 12345]);
  1814. $this->Email->viewBuilder()
  1815. ->setTemplate('custom')
  1816. ->setLayout('TestPlugin.plug_default');
  1817. $result = $this->Email->send();
  1818. $this->assertContains('Here is your value: 12345', $result['message']);
  1819. $this->assertContains('This email was sent using the TestPlugin.', $result['message']);
  1820. $this->clearPlugins();
  1821. }
  1822. /**
  1823. * Test that a MissingTemplateException is thrown
  1824. *
  1825. * @return void
  1826. * @expectedException \Cake\View\Exception\MissingTemplateException
  1827. */
  1828. public function testMissingTemplateException()
  1829. {
  1830. $this->Email->reset();
  1831. $this->Email->setTransport('debug');
  1832. $this->Email->setFrom('cake@cakephp.org');
  1833. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1834. $this->Email->setSubject('My title');
  1835. $this->Email->viewBuilder()->setTemplate('fooo');
  1836. $this->Email->send();
  1837. }
  1838. /**
  1839. * testSendMultipleMIME method
  1840. *
  1841. * @return void
  1842. */
  1843. public function testSendMultipleMIME()
  1844. {
  1845. $this->Email->reset();
  1846. $this->Email->setTransport('debug');
  1847. $this->Email->setFrom('cake@cakephp.org');
  1848. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1849. $this->Email->setSubject('My title');
  1850. $this->Email->viewBuilder()->setTemplate('custom', 'default');
  1851. $this->Email->setProfile([]);
  1852. $this->Email->setViewVars(['value' => 12345]);
  1853. $this->Email->setEmailFormat('both');
  1854. $this->Email->send();
  1855. $message = $this->Email->message();
  1856. $boundary = $this->Email->getBoundary();
  1857. $this->assertNotEmpty($boundary);
  1858. $this->assertContains('--' . $boundary, $message);
  1859. $this->assertContains('--' . $boundary . '--', $message);
  1860. $this->Email->setAttachments(['fake.php' => __FILE__]);
  1861. $this->Email->send();
  1862. $message = $this->Email->message();
  1863. $boundary = $this->Email->getBoundary();
  1864. $this->assertNotEmpty($boundary);
  1865. $this->assertContains('--' . $boundary, $message);
  1866. $this->assertContains('--' . $boundary . '--', $message);
  1867. $this->assertContains('--alt-' . $boundary, $message);
  1868. $this->assertContains('--alt-' . $boundary . '--', $message);
  1869. }
  1870. /**
  1871. * testSendAttachment method
  1872. *
  1873. * @return void
  1874. */
  1875. public function testSendAttachment()
  1876. {
  1877. $this->Email->reset();
  1878. $this->Email->setTransport('debug');
  1879. $this->Email->setFrom('cake@cakephp.org');
  1880. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1881. $this->Email->setSubject('My title');
  1882. $this->Email->setProfile([]);
  1883. $this->Email->setAttachments([CAKE . 'basics.php']);
  1884. $result = $this->Email->send('body');
  1885. $expected = "Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
  1886. "Content-Type: text/x-php\r\n" .
  1887. "Content-Transfer-Encoding: base64\r\n";
  1888. $this->assertContains($expected, $result['message']);
  1889. $this->Email->setAttachments(['my.file.txt' => CAKE . 'basics.php']);
  1890. $result = $this->Email->send('body');
  1891. $expected = "Content-Disposition: attachment; filename=\"my.file.txt\"\r\n" .
  1892. "Content-Type: text/x-php\r\n" .
  1893. "Content-Transfer-Encoding: base64\r\n";
  1894. $this->assertContains($expected, $result['message']);
  1895. $this->Email->setAttachments(['file.txt' => ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain']]);
  1896. $result = $this->Email->send('body');
  1897. $expected = "Content-Disposition: attachment; filename=\"file.txt\"\r\n" .
  1898. "Content-Type: text/plain\r\n" .
  1899. "Content-Transfer-Encoding: base64\r\n";
  1900. $this->assertContains($expected, $result['message']);
  1901. $this->Email->setAttachments(['file2.txt' => ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain', 'contentId' => 'a1b1c1']]);
  1902. $result = $this->Email->send('body');
  1903. $expected = "Content-Disposition: inline; filename=\"file2.txt\"\r\n" .
  1904. "Content-Type: text/plain\r\n" .
  1905. "Content-Transfer-Encoding: base64\r\n" .
  1906. "Content-ID: <a1b1c1>\r\n";
  1907. $this->assertContains($expected, $result['message']);
  1908. }
  1909. /**
  1910. * testDeliver method
  1911. *
  1912. * @return void
  1913. */
  1914. public function testDeliver()
  1915. {
  1916. $this->deprecated(function () {
  1917. Email::dropTransport('default');
  1918. Email::setConfigTransport('default', ['className' => 'Debug']);
  1919. });
  1920. $instance = Email::deliver('all@cakephp.org', 'About', 'Everything ok', ['from' => 'root@cakephp.org'], false);
  1921. $this->assertInstanceOf('Cake\Mailer\Email', $instance);
  1922. $this->assertSame($instance->getTo(), ['all@cakephp.org' => 'all@cakephp.org']);
  1923. $this->assertSame($instance->getSubject(), 'About');
  1924. $this->assertSame($instance->getFrom(), ['root@cakephp.org' => 'root@cakephp.org']);
  1925. $this->assertInstanceOf('Cake\Mailer\AbstractTransport', $instance->getTransport());
  1926. $config = [
  1927. 'from' => 'cake@cakephp.org',
  1928. 'to' => 'debug@cakephp.org',
  1929. 'subject' => 'Update ok',
  1930. 'template' => 'custom',
  1931. 'layout' => 'custom_layout',
  1932. 'viewVars' => ['value' => 123],
  1933. 'cc' => ['cake@cakephp.org' => 'Myself'],
  1934. ];
  1935. $instance = Email::deliver(null, null, ['name' => 'CakePHP'], $config, false);
  1936. $this->assertSame($instance->getFrom(), ['cake@cakephp.org' => 'cake@cakephp.org']);
  1937. $this->assertSame($instance->getTo(), ['debug@cakephp.org' => 'debug@cakephp.org']);
  1938. $this->assertSame($instance->getSubject(), 'Update ok');
  1939. $this->assertSame($instance->viewBuilder()->getTemplate(), 'custom');
  1940. $this->assertSame($instance->viewBuilder()->getLayout(), 'custom_layout');
  1941. $this->assertEquals($instance->getViewVars(), ['value' => 123, 'name' => 'CakePHP']);
  1942. $this->assertSame($instance->getCc(), ['cake@cakephp.org' => 'Myself']);
  1943. $configs = ['from' => 'root@cakephp.org', 'message' => 'Message from configs', 'transport' => 'debug'];
  1944. $instance = Email::deliver('all@cakephp.org', 'About', null, $configs, true);
  1945. $message = $instance->message();
  1946. $this->assertEquals($configs['message'], $message[0]);
  1947. }
  1948. /**
  1949. * testMessage method
  1950. *
  1951. * @return void
  1952. */
  1953. public function testMessage()
  1954. {
  1955. $this->Email->reset();
  1956. $this->Email->setTransport('debug');
  1957. $this->Email->setFrom('cake@cakephp.org');
  1958. $this->Email->setTo(['you@cakephp.org' => 'You']);
  1959. $this->Email->setSubject('My title');
  1960. $this->Email->setProfile(['empty']);
  1961. $this->Email->viewBuilder()->setTemplate('default', 'default');
  1962. $this->Email->setEmailFormat('both');
  1963. $this->Email->send();
  1964. $expected = '<p>This email was sent using the <a href="https://cakephp.org">CakePHP Framework</a></p>';
  1965. $this->assertContains($expected, $this->Email->message(Email::MESSAGE_HTML));
  1966. $expected = 'This email was sent using the CakePHP Framework, https://cakephp.org.';
  1967. $this->assertContains($expected, $this->Email->message(Email::MESSAGE_TEXT));
  1968. $message = $this->Email->message();
  1969. $this->assertContains('Content-Type: text/plain; charset=UTF-8', $message);
  1970. $this->assertContains('Content-Type: text/html; charset=UTF-8', $message);
  1971. // UTF-8 is 8bit
  1972. $this->assertTrue($this->_checkContentTransferEncoding($message, '8bit'));
  1973. $this->Email->charset = 'ISO-2022-JP';
  1974. $this->Email->send();
  1975. $message = $this->Email->message();
  1976. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $message);
  1977. $this->assertContains('Content-Type: text/html; charset=ISO-2022-JP', $message);
  1978. // ISO-2022-JP is 7bit
  1979. $this->assertTrue($this->_checkContentTransferEncoding($message, '7bit'));
  1980. }
  1981. /**
  1982. * testReset method
  1983. *
  1984. * @return void
  1985. */
  1986. public function testReset()
  1987. {
  1988. $this->Email->setTo('cake@cakephp.org');
  1989. $this->Email->viewBuilder()->setTheme('TestTheme');
  1990. $this->Email->setEmailPattern('/.+@.+\..+/i');
  1991. $this->assertSame(['cake@cakephp.org' => 'cake@cakephp.org'], $this->Email->getTo());
  1992. $this->Email->reset();
  1993. $this->assertSame([], $this->Email->getTo());
  1994. $this->assertFalse($this->Email->viewBuilder()->getTheme());
  1995. $this->assertSame(Email::EMAIL_PATTERN, $this->Email->getEmailPattern());
  1996. }
  1997. /**
  1998. * testReset with charset
  1999. *
  2000. * @return void
  2001. */
  2002. public function testResetWithCharset()
  2003. {
  2004. $this->Email->charset = 'ISO-2022-JP';
  2005. $this->Email->reset();
  2006. $this->assertSame('utf-8', $this->Email->charset, $this->Email->charset);
  2007. $this->assertNull($this->Email->headerCharset, $this->Email->headerCharset);
  2008. }
  2009. /**
  2010. * testWrap method
  2011. *
  2012. * @return void
  2013. */
  2014. public function testWrap()
  2015. {
  2016. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  2017. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  2018. $expected = [
  2019. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac turpis orci,',
  2020. 'non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.',
  2021. '',
  2022. ];
  2023. $this->assertSame($expected, $result);
  2024. $text = 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan amet.';
  2025. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  2026. $expected = [
  2027. 'Lorem ipsum dolor sit amet, consectetur < adipiscing elit. Donec ac turpis',
  2028. 'orci, non commodo odio. Morbi nibh nisi, vehicula > pellentesque accumsan',
  2029. 'amet.',
  2030. '',
  2031. ];
  2032. $this->assertSame($expected, $result);
  2033. $text = '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula pellentesque accumsan amet.<hr></p>';
  2034. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  2035. $expected = [
  2036. '<p>Lorem ipsum dolor sit amet,<br> consectetur adipiscing elit.<br> Donec ac',
  2037. 'turpis orci, non <b>commodo</b> odio. <br /> Morbi nibh nisi, vehicula',
  2038. 'pellentesque accumsan amet.<hr></p>',
  2039. '',
  2040. ];
  2041. $this->assertSame($expected, $result);
  2042. $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac <a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh nisi, vehicula pellentesque accumsan amet.';
  2043. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  2044. $expected = [
  2045. 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac',
  2046. '<a href="http://cakephp.org">turpis</a> orci, non commodo odio. Morbi nibh',
  2047. 'nisi, vehicula pellentesque accumsan amet.',
  2048. '',
  2049. ];
  2050. $this->assertSame($expected, $result);
  2051. $text = 'Lorem ipsum <a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">ok</a>';
  2052. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  2053. $expected = [
  2054. 'Lorem ipsum',
  2055. '<a href="http://www.cakephp.org/controller/action/param1/param2" class="nice cool fine amazing awesome">',
  2056. 'ok</a>',
  2057. '',
  2058. ];
  2059. $this->assertSame($expected, $result);
  2060. $text = 'Lorem ipsum withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite ok.';
  2061. $result = $this->Email->wrap($text, Email::LINE_LENGTH_SHOULD);
  2062. $expected = [
  2063. 'Lorem ipsum',
  2064. 'withonewordverybigMorethanthelineshouldsizeofrfcspecificationbyieeeavailableonieeesite',
  2065. 'ok.',
  2066. '',
  2067. ];
  2068. $this->assertSame($expected, $result);
  2069. }
  2070. /**
  2071. * testRender method
  2072. *
  2073. * @return void
  2074. */
  2075. public function testRenderWithLayoutAndAttachment()
  2076. {
  2077. $this->Email->setEmailFormat('html');
  2078. $this->Email->viewBuilder()->setTemplate('html', 'default');
  2079. $this->Email->setAttachments([CAKE . 'basics.php']);
  2080. $result = $this->Email->render([]);
  2081. $this->assertNotEmpty($result);
  2082. $result = $this->Email->getBoundary();
  2083. $this->assertRegExp('/^[0-9a-f]{32}$/', $result);
  2084. }
  2085. /**
  2086. * testConstructWithConfigArray method
  2087. *
  2088. * @return void
  2089. */
  2090. public function testConstructWithConfigArray()
  2091. {
  2092. $configs = [
  2093. 'from' => ['some@example.com' => 'My website'],
  2094. 'to' => 'test@example.com',
  2095. 'subject' => 'Test mail subject',
  2096. 'transport' => 'debug',
  2097. ];
  2098. $this->Email = new Email($configs);
  2099. $result = $this->Email->getTo();
  2100. $this->assertEquals([$configs['to'] => $configs['to']], $result);
  2101. $result = $this->Email->getFrom();
  2102. $this->assertEquals($configs['from'], $result);
  2103. $result = $this->Email->getSubject();
  2104. $this->assertEquals($configs['subject'], $result);
  2105. $result = $this->Email->getTransport();
  2106. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  2107. $result = $this->Email->send('This is the message');
  2108. $this->assertContains('Message-ID: ', $result['headers']);
  2109. $this->assertContains('To: ', $result['headers']);
  2110. }
  2111. /**
  2112. * testConfigArrayWithLayoutWithoutTemplate method
  2113. *
  2114. * @return void
  2115. */
  2116. public function testConfigArrayWithLayoutWithoutTemplate()
  2117. {
  2118. $configs = [
  2119. 'from' => ['some@example.com' => 'My website'],
  2120. 'to' => 'test@example.com',
  2121. 'subject' => 'Test mail subject',
  2122. 'transport' => 'debug',
  2123. 'layout' => 'custom',
  2124. ];
  2125. $this->Email = new Email($configs);
  2126. $template = $this->Email->viewBuilder()->getTemplate();
  2127. $layout = $this->Email->viewBuilder()->getLayout();
  2128. $this->assertEquals('', $template);
  2129. $this->assertEquals($configs['layout'], $layout);
  2130. }
  2131. /**
  2132. * testConstructWithConfigString method
  2133. *
  2134. * @return void
  2135. */
  2136. public function testConstructWithConfigString()
  2137. {
  2138. $configs = [
  2139. 'from' => ['some@example.com' => 'My website'],
  2140. 'to' => 'test@example.com',
  2141. 'subject' => 'Test mail subject',
  2142. 'transport' => 'debug',
  2143. ];
  2144. Email::setConfig('test', $configs);
  2145. $this->Email = new Email('test');
  2146. $result = $this->Email->getTo();
  2147. $this->assertEquals([$configs['to'] => $configs['to']], $result);
  2148. $result = $this->Email->getFrom();
  2149. $this->assertEquals($configs['from'], $result);
  2150. $result = $this->Email->getSubject();
  2151. $this->assertEquals($configs['subject'], $result);
  2152. $result = $this->Email->getTransport();
  2153. $this->assertInstanceOf('Cake\Mailer\Transport\DebugTransport', $result);
  2154. $result = $this->Email->send('This is the message');
  2155. $this->assertContains('Message-ID: ', $result['headers']);
  2156. $this->assertContains('To: ', $result['headers']);
  2157. }
  2158. /**
  2159. * testViewRender method
  2160. *
  2161. * @return void
  2162. */
  2163. public function testViewRender()
  2164. {
  2165. $result = $this->Email->getViewRenderer();
  2166. $this->assertEquals('Cake\View\View', $result);
  2167. $result = $this->Email->setViewRenderer('Cake\View\ThemeView');
  2168. $this->assertInstanceOf('Cake\Mailer\Email', $result);
  2169. $result = $this->Email->getViewRenderer();
  2170. $this->assertEquals('Cake\View\ThemeView', $result);
  2171. }
  2172. /**
  2173. * testEmailFormat method
  2174. *
  2175. * @return void
  2176. */
  2177. public function testEmailFormat()
  2178. {
  2179. $result = $this->Email->getEmailFormat();
  2180. $this->assertEquals('text', $result);
  2181. $result = $this->Email->setEmailFormat('html');
  2182. $this->assertInstanceOf('Cake\Mailer\Email', $result);
  2183. $result = $this->Email->getEmailFormat();
  2184. $this->assertEquals('html', $result);
  2185. $this->expectException(\InvalidArgumentException::class);
  2186. $result = $this->Email->setEmailFormat('invalid');
  2187. }
  2188. /**
  2189. * Tests that it is possible to add charset configuration to a CakeEmail object
  2190. *
  2191. * @return void
  2192. */
  2193. public function testConfigCharset()
  2194. {
  2195. $email = new Email();
  2196. $this->assertEquals(Configure::read('App.encoding'), $email->charset);
  2197. $this->assertEquals(Configure::read('App.encoding'), $email->headerCharset);
  2198. $email = new Email(['charset' => 'iso-2022-jp', 'headerCharset' => 'iso-2022-jp-ms']);
  2199. $this->assertEquals('iso-2022-jp', $email->charset);
  2200. $this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
  2201. $email = new Email(['charset' => 'iso-2022-jp']);
  2202. $this->assertEquals('iso-2022-jp', $email->charset);
  2203. $this->assertEquals('iso-2022-jp', $email->headerCharset);
  2204. $email = new Email(['headerCharset' => 'iso-2022-jp-ms']);
  2205. $this->assertEquals(Configure::read('App.encoding'), $email->charset);
  2206. $this->assertEquals('iso-2022-jp-ms', $email->headerCharset);
  2207. }
  2208. /**
  2209. * Tests that the header is encoded using the configured headerCharset
  2210. *
  2211. * @return void
  2212. */
  2213. public function testHeaderEncoding()
  2214. {
  2215. $email = new Email(['headerCharset' => 'iso-2022-jp-ms', 'transport' => 'debug']);
  2216. $email->setSubject('あれ?もしかしての前と');
  2217. $headers = $email->getHeaders(['subject']);
  2218. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2219. $this->assertContains($expected, $headers['Subject']);
  2220. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2221. $result = $email->send('ってテーブルを作ってやってたらう');
  2222. $this->assertContains('ってテーブルを作ってやってたらう', $result['message']);
  2223. }
  2224. /**
  2225. * Tests that the body is encoded using the configured charset
  2226. *
  2227. * @return void
  2228. */
  2229. public function testBodyEncoding()
  2230. {
  2231. $email = new Email([
  2232. 'charset' => 'iso-2022-jp',
  2233. 'headerCharset' => 'iso-2022-jp-ms',
  2234. 'transport' => 'debug',
  2235. ]);
  2236. $email->setSubject('あれ?もしかしての前と');
  2237. $headers = $email->getHeaders(['subject']);
  2238. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2239. $this->assertContains($expected, $headers['Subject']);
  2240. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2241. $result = $email->send('ってテーブルを作ってやってたらう');
  2242. $this->assertContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
  2243. $this->assertContains(mb_convert_encoding('ってテーブルを作ってやってたらう', 'ISO-2022-JP'), $result['message']);
  2244. }
  2245. /**
  2246. * Tests that the body is encoded using the configured charset (Japanese standard encoding)
  2247. *
  2248. * @return void
  2249. */
  2250. public function testBodyEncodingIso2022Jp()
  2251. {
  2252. $email = new Email([
  2253. 'charset' => 'iso-2022-jp',
  2254. 'headerCharset' => 'iso-2022-jp',
  2255. 'transport' => 'debug',
  2256. ]);
  2257. $email->setSubject('あれ?もしかしての前と');
  2258. $headers = $email->getHeaders(['subject']);
  2259. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2260. $this->assertContains($expected, $headers['Subject']);
  2261. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2262. $result = $email->send('①㈱');
  2263. $this->assertTextContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
  2264. $this->assertTextNotContains('Content-Type: text/plain; charset=ISO-2022-JP-MS', $result['headers']); // not charset=iso-2022-jp-ms
  2265. $this->assertTextNotContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
  2266. }
  2267. /**
  2268. * Tests that the body is encoded using the configured charset (Japanese irregular encoding, but sometime use this)
  2269. *
  2270. * @return void
  2271. */
  2272. public function testBodyEncodingIso2022JpMs()
  2273. {
  2274. $email = new Email([
  2275. 'charset' => 'iso-2022-jp-ms',
  2276. 'headerCharset' => 'iso-2022-jp-ms',
  2277. 'transport' => 'debug',
  2278. ]);
  2279. $email->setSubject('あれ?もしかしての前と');
  2280. $headers = $email->getHeaders(['subject']);
  2281. $expected = '?ISO-2022-JP?B?GyRCJCIkbCEpJGIkNyQrJDckRiROQTAkSBsoQg==?=';
  2282. $this->assertContains($expected, $headers['Subject']);
  2283. $email->setTo('someone@example.com')->setFrom('someone@example.com');
  2284. $result = $email->send('①㈱');
  2285. $this->assertTextContains('Content-Type: text/plain; charset=ISO-2022-JP', $result['headers']);
  2286. $this->assertTextNotContains('Content-Type: text/plain; charset=iso-2022-jp-ms', $result['headers']); // not charset=iso-2022-jp-ms
  2287. $this->assertContains(mb_convert_encoding('①㈱', 'ISO-2022-JP-MS'), $result['message']);
  2288. }
  2289. protected function _checkContentTransferEncoding($message, $charset)
  2290. {
  2291. $boundary = '--' . $this->Email->getBoundary();
  2292. $result['text'] = false;
  2293. $result['html'] = false;
  2294. $length = count($message);
  2295. for ($i = 0; $i < $length; ++$i) {
  2296. if ($message[$i] === $boundary) {
  2297. $flag = false;
  2298. $type = '';
  2299. while (!preg_match('/^$/', $message[$i])) {
  2300. if (preg_match('/^Content-Type: text\/plain/', $message[$i])) {
  2301. $type = 'text';
  2302. }
  2303. if (preg_match('/^Content-Type: text\/html/', $message[$i])) {
  2304. $type = 'html';
  2305. }
  2306. if ($message[$i] === 'Content-Transfer-Encoding: ' . $charset) {
  2307. $flag = true;
  2308. }
  2309. ++$i;
  2310. }
  2311. $result[$type] = $flag;
  2312. }
  2313. }
  2314. return $result['text'] && $result['html'];
  2315. }
  2316. /**
  2317. * Test CakeEmail::_encode function
  2318. *
  2319. * @return void
  2320. */
  2321. public function testEncode()
  2322. {
  2323. $this->Email->headerCharset = 'ISO-2022-JP';
  2324. $result = $this->Email->encode('日本語');
  2325. $expected = '=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=';
  2326. $this->assertSame($expected, $result);
  2327. $this->Email->headerCharset = 'ISO-2022-JP';
  2328. $result = $this->Email->encode('長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?');
  2329. $expected = "=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  2330. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  2331. ' =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=';
  2332. $this->assertSame($expected, $result);
  2333. }
  2334. /**
  2335. * Test CakeEmail::_decode function
  2336. *
  2337. * @return void
  2338. */
  2339. public function testDecode()
  2340. {
  2341. $this->Email->headerCharset = 'ISO-2022-JP';
  2342. $result = $this->Email->decode('=?ISO-2022-JP?B?GyRCRnxLXDhsGyhC?=');
  2343. $expected = '日本語';
  2344. $this->assertSame($expected, $result);
  2345. $this->Email->headerCharset = 'ISO-2022-JP';
  2346. $result = $this->Email->decode("=?ISO-2022-JP?B?GyRCRDkkJEQ5JCREOSQkGyhCU3ViamVjdBskQiROPmw5ZyRPGyhCZm9s?=\r\n" .
  2347. " =?ISO-2022-JP?B?ZGluZxskQiQ5JGskTiQsQDUkNyQkJHMkQCQxJEkkJCRDJD8kJCRJGyhC?=\r\n" .
  2348. ' =?ISO-2022-JP?B?GyRCJCYkSiRrJHMkQCRtJCYhKRsoQg==?=');
  2349. $expected = '長い長い長いSubjectの場合はfoldingするのが正しいんだけどいったいどうなるんだろう?';
  2350. $this->assertSame($expected, $result);
  2351. }
  2352. /**
  2353. * Tests charset setter/getter
  2354. *
  2355. * @return void
  2356. */
  2357. public function testCharset()
  2358. {
  2359. $this->Email->setCharset('UTF-8');
  2360. $this->assertSame($this->Email->getCharset(), 'UTF-8');
  2361. $this->Email->setCharset('ISO-2022-JP');
  2362. $this->assertSame($this->Email->getCharset(), 'ISO-2022-JP');
  2363. $charset = $this->Email->setCharset('Shift_JIS');
  2364. $this->assertSame('Shift_JIS', $charset->getCharset());
  2365. }
  2366. /**
  2367. * Tests headerCharset setter/getter
  2368. *
  2369. * @return void
  2370. */
  2371. public function testHeaderCharset()
  2372. {
  2373. $this->Email->setHeaderCharset('UTF-8');
  2374. $this->assertSame($this->Email->getHeaderCharset(), 'UTF-8');
  2375. $this->Email->setHeaderCharset('ISO-2022-JP');
  2376. $this->assertSame($this->Email->getHeaderCharset(), 'ISO-2022-JP');
  2377. $charset = $this->Email->setHeaderCharset('Shift_JIS');
  2378. $this->assertSame('Shift_JIS', $charset->getHeaderCharset());
  2379. }
  2380. /**
  2381. * Tests headerCharset on reset
  2382. *
  2383. * @return void
  2384. */
  2385. public function testHeaderCharsetReset()
  2386. {
  2387. $email = new Email(['headerCharset' => 'ISO-2022-JP']);
  2388. $email->reset();
  2389. $this->assertSame('utf-8', $email->getCharset());
  2390. $this->assertSame('utf-8', $email->getHeaderCharset());
  2391. }
  2392. /**
  2393. * Test transferEncoding
  2394. *
  2395. * @return void
  2396. */
  2397. public function testTransferEncoding()
  2398. {
  2399. // Test new transfer encoding
  2400. $expected = 'quoted-printable';
  2401. $this->Email->setTransferEncoding($expected);
  2402. $this->assertSame($expected, $this->Email->getTransferEncoding());
  2403. $this->assertSame($expected, $this->Email->getContentTransferEncoding());
  2404. // Test default charset/encoding : utf8/8bit
  2405. $expected = '8bit';
  2406. $this->Email->reset();
  2407. $this->assertNull($this->Email->getTransferEncoding());
  2408. $this->assertSame($expected, $this->Email->getContentTransferEncoding());
  2409. //Test wrong encoding
  2410. $this->expectException(\InvalidArgumentException::class);
  2411. $this->Email->setTransferEncoding('invalid');
  2412. }
  2413. /**
  2414. * Tests for compatible check.
  2415. * charset property and charset() method.
  2416. * headerCharset property and headerCharset() method.
  2417. *
  2418. * @return void
  2419. */
  2420. public function testCharsetsCompatible()
  2421. {
  2422. $checkHeaders = [
  2423. 'from' => true,
  2424. 'to' => true,
  2425. 'cc' => true,
  2426. 'subject' => true,
  2427. ];
  2428. // Header Charset : null (used by default UTF-8)
  2429. // Body Charset : ISO-2022-JP
  2430. $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', null);
  2431. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  2432. $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', null);
  2433. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  2434. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  2435. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  2436. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  2437. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  2438. // Header Charset : UTF-8
  2439. // Boby Charset : ISO-2022-JP
  2440. $oldStyleEmail = $this->_getEmailByOldStyleCharset('iso-2022-jp', 'utf-8');
  2441. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  2442. $newStyleEmail = $this->_getEmailByNewStyleCharset('iso-2022-jp', 'utf-8');
  2443. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  2444. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  2445. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  2446. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  2447. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  2448. // Header Charset : ISO-2022-JP
  2449. // Boby Charset : UTF-8
  2450. $oldStyleEmail = $this->_getEmailByOldStyleCharset('utf-8', 'iso-2022-jp');
  2451. $oldStyleHeaders = $oldStyleEmail->getHeaders($checkHeaders);
  2452. $newStyleEmail = $this->_getEmailByNewStyleCharset('utf-8', 'iso-2022-jp');
  2453. $newStyleHeaders = $newStyleEmail->getHeaders($checkHeaders);
  2454. $this->assertSame($oldStyleHeaders['From'], $newStyleHeaders['From']);
  2455. $this->assertSame($oldStyleHeaders['To'], $newStyleHeaders['To']);
  2456. $this->assertSame($oldStyleHeaders['Cc'], $newStyleHeaders['Cc']);
  2457. $this->assertSame($oldStyleHeaders['Subject'], $newStyleHeaders['Subject']);
  2458. }
  2459. /**
  2460. * @param mixed $charset
  2461. * @param mixed $headerCharset
  2462. * @return \Cake\Mailer\Email
  2463. */
  2464. protected function _getEmailByOldStyleCharset($charset, $headerCharset)
  2465. {
  2466. $email = new Email(['transport' => 'debug']);
  2467. if (! empty($charset)) {
  2468. $email->charset = $charset;
  2469. }
  2470. if (! empty($headerCharset)) {
  2471. $email->headerCharset = $headerCharset;
  2472. }
  2473. $email->setFrom('someone@example.com', 'どこかの誰か');
  2474. $email->setTo('someperson@example.jp', 'どこかのどなたか');
  2475. $email->setCc('miku@example.net', 'ミク');
  2476. $email->setSubject('テストメール');
  2477. $email->send('テストメールの本文');
  2478. return $email;
  2479. }
  2480. /**
  2481. * @param mixed $charset
  2482. * @param mixed $headerCharset
  2483. * @return \Cake\Mailer\Email
  2484. */
  2485. protected function _getEmailByNewStyleCharset($charset, $headerCharset)
  2486. {
  2487. $email = new Email(['transport' => 'debug']);
  2488. if (! empty($charset)) {
  2489. $email->setCharset($charset);
  2490. }
  2491. if (! empty($headerCharset)) {
  2492. $email->setHeaderCharset($headerCharset);
  2493. }
  2494. $email->setFrom('someone@example.com', 'どこかの誰か');
  2495. $email->setTo('someperson@example.jp', 'どこかのどなたか');
  2496. $email->setCc('miku@example.net', 'ミク');
  2497. $email->setSubject('テストメール');
  2498. $email->send('テストメールの本文');
  2499. return $email;
  2500. }
  2501. /**
  2502. * testWrapLongLine()
  2503. *
  2504. * @return void
  2505. */
  2506. public function testWrapLongLine()
  2507. {
  2508. $message = '<a href="http://cakephp.org">' . str_repeat('x', Email::LINE_LENGTH_MUST) . '</a>';
  2509. $this->Email->reset();
  2510. $this->Email->setTransport('debug');
  2511. $this->Email->setFrom('cake@cakephp.org');
  2512. $this->Email->setTo('cake@cakephp.org');
  2513. $this->Email->setSubject('Wordwrap Test');
  2514. $this->Email->setProfile(['empty']);
  2515. $result = $this->Email->send($message);
  2516. $expected = "<a\r\n" . 'href="http://cakephp.org">' . str_repeat('x', Email::LINE_LENGTH_MUST - 26) . "\r\n" .
  2517. str_repeat('x', 26) . "\r\n</a>\r\n\r\n";
  2518. $this->assertEquals($expected, $result['message']);
  2519. $this->assertLineLengths($result['message']);
  2520. $str1 = 'a ';
  2521. $str2 = ' b';
  2522. $length = strlen($str1) + strlen($str2);
  2523. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length - 1) . $str2;
  2524. $result = $this->Email->send($message);
  2525. $expected = "{$message}\r\n\r\n";
  2526. $this->assertEquals($expected, $result['message']);
  2527. $this->assertLineLengths($result['message']);
  2528. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length) . $str2;
  2529. $result = $this->Email->send($message);
  2530. $expected = "{$message}\r\n\r\n";
  2531. $this->assertEquals($expected, $result['message']);
  2532. $this->assertLineLengths($result['message']);
  2533. $message = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1) . $str2;
  2534. $result = $this->Email->send($message);
  2535. $expected = $str1 . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1) . sprintf("\r\n%s\r\n\r\n", trim($str2));
  2536. $this->assertEquals($expected, $result['message']);
  2537. $this->assertLineLengths($result['message']);
  2538. $line = 'some text <b>with html</b>';
  2539. $trailing = str_repeat('X', Email::LINE_LENGTH_MUST - strlen($line));
  2540. $result = $this->Email->send($line . $trailing);
  2541. $expected = 'some text <b>with' . "\r\nhtml</b>" . $trailing . "\r\n\r\n";
  2542. $this->assertEquals($expected, $result['message']);
  2543. }
  2544. /**
  2545. * testWrapWithTagsAcrossLines()
  2546. *
  2547. * @return void
  2548. */
  2549. public function testWrapWithTagsAcrossLines()
  2550. {
  2551. $str = <<<HTML
  2552. <table>
  2553. <th align="right" valign="top"
  2554. style="font-weight: bold">The tag is across multiple lines</th>
  2555. </table>
  2556. HTML;
  2557. $length = strlen($str);
  2558. $message = $str . str_repeat('x', Email::LINE_LENGTH_MUST + 1);
  2559. $this->Email->reset();
  2560. $this->Email->setTransport('debug');
  2561. $this->Email->setFrom('cake@cakephp.org');
  2562. $this->Email->setTo('cake@cakephp.org');
  2563. $this->Email->setSubject('Wordwrap Test');
  2564. $this->Email->setProfile(['empty']);
  2565. $result = $this->Email->send($message);
  2566. $message = str_replace("\r\n", "\n", substr($message, 0, -9));
  2567. $message = str_replace("\n", "\r\n", $message);
  2568. $expected = "{$message}\r\nxxxxxxxxx\r\n\r\n";
  2569. $this->assertEquals($expected, $result['message']);
  2570. $this->assertLineLengths($result['message']);
  2571. }
  2572. /**
  2573. * CakeEmailTest::testWrapIncludeLessThanSign()
  2574. *
  2575. * @return void
  2576. */
  2577. public function testWrapIncludeLessThanSign()
  2578. {
  2579. $str = 'foo<bar';
  2580. $length = strlen($str);
  2581. $message = $str . str_repeat('x', Email::LINE_LENGTH_MUST - $length + 1);
  2582. $this->Email->reset();
  2583. $this->Email->setTransport('debug');
  2584. $this->Email->setFrom('cake@cakephp.org');
  2585. $this->Email->setTo('cake@cakephp.org');
  2586. $this->Email->setSubject('Wordwrap Test');
  2587. $this->Email->setProfile(['empty']);
  2588. $result = $this->Email->send($message);
  2589. $message = substr($message, 0, -1);
  2590. $expected = "{$message}\r\nx\r\n\r\n";
  2591. $this->assertEquals($expected, $result['message']);
  2592. $this->assertLineLengths($result['message']);
  2593. }
  2594. /**
  2595. * CakeEmailTest::testWrapForJapaneseEncoding()
  2596. *
  2597. * @return void
  2598. */
  2599. public function testWrapForJapaneseEncoding()
  2600. {
  2601. $this->skipIf(!function_exists('mb_convert_encoding'));
  2602. $message = mb_convert_encoding('受け付けました', 'iso-2022-jp', 'UTF-8');
  2603. $this->Email->reset();
  2604. $this->Email->setTransport('debug');
  2605. $this->Email->setFrom('cake@cakephp.org');
  2606. $this->Email->setTo('cake@cakephp.org');
  2607. $this->Email->setSubject('Wordwrap Test');
  2608. $this->Email->setProfile(['empty']);
  2609. $this->Email->setCharset('iso-2022-jp');
  2610. $this->Email->setHeaderCharset('iso-2022-jp');
  2611. $result = $this->Email->send($message);
  2612. $expected = "{$message}\r\n\r\n";
  2613. $this->assertEquals($expected, $result['message']);
  2614. }
  2615. /**
  2616. * CakeEmailTest::testMockTransport()
  2617. */
  2618. public function testMockTransport()
  2619. {
  2620. $this->deprecated(function () {
  2621. Email::dropTransport('default');
  2622. });
  2623. $mock = $this->getMockBuilder('\Cake\Mailer\AbstractTransport')->getMock();
  2624. $config = ['from' => 'tester@example.org', 'transport' => 'default'];
  2625. Email::setConfig('default', $config);
  2626. $this->deprecated(function () use ($mock) {
  2627. Email::setConfigTransport('default', $mock);
  2628. });
  2629. $em = new Email('default');
  2630. $this->assertSame($mock, $em->getTransport());
  2631. }
  2632. /**
  2633. * testZeroOnlyLinesNotBeingEmptied()
  2634. *
  2635. * @return void
  2636. */
  2637. public function testZeroOnlyLinesNotBeingEmptied()
  2638. {
  2639. $message = "Lorem\r\n0\r\n0\r\nipsum";
  2640. $this->Email->reset();
  2641. $this->Email->setTransport('debug');
  2642. $this->Email->setFrom('cake@cakephp.org');
  2643. $this->Email->setTo('cake@cakephp.org');
  2644. $this->Email->setSubject('Wordwrap Test');
  2645. $result = $this->Email->send($message);
  2646. $expected = "{$message}\r\n\r\n";
  2647. $this->assertEquals($expected, $result['message']);
  2648. }
  2649. /**
  2650. * testJsonSerialize()
  2651. *
  2652. * @return void
  2653. */
  2654. public function testJsonSerialize()
  2655. {
  2656. $xmlstr = <<<XML
  2657. <?xml version='1.0' standalone='yes'?>
  2658. <framework>
  2659. <name>CakePHP</name>
  2660. <url>http://cakephp.org</url>
  2661. </framework>
  2662. XML;
  2663. $this->Email->reset()
  2664. ->setTo(['cakephp@cakephp.org' => 'CakePHP'])
  2665. ->setFrom('noreply@cakephp.org')
  2666. ->setReplyTo('cakephp@cakephp.org')
  2667. ->setCc(['mark@cakephp.org', 'juan@cakephp.org' => 'Juan Basso'])
  2668. ->setBcc('phpnut@cakephp.org')
  2669. ->setSubject('Test Serialize')
  2670. ->setMessageId('<uuid@server.com>')
  2671. ->setDomain('foo.bar')
  2672. ->setViewVars([
  2673. 'users' => $this->getTableLocator()->get('Users')->get(1, ['fields' => ['id', 'username']]),
  2674. 'xml' => new SimpleXmlElement($xmlstr),
  2675. 'exception' => new Exception('test'),
  2676. ])
  2677. ->setAttachments([
  2678. 'test.txt' => TEST_APP . 'config' . DS . 'empty.ini',
  2679. 'image' => [
  2680. 'data' => file_get_contents(TEST_APP . 'webroot' . DS . 'img' . DS . 'cake.icon.png'),
  2681. 'mimetype' => 'image/png',
  2682. ],
  2683. ]);
  2684. $this->Email->viewBuilder()
  2685. ->setTemplate('default')
  2686. ->setLayout('test');
  2687. $result = json_decode(json_encode($this->Email), true);
  2688. $this->assertContains('test', $result['viewVars']['exception']);
  2689. unset($result['viewVars']['exception']);
  2690. $encode = function ($path) {
  2691. return chunk_split(base64_encode(file_get_contents($path)), 76, "\r\n");
  2692. };
  2693. $expected = [
  2694. '_to' => ['cakephp@cakephp.org' => 'CakePHP'],
  2695. '_from' => ['noreply@cakephp.org' => 'noreply@cakephp.org'],
  2696. '_replyTo' => ['cakephp@cakephp.org' => 'cakephp@cakephp.org'],
  2697. '_cc' => ['mark@cakephp.org' => 'mark@cakephp.org', 'juan@cakephp.org' => 'Juan Basso'],
  2698. '_bcc' => ['phpnut@cakephp.org' => 'phpnut@cakephp.org'],
  2699. '_subject' => 'Test Serialize',
  2700. '_emailFormat' => 'text',
  2701. '_messageId' => '<uuid@server.com>',
  2702. '_domain' => 'foo.bar',
  2703. '_appCharset' => 'UTF-8',
  2704. 'charset' => 'utf-8',
  2705. 'headerCharset' => 'utf-8',
  2706. 'viewConfig' => [
  2707. '_template' => 'default',
  2708. '_layout' => 'test',
  2709. '_helpers' => ['Html'],
  2710. '_className' => 'Cake\View\View',
  2711. ],
  2712. 'viewVars' => [
  2713. 'users' => [
  2714. 'id' => 1,
  2715. 'username' => 'mariano',
  2716. ],
  2717. 'xml' => [
  2718. 'name' => 'CakePHP',
  2719. 'url' => 'http://cakephp.org',
  2720. ],
  2721. ],
  2722. '_attachments' => [
  2723. 'test.txt' => [
  2724. 'data' => $encode(TEST_APP . 'config' . DS . 'empty.ini'),
  2725. 'mimetype' => 'text/plain',
  2726. ],
  2727. 'image' => [
  2728. 'data' => $encode(TEST_APP . 'webroot' . DS . 'img' . DS . 'cake.icon.png'),
  2729. 'mimetype' => 'image/png',
  2730. ],
  2731. ],
  2732. '_emailPattern' => '/^((?:[\p{L}0-9.!#$%&\'*+\/=?^_`{|}~-]+)*@[\p{L}0-9-._]+)$/ui',
  2733. ];
  2734. $this->assertEquals($expected, $result);
  2735. $result = json_decode(json_encode(unserialize(serialize($this->Email))), true);
  2736. $this->assertContains('test', $result['viewVars']['exception']);
  2737. unset($result['viewVars']['exception']);
  2738. $this->assertEquals($expected, $result);
  2739. }
  2740. /**
  2741. * CakeEmailTest::assertLineLengths()
  2742. *
  2743. * @param string $message
  2744. * @return void
  2745. */
  2746. public function assertLineLengths($message)
  2747. {
  2748. $lines = explode("\r\n", $message);
  2749. foreach ($lines as $line) {
  2750. $this->assertTrue(
  2751. strlen($line) <= Email::LINE_LENGTH_MUST,
  2752. 'Line length exceeds the max. limit of Email::LINE_LENGTH_MUST'
  2753. );
  2754. }
  2755. }
  2756. /**
  2757. * Test deprecated methods
  2758. *
  2759. * @return void
  2760. */
  2761. public function testDeprecatedMethods()
  2762. {
  2763. $this->deprecated(function () {
  2764. $this->Email
  2765. ->setTemplate('foo')
  2766. ->setLayout('bar')
  2767. ->setTheme('baz')
  2768. ->setHelpers(['A', 'B']);
  2769. $this->assertSame('foo', $this->Email->getTemplate());
  2770. $this->assertSame('bar', $this->Email->getLayout());
  2771. $this->assertSame('baz', $this->Email->getTheme());
  2772. $this->assertSame(['A', 'B'], $this->Email->getHelpers());
  2773. $this->Email->setLayout('');
  2774. $this->assertFalse($this->Email->getLayout());
  2775. });
  2776. }
  2777. }