Merge branch 'support/3.2' into develop

This commit is contained in:
Stephen Abello
2024-11-08 09:45:05 +01:00
2 changed files with 44 additions and 42 deletions

View File

@@ -445,19 +445,6 @@ class EMailLaminas extends Email
$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);
}
@@ -478,22 +465,13 @@ class EMailLaminas extends Email
$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();
}
@@ -504,23 +482,8 @@ class EMailLaminas extends Email
$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)

View File

@@ -45,4 +45,43 @@ class EMailTest extends ItopTestCase {
$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);
}
}