Skip to content

Commit

Permalink
Merge branch 'support/3.1' into support/3.2
Browse files Browse the repository at this point in the history
# Conflicts:
#	sources/Core/Email/EmailLaminas.php
  • Loading branch information
steffunky committed Nov 8, 2024
2 parents 82bc2f3 + c70d62a commit ab93d59
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 42 deletions.
47 changes: 5 additions & 42 deletions sources/Core/Email/EmailLaminas.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,19 +445,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 @@ -478,22 +465,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 @@ -504,23 +482,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 ab93d59

Please sign in to comment.