EmailTest.php 99 KB

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