EmailLibTest.php 16 KB

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