EmailTest.php 100 KB

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