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