EmailLibTest.php 7.0 KB

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