@@ -246,76 +246,116 @@ public function testGetBody()
246
246
$ this ->assertEquals ($ text , $ e ->getBody ());
247
247
}
248
248
249
- public function testGenerateBody ()
249
+ public function testGenerateBodyWithTextOnly ()
250
250
{
251
251
$ text = new TextPart ('text content ' );
252
- $ html = new TextPart ('html content ' , 'utf-8 ' , 'html ' );
253
- $ att = new DataPart ($ file = fopen (__DIR__ .'/Fixtures/mimetypes/test ' , 'r ' ));
254
- $ img = new DataPart ($ image = fopen (__DIR__ .'/Fixtures/mimetypes/test.gif ' , 'r ' ), 'test.gif ' );
255
-
256
252
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
257
253
$ e ->text ('text content ' );
258
254
$ this ->assertEquals ($ text , $ e ->getBody ());
259
255
$ this ->assertEquals ('text content ' , $ e ->getTextBody ());
256
+ }
260
257
258
+ public function testGenerateBodyWithHtmlOnly ()
259
+ {
260
+ $ html = new TextPart ('html content ' , 'utf-8 ' , 'html ' );
261
261
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
262
262
$ e ->html ('html content ' );
263
263
$ this ->assertEquals ($ html , $ e ->getBody ());
264
264
$ this ->assertEquals ('html content ' , $ e ->getHtmlBody ());
265
+ }
265
266
267
+ public function testGenerateBodyWithTextAndHtml ()
268
+ {
269
+ $ text = new TextPart ('text content ' );
270
+ $ html = new TextPart ('html content ' , 'utf-8 ' , 'html ' );
266
271
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
267
272
$ e ->html ('html content ' );
268
273
$ e ->text ('text content ' );
269
274
$ this ->assertEquals (new AlternativePart ($ text , $ html ), $ e ->getBody ());
275
+ }
270
276
277
+ public function testGenerateBodyWithTextAndHtmlNotUtf8 ()
278
+ {
271
279
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
272
280
$ e ->html ('html content ' , 'iso-8859-1 ' );
273
281
$ e ->text ('text content ' , 'iso-8859-1 ' );
274
282
$ this ->assertEquals ('iso-8859-1 ' , $ e ->getTextCharset ());
275
283
$ this ->assertEquals ('iso-8859-1 ' , $ e ->getHtmlCharset ());
276
284
$ this ->assertEquals (new AlternativePart (new TextPart ('text content ' , 'iso-8859-1 ' ), new TextPart ('html content ' , 'iso-8859-1 ' , 'html ' )), $ e ->getBody ());
285
+ }
277
286
287
+ public function testGenerateBodyWithTextContentAndAttachedFile ()
288
+ {
289
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
278
290
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
279
291
$ e ->attach ($ file );
280
292
$ e ->text ('text content ' );
281
- $ this ->assertEquals (new MixedPart ($ text , $ att ), $ e ->getBody ());
293
+ $ this ->assertEquals (new MixedPart ($ text , $ filePart ), $ e ->getBody ());
294
+ }
282
295
296
+ public function testGenerateBodyWithHtmlContentAndAttachedFile ()
297
+ {
298
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
283
299
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
284
300
$ e ->attach ($ file );
285
301
$ e ->html ('html content ' );
286
- $ this ->assertEquals (new MixedPart ($ html , $ att ), $ e ->getBody ());
302
+ $ this ->assertEquals (new MixedPart ($ html , $ filePart ), $ e ->getBody ());
303
+ }
287
304
305
+ public function testGenerateBodyWithAttachedFileOnly ()
306
+ {
307
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
288
308
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
289
309
$ e ->attach ($ file );
290
- $ this ->assertEquals (new MixedPart ($ att ), $ e ->getBody ());
310
+ $ this ->assertEquals (new MixedPart ($ filePart ), $ e ->getBody ());
311
+ }
291
312
313
+ public function testGenerateBodyWithTextAndHtmlContentAndAttachedFile ()
314
+ {
315
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
292
316
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
293
317
$ e ->html ('html content ' );
294
318
$ e ->text ('text content ' );
295
319
$ e ->attach ($ file );
296
- $ this ->assertEquals (new MixedPart (new AlternativePart ($ text , $ html ), $ att ), $ e ->getBody ());
320
+ $ this ->assertEquals (new MixedPart (new AlternativePart ($ text , $ html ), $ filePart ), $ e ->getBody ());
321
+ }
297
322
323
+ public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImageNotReferenced ()
324
+ {
325
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
298
326
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
299
327
$ e ->html ('html content ' );
300
328
$ e ->text ('text content ' );
301
329
$ e ->attach ($ file );
302
330
$ e ->attach ($ image , 'test.gif ' );
303
- $ this ->assertEquals (new MixedPart (new AlternativePart ($ text , $ html ), $ att , $ img ), $ e ->getBody ());
331
+ $ this ->assertEquals (new MixedPart (new AlternativePart ($ text , $ html ), $ filePart , $ imagePart ), $ e ->getBody ());
332
+ }
304
333
334
+ public function testGenerateBodyWithTextAndAttachedFileAndAttachedImageNotReferenced ()
335
+ {
336
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
305
337
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
306
338
$ e ->text ('text content ' );
307
339
$ e ->attach ($ file );
308
340
$ e ->attach ($ image , 'test.gif ' );
309
- $ this ->assertEquals (new MixedPart ($ text , $ att , $ img ), $ e ->getBody ());
341
+ $ this ->assertEquals (new MixedPart ($ text , $ filePart , $ imagePart ), $ e ->getBody ());
342
+ }
310
343
344
+ public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImageNotReferencedViaCid ()
345
+ {
346
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
311
347
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
312
348
$ e ->html ($ content = 'html content <img src="test.gif"> ' );
313
349
$ e ->text ('text content ' );
314
350
$ e ->attach ($ file );
315
351
$ e ->attach ($ image , 'test.gif ' );
316
352
$ fullhtml = new TextPart ($ content , 'utf-8 ' , 'html ' );
317
- $ this ->assertEquals (new MixedPart (new AlternativePart ($ text , $ fullhtml ), $ att , $ img ), $ e ->getBody ());
353
+ $ this ->assertEquals (new MixedPart (new AlternativePart ($ text , $ fullhtml ), $ filePart , $ imagePart ), $ e ->getBody ());
354
+ }
318
355
356
+ public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImageReferencedViaCid ()
357
+ {
358
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
319
359
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
320
360
$ e ->html ($ content = 'html content <img src="cid:test.gif"> ' );
321
361
$ e ->text ('text content ' );
@@ -325,12 +365,35 @@ public function testGenerateBody()
325
365
$ this ->assertInstanceOf (MixedPart::class, $ body );
326
366
$ this ->assertCount (2 , $ related = $ body ->getParts ());
327
367
$ this ->assertInstanceOf (RelatedPart::class, $ related [0 ]);
328
- $ this ->assertEquals ($ att , $ related [1 ]);
368
+ $ this ->assertEquals ($ filePart , $ related [1 ]);
329
369
$ this ->assertCount (2 , $ parts = $ related [0 ]->getParts ());
330
370
$ this ->assertInstanceOf (AlternativePart::class, $ parts [0 ]);
331
371
$ generatedHtml = $ parts [0 ]->getParts ()[1 ];
332
372
$ this ->assertStringContainsString ('cid: ' .$ parts [1 ]->getContentId (), $ generatedHtml ->getBody ());
373
+ }
333
374
375
+ public function testGenerateBodyWithTextAndHtmlAndAttachedFileAndAttachedImagePartAsInlineReferencedViaCid ()
376
+ {
377
+ [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ] = $ this ->generateSomeParts ();
378
+ $ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
379
+ $ e ->html ($ content = 'html content <img src="cid:test.gif"> ' );
380
+ $ e ->text ('text content ' );
381
+ $ e ->attach ($ file );
382
+ $ e ->attachPart ((new DataPart ($ image , 'test.gif ' ))->asInline ());
383
+ $ body = $ e ->getBody ();
384
+ $ this ->assertInstanceOf (MixedPart::class, $ body );
385
+ $ this ->assertCount (2 , $ related = $ body ->getParts ());
386
+ $ this ->assertInstanceOf (RelatedPart::class, $ related [0 ]);
387
+ $ this ->assertEquals ($ filePart , $ related [1 ]);
388
+ $ this ->assertCount (2 , $ parts = $ related [0 ]->getParts ());
389
+ $ this ->assertInstanceOf (AlternativePart::class, $ parts [0 ]);
390
+ $ generatedHtml = $ parts [0 ]->getParts ()[1 ];
391
+ $ this ->assertStringContainsString ('cid: ' .$ parts [1 ]->getContentId (), $ generatedHtml ->getBody ());
392
+ }
393
+
394
+ public function testGenerateBodyWithHtmlAndInlinedImageTwiceReferencedViaCid ()
395
+ {
396
+ // inline image (twice) referenced in the HTML content
334
397
$ content = 'html content <img src="cid:test.gif"> ' ;
335
398
$ r = fopen ('php://memory ' , 'r+ ' , false );
336
399
fwrite ($ r , $ content );
@@ -339,6 +402,7 @@ public function testGenerateBody()
339
402
$ e = (
new Email ())->
from (
'[email protected] ' )->
to (
'[email protected] ' );
340
403
$ e ->html ($ r );
341
404
// embedding the same image twice results in one image only in the email
405
+ $ image = fopen (__DIR__ .'/Fixtures/mimetypes/test.gif ' , 'r ' );
342
406
$ e ->embed ($ image , 'test.gif ' );
343
407
$ e ->embed ($ image , 'test.gif ' );
344
408
$ body = $ e ->getBody ();
@@ -348,8 +412,19 @@ public function testGenerateBody()
348
412
$ this ->assertStringMatchesFormat ('html content <img src=3D"cid:%s@symfony"> ' , $ parts [0 ]->bodyToString ());
349
413
}
350
414
415
+ private function generateSomeParts (): array
416
+ {
417
+ $ text = new TextPart ('text content ' );
418
+ $ html = new TextPart ('html content ' , 'utf-8 ' , 'html ' );
419
+ $ filePart = new DataPart ($ file = fopen (__DIR__ .'/Fixtures/mimetypes/test ' , 'r ' ));
420
+ $ imagePart = new DataPart ($ image = fopen (__DIR__ .'/Fixtures/mimetypes/test.gif ' , 'r ' ), 'test.gif ' );
421
+
422
+ return [$ text , $ html , $ filePart , $ file , $ imagePart , $ image ];
423
+ }
424
+
351
425
public function testAttachments ()
352
426
{
427
+ // inline part
353
428
$ contents = file_get_contents ($ name = __DIR__ .'/Fixtures/mimetypes/test ' , 'r ' );
354
429
$ att = new DataPart ($ file = fopen ($ name , 'r ' ), 'test ' );
355
430
$ inline = (new DataPart ($ contents , 'test ' ))->asInline ();
@@ -358,6 +433,7 @@ public function testAttachments()
358
433
$ e ->embed ($ contents , 'test ' );
359
434
$ this ->assertEquals ([$ att , $ inline ], $ e ->getAttachments ());
360
435
436
+ // inline part from path
361
437
$ att = DataPart::fromPath ($ name , 'test ' );
362
438
$ inline = DataPart::fromPath ($ name , 'test ' )->asInline ();
363
439
$ e = new Email ();
0 commit comments