EmailLibTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. <?php
  2. App::uses('MyCakeTestCase', 'Tools.TestSuite');
  3. App::uses('EmailLib', 'Tools.Lib');
  4. //Configure::write('Config.admin_email', '...');
  5. class EmailLibTest extends MyCakeTestCase {
  6. public $Email;
  7. public $sendEmails = false;
  8. public function setUp() {
  9. parent::setUp();
  10. $this->skipIf(!file_exists(APP . 'Config' . DS . 'email.php'), 'no email.php');
  11. $this->Email = new TestEmailLib();
  12. }
  13. public function testObject() {
  14. $this->assertTrue(is_object($this->Email));
  15. $this->assertInstanceOf('EmailLib', $this->Email);
  16. }
  17. public function testSendDefault() {
  18. # start
  19. $this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  20. $this->Email->subject('Test Subject');
  21. $res = $this->Email->send('xyz xyz');
  22. # end
  23. if ($error = $this->Email->getError()) {
  24. $this->out($error);
  25. }
  26. $this->assertEquals('', $this->Email->getError());
  27. $this->assertTrue($res);
  28. $this->Email->resetAndSet();
  29. # start
  30. $this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  31. $this->Email->subject('Test Subject 2');
  32. $this->Email->template('default', 'internal');
  33. $this->Email->viewVars(array('x'=>'y', 'xx'=>'yy', 'text'=>''));
  34. $this->Email->addAttachments(array(APP.'webroot'.DS.'img'.DS.'icons'.DS.'edit.gif'));
  35. $res = $this->Email->send('xyz');
  36. # end
  37. if ($error = $this->Email->getError()) {
  38. $this->out($error);
  39. }
  40. $this->assertEquals('', $this->Email->getError());
  41. $this->assertTrue($res);
  42. }
  43. public function testSendFast() {
  44. //$this->Email->resetAndSet();
  45. //$this->Email->from(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  46. $res = EmailLib::systemEmail('system-mail test', 'some fast email to admin test');
  47. //debug($res);
  48. $this->assertTrue($res);
  49. }
  50. public function _testSendWithInlineAttachments() {
  51. $this->Email = new TestEmailLib();
  52. $this->Email->transport('debug');
  53. $this->Email->from('cake@cakephp.org');
  54. $this->Email->to('cake@cakephp.org');
  55. $this->Email->subject('My title');
  56. $this->Email->emailFormat('both');
  57. $result = $this->Email->send();
  58. //debug($result);
  59. $boundary = $this->Email->getBoundary();
  60. /*
  61. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  62. $expected = "--$boundary\r\n" .
  63. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  64. "\r\n" .
  65. "--rel-$boundary\r\n" .
  66. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  67. "\r\n" .
  68. "--alt-$boundary\r\n" .
  69. "Content-Type: text/plain; charset=UTF-8\r\n" .
  70. "Content-Transfer-Encoding: 8bit\r\n" .
  71. "\r\n" .
  72. "Hello" .
  73. "\r\n" .
  74. "\r\n" .
  75. "\r\n" .
  76. "--alt-$boundary\r\n" .
  77. "Content-Type: text/html; charset=UTF-8\r\n" .
  78. "Content-Transfer-Encoding: 8bit\r\n" .
  79. "\r\n" .
  80. "Hello" .
  81. "\r\n" .
  82. "\r\n" .
  83. "\r\n" .
  84. "--alt-{$boundary}--\r\n" .
  85. "\r\n" .
  86. "--rel-$boundary\r\n" .
  87. "Content-Type: application/octet-stream\r\n" .
  88. "Content-Transfer-Encoding: base64\r\n" .
  89. "Content-ID: <abc123>\r\n" .
  90. "Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
  91. $this->assertContains($expected, $result['message']);
  92. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  93. $this->assertContains('--' . $boundary . '--', $result['message']);
  94. */
  95. //debug($boundary);
  96. die();
  97. }
  98. /**
  99. * EmailLibTest::testAddAttachment()
  100. *
  101. * @return void
  102. */
  103. public function testAddAttachment() {
  104. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  105. $this->assertTrue(file_exists($file));
  106. $this->Email->addAttachment($file);
  107. $res = $this->Email->getProtected('attachments');
  108. $expected = array(
  109. 'hotel.png' => array(
  110. 'file' => $file,
  111. 'mimetype' => 'image/png',
  112. )
  113. );
  114. $this->assertEquals($expected, $res);
  115. $this->Email->addAttachment($file, 'my_image.jpg');
  116. $res = $this->Email->getProtected('attachments');
  117. $expected = array(
  118. 'file' => $file,
  119. 'mimetype' => 'image/jpeg',
  120. );
  121. $this->assertEquals($expected, $res['my_image.jpg']);
  122. }
  123. /**
  124. * EmailLibTest::testAddAttachment()
  125. *
  126. * @return void
  127. */
  128. public function testAddAttachmentSend() {
  129. $this->skipIf(!$this->sendEmails);
  130. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  131. $this->assertTrue(file_exists($file));
  132. Configure::write('debug', 0);
  133. $this->Email->to(Configure::read('Config.admin_email'));
  134. $this->Email->addAttachment($file);
  135. $res = $this->Email->send('test_default', 'internal');
  136. if ($error = $this->Email->getError()) {
  137. $this->out($error);
  138. }
  139. $this->assertEquals('', $this->Email->getError());
  140. $this->assertTrue($res);
  141. $this->Email->resetAndSet();
  142. $this->Email->to(Configure::read('Config.admin_email'));
  143. $this->Email->addAttachment($file, 'x.jpg');
  144. $res = $this->Email->send('test_custom_filename');
  145. Configure::write('debug', 2);
  146. $this->assertEquals('', $this->Email->getError());
  147. $this->assertTrue($res);
  148. }
  149. /**
  150. * EmailLibTest::testAddBlobAttachment()
  151. *
  152. * @return void
  153. */
  154. public function testAddBlobAttachment() {
  155. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  156. $content = file_get_contents($file);
  157. $this->Email->addBlobAttachment($content, 'hotel.png');
  158. $res = $this->Email->getProtected('attachments');
  159. $expected = array(
  160. 'hotel.png' => array(
  161. 'content' => $content,
  162. 'mimetype' => 'image/png',
  163. )
  164. );
  165. $this->assertEquals($expected, $res);
  166. $this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
  167. $res = $this->Email->getProtected('attachments');
  168. $expected = array(
  169. 'content' => $content,
  170. 'mimetype' => 'image/jpeg',
  171. );
  172. $this->assertEquals($expected, $res['hotel.gif']);#
  173. $this->assertSame(2, count($res));
  174. }
  175. /**
  176. * EmailLibTest::testAddEmbeddedAttachment()
  177. *
  178. * @return void
  179. */
  180. public function testAddEmbeddedAttachment() {
  181. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  182. $this->assertTrue(file_exists($file));
  183. $this->Email = new TestEmailLib();
  184. $this->Email->emailFormat('both');
  185. $cid = $this->Email->addEmbeddedAttachment($file);
  186. $cid2 = $this->Email->addEmbeddedAttachment($file);
  187. $this->assertSame($cid, $cid2);
  188. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  189. $res = $this->Email->getProtected('attachments');
  190. $expected = array(
  191. 'hotel.png' => array(
  192. 'file' => $file,
  193. 'mimetype' => 'image/png; charset=binary',
  194. 'contentId' => $cid
  195. )
  196. );
  197. $this->assertSame($expected, $res);
  198. }
  199. /**
  200. * html email
  201. */
  202. public function testAddEmbeddedAttachmentSend() {
  203. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  204. Configure::write('debug', 0);
  205. $this->Email = new TestEmailLib();
  206. $this->Email->emailFormat('both');
  207. $this->Email->to(Configure::read('Config.admin_email'));
  208. $cid = $this->Email->addEmbeddedAttachment($file);
  209. $cid2 = $this->Email->addEmbeddedAttachment($file);
  210. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  211. $html = '<head>
  212. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  213. <meta name="author" content="ohyeah" />
  214. <title>Untitled 6</title>
  215. </head>
  216. <body>
  217. test_embedded_default äöü <img src="cid:'.$cid.'" /> end
  218. another image <img src="cid:'.$cid2.'" /> end
  219. html-part
  220. </body>
  221. </html>';
  222. $text = trim(strip_tags($html));
  223. $this->Email->viewVars(compact('text', 'html'));
  224. if (!$this->sendEmails) {
  225. Configure::write('debug', 2);
  226. }
  227. $this->skipIf(!$this->sendEmails);
  228. $res = $this->Email->send();
  229. Configure::write('debug', 2);
  230. if ($error = $this->Email->getError()) {
  231. $this->out($error);
  232. }
  233. $this->assertEquals('', $this->Email->getError());
  234. $this->assertTrue($res);
  235. }
  236. /**
  237. * EmailLibTest::testAddEmbeddedBlobAttachment()
  238. *
  239. * @return void
  240. */
  241. public function testAddEmbeddedBlobAttachment() {
  242. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  243. $this->assertTrue(file_exists($file));
  244. $this->Email = new TestEmailLib();
  245. $this->Email->emailFormat('both');
  246. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  247. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  248. $res = $this->Email->getProtected('attachments');
  249. $expected = array(
  250. 'my_hotel.png' => array(
  251. 'content' => file_get_contents($file),
  252. 'mimetype' => 'image/png',
  253. 'contentId' => $cid,
  254. )
  255. );
  256. $this->assertEquals($expected, $res);
  257. $options = array(
  258. 'contentDisposition' => true,
  259. );
  260. $cid = 'abcdef';
  261. $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
  262. $res = $this->Email->getProtected('attachments');
  263. $expected = array(
  264. 'contentDisposition' => true,
  265. 'content' => file_get_contents($file),
  266. 'mimetype' => 'image/jpeg',
  267. 'contentId' => $cid,
  268. );
  269. $this->assertEquals($expected, $res['my_other_hotel.png']);
  270. }
  271. public function testValidates() {
  272. $res = $this->Email->validates();
  273. $this->assertFalse($res);
  274. $this->Email->subject('foo');
  275. $res = $this->Email->validates();
  276. $this->assertFalse($res);
  277. $this->Email->to('some@web.de');
  278. $res = $this->Email->validates();
  279. $this->assertTrue($res);
  280. }
  281. /**
  282. * html email
  283. */
  284. public function testAddEmbeddedBlobAttachmentSend() {
  285. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  286. Configure::write('debug', 0);
  287. $this->Email = new TestEmailLib();
  288. $this->Email->emailFormat('both');
  289. $this->Email->to(Configure::read('Config.admin_email'));
  290. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
  291. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  292. $html = '<head>
  293. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  294. <meta name="author" content="ohyeah" />
  295. <title>Untitled 6</title>
  296. </head>
  297. <body>
  298. test_embedded_blob_default äöü <img src="cid:'.$cid.'" /> end
  299. html-part
  300. </body>
  301. </html>';
  302. $text = trim(strip_tags($html));
  303. $this->Email->viewVars(compact('text', 'html'));
  304. if (!$this->sendEmails) {
  305. Configure::write('debug', 2);
  306. }
  307. $this->skipIf(!$this->sendEmails);
  308. $res = $this->Email->send();
  309. Configure::write('debug', 2);
  310. if ($error = $this->Email->getError()) {
  311. $this->out($error);
  312. }
  313. $this->assertEquals('', $this->Email->getError());
  314. $this->assertTrue($res);
  315. }
  316. public function _testComplexeHtmlWithEmbeddedImages() {
  317. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  318. $this->assertTrue(file_exists($file));
  319. //TODO
  320. }
  321. public function testWrapLongEmailContent() {
  322. $this->Email = new TestEmailLib();
  323. $html = <<<HTML
  324. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  325. <html><head></head><body style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px; text-align: left; vertical-align: top; margin: 0;">
  326. sjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsf
  327. </body></html>
  328. HTML;
  329. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  330. $is = $this->Email->wrap($html);
  331. foreach ($is as $line => $content) {
  332. $this->assertTrue(strlen($content) <= EmailLib::LINE_LENGTH_MUST);
  333. }
  334. $this->debug($is);
  335. $this->assertTrue(count($is) >= 5);
  336. }
  337. public function testWrapCustomized() {
  338. $this->Email = new TestEmailLib();
  339. $html = <<<HTML
  340. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  341. <html><head></head><body style="color: #000000; font-family: Arial, Helvetica, sans-serif; font-size: 12px; line-height: 16px; text-align: left; vertical-align: top; margin: 0;">
  342. sjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsfsjdf ojdshfdsf odsfh dsfhodsf hodshfhdsjdshfjdshfjdshfj dsjfh jdsfh ojds hfposjdf pohpojds fojds hfojds fpojds foijds fpodsij fojdsnhfojdshf dsufhpodsufds fuds foudshf ouds hfoudshf udsofhuds hfouds hfouds hfoudshf udsh fouhds fluds hflsdu hflsud hfuldsuhf dsf
  343. </body></html>
  344. HTML;
  345. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  346. $this->Email->wrapLength(100);
  347. $is = $this->Email->wrap($html);
  348. foreach ($is as $line => $content) {
  349. $this->assertTrue(strlen($content) <= EmailLib::LINE_LENGTH_MUST);
  350. }
  351. $this->debug($is);
  352. $this->assertTrue(count($is) >= 16);
  353. }
  354. }
  355. /**
  356. * Help to test EmailLib
  357. *
  358. */
  359. class TestEmailLib extends EmailLib {
  360. /**
  361. * Wrap to protected method
  362. *
  363. */
  364. public function formatAddress($address) {
  365. return parent::_formatAddress($address);
  366. }
  367. /**
  368. * Wrap to protected method
  369. *
  370. */
  371. public function wrap($text) {
  372. return parent::_wrap($text);
  373. }
  374. /**
  375. * Get the boundary attribute
  376. *
  377. * @return string
  378. */
  379. public function getBoundary() {
  380. return $this->_boundary;
  381. }
  382. public function getProtected($attribute) {
  383. $attribute = '_' . $attribute;
  384. return $this->$attribute;
  385. }
  386. /**
  387. * Encode to protected method
  388. *
  389. */
  390. public function encode($text) {
  391. return $this->_encode($text);
  392. }
  393. }