EmailTest.php 100 KB

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