EmailLibTest.php 17 KB

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