EmailLibTest.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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 EmailLib();
  12. }
  13. public function testObject() {
  14. $this->assertTrue(is_object($this->Email));
  15. $this->assertTrue(is_a($this->Email, 'EmailLib'));
  16. ;
  17. }
  18. public function testSendDefault() {
  19. # start
  20. $this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  21. $this->Email->subject('Test Subject');
  22. $res = $this->Email->send('xyz xyz');
  23. # end
  24. if ($error = $this->Email->getError()) {
  25. $this->out($error);
  26. }
  27. $this->assertEquals('', $this->Email->getError());
  28. $this->assertTrue($res);
  29. $this->Email->resetAndSet();
  30. # start
  31. $this->Email->to(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  32. $this->Email->subject('Test Subject 2');
  33. $this->Email->template('default', 'internal');
  34. $this->Email->viewVars(array('x'=>'y', 'xx'=>'yy', 'text'=>''));
  35. $this->Email->addAttachments(array(APP.'webroot'.DS.'img'.DS.'icons'.DS.'edit.gif'));
  36. $res = $this->Email->send('xyz');
  37. # end
  38. if ($error = $this->Email->getError()) {
  39. $this->out($error);
  40. }
  41. $this->assertEquals('', $this->Email->getError());
  42. $this->assertTrue($res);
  43. }
  44. public function testSendFast() {
  45. //$this->Email->resetAndSet();
  46. //$this->Email->from(Configure::read('Config.admin_email'), Configure::read('Config.admin_emailname'));
  47. $res = EmailLib::systemEmail('system-mail test', 'some fast email to admin test');
  48. //debug($res);
  49. $this->assertTrue($res);
  50. }
  51. public function _testSendWithInlineAttachments() {
  52. $this->Email = new TestEmailLib();
  53. $this->Email->transport('debug');
  54. $this->Email->from('cake@cakephp.org');
  55. $this->Email->to('cake@cakephp.org');
  56. $this->Email->subject('My title');
  57. $this->Email->emailFormat('both');
  58. $result = $this->Email->send();
  59. //debug($result);
  60. $boundary = $this->Email->getBoundary();
  61. /*
  62. $this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
  63. $expected = "--$boundary\r\n" .
  64. "Content-Type: multipart/related; boundary=\"rel-$boundary\"\r\n" .
  65. "\r\n" .
  66. "--rel-$boundary\r\n" .
  67. "Content-Type: multipart/alternative; boundary=\"alt-$boundary\"\r\n" .
  68. "\r\n" .
  69. "--alt-$boundary\r\n" .
  70. "Content-Type: text/plain; charset=UTF-8\r\n" .
  71. "Content-Transfer-Encoding: 8bit\r\n" .
  72. "\r\n" .
  73. "Hello" .
  74. "\r\n" .
  75. "\r\n" .
  76. "\r\n" .
  77. "--alt-$boundary\r\n" .
  78. "Content-Type: text/html; charset=UTF-8\r\n" .
  79. "Content-Transfer-Encoding: 8bit\r\n" .
  80. "\r\n" .
  81. "Hello" .
  82. "\r\n" .
  83. "\r\n" .
  84. "\r\n" .
  85. "--alt-{$boundary}--\r\n" .
  86. "\r\n" .
  87. "--rel-$boundary\r\n" .
  88. "Content-Type: application/octet-stream\r\n" .
  89. "Content-Transfer-Encoding: base64\r\n" .
  90. "Content-ID: <abc123>\r\n" .
  91. "Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
  92. $this->assertContains($expected, $result['message']);
  93. $this->assertContains('--rel-' . $boundary . '--', $result['message']);
  94. $this->assertContains('--' . $boundary . '--', $result['message']);
  95. */
  96. //debug($boundary);
  97. die();
  98. }
  99. public function testAddAttachment() {
  100. $this->skipIf(!$this->sendEmails);
  101. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  102. $this->assertTrue(file_exists($file));
  103. Configure::write('debug', 0);
  104. $this->Email->to(Configure::read('Config.admin_email'));
  105. $this->Email->addAttachment($file);
  106. $res = $this->Email->send('test_default', 'internal');
  107. if ($error = $this->Email->getError()) {
  108. $this->out($error);
  109. }
  110. $this->assertEquals('', $this->Email->getError());
  111. $this->assertTrue($res);
  112. $this->Email->resetAndSet();
  113. $this->Email->to(Configure::read('Config.admin_email'));
  114. $this->Email->addAttachment($file, 'x.jpg');
  115. $res = $this->Email->send('test_custom_filename');
  116. Configure::write('debug', 2);
  117. $this->assertEquals('', $this->Email->getError());
  118. $this->assertTrue($res);
  119. }
  120. /**
  121. * html email
  122. */
  123. public function testAddEmbeddedAttachment() {
  124. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  125. $this->assertTrue(file_exists($file));
  126. Configure::write('debug', 0);
  127. $this->Email = new EmailLib();
  128. $this->Email->emailFormat('both');
  129. $this->Email->to(Configure::read('Config.admin_email'));
  130. $cid = $this->Email->addEmbeddedAttachment($file);
  131. $cid2 = $this->Email->addEmbeddedAttachment($file);
  132. $this->assertContains('@'.env('HTTP_HOST'), $cid);
  133. $html = '<head>
  134. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  135. <meta name="author" content="ohyeah" />
  136. <title>Untitled 6</title>
  137. </head>
  138. <body>
  139. test_embedded_default äöü <img src="cid:'.$cid.'" /> end
  140. another image <img src="cid:'.$cid2.'" /> end
  141. html-part
  142. </body>
  143. </html>';
  144. $text = trim(strip_tags($html));
  145. $this->Email->viewVars(compact('text', 'html'));
  146. if (!$this->sendEmails) {
  147. Configure::write('debug', 2);
  148. }
  149. $this->skipIf(!$this->sendEmails);
  150. $res = $this->Email->send();
  151. Configure::write('debug', 2);
  152. if ($error = $this->Email->getError()) {
  153. $this->out($error);
  154. }
  155. $this->assertEquals('', $this->Email->getError());
  156. $this->assertTrue($res);
  157. }
  158. /**
  159. * html email
  160. */
  161. public function testAddEmbeddedBlobAttachment() {
  162. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  163. $this->assertTrue(file_exists($file));
  164. Configure::write('debug', 0);
  165. $this->Email = new EmailLib();
  166. $this->Email->emailFormat('both');
  167. $this->Email->to(Configure::read('Config.admin_email'));
  168. $cid = $this->Email->addEmbeddedBlobAttachment(file_get_contents($file), 'my_hotel.png', 'png');
  169. $this->assertContains('@'.env('HTTP_HOST'), $cid);
  170. $html = '<head>
  171. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  172. <meta name="author" content="ohyeah" />
  173. <title>Untitled 6</title>
  174. </head>
  175. <body>
  176. test_embedded_blob_default äöü <img src="cid:'.$cid.'" /> end
  177. html-part
  178. </body>
  179. </html>';
  180. $text = trim(strip_tags($html));
  181. $this->Email->viewVars(compact('text', 'html'));
  182. if (!$this->sendEmails) {
  183. Configure::write('debug', 2);
  184. }
  185. $this->skipIf(!$this->sendEmails);
  186. $res = $this->Email->send();
  187. Configure::write('debug', 2);
  188. if ($error = $this->Email->getError()) {
  189. $this->out($error);
  190. }
  191. $this->assertEquals('', $this->Email->getError());
  192. $this->assertTrue($res);
  193. }
  194. public function _testComplexeHtmlWithEmbeddedImages() {
  195. $file = CakePlugin::path('Tools').'Test'.DS.'test_files'.DS.'img'.DS.'hotel.png';
  196. $this->assertTrue(file_exists($file));
  197. }
  198. public function testWrapLongEmailContent() {
  199. $this->Email = new TestEmailLib();
  200. $html = <<<HTML
  201. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  202. <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;">
  203. 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
  204. </body></html>
  205. HTML;
  206. //$html = str_replace(array("\r\n", "\n", "\r"), "", $html);
  207. $is = $this->Email->wrap($html);
  208. foreach ($is as $line => $content) {
  209. $this->assertTrue(strlen($content) <= EmailLib::LINE_LENGTH_MUST);
  210. }
  211. $this->debug($is);
  212. }
  213. }
  214. /**
  215. * Help to test EmailLib
  216. *
  217. */
  218. class TestEmailLib extends EmailLib {
  219. /**
  220. * Wrap to protected method
  221. *
  222. */
  223. public function formatAddress($address) {
  224. return parent::_formatAddress($address);
  225. }
  226. /**
  227. * Wrap to protected method
  228. *
  229. */
  230. public function wrap($text) {
  231. return parent::_wrap($text);
  232. }
  233. /**
  234. * Get the boundary attribute
  235. *
  236. * @return string
  237. */
  238. public function getBoundary() {
  239. return $this->_boundary;
  240. }
  241. /**
  242. * Encode to protected method
  243. *
  244. */
  245. public function encode($text) {
  246. return $this->_encode($text);
  247. }
  248. }