Skip to content

Commit

Permalink
🐛 N°7916 SF#2274 EmailLaminas.php: Keep charset with part header in m…
Browse files Browse the repository at this point in the history
…ultipart email (#672)

* 🐛 N°2274 EmailLaminas.php: Keep charset with part header in multipart email

* Add a unit test

---------

Co-authored-by: Stephen Abello <stephen.abello@combodo.com>
  • Loading branch information
vlk-charles and steffunky authored Nov 8, 2024
1 parent 0d5ff26 commit c70d62a
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 43 deletions.
48 changes: 5 additions & 43 deletions sources/Core/Email/EmailLaminas.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

use Combodo\iTop\Core\Authentication\Client\OAuth\OAuthClientProviderFactory;
use Laminas\Mail\Header\ContentType;
use Laminas\Mail\Header\InReplyTo;
use Laminas\Mail\Header\MessageId;
use Laminas\Mail\Message;
Expand Down Expand Up @@ -404,19 +403,6 @@ public function SetBody($sBody, $sMimeType = Mime::TYPE_HTML, $sCustomStyles = n
$oBody->addPart($oAdditionalPart);
}

if ($oBody->isMultiPart()) {
$oContentTypeHeader = $this->m_oMessage->getHeaders();
foreach ($oContentTypeHeader as $oHeader) {
if (!$oHeader instanceof ContentType) {
continue;
}

$oHeader->setType(Mime::MULTIPART_MIXED);
$oHeader->addParameter('boundary', $oBody->getMime()->boundary());
break;
}
}

$this->m_oMessage->setBody($oBody);
}

Expand All @@ -437,22 +423,13 @@ public function AddPart($sText, $sMimeType = Mime::TYPE_HTML)
$oNewPart = new Part($sText);
$oNewPart->encoding = Mime::ENCODING_8BIT;
$oNewPart->type = $sMimeType;
$this->m_oMessage->getBody()->addPart($oNewPart);

// setBody called only to refresh Content-Type to multipart/mixed
$this->m_oMessage->setBody($this->m_oMessage->getBody()->addPart($oNewPart));
}

public function AddAttachment($data, $sFileName, $sMimeType)
{
$oBody = $this->m_oMessage->getBody();

if (!$oBody->isMultiPart()) {
$multipart_content = new Part($oBody->generateMessage());
$multipart_content->setType($oBody->getParts()[0]->getType());
$multipart_content->setBoundary($oBody->getMime()->boundary());

$oBody = new Laminas\Mime\Message();
$oBody->addPart($multipart_content);
}

if (!array_key_exists('attachments', $this->m_aData)) {
$this->m_aData['attachments'] = array();
}
Expand All @@ -463,23 +440,8 @@ public function AddAttachment($data, $sFileName, $sMimeType)
$oNewAttachment->disposition = Mime::DISPOSITION_ATTACHMENT;
$oNewAttachment->encoding = Mime::ENCODING_BASE64;


$oBody->addPart($oNewAttachment);

if ($oBody->isMultiPart()) {
$oContentTypeHeader = $this->m_oMessage->getHeaders();
foreach ($oContentTypeHeader as $oHeader) {
if (!$oHeader instanceof ContentType) {
continue;
}

$oHeader->setType(Mime::MULTIPART_MIXED);
$oHeader->addParameter('boundary', $oBody->getMime()->boundary());
break;
}
}

$this->m_oMessage->setBody($oBody);
// setBody called only to refresh Content-Type to multipart/mixed
$this->m_oMessage->setBody($this->m_oMessage->getBody()->addPart($oNewAttachment));
}

public function SetSubject($sSubject)
Expand Down
39 changes: 39 additions & 0 deletions tests/php-unit-tests/unitary-tests/core/EMailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,43 @@ public function testCheckHeadersOnSendEmail(): void
$oConfig->Set('email_transport', $sCurrentEmailTransport);
$oConfig->Set('email_asynchronous', $sCurrentEmailAsync);
}

/**
* @return void
* @throws \ConfigException
* @throws \CoreException
* @covers Email::SetBody()
* @covers Email::Send()
*/
public function testCheckPartsHeadersOnSendEmailWithAttachment(): void
{
$oConfig = utils::GetConfig();
$sCurrentEmailTransport = $oConfig->Get('email_transport');
$sCurrentEmailAsync = $oConfig->Get('email_asynchronous');

// Set our email transport to file, so we can read it after
$oConfig->Set('email_transport', 'LogFile');
$oConfig->Set('email_asynchronous', false);

$oEmail = new Email();
$oEmail->SetRecipientTO('email@email.com');
$oEmail->SetRecipientFrom('email2@email2.com');
$oEmail->SetSubject('dummy subject');
$oEmail->SetBody('dummy body', 'text/plain');
$oEmail->AddAttachment('Dummy attachment', 'attachment.txt', 'text/plain');

// Send the mail and check if there's any issue
$aIssues = [];
$oEmail->Send($aIssues);
$this->assertEmpty($aIssues);

// Check if our charset is correctly set
// We know this file may be used by other future test, but as we can't configure output filename, it is what it is
$sEmailContent = file_get_contents(APPROOT.'log/mail.log');
$this->assertStringContainsString('Content-Type: text/plain; charset=UTF-8', $sEmailContent);

// Set our previous email transport value back, so it doesn't affect other tests
$oConfig->Set('email_transport', $sCurrentEmailTransport);
$oConfig->Set('email_asynchronous', $sCurrentEmailAsync);
}
}

0 comments on commit c70d62a

Please sign in to comment.