EmailLibTest.php 6.9 KB

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