Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 N°7916 SF#2274 EmailLaminas.php: Keep charset with part header in multipart email #672

Merged
merged 3 commits into from
Nov 8, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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\Message;
use Laminas\Mail\Protocol\Smtp\Auth\Oauth;
use Laminas\Mail\Transport\File;
Expand Down Expand Up @@ -398,19 +397,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 @@ -431,22 +417,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));
Comment on lines +427 to +428
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar as previous suggestion:

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • First added line should be $oBody = $this->m_oMessage->getBody();.
  • Second added line is missing a ;.
  • First two lines can be combined to $oBody = $this->m_oMessage->getBody()->addPart($oNewPart);. The method returns the object. They call it "fluent interface". It is up to you, if you want 1, 2, or 3 lines. I am fine with either.
  • I would keep the comment about why setBody is called at all. It is not obvious, because the previous addPart already modifies the messages body. What is being done is essentially $this->m_oMessage->setBody($this->m_oMessage->getBody());. The call is only necessary if the body was single-part previously. This ensures that the main Content-Type header gets updated accordingly (here). IMHO, this is a workaround in the library's deficiency.

Everything said applies to the first suggestion as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would keep the comment about why setBody is called at all. It is not obvious, because the previous addPart already modifies the messages body.

That's exactly why I suggested to break that apart to make it more clear 😉
I was indeed a bit too quick, here's a better suggestion:

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

Could also be

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

I thought the idea was clear enough without being lexically/syntaxically correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which one do you prefer?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's up to @Combodo team to decide, I was just suggesting. I'm a person from the community just like you.

PS: You can join us on Slack if you like.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no preferences on this matter, I'll let others answer 😅

}

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave this line together with my following suggestion..


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 @@ -457,23 +434,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));
Comment on lines +443 to +444
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

}

public function SetSubject($sSubject)
Expand Down