laminas-mail icon indicating copy to clipboard operation
laminas-mail copied to clipboard

`Message->setBody` sets Content-Type but keeps parameters such as `charset`

Open vlk-charles opened this issue 6 months ago • 3 comments

Bug Report

Q A
Version(s) 2.26

Summary

If a Mail\Message had a single-part body previously and setBody() is called again with a multi-part Mime\Message, the Content-Type gets changed to multipart/mixed but the additional parameters such as charset are kept. I don't think charset is allowed on multipart/mixed or at least it doesn't make much sense.

Current behavior

Content-Type: multipart/mixed;
 charset="UTF-8";
 boundary="=_e3ca65ea3f6735b5b583897c5f6a8609"

How to reproduce

$mailMessage1 = new Laminas\Mail\Message();
$mailMessage2 = new Laminas\Mail\Message();
$mimeMessage = new Laminas\Mime\Message();
$mimeMessage->addPart((new Laminas\Mime\Part('part one'))->setType('text/plain')->setCharset('UTF-8'));
$mailMessage1->setBody($mimeMessage);
$mimeMessage->addPart((new Laminas\Mime\Part('part two'))->setFileName('attachment.txt')->setDisposition(Laminas\Mime\Mime::DISPOSITION_ATTACHMENT)->setEncoding(Laminas\Mime\Mime::ENCODING_BASE64));
$mailMessage1->setBody($mimeMessage);
$mailMessage2->setBody($mimeMessage);

$transport = new Laminas\Mail\Transport\File(new Laminas\Mail\Transport\FileOptions(['path' => '/tmp', 'callback' => function() {return 'mail1.log';}]));
$transport->send($mailMessage1);
$transport->getOptions()->setCallback(function() {return 'mail2.log';});
$transport->send($mailMessage2);
$ for i in 1 2; do head -n 6 </tmp/mail$i.log; echo; done
Date: Fri, 23 Aug 2024 23:55:35 +0000
MIME-Version: 1.0
Content-Type: multipart/mixed;
 charset="UTF-8";
 boundary="=_2bba31450910012ee491af25ee700887"
Content-Transfer-Encoding: 8bit

Date: Fri, 23 Aug 2024 23:55:59 +0000
MIME-Version: 1.0
Content-Type: multipart/mixed;
 boundary="=_2bba31450910012ee491af25ee700887"

This is a message in Mime Format.  If you see this, your mail reader does not support this format.

Note that mailMessage2->setBody was only called at the end and has therefore no charset whereas mailMessage1->setBody was also called intermediately and has charset="UTF-8" set.

Expected behavior

Content-Type: multipart/mixed;
 boundary="=_e3ca65ea3f6735b5b583897c5f6a8609"

vlk-charles avatar Aug 24 '24 00:08 vlk-charles