Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

EmailTest.php 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Mime\Address;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Component\Mime\NamedAddress;
  15. use Symfony\Component\Mime\Part\DataPart;
  16. use Symfony\Component\Mime\Part\Multipart\AlternativePart;
  17. use Symfony\Component\Mime\Part\Multipart\MixedPart;
  18. use Symfony\Component\Mime\Part\Multipart\RelatedPart;
  19. use Symfony\Component\Mime\Part\TextPart;
  20. class EmailTest extends TestCase
  21. {
  22. public function testSubject()
  23. {
  24. $e = new Email();
  25. $e->subject('Subject');
  26. $this->assertEquals('Subject', $e->getSubject());
  27. }
  28. public function testDate()
  29. {
  30. $e = new Email();
  31. $e->date($d = new \DateTimeImmutable());
  32. $this->assertSame($d, $e->getDate());
  33. }
  34. public function testReturnPath()
  35. {
  36. $e = new Email();
  37. $e->returnPath('fabien@symfony.com');
  38. $this->assertEquals(new Address('fabien@symfony.com'), $e->getReturnPath());
  39. }
  40. public function testSender()
  41. {
  42. $e = new Email();
  43. $e->sender('fabien@symfony.com');
  44. $this->assertEquals(new Address('fabien@symfony.com'), $e->getSender());
  45. $e->sender($fabien = new Address('fabien@symfony.com'));
  46. $this->assertSame($fabien, $e->getSender());
  47. }
  48. public function testFrom()
  49. {
  50. $e = new Email();
  51. $helene = new Address('helene@symfony.com');
  52. $thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
  53. $caramel = new Address('caramel@symfony.com');
  54. $this->assertSame($e, $e->from('fabien@symfony.com', $helene, $thomas));
  55. $v = $e->getFrom();
  56. $this->assertCount(3, $v);
  57. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  58. $this->assertSame($helene, $v[1]);
  59. $this->assertSame($thomas, $v[2]);
  60. $this->assertSame($e, $e->addFrom('lucas@symfony.com', $caramel));
  61. $v = $e->getFrom();
  62. $this->assertCount(5, $v);
  63. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  64. $this->assertSame($helene, $v[1]);
  65. $this->assertSame($thomas, $v[2]);
  66. $this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
  67. $this->assertSame($caramel, $v[4]);
  68. $e = new Email();
  69. $e->addFrom('lucas@symfony.com', $caramel);
  70. $this->assertCount(2, $e->getFrom());
  71. $e = new Email();
  72. $e->from('lucas@symfony.com');
  73. $e->from($caramel);
  74. $this->assertSame([$caramel], $e->getFrom());
  75. }
  76. public function testReplyTo()
  77. {
  78. $e = new Email();
  79. $helene = new Address('helene@symfony.com');
  80. $thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
  81. $caramel = new Address('caramel@symfony.com');
  82. $this->assertSame($e, $e->replyTo('fabien@symfony.com', $helene, $thomas));
  83. $v = $e->getReplyTo();
  84. $this->assertCount(3, $v);
  85. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  86. $this->assertSame($helene, $v[1]);
  87. $this->assertSame($thomas, $v[2]);
  88. $this->assertSame($e, $e->addReplyTo('lucas@symfony.com', $caramel));
  89. $v = $e->getReplyTo();
  90. $this->assertCount(5, $v);
  91. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  92. $this->assertSame($helene, $v[1]);
  93. $this->assertSame($thomas, $v[2]);
  94. $this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
  95. $this->assertSame($caramel, $v[4]);
  96. $e = new Email();
  97. $e->addReplyTo('lucas@symfony.com', $caramel);
  98. $this->assertCount(2, $e->getReplyTo());
  99. $e = new Email();
  100. $e->replyTo('lucas@symfony.com');
  101. $e->replyTo($caramel);
  102. $this->assertSame([$caramel], $e->getReplyTo());
  103. }
  104. public function testTo()
  105. {
  106. $e = new Email();
  107. $helene = new Address('helene@symfony.com');
  108. $thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
  109. $caramel = new Address('caramel@symfony.com');
  110. $this->assertSame($e, $e->to('fabien@symfony.com', $helene, $thomas));
  111. $v = $e->getTo();
  112. $this->assertCount(3, $v);
  113. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  114. $this->assertSame($helene, $v[1]);
  115. $this->assertSame($thomas, $v[2]);
  116. $this->assertSame($e, $e->addTo('lucas@symfony.com', $caramel));
  117. $v = $e->getTo();
  118. $this->assertCount(5, $v);
  119. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  120. $this->assertSame($helene, $v[1]);
  121. $this->assertSame($thomas, $v[2]);
  122. $this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
  123. $this->assertSame($caramel, $v[4]);
  124. $e = new Email();
  125. $e->addTo('lucas@symfony.com', $caramel);
  126. $this->assertCount(2, $e->getTo());
  127. $e = new Email();
  128. $e->to('lucas@symfony.com');
  129. $e->to($caramel);
  130. $this->assertSame([$caramel], $e->getTo());
  131. }
  132. public function testCc()
  133. {
  134. $e = new Email();
  135. $helene = new Address('helene@symfony.com');
  136. $thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
  137. $caramel = new Address('caramel@symfony.com');
  138. $this->assertSame($e, $e->cc('fabien@symfony.com', $helene, $thomas));
  139. $v = $e->getCc();
  140. $this->assertCount(3, $v);
  141. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  142. $this->assertSame($helene, $v[1]);
  143. $this->assertSame($thomas, $v[2]);
  144. $this->assertSame($e, $e->addCc('lucas@symfony.com', $caramel));
  145. $v = $e->getCc();
  146. $this->assertCount(5, $v);
  147. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  148. $this->assertSame($helene, $v[1]);
  149. $this->assertSame($thomas, $v[2]);
  150. $this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
  151. $this->assertSame($caramel, $v[4]);
  152. $e = new Email();
  153. $e->addCc('lucas@symfony.com', $caramel);
  154. $this->assertCount(2, $e->getCc());
  155. $e = new Email();
  156. $e->cc('lucas@symfony.com');
  157. $e->cc($caramel);
  158. $this->assertSame([$caramel], $e->getCc());
  159. }
  160. public function testBcc()
  161. {
  162. $e = new Email();
  163. $helene = new Address('helene@symfony.com');
  164. $thomas = new NamedAddress('thomas@symfony.com', 'Thomas');
  165. $caramel = new Address('caramel@symfony.com');
  166. $this->assertSame($e, $e->bcc('fabien@symfony.com', $helene, $thomas));
  167. $v = $e->getBcc();
  168. $this->assertCount(3, $v);
  169. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  170. $this->assertSame($helene, $v[1]);
  171. $this->assertSame($thomas, $v[2]);
  172. $this->assertSame($e, $e->addBcc('lucas@symfony.com', $caramel));
  173. $v = $e->getBcc();
  174. $this->assertCount(5, $v);
  175. $this->assertEquals(new Address('fabien@symfony.com'), $v[0]);
  176. $this->assertSame($helene, $v[1]);
  177. $this->assertSame($thomas, $v[2]);
  178. $this->assertEquals(new Address('lucas@symfony.com'), $v[3]);
  179. $this->assertSame($caramel, $v[4]);
  180. $e = new Email();
  181. $e->addBcc('lucas@symfony.com', $caramel);
  182. $this->assertCount(2, $e->getBcc());
  183. $e = new Email();
  184. $e->bcc('lucas@symfony.com');
  185. $e->bcc($caramel);
  186. $this->assertSame([$caramel], $e->getBcc());
  187. }
  188. public function testPriority()
  189. {
  190. $e = new Email();
  191. $this->assertEquals(3, $e->getPriority());
  192. $e->priority(1);
  193. $this->assertEquals(1, $e->getPriority());
  194. $e->priority(10);
  195. $this->assertEquals(5, $e->getPriority());
  196. $e->priority(-10);
  197. $this->assertEquals(1, $e->getPriority());
  198. }
  199. public function testGenerateBodyThrowsWhenEmptyBody()
  200. {
  201. $this->expectException(\LogicException::class);
  202. (new Email())->getBody();
  203. }
  204. public function testGetBody()
  205. {
  206. $e = new Email();
  207. $e->setBody($text = new TextPart('text content'));
  208. $this->assertEquals($text, $e->getBody());
  209. }
  210. public function testGenerateBody()
  211. {
  212. $text = new TextPart('text content');
  213. $html = new TextPart('html content', 'utf-8', 'html');
  214. $att = new DataPart($file = fopen(__DIR__.'/Fixtures/mimetypes/test', 'r'));
  215. $img = new DataPart($image = fopen(__DIR__.'/Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
  216. $e = new Email();
  217. $e->text('text content');
  218. $this->assertEquals($text, $e->getBody());
  219. $this->assertEquals('text content', $e->getTextBody());
  220. $e = new Email();
  221. $e->html('html content');
  222. $this->assertEquals($html, $e->getBody());
  223. $this->assertEquals('html content', $e->getHtmlBody());
  224. $e = new Email();
  225. $e->html('html content');
  226. $e->text('text content');
  227. $this->assertEquals(new AlternativePart($text, $html), $e->getBody());
  228. $e = new Email();
  229. $e->html('html content', 'iso-8859-1');
  230. $e->text('text content', 'iso-8859-1');
  231. $this->assertEquals('iso-8859-1', $e->getTextCharset());
  232. $this->assertEquals('iso-8859-1', $e->getHtmlCharset());
  233. $this->assertEquals(new AlternativePart(new TextPart('text content', 'iso-8859-1'), new TextPart('html content', 'iso-8859-1', 'html')), $e->getBody());
  234. $e = new Email();
  235. $e->attach($file);
  236. $e->text('text content');
  237. $this->assertEquals(new MixedPart($text, $att), $e->getBody());
  238. $e = new Email();
  239. $e->attach($file);
  240. $e->html('html content');
  241. $this->assertEquals(new MixedPart($html, $att), $e->getBody());
  242. $e = new Email();
  243. $e->html('html content');
  244. $e->text('text content');
  245. $e->attach($file);
  246. $this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att), $e->getBody());
  247. $e = new Email();
  248. $e->html('html content');
  249. $e->text('text content');
  250. $e->attach($file);
  251. $e->attach($image, 'test.gif');
  252. $this->assertEquals(new MixedPart(new AlternativePart($text, $html), $att, $img), $e->getBody());
  253. $e = new Email();
  254. $e->text('text content');
  255. $e->attach($file);
  256. $e->attach($image, 'test.gif');
  257. $this->assertEquals(new MixedPart($text, $att, $img), $e->getBody());
  258. $e = new Email();
  259. $e->html($content = 'html content <img src="test.gif">');
  260. $e->text('text content');
  261. $e->attach($file);
  262. $e->attach($image, 'test.gif');
  263. $fullhtml = new TextPart($content, 'utf-8', 'html');
  264. $this->assertEquals(new MixedPart(new AlternativePart($text, $fullhtml), $att, $img), $e->getBody());
  265. $e = new Email();
  266. $e->html($content = 'html content <img src="cid:test.gif">');
  267. $e->text('text content');
  268. $e->attach($file);
  269. $e->attach($image, 'test.gif');
  270. $fullhtml = new TextPart($content, 'utf-8', 'html');
  271. $inlinedimg = (new DataPart($image, 'test.gif'))->asInline();
  272. $body = $e->getBody();
  273. $this->assertInstanceOf(MixedPart::class, $body);
  274. $this->assertCount(2, $related = $body->getParts());
  275. $this->assertInstanceOf(RelatedPart::class, $related[0]);
  276. $this->assertEquals($att, $related[1]);
  277. $this->assertCount(2, $parts = $related[0]->getParts());
  278. $this->assertInstanceOf(AlternativePart::class, $parts[0]);
  279. $generatedHtml = $parts[0]->getParts()[1];
  280. $this->assertContains('cid:'.$parts[1]->getContentId(), $generatedHtml->getBody());
  281. $content = 'html content <img src="cid:test.gif">';
  282. $r = fopen('php://memory', 'r+', false);
  283. fwrite($r, $content);
  284. rewind($r);
  285. $e = new Email();
  286. $e->html($r);
  287. // embedding the same image twice results in one image only in the email
  288. $e->embed($image, 'test.gif');
  289. $e->embed($image, 'test.gif');
  290. $body = $e->getBody();
  291. $this->assertInstanceOf(RelatedPart::class, $body);
  292. // 2 parts only, not 3 (text + embedded image once)
  293. $this->assertCount(2, $parts = $body->getParts());
  294. $this->assertStringMatchesFormat('html content <img src=3D"cid:%s@symfony">', $parts[0]->bodyToString());
  295. }
  296. public function testAttachments()
  297. {
  298. $contents = file_get_contents($name = __DIR__.'/Fixtures/mimetypes/test', 'r');
  299. $att = new DataPart($file = fopen($name, 'r'), 'test');
  300. $inline = (new DataPart($contents, 'test'))->asInline();
  301. $e = new Email();
  302. $e->attach($file, 'test');
  303. $e->embed($contents, 'test');
  304. $this->assertEquals([$att, $inline], $e->getAttachments());
  305. $att = DataPart::fromPath($name, 'test');
  306. $inline = DataPart::fromPath($name, 'test')->asInline();
  307. $e = new Email();
  308. $e->attachFromPath($name);
  309. $e->embedFromPath($name);
  310. $this->assertEquals([$att->bodyToString(), $inline->bodyToString()], array_map(function (DataPart $a) { return $a->bodyToString(); }, $e->getAttachments()));
  311. $this->assertEquals([$att->getPreparedHeaders(), $inline->getPreparedHeaders()], array_map(function (DataPart $a) { return $a->getPreparedHeaders(); }, $e->getAttachments()));
  312. }
  313. public function testSerialize()
  314. {
  315. $r = fopen('php://memory', 'r+', false);
  316. fwrite($r, 'Text content');
  317. rewind($r);
  318. $e = new Email();
  319. $e->from('fabien@symfony.com');
  320. $e->text($r);
  321. $e->html($r);
  322. $contents = file_get_contents($name = __DIR__.'/Fixtures/mimetypes/test', 'r');
  323. $file = fopen($name, 'r');
  324. $e->attach($file, 'test');
  325. $expected = clone $e;
  326. $n = unserialize(serialize($e));
  327. $this->assertEquals($expected->getHeaders(), $n->getHeaders());
  328. $this->assertEquals($e->getBody(), $n->getBody());
  329. }
  330. }