EmailTest.php 98 KB

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