EmailLibTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 testXMailer() {
  51. $this->Email = new TestEmailLib();
  52. $this->Email->from('cake@cakephp.org');
  53. $this->Email->to('cake@cakephp.org');
  54. $this->Email->subject('My title');
  55. $this->Email->emailFormat('both');
  56. $result = $this->Email->send();
  57. $this->assertTrue($result);
  58. $result = $this->Email->getDebug();
  59. $this->assertTextContains('X-Mailer: CakePHP Email', $result['headers']);
  60. Configure::write('Config.x-mailer', 'Tools Plugin');
  61. $this->Email = new TestEmailLib();
  62. $this->Email->from('cake@cakephp.org');
  63. $this->Email->to('cake@cakephp.org');
  64. $this->Email->subject('My title');
  65. $this->Email->emailFormat('both');
  66. $result = $this->Email->send();
  67. $this->assertTrue($result);
  68. $result = $this->Email->getDebug();
  69. $this->assertTextNotContains('X-Mailer: CakePHP Email', $result['headers']);
  70. $this->assertTextContains('X-Mailer: Tools Plugin', $result['headers']);
  71. }
  72. public function _testSendWithInlineAttachments() {
  73. $this->Email = new TestEmailLib();
  74. $this->Email->transport('debug');
  75. $this->Email->from('cake@cakephp.org');
  76. $this->Email->to('cake@cakephp.org');
  77. $this->Email->subject('My title');
  78. $this->Email->emailFormat('both');
  79. $result = $this->Email->send();
  80. //debug($result);
  81. $boundary = $this->Email->getBoundary();
  82. /*
  83. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  84. $expected = "--$boundary\r\n" .
  85. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  86. "\r\n" .
  87. "--rel-$boundary\r\n" .
  88. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  89. "\r\n" .
  90. "--alt-$boundary\r\n" .
  91. "Content-Type: text/plain; charset=UTF-8\r\n" .
  92. "Content-Transfer-Encoding: 8bit\r\n" .
  93. "\r\n" .
  94. "Hello" .
  95. "\r\n" .
  96. "\r\n" .
  97. "\r\n" .
  98. "--alt-$boundary\r\n" .
  99. "Content-Type: text/html; charset=UTF-8\r\n" .
  100. "Content-Transfer-Encoding: 8bit\r\n" .
  101. "\r\n" .
  102. "Hello" .
  103. "\r\n" .
  104. "\r\n" .
  105. "\r\n" .
  106. "--alt-{$boundary}--\r\n" .
  107. "\r\n" .
  108. "--rel-$boundary\r\n" .
  109. "Content-Type: application/octet-stream\r\n" .
  110. "Content-Transfer-Encoding: base64\r\n" .
  111. "Content-ID: <abc123>\r\n" .
  112. "Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
  113. $this->assertContains($expected, $result['message']);
  114. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  115. $this->assertContains('--' . $boundary . '--', $result['message']);
  116. */
  117. //debug($boundary);
  118. die();
  119. }
  120. /**
  121. * EmailLibTest::testAddAttachment()
  122. *
  123. * @return void
  124. */
  125. public function testAddAttachment() {
  126. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  127. $this->assertTrue(file_exists($file));
  128. $this->Email->addAttachment($file);
  129. $res = $this->Email->getProtected('attachments');
  130. $expected = array(
  131. 'hotel.png' => array(
  132. 'file' => $file,
  133. 'mimetype' => 'image/png',
  134. )
  135. );
  136. $this->assertEquals($expected, $res);
  137. $this->Email->addAttachment($file, 'my_image.jpg');
  138. $res = $this->Email->getProtected('attachments');
  139. $expected = array(
  140. 'file' => $file,
  141. 'mimetype' => 'image/jpeg',
  142. );
  143. $this->assertEquals($expected, $res['my_image.jpg']);
  144. }
  145. /**
  146. * EmailLibTest::testAddAttachment()
  147. *
  148. * @return void
  149. */
  150. public function testAddAttachmentSend() {
  151. $this->skipIf(!$this->sendEmails);
  152. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  153. $this->assertTrue(file_exists($file));
  154. Configure::write('debug', 0);
  155. $this->Email->to(Configure::read('Config.admin_email'));
  156. $this->Email->addAttachment($file);
  157. $res = $this->Email->send('test_default', 'internal');
  158. if ($error = $this->Email->getError()) {
  159. $this->out($error);
  160. }
  161. $this->assertEquals('', $this->Email->getError());
  162. $this->assertTrue($res);
  163. $this->Email->resetAndSet();
  164. $this->Email->to(Configure::read('Config.admin_email'));
  165. $this->Email->addAttachment($file, 'x.jpg');
  166. $res = $this->Email->send('test_custom_filename');
  167. Configure::write('debug', 2);
  168. $this->assertEquals('', $this->Email->getError());
  169. $this->assertTrue($res);
  170. }
  171. /**
  172. * EmailLibTest::testAddBlobAttachment()
  173. *
  174. * @return void
  175. */
  176. public function testAddBlobAttachment() {
  177. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  178. $content = file_get_contents($file);
  179. $this->Email->addBlobAttachment($content, 'hotel.png');
  180. $res = $this->Email->getProtected('attachments');
  181. $expected = array(
  182. 'hotel.png' => array(
  183. 'content' => $content,
  184. 'mimetype' => 'image/png',
  185. )
  186. );
  187. $this->assertEquals($expected, $res);
  188. $this->Email->addBlobAttachment($content, 'hotel.gif', 'image/jpeg');
  189. $res = $this->Email->getProtected('attachments');
  190. $expected = array(
  191. 'content' => $content,
  192. 'mimetype' => 'image/jpeg',
  193. );
  194. $this->assertEquals($expected, $res['hotel.gif']);#
  195. $this->assertSame(2, count($res));
  196. }
  197. /**
  198. * EmailLibTest::testAddEmbeddedAttachment()
  199. *
  200. * @return void
  201. */
  202. public function testAddEmbeddedAttachment() {
  203. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  204. $this->assertTrue(file_exists($file));
  205. $this->Email = new TestEmailLib();
  206. $this->Email->emailFormat('both');
  207. $cid = $this->Email->addEmbeddedAttachment($file);
  208. $cid2 = $this->Email->addEmbeddedAttachment($file);
  209. $this->assertSame($cid, $cid2);
  210. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  211. $res = $this->Email->getProtected('attachments');
  212. $expected = array(
  213. 'hotel.png' => array(
  214. 'file' => $file,
  215. 'mimetype' => 'image/png; charset=binary',
  216. 'contentId' => $cid
  217. )
  218. );
  219. $this->assertSame($expected, $res);
  220. }
  221. /**
  222. * html email
  223. */
  224. public function testAddEmbeddedAttachmentSend() {
  225. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  226. Configure::write('debug', 0);
  227. $this->Email = new TestEmailLib();
  228. $this->Email->emailFormat('both');
  229. $this->Email->to(Configure::read('Config.admin_email'));
  230. $cid = $this->Email->addEmbeddedAttachment($file);
  231. $cid2 = $this->Email->addEmbeddedAttachment($file);
  232. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  233. $html = '<head>
  234. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  235. <meta name="author" content="ohyeah" />
  236. <title>Untitled 6</title>
  237. </head>
  238. <body>
  239. test_embedded_default äöü <img src="cid:'.$cid.'" /> end
  240. another image <img src="cid:'.$cid2.'" /> end
  241. html-part
  242. </body>
  243. </html>';
  244. $text = trim(strip_tags($html));
  245. $this->Email->viewVars(compact('text', 'html'));
  246. if (!$this->sendEmails) {
  247. Configure::write('debug', 2);
  248. }
  249. $this->skipIf(!$this->sendEmails);
  250. $res = $this->Email->send();
  251. Configure::write('debug', 2);
  252. if ($error = $this->Email->getError()) {
  253. $this->out($error);
  254. }
  255. $this->assertEquals('', $this->Email->getError());
  256. $this->assertTrue($res);
  257. }
  258. /**
  259. * EmailLibTest::testAddEmbeddedBlobAttachment()
  260. *
  261. * @return void
  262. */
  263. public function testAddEmbeddedBlobAttachment() {
  264. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  265. $this->assertTrue(file_exists($file));
  266. $this->Email = new TestEmailLib();
  267. $this->Email->emailFormat('both');
  268. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png');
  269. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  270. $res = $this->Email->getProtected('attachments');
  271. $expected = array(
  272. 'my_hotel.png' => array(
  273. 'content' => file_get_contents($file),
  274. 'mimetype' => 'image/png',
  275. 'contentId' => $cid,
  276. )
  277. );
  278. $this->assertEquals($expected, $res);
  279. $options = array(
  280. 'contentDisposition' => true,
  281. );
  282. $cid = 'abcdef';
  283. $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_other_hotel.png', 'image/jpeg', $cid, $options);
  284. $res = $this->Email->getProtected('attachments');
  285. $expected = array(
  286. 'contentDisposition' => true,
  287. 'content' => file_get_contents($file),
  288. 'mimetype' => 'image/jpeg',
  289. 'contentId' => $cid,
  290. );
  291. $this->assertEquals($expected, $res['my_other_hotel.png']);
  292. }
  293. public function testValidates() {
  294. $res = $this->Email->validates();
  295. $this->assertFalse($res);
  296. $this->Email->subject('foo');
  297. $res = $this->Email->validates();
  298. $this->assertFalse($res);
  299. $this->Email->to('some@web.de');
  300. $res = $this->Email->validates();
  301. $this->assertTrue($res);
  302. }
  303. /**
  304. * html email
  305. */
  306. public function testAddEmbeddedBlobAttachmentSend() {
  307. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  308. Configure::write('debug', 0);
  309. $this->Email = new TestEmailLib();
  310. $this->Email->emailFormat('both');
  311. $this->Email->to(Configure::read('Config.admin_email'));
  312. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'image/png');
  313. $this->assertContains('@' . env('HTTP_HOST'), $cid);
  314. $html = '<head>
  315. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  316. <meta name="author" content="ohyeah" />
  317. <title>Untitled 6</title>
  318. </head>
  319. <body>
  320. test_embedded_blob_default äöü <img src="cid:'.$cid.'" /> end
  321. html-part
  322. </body>
  323. </html>';
  324. $text = trim(strip_tags($html));
  325. $this->Email->viewVars(compact('text', 'html'));
  326. if (!$this->sendEmails) {
  327. Configure::write('debug', 2);
  328. }
  329. $this->skipIf(!$this->sendEmails);
  330. $res = $this->Email->send();
  331. Configure::write('debug', 2);
  332. if ($error = $this->Email->getError()) {
  333. $this->out($error);
  334. }
  335. $this->assertEquals('', $this->Email->getError());
  336. $this->assertTrue($res);
  337. }
  338. public function _testComplexeHtmlWithEmbeddedImages() {
  339. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  340. $this->assertTrue(file_exists($file));
  341. //TODO
  342. }
  343. public function testWrapLongEmailContent() {
  344. $this->Email = new TestEmailLib();
  345. $html = <<<HTML
  346. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  347. <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;">
  348. 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
  349. </body></html>
  350. HTML;
  351. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  352. $is = $this->Email->wrap($html);
  353. foreach ($is as $line => $content) {
  354. $this->assertTrue(strlen($content) <= EmailLib::LINE_LENGTH_MUST);
  355. }
  356. $this->debug($is);
  357. $this->assertTrue(count($is) >= 5);
  358. }
  359. public function testWrapCustomized() {
  360. $this->Email = new TestEmailLib();
  361. $html = <<<HTML
  362. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  363. <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;">
  364. 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
  365. </body></html>
  366. HTML;
  367. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  368. $this->Email->wrapLength(100);
  369. $is = $this->Email->wrap($html);
  370. foreach ($is as $line => $content) {
  371. $this->assertTrue(strlen($content) <= EmailLib::LINE_LENGTH_MUST);
  372. }
  373. $this->debug($is);
  374. $this->assertTrue(count($is) >= 16);
  375. }
  376. }
  377. /**
  378. * Help to test EmailLib
  379. *
  380. */
  381. class TestEmailLib extends EmailLib {
  382. /**
  383. * Wrap to protected method
  384. *
  385. */
  386. public function formatAddress($address) {
  387. return parent::_formatAddress($address);
  388. }
  389. /**
  390. * Wrap to protected method
  391. *
  392. */
  393. public function wrap($text) {
  394. return parent::_wrap($text);
  395. }
  396. /**
  397. * Get the boundary attribute
  398. *
  399. * @return string
  400. */
  401. public function getBoundary() {
  402. return $this->_boundary;
  403. }
  404. public function getProtected($attribute) {
  405. $attribute = '_' . $attribute;
  406. return $this->$attribute;
  407. }
  408. /**
  409. * Encode to protected method
  410. *
  411. */
  412. public function encode($text) {
  413. return $this->_encode($text);
  414. }
  415. }