MailTest.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Mail
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. /**
  23. * Test helper
  24. */
  25. require_once dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'TestHelper.php';
  26. /**
  27. * Zend_Mail
  28. */
  29. require_once 'Zend/Mail.php';
  30. /**
  31. * Zend_Mail_Transport_Abstract
  32. */
  33. require_once 'Zend/Mail/Transport/Abstract.php';
  34. /**
  35. * Zend_Mail_Transport_Sendmail
  36. */
  37. require_once 'Zend/Mail/Transport/Sendmail.php';
  38. /**
  39. * Zend_Mail_Transport_Smtp
  40. */
  41. require_once 'Zend/Mail/Transport/Smtp.php';
  42. /**
  43. * Zend_Date
  44. */
  45. require_once 'Zend/Date.php';
  46. /**
  47. * Mock mail transport class for testing purposes
  48. *
  49. * @category Zend
  50. * @package Zend_Mail
  51. * @subpackage UnitTests
  52. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  53. * @license http://framework.zend.com/license/new-bsd New BSD License
  54. */
  55. class Zend_Mail_Transport_Mock extends Zend_Mail_Transport_Abstract
  56. {
  57. /**
  58. * @var Zend_Mail
  59. */
  60. public $mail = null;
  61. public $returnPath = null;
  62. public $subject = null;
  63. public $from = null;
  64. public $headers = null;
  65. public $called = false;
  66. public function _sendMail()
  67. {
  68. $this->mail = $this->_mail;
  69. $this->subject = $this->_mail->getSubject();
  70. $this->from = $this->_mail->getFrom();
  71. $this->returnPath = $this->_mail->getReturnPath();
  72. $this->headers = $this->_headers;
  73. $this->called = true;
  74. }
  75. }
  76. /**
  77. * Mock mail transport class for testing Sendmail transport
  78. *
  79. * @category Zend
  80. * @package Zend_Mail
  81. * @subpackage UnitTests
  82. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  83. * @license http://framework.zend.com/license/new-bsd New BSD License
  84. */
  85. class Zend_Mail_Transport_Sendmail_Mock extends Zend_Mail_Transport_Sendmail
  86. {
  87. /**
  88. * @var Zend_Mail
  89. */
  90. public $mail = null;
  91. public $from = null;
  92. public $subject = null;
  93. public $called = false;
  94. public function _sendMail()
  95. {
  96. $this->mail = $this->_mail;
  97. $this->from = $this->_mail->getFrom();
  98. $this->subject = $this->_mail->getSubject();
  99. $this->called = true;
  100. }
  101. }
  102. /**
  103. * @category Zend
  104. * @package Zend_Mail
  105. * @subpackage UnitTests
  106. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  107. * @license http://framework.zend.com/license/new-bsd New BSD License
  108. * @group Zend_Mail
  109. */
  110. class Zend_Mail_MailTest extends PHPUnit_Framework_TestCase
  111. {
  112. /**
  113. * Test case for a simple email text message with
  114. * multiple recipients.
  115. *
  116. */
  117. public function testOnlyText()
  118. {
  119. $mail = new Zend_Mail();
  120. $res = $mail->setBodyText('This is a test.');
  121. $mail->setFrom('testmail@example.com', 'test Mail User');
  122. $mail->setSubject('My Subject');
  123. $mail->addTo('recipient1@example.com');
  124. $mail->addTo('recipient2@example.com');
  125. $mail->addBcc('recipient1_bcc@example.com');
  126. $mail->addBcc('recipient2_bcc@example.com');
  127. $mail->addCc('recipient1_cc@example.com', 'Example no. 1 for cc');
  128. $mail->addCc('recipient2_cc@example.com', 'Example no. 2 for cc');
  129. $mock = new Zend_Mail_Transport_Mock();
  130. $mail->send($mock);
  131. $this->assertTrue($mock->called);
  132. $this->assertEquals('My Subject', $mock->subject);
  133. $this->assertEquals('testmail@example.com', $mock->from);
  134. $this->assertContains('recipient1@example.com', $mock->recipients);
  135. $this->assertContains('recipient2@example.com', $mock->recipients);
  136. $this->assertContains('recipient1_bcc@example.com', $mock->recipients);
  137. $this->assertContains('recipient2_bcc@example.com', $mock->recipients);
  138. $this->assertContains('recipient1_cc@example.com', $mock->recipients);
  139. $this->assertContains('recipient2_cc@example.com', $mock->recipients);
  140. $this->assertContains('This is a test.', $mock->body);
  141. $this->assertContains('Content-Transfer-Encoding: quoted-printable', $mock->header);
  142. $this->assertContains('Content-Type: text/plain', $mock->header);
  143. $this->assertContains('From: test Mail User <testmail@example.com>', $mock->header);
  144. $this->assertContains('Subject: My Subject', $mock->header);
  145. $this->assertContains('To: recipient1@example.com', $mock->header);
  146. $this->assertContains('Cc: Example no. 1 for cc <recipient1_cc@example.com>', $mock->header);
  147. }
  148. /**
  149. * Check if Header Fields are encoded correctly and if
  150. * header injection is prevented.
  151. */
  152. public function testHeaderEncoding()
  153. {
  154. $mail = new Zend_Mail("UTF-8");
  155. $mail->setBodyText('My Nice Test Text');
  156. // try header injection:
  157. $mail->addTo("testmail@example.com\nCc:foobar@example.com");
  158. $mail->addHeader('X-MyTest', "Test\nCc:foobar2@example.com", true);
  159. // try special Chars in Header Fields:
  160. $mail->setFrom('mymail@example.com', "\xC6\x98\xC6\x90\xC3\xA4\xC4\xB8");
  161. $mail->addTo('testmail2@example.com', "\xC4\xA7\xC4\xAF\xC7\xAB");
  162. $mail->addCc('testmail3@example.com', "\xC7\xB6\xC7\xB7");
  163. $mail->setSubject("\xC7\xB1\xC7\xAE");
  164. $mail->addHeader('X-MyTest', "Test-\xC7\xB1", true);
  165. $mock = new Zend_Mail_Transport_Mock();
  166. $mail->send($mock);
  167. $this->assertTrue($mock->called);
  168. $this->assertContains(
  169. 'From: =?UTF-8?Q?=C6=98=C6=90=C3=A4=C4=B8?=',
  170. $mock->header,
  171. "From: Header was encoded unexpectedly."
  172. );
  173. $this->assertContains(
  174. "Cc:foobar@example.com",
  175. $mock->header
  176. );
  177. $this->assertNotContains(
  178. "\nCc:foobar@example.com",
  179. $mock->header,
  180. "Injection into From: header is possible."
  181. );
  182. $this->assertContains(
  183. '=?UTF-8?Q?=C4=A7=C4=AF=C7=AB?= <testmail2@example.com>',
  184. $mock->header
  185. );
  186. $this->assertContains(
  187. 'Cc: =?UTF-8?Q?=C7=B6=C7=B7?= <testmail3@example.com>',
  188. $mock->header
  189. );
  190. $this->assertContains(
  191. 'Subject: =?UTF-8?Q?=C7=B1=C7=AE?=',
  192. $mock->header
  193. );
  194. $this->assertContains(
  195. 'X-MyTest:',
  196. $mock->header
  197. );
  198. $this->assertNotContains(
  199. "\nCc:foobar2@example.com",
  200. $mock->header
  201. );
  202. $this->assertContains(
  203. '=?UTF-8?Q?Test-=C7=B1?=',
  204. $mock->header
  205. );
  206. }
  207. /**
  208. * @group ZF-7799
  209. */
  210. public function testHeaderSendMailTransportHaveNoRightTrim()
  211. {
  212. $mail = new Zend_Mail("UTF-8");
  213. $mail->setBodyText('My Nice Test Text');
  214. $mail->addTo("foobar@example.com");
  215. $mail->setSubject("hello world!");
  216. $transportMock = new Zend_Mail_Transport_Sendmail_Mock();
  217. $mail->send($transportMock);
  218. $this->assertEquals($transportMock->header, rtrim($transportMock->header));
  219. }
  220. /**
  221. * Check if Header Fields are stripped accordingly in sendmail transport;
  222. * also check for header injection
  223. * @todo Determine why this fails in Windows (testmail3@example.com example)
  224. */
  225. public function testHeaderEncoding2()
  226. {
  227. $mail = new Zend_Mail("UTF-8");
  228. $mail->setBodyText('My Nice Test Text');
  229. // try header injection:
  230. $mail->addTo("testmail@example.com\nCc:foobar@example.com");
  231. $mail->addHeader('X-MyTest', "Test\nCc:foobar2@example.com", true);
  232. // try special Chars in Header Fields:
  233. $mail->setFrom('mymail@example.com', "\xC6\x98\xC6\x90\xC3\xA4\xC4\xB8");
  234. $mail->addTo('testmail2@example.com', "\xC4\xA7\xC4\xAF\xC7\xAB");
  235. $mail->addCc('testmail3@example.com', "\xC7\xB6\xC7\xB7");
  236. $mail->setSubject("\xC7\xB1\xC7\xAE");
  237. $mail->addHeader('X-MyTest', "Test-\xC7\xB1", true);
  238. $mock = new Zend_Mail_Transport_Sendmail_Mock();
  239. $mail->send($mock);
  240. $this->assertTrue($mock->called);
  241. $this->assertContains(
  242. 'From: =?UTF-8?Q?=C6=98=C6=90=C3=A4=C4=B8?=',
  243. $mock->header,
  244. "From: Header was encoded unexpectedly."
  245. );
  246. $this->assertNotContains(
  247. "\nCc:foobar@example.com",
  248. $mock->header,
  249. "Injection into From: header is possible."
  250. );
  251. // To is done by mail() not in headers
  252. $this->assertNotContains(
  253. 'To: =?UTF-8?Q?=C4=A7=C4=AF=C7=AB?= <testmail2@example.com>',
  254. $mock->header
  255. );
  256. $this->assertContains(
  257. 'Cc: =?UTF-8?Q?=C7=B6=C7=B7?= <testmail3@example.com>',
  258. $mock->header
  259. );
  260. // Subject is done by mail() not in headers
  261. $this->assertNotContains(
  262. 'Subject: =?UTF-8?Q?=C7=B1=C7=AE?=',
  263. $mock->header
  264. );
  265. $this->assertContains(
  266. 'X-MyTest:',
  267. $mock->header
  268. );
  269. $this->assertNotContains(
  270. "\nCc:foobar2@example.com",
  271. $mock->header
  272. );
  273. $this->assertContains(
  274. '=?UTF-8?Q?Test-=C7=B1?=',
  275. $mock->header
  276. );
  277. }
  278. /**
  279. * Check if Mails with HTML and Text Body are generated correctly.
  280. *
  281. */
  282. public function testMultipartAlternative()
  283. {
  284. $mail = new Zend_Mail();
  285. $mail->setBodyText('My Nice Test Text');
  286. $mail->setBodyHtml('My Nice <b>Test</b> Text');
  287. $mail->addTo('testmail@example.com', 'Test Recipient');
  288. $mail->setFrom('mymail@example.com', 'Test Sender');
  289. $mail->setSubject('Test: Alternate Mail with Zend_Mail');
  290. $mock = new Zend_Mail_Transport_Mock();
  291. $mail->send($mock);
  292. // check headers
  293. $this->assertTrue($mock->called);
  294. $this->assertContains('multipart/alternative', $mock->header);
  295. $boundary = $mock->boundary;
  296. $this->assertContains('boundary="' . $boundary . '"', $mock->header);
  297. $this->assertContains('MIME-Version: 1.0', $mock->header);
  298. // check body
  299. // search for first boundary
  300. $p1 = strpos($mock->body, "--$boundary\n");
  301. $this->assertNotNull($p1, $boundary . ': ' . $mock->body);
  302. // cut out first (Text) part
  303. $start1 = $p1 + 3 + strlen($boundary);
  304. $p2 = strpos($mock->body, "--$boundary\n", $start1);
  305. $this->assertNotNull($p2);
  306. $partBody1 = substr($mock->body, $start1, ($p2 - $start1));
  307. $this->assertContains('Content-Type: text/plain', $partBody1);
  308. $this->assertContains('My Nice Test Text', $partBody1);
  309. // check second (HTML) part
  310. // search for end boundary
  311. $start2 = $p2 + 3 + strlen($boundary);
  312. $p3 = strpos($mock->body, "--$boundary--");
  313. $this->assertNotNull($p3);
  314. $partBody2 = substr($mock->body, $start2, ($p3 - $start2));
  315. $this->assertContains('Content-Type: text/html', $partBody2);
  316. $this->assertContains('My Nice <b>Test</b> Text', $partBody2);
  317. }
  318. /**
  319. * check if attachment handling works
  320. *
  321. */
  322. public function testAttachment()
  323. {
  324. $mail = new Zend_Mail();
  325. $mail->setBodyText('My Nice Test Text');
  326. $mail->addTo('testmail@example.com', 'Test Recipient');
  327. $mail->setFrom('mymail@example.com', 'Test Sender');
  328. $mail->setSubject('Test: Attachment Test with Zend_Mail');
  329. $at = $mail->createAttachment('abcdefghijklmnopqrstuvexyz');
  330. $at->type = 'image/gif';
  331. $at->id = 12;
  332. $at->filename = 'test.gif';
  333. $mock = new Zend_Mail_Transport_Mock();
  334. $mail->send($mock);
  335. // now check what was generated by Zend_Mail.
  336. // first the mail headers:
  337. $this->assertContains('Content-Type: multipart/mixed', $mock->header, $mock->header);
  338. $boundary = $mock->boundary;
  339. $this->assertContains('boundary="' . $boundary . '"', $mock->header);
  340. $this->assertContains('MIME-Version: 1.0', $mock->header);
  341. // check body
  342. // search for first boundary
  343. $p1 = strpos($mock->body, "--$boundary\n");
  344. $this->assertNotNull($p1);
  345. // cut out first (Text) part
  346. $start1 = $p1 + 3 + strlen($boundary);
  347. $p2 = strpos($mock->body, "--$boundary\n", $start1);
  348. $this->assertNotNull($p2);
  349. $partBody1 = substr($mock->body, $start1, ($p2 - $start1));
  350. $this->assertContains('Content-Type: text/plain', $partBody1);
  351. $this->assertContains('My Nice Test Text', $partBody1);
  352. // check second (HTML) part
  353. // search for end boundary
  354. $start2 = $p2 + 3 + strlen($boundary);
  355. $p3 = strpos($mock->body, "--$boundary--");
  356. $this->assertNotNull($p3);
  357. $partBody2 = substr($mock->body, $start2, ($p3 - $start2));
  358. $this->assertContains('Content-Type: image/gif', $partBody2);
  359. $this->assertContains('Content-Transfer-Encoding: base64', $partBody2);
  360. $this->assertContains('Content-ID: <12>', $partBody2);
  361. }
  362. /**
  363. * Check if Mails with HTML and Text Body are generated correctly.
  364. *
  365. */
  366. public function testMultipartAlternativePlusAttachment()
  367. {
  368. $mail = new Zend_Mail();
  369. $mail->setBodyText('My Nice Test Text');
  370. $mail->setBodyHtml('My Nice <b>Test</b> Text');
  371. $mail->addTo('testmail@example.com', 'Test Recipient');
  372. $mail->setFrom('mymail@example.com', 'Test Sender');
  373. $mail->setSubject('Test: Alternate Mail with Zend_Mail');
  374. $at = $mail->createAttachment('abcdefghijklmnopqrstuvexyz');
  375. $at->type = 'image/gif';
  376. $at->id = 12;
  377. $at->filename = 'test.gif';
  378. $mock = new Zend_Mail_Transport_Mock();
  379. $mail->send($mock);
  380. // check headers
  381. $this->assertTrue($mock->called);
  382. $this->assertContains('multipart/mixed', $mock->header);
  383. $boundary = $mock->boundary;
  384. $this->assertContains('boundary="' . $boundary . '"', $mock->header);
  385. $this->assertContains('MIME-Version: 1.0', $mock->header);
  386. // check body
  387. // search for first boundary
  388. $p1 = strpos($mock->body, "--$boundary\n");
  389. $this->assertNotNull($p1);
  390. // cut out first (multipart/alternative) part
  391. $start1 = $p1 + 3 + strlen($boundary);
  392. $p2 = strpos($mock->body, "--$boundary\n", $start1);
  393. $this->assertNotNull($p2);
  394. $partBody1 = substr($mock->body, $start1, ($p2 - $start1));
  395. $this->assertContains('Content-Type: multipart/alternative', $partBody1);
  396. $this->assertContains('Content-Type: text/plain', $partBody1);
  397. $this->assertContains('Content-Type: text/html', $partBody1);
  398. $this->assertContains('My Nice Test Text', $partBody1);
  399. $this->assertContains('My Nice <b>Test</b> Text', $partBody1);
  400. // check second (image) part
  401. // search for end boundary
  402. $start2 = $p2 + 3 + strlen($boundary);
  403. $p3 = strpos($mock->body, "--$boundary--");
  404. $this->assertNotNull($p3);
  405. $partBody2 = substr($mock->body, $start2, ($p3 - $start2));
  406. $this->assertContains('Content-Type: image/gif', $partBody2);
  407. $this->assertContains('Content-Transfer-Encoding: base64', $partBody2);
  408. $this->assertContains('Content-ID: <12>', $partBody2);
  409. }
  410. public function testReturnPath()
  411. {
  412. $mail = new Zend_Mail();
  413. $res = $mail->setBodyText('This is a test.');
  414. $mail->setFrom('testmail@example.com', 'test Mail User');
  415. $mail->setSubject('My Subject');
  416. $mail->addTo('recipient1@example.com');
  417. $mail->addTo('recipient2@example.com');
  418. $mail->addBcc('recipient1_bcc@example.com');
  419. $mail->addBcc('recipient2_bcc@example.com');
  420. $mail->addCc('recipient1_cc@example.com', 'Example no. 1 for cc');
  421. $mail->addCc('recipient2_cc@example.com', 'Example no. 2 for cc');
  422. // First example: from and return-path should be equal
  423. $mock = new Zend_Mail_Transport_Mock();
  424. $mail->send($mock);
  425. $this->assertTrue($mock->called);
  426. $this->assertEquals($mail->getFrom(), $mock->returnPath);
  427. // Second example: from and return-path should not be equal
  428. $mail->setReturnPath('sender2@example.com');
  429. $mock = new Zend_Mail_Transport_Mock();
  430. $mail->send($mock);
  431. $this->assertTrue($mock->called);
  432. $this->assertNotEquals($mail->getFrom(), $mock->returnPath);
  433. $this->assertEquals($mail->getReturnPath(), $mock->returnPath);
  434. $this->assertNotEquals($mock->returnPath, $mock->from);
  435. }
  436. public function testNoBody()
  437. {
  438. $mail = new Zend_Mail();
  439. $mail->setFrom('testmail@example.com', 'test Mail User');
  440. $mail->setSubject('My Subject');
  441. $mail->addTo('recipient1@example.com');
  442. // First example: from and return-path should be equal
  443. $mock = new Zend_Mail_Transport_Mock();
  444. try {
  445. $mail->send($mock);
  446. $this->assertTrue($mock->called);
  447. } catch (Exception $e) {
  448. // success
  449. $this->assertContains('No body specified', $e->getMessage());
  450. }
  451. }
  452. /**
  453. * Helper method for {@link testZf928ToAndBccHeadersShouldNotMix()}; extracts individual header lines
  454. *
  455. * @param Zend_Mail_Transport_Abstract $mock
  456. * @param string $type
  457. * @return string
  458. */
  459. protected function _getHeader(Zend_Mail_Transport_Abstract $mock, $type = 'To')
  460. {
  461. $headers = str_replace("\r\n", "\n", $mock->header);
  462. $headers = explode("\n", $mock->header);
  463. $return = '';
  464. foreach ($headers as $header) {
  465. if (!empty($return)) {
  466. // Check for header continuation
  467. if (!preg_match('/^[a-z-]+:/i', $header)) {
  468. $return .= "\r\n" . $header;
  469. continue;
  470. } else {
  471. break;
  472. }
  473. }
  474. if (preg_match('/^' . $type . ': /', $header)) {
  475. $return = $header;
  476. }
  477. }
  478. return $return;
  479. }
  480. public function testZf928ToAndBccHeadersShouldNotMix()
  481. {
  482. $mail = new Zend_Mail();
  483. $mail->setSubject('my subject');
  484. $mail->setBodyText('my body');
  485. $mail->setFrom('info@onlime.ch');
  486. $mail->addTo('to.address@email.com');
  487. $mail->addBcc('first.bcc@email.com');
  488. $mail->addBcc('second.bcc@email.com');
  489. // test with generic transport
  490. $mock = new Zend_Mail_Transport_Mock();
  491. $mail->send($mock);
  492. $to = $this->_getHeader($mock);
  493. $bcc = $this->_getHeader($mock, 'Bcc');
  494. $this->assertContains('to.address@email.com', $to, $to);
  495. $this->assertNotContains('second.bcc@email.com', $to, $bcc);
  496. // test with sendmail-like transport
  497. $mock = new Zend_Mail_Transport_Sendmail_Mock();
  498. $mail->send($mock);
  499. $to = $this->_getHeader($mock);
  500. $bcc = $this->_getHeader($mock, 'Bcc');
  501. // Remove the following line due to fixes by Simon
  502. // $this->assertNotContains('to.address@email.com', $to, $mock->header);
  503. $this->assertNotContains('second.bcc@email.com', $to, $bcc);
  504. }
  505. public function testZf927BlankLinesShouldPersist()
  506. {
  507. $mail = new Zend_Mail();
  508. $mail->setSubject('my subject');
  509. $mail->setBodyText("my body\r\n\r\n...after two newlines");
  510. $mail->setFrom('test@email.com');
  511. $mail->addTo('test@email.com');
  512. // test with generic transport
  513. $mock = new Zend_Mail_Transport_Sendmail_Mock();
  514. $mail->send($mock);
  515. $body = quoted_printable_decode($mock->body);
  516. $this->assertContains("\r\n\r\n...after", $body, $body);
  517. }
  518. public function testGetJustBodyText()
  519. {
  520. $text = "my body\r\n\r\n...after two newlines";
  521. $mail = new Zend_Mail();
  522. $mail->setBodyText($text);
  523. $this->assertContains('my body', $mail->getBodyText(true));
  524. $this->assertContains('after two newlines', $mail->getBodyText(true));
  525. }
  526. public function testGetJustBodyHtml()
  527. {
  528. $text = "<html><head></head><body><p>Some body text</p></body></html>";
  529. $mail = new Zend_Mail();
  530. $mail->setBodyHtml($text);
  531. $this->assertContains('Some body text', $mail->getBodyHtml(true));
  532. }
  533. public function testTypeAccessor()
  534. {
  535. $mail = new Zend_Mail();
  536. $this->assertNull($mail->getType());
  537. $mail->setType(Zend_Mime::MULTIPART_ALTERNATIVE);
  538. $this->assertEquals(Zend_Mime::MULTIPART_ALTERNATIVE, $mail->getType());
  539. $mail->setType(Zend_Mime::MULTIPART_RELATED);
  540. $this->assertEquals(Zend_Mime::MULTIPART_RELATED, $mail->getType());
  541. try {
  542. $mail->setType('text/plain');
  543. $this->fail('Invalid Zend_Mime type should throw an exception');
  544. } catch (Exception $e) {
  545. }
  546. }
  547. public function testDateSet()
  548. {
  549. $mail = new Zend_Mail();
  550. $res = $mail->setBodyText('Date Test');
  551. $mail->setFrom('testmail@example.com', 'test Mail User');
  552. $mail->setSubject('Date Test');
  553. $mail->addTo('recipient@example.com');
  554. $mock = new Zend_Mail_Transport_Mock();
  555. $mail->send($mock);
  556. $this->assertTrue($mock->called);
  557. $this->assertTrue(isset($mock->headers['Date']));
  558. $this->assertTrue(isset($mock->headers['Date'][0]));
  559. $this->assertTrue(strlen($mock->headers['Date'][0]) > 0);
  560. }
  561. public function testSetDateInt()
  562. {
  563. $mail = new Zend_Mail();
  564. $res = $mail->setBodyText('Date Test');
  565. $mail->setFrom('testmail@example.com', 'test Mail User');
  566. $mail->setSubject('Date Test');
  567. $mail->addTo('recipient@example.com');
  568. $mail->setDate(362656800);
  569. $mock = new Zend_Mail_Transport_Mock();
  570. $mail->send($mock);
  571. $this->assertTrue($mock->called);
  572. $this->assertTrue(strpos(implode('', $mock->headers['Date']), 'Mon, 29 Jun 1981') === 0);
  573. }
  574. public function testSetDateString()
  575. {
  576. $mail = new Zend_Mail();
  577. $res = $mail->setBodyText('Date Test');
  578. $mail->setFrom('testmail@example.com', 'test Mail User');
  579. $mail->setSubject('Date Test');
  580. $mail->addTo('recipient@example.com');
  581. $mail->setDate('1981-06-29T12:00:00');
  582. $mock = new Zend_Mail_Transport_Mock();
  583. $mail->send($mock);
  584. $this->assertTrue($mock->called);
  585. $this->assertTrue(strpos(implode('', $mock->headers['Date']), 'Mon, 29 Jun 1981') === 0);
  586. }
  587. public function testSetDateObject()
  588. {
  589. $mail = new Zend_Mail();
  590. $res = $mail->setBodyText('Date Test');
  591. $mail->setFrom('testmail@example.com', 'test Mail User');
  592. $mail->setSubject('Date Test');
  593. $mail->addTo('recipient@example.com');
  594. $mail->setDate(new Zend_Date('1981-06-29T12:00:00', Zend_Date::ISO_8601));
  595. $mock = new Zend_Mail_Transport_Mock();
  596. $mail->send($mock);
  597. $this->assertTrue($mock->called);
  598. $this->assertTrue(strpos(implode('', $mock->headers['Date']), 'Mon, 29 Jun 1981') === 0);
  599. }
  600. public function testSetDateInvalidString()
  601. {
  602. $mail = new Zend_Mail();
  603. try {
  604. $mail->setDate('invalid date');
  605. $this->fail('Invalid date should throw an exception');
  606. } catch (Exception $e) {
  607. }
  608. }
  609. public function testSetDateInvalidType()
  610. {
  611. $mail = new Zend_Mail();
  612. try {
  613. $mail->setDate(true);
  614. $this->fail('Invalid date should throw an exception');
  615. } catch (Exception $e) {
  616. }
  617. }
  618. public function testSetDateInvalidObject()
  619. {
  620. $mail = new Zend_Mail();
  621. try {
  622. $mail->setDate($mail);
  623. $this->fail('Invalid date should throw an exception');
  624. } catch (Exception $e) {
  625. }
  626. }
  627. public function testSetDateTwice()
  628. {
  629. $mail = new Zend_Mail();
  630. $mail->setDate();
  631. try {
  632. $mail->setDate(123456789);
  633. $this->fail('setting date twice should throw an exception');
  634. } catch (Exception $e) {
  635. }
  636. }
  637. /**
  638. * @group ZF-6872
  639. */
  640. public function testSetReplyTo()
  641. {
  642. $mail = new Zend_Mail('UTF-8');
  643. $mail->setReplyTo("foo@zend.com", "\xe2\x82\xa0!");
  644. $headers = $mail->getHeaders();
  645. $this->assertEquals("=?UTF-8?Q?=E2=82=A0!?= <foo@zend.com>", $headers["Reply-To"][0]);
  646. }
  647. /**
  648. * @group ZF-1688
  649. * @group ZF-2559
  650. */
  651. public function testSetHeaderEncoding()
  652. {
  653. $mail = new Zend_Mail();
  654. $this->assertEquals(Zend_Mime::ENCODING_QUOTEDPRINTABLE, $mail->getHeaderEncoding());
  655. $mail->setHeaderEncoding(Zend_Mime::ENCODING_BASE64);
  656. $this->assertEquals(Zend_Mime::ENCODING_BASE64, $mail->getHeaderEncoding());
  657. }
  658. /**
  659. * @group ZF-1688
  660. * @dataProvider dataSubjects
  661. */
  662. public function testIfLongSubjectsHaveCorrectLineBreaksAndEncodingMarks($subject)
  663. {
  664. $mail = new Zend_Mail("UTF-8");
  665. $mail->setSubject($subject);
  666. $headers = $mail->getHeaders();
  667. $this->assertMailHeaderConformsToRfc($headers['Subject'][0]);
  668. }
  669. /**
  670. * @group ZF-7702
  671. */
  672. public function testReplyToIsNoRecipient() {
  673. $mail = new Zend_Mail();
  674. $mail->setReplyTo('foo@example.com','foobar');
  675. $this->assertEquals(0, count($mail->getRecipients()));
  676. }
  677. public function testGetReplyToReturnsReplyTo() {
  678. $mail = new Zend_Mail();
  679. $mail->setReplyTo('foo@example.com');
  680. $this->assertEquals('foo@example.com',$mail->getReplyTo());
  681. }
  682. /**
  683. * @expectedException Zend_Mail_Exception
  684. */
  685. public function testReplyToCantBeSetTwice() {
  686. $mail = new Zend_Mail();
  687. $mail->setReplyTo('user@example.com');
  688. $mail->setReplyTo('user2@example.com');
  689. }
  690. public static function dataSubjects()
  691. {
  692. return array(
  693. array("Simple Ascii Subject"),
  694. array("Subject with US Specialchars: &%$/()"),
  695. array("Gimme more \xe2\x82\xa0!"),
  696. array("This is \xc3\xa4n germ\xc3\xa4n multiline s\xc3\xbcbject with rand\xc3\xb6m \xc3\xbcml\xc3\xa4uts."),
  697. array("Alle meine Entchen schwimmen in dem See, schwimmen in dem See, K\xc3\xb6pfchen in das Wasser, Schw\xc3\xa4nzchen in die H\xc3\xb6h!"),
  698. array("\xc3\xa4\xc3\xa4xxxxx\xc3\xa4\xc3\xa4\xc3\xa4\xc3\xa4\xc3\xa4\xc3\xa4\xc3\xa4"),
  699. array("\xd0\x90\xd0\x91\xd0\x92\xd0\x93\xd0\x94\xd0\x95 \xd0\x96\xd0\x97\xd0\x98\xd0\x99 \xd0\x9a\xd0\x9b\xd0\x9c\xd0\x9d"),
  700. array("Ich. Denke. Also. Bin. Ich! (Ein \xc3\xbcml\xc3\xa4\xc3\xbctautomat!)"),
  701. );
  702. }
  703. /**
  704. * Assertion that checks if a given mailing header string is RFC conform.
  705. *
  706. * @param string $header
  707. * @return void
  708. */
  709. protected function assertMailHeaderConformsToRfc($header)
  710. {
  711. $this->numAssertions++;
  712. $parts = explode(Zend_Mime::LINEEND, $header);
  713. if(count($parts) > 0) {
  714. for($i = 0; $i < count($parts); $i++) {
  715. if(preg_match('/(=?[a-z0-9-_]+\?[q|b]{1}\?)/i', $parts[$i], $matches)) {
  716. $dce = sprintf("=?%s", $matches[0]);
  717. // Check that Delimiter, Charset, Encoding are at the front of the string
  718. if(substr(trim($parts[$i]), 0, strlen($dce)) != $dce) {
  719. $this->fail(sprintf(
  720. "Header-Part '%s' in line '%d' has missing or malformated delimiter, charset, encoding information.",
  721. $parts[$i],
  722. $i+1
  723. ));
  724. }
  725. // check that the encoded word is not too long.);
  726. // this is only some kind of suggestion by the standard, in PHP its hard to hold it, so we do not enforce it here.
  727. /*if(strlen($parts[$i]) > 75) {
  728. $this->fail(sprintf(
  729. "Each encoded-word is only allowed to be 75 chars long, but line %d is %s chars long: %s",
  730. $i+1,
  731. strlen($parts[$i]),
  732. $parts[$i]
  733. ));
  734. }*/
  735. // Check that the end-delmiter ?= is correctly placed
  736. if(substr(trim($parts[$i]), -2, 2) != "?=") {
  737. $this->fail(sprintf(
  738. "Lines with an encoded-word have to end in ?=, but line %d does not: %s",
  739. $i+1,
  740. substr(trim($parts[$i]), -2, 2)
  741. ));
  742. }
  743. // Check that only one encoded-word can be found per line.
  744. if(substr_count($parts[$i], "=?") != 1) {
  745. $this->fail(sprintf(
  746. "Only one encoded-word is allowed per line in the header. It seems line %d contains more: %s",
  747. $i+1,
  748. $parts[$i]
  749. ));
  750. }
  751. // Check that the encoded-text only contains US-ASCII chars, and no space
  752. $encodedText = substr(trim($parts[$i]), strlen($dce), -2);
  753. if(preg_match('/([\s]+)/', $encodedText)) {
  754. $this->fail(sprintf(
  755. "No whitespace characters allowed in encoded-text of line %d: %s",
  756. $i+1,
  757. $parts[$i]
  758. ));
  759. }
  760. for($i = 0; $i < strlen($encodedText); $i++) {
  761. if(ord($encodedText[$i]) > 127) {
  762. $this->fail(sprintf(
  763. "No non US-ASCII characters allowed, but line %d has them: %s",
  764. $i+1,
  765. $parts[$i]
  766. ));
  767. }
  768. }
  769. } else if(Zend_Mime::isPrintable($parts[$i]) == false) {
  770. $this->fail(sprintf(
  771. "Encoded-word in line %d contains non printable characters.",
  772. $i+1
  773. ));
  774. }
  775. }
  776. }
  777. }
  778. }