EmailLibTest.php 16 KB

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