EmailTest.php 98 KB

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