EmailTest.php 100 KB

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