mirror of
https://github.com/the-djmaze/snappymail.git
synced 2026-09-08 17:07:03 +03:00
Cleanup mime message builder
This commit is contained in:
parent
119f22ae61
commit
ab5715b536
3 changed files with 58 additions and 73 deletions
|
|
@ -727,7 +727,7 @@ abstract class HtmlUtils
|
||||||
/**
|
/**
|
||||||
* Used by DoSaveMessage() and DoSendMessage()
|
* Used by DoSaveMessage() and DoSendMessage()
|
||||||
*/
|
*/
|
||||||
public static function BuildHtml(string $sHtml, array &$aFoundCids = array(), &$mFoundDataURL = null, array &$aFoundContentLocationUrls = array()) : string
|
public static function BuildHtml(string $sHtml, array &$aFoundCids, array &$aFoundDataURL, array &$aFoundContentLocationUrls) : string
|
||||||
{
|
{
|
||||||
$oDom = static::GetDomFromText($sHtml);
|
$oDom = static::GetDomFromText($sHtml);
|
||||||
|
|
||||||
|
|
@ -842,13 +842,13 @@ abstract class HtmlUtils
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ('img' === $sTagNameLower && \is_array($mFoundDataURL))
|
if ('img' === $sTagNameLower)
|
||||||
{
|
{
|
||||||
$sSrc = $oElement->getAttribute('src');
|
$sSrc = $oElement->getAttribute('src');
|
||||||
if ('data:image/' === \strtolower(\substr($sSrc, 0, 11)))
|
if ('data:image/' === \strtolower(\substr($sSrc, 0, 11)))
|
||||||
{
|
{
|
||||||
$sHash = \md5($sSrc);
|
$sHash = \md5($sSrc);
|
||||||
$mFoundDataURL[$sHash] = $sSrc;
|
$aFoundDataURL[$sHash] = $sSrc;
|
||||||
|
|
||||||
$oElement->setAttribute('src', 'cid:'.$sHash);
|
$oElement->setAttribute('src', 'cid:'.$sHash);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -114,6 +114,17 @@ class Message
|
||||||
return new EmailCollection;
|
return new EmailCollection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function GetCc() : ?EmailCollection
|
||||||
|
{
|
||||||
|
if (isset($this->aHeadersValue[Enumerations\Header::CC]) &&
|
||||||
|
$this->aHeadersValue[Enumerations\Header::CC] instanceof EmailCollection)
|
||||||
|
{
|
||||||
|
return $this->aHeadersValue[Enumerations\Header::CC]->Unique();
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public function GetBcc() : ?EmailCollection
|
public function GetBcc() : ?EmailCollection
|
||||||
{
|
{
|
||||||
if (isset($this->aHeadersValue[Enumerations\Header::BCC]) &&
|
if (isset($this->aHeadersValue[Enumerations\Header::BCC]) &&
|
||||||
|
|
|
||||||
|
|
@ -915,21 +915,7 @@ trait Messages
|
||||||
|
|
||||||
private function buildMessage(Account $oAccount, bool $bWithDraftInfo = true) : \MailSo\Mime\Message
|
private function buildMessage(Account $oAccount, bool $bWithDraftInfo = true) : \MailSo\Mime\Message
|
||||||
{
|
{
|
||||||
$sIdentityID = $this->GetActionParam('IdentityID', '');
|
|
||||||
$sTo = $this->GetActionParam('To', '');
|
|
||||||
$sCc = $this->GetActionParam('Cc', '');
|
|
||||||
$sBcc = $this->GetActionParam('Bcc', '');
|
|
||||||
$sReplyTo = $this->GetActionParam('ReplyTo', '');
|
|
||||||
$sSubject = $this->GetActionParam('Subject', '');
|
|
||||||
$bTextIsHtml = '1' === $this->GetActionParam('TextIsHtml', '0');
|
$bTextIsHtml = '1' === $this->GetActionParam('TextIsHtml', '0');
|
||||||
$bReadReceiptRequest = '1' === $this->GetActionParam('ReadReceiptRequest', '0');
|
|
||||||
$bMarkAsImportant = '1' === $this->GetActionParam('MarkAsImportant', '0');
|
|
||||||
$sText = $this->GetActionParam('Text', '');
|
|
||||||
$aAttachments = $this->GetActionParam('Attachments', null);
|
|
||||||
|
|
||||||
$aDraftInfo = $this->GetActionParam('DraftInfo', null);
|
|
||||||
$sInReplyTo = $this->GetActionParam('InReplyTo', '');
|
|
||||||
$sReferences = $this->GetActionParam('References', '');
|
|
||||||
|
|
||||||
$oMessage = new \MailSo\Mime\Message();
|
$oMessage = new \MailSo\Mime\Message();
|
||||||
|
|
||||||
|
|
@ -940,7 +926,7 @@ trait Messages
|
||||||
$oMessage->DoesNotAddDefaultXMailer();
|
$oMessage->DoesNotAddDefaultXMailer();
|
||||||
}
|
}
|
||||||
|
|
||||||
$oFromIdentity = $this->GetIdentityByID($oAccount, $sIdentityID);
|
$oFromIdentity = $this->GetIdentityByID($oAccount, $this->GetActionParam('IdentityID', ''));
|
||||||
if ($oFromIdentity)
|
if ($oFromIdentity)
|
||||||
{
|
{
|
||||||
$oMessage->SetFrom(new \MailSo\Mime\Email(
|
$oMessage->SetFrom(new \MailSo\Mime\Email(
|
||||||
|
|
@ -957,81 +943,72 @@ trait Messages
|
||||||
$oFrom = $oMessage->GetFrom();
|
$oFrom = $oMessage->GetFrom();
|
||||||
$oMessage->RegenerateMessageId($oFrom ? $oFrom->GetDomain() : '');
|
$oMessage->RegenerateMessageId($oFrom ? $oFrom->GetDomain() : '');
|
||||||
|
|
||||||
if (!empty($sReplyTo))
|
$oReplyTo = new \MailSo\Mime\EmailCollection($this->GetActionParam('ReplyTo', ''));
|
||||||
{
|
if ($oReplyTo->count()) {
|
||||||
$oReplyTo = new \MailSo\Mime\EmailCollection($sReplyTo);
|
$oMessage->SetReplyTo($oReplyTo);
|
||||||
if ($oReplyTo && 0 < $oReplyTo->Count())
|
|
||||||
{
|
|
||||||
$oMessage->SetReplyTo($oReplyTo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($bReadReceiptRequest)
|
if ('1' === $this->GetActionParam('ReadReceiptRequest', '0')) {
|
||||||
{
|
|
||||||
// Read Receipts Reference Main Account Email, Not Identities #147
|
// Read Receipts Reference Main Account Email, Not Identities #147
|
||||||
// $oMessage->SetReadReceipt(($oFromIdentity ?: $oAccount)->Email());
|
// $oMessage->SetReadReceipt(($oFromIdentity ?: $oAccount)->Email());
|
||||||
$oMessage->SetReadReceipt($oFrom->GetEmail());
|
$oMessage->SetReadReceipt($oFrom->GetEmail());
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($bMarkAsImportant)
|
if ('1' === $this->GetActionParam('MarkAsImportant', '0')) {
|
||||||
{
|
|
||||||
$oMessage->SetPriority(\MailSo\Mime\Enumerations\MessagePriority::HIGH);
|
$oMessage->SetPriority(\MailSo\Mime\Enumerations\MessagePriority::HIGH);
|
||||||
}
|
}
|
||||||
|
|
||||||
$oMessage->SetSubject($sSubject);
|
$oMessage->SetSubject($this->GetActionParam('Subject', ''));
|
||||||
|
|
||||||
$oToEmails = new \MailSo\Mime\EmailCollection($sTo);
|
$oToEmails = new \MailSo\Mime\EmailCollection($this->GetActionParam('To', ''));
|
||||||
if ($oToEmails && $oToEmails->Count())
|
if ($oToEmails->count()) {
|
||||||
{
|
|
||||||
$oMessage->SetTo($oToEmails);
|
$oMessage->SetTo($oToEmails);
|
||||||
}
|
}
|
||||||
|
|
||||||
$oCcEmails = new \MailSo\Mime\EmailCollection($sCc);
|
$oCcEmails = new \MailSo\Mime\EmailCollection($this->GetActionParam('Cc', ''));
|
||||||
if ($oCcEmails && $oCcEmails->Count())
|
if ($oCcEmails->count()) {
|
||||||
{
|
|
||||||
$oMessage->SetCc($oCcEmails);
|
$oMessage->SetCc($oCcEmails);
|
||||||
}
|
}
|
||||||
|
|
||||||
$oBccEmails = new \MailSo\Mime\EmailCollection($sBcc);
|
$oBccEmails = new \MailSo\Mime\EmailCollection($this->GetActionParam('Bcc', ''));
|
||||||
if ($oBccEmails && $oBccEmails->Count())
|
if ($oBccEmails->count()) {
|
||||||
{
|
|
||||||
$oMessage->SetBcc($oBccEmails);
|
$oMessage->SetBcc($oBccEmails);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$aDraftInfo = $this->GetActionParam('DraftInfo', null);
|
||||||
if ($bWithDraftInfo && \is_array($aDraftInfo) && !empty($aDraftInfo[0]) && !empty($aDraftInfo[1]) && !empty($aDraftInfo[2]))
|
if ($bWithDraftInfo && \is_array($aDraftInfo) && !empty($aDraftInfo[0]) && !empty($aDraftInfo[1]) && !empty($aDraftInfo[2]))
|
||||||
{
|
{
|
||||||
$oMessage->SetDraftInfo($aDraftInfo[0], $aDraftInfo[1], $aDraftInfo[2]);
|
$oMessage->SetDraftInfo($aDraftInfo[0], $aDraftInfo[1], $aDraftInfo[2]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\strlen($sInReplyTo))
|
$sInReplyTo = $this->GetActionParam('InReplyTo', '');
|
||||||
{
|
if (\strlen($sInReplyTo)) {
|
||||||
$oMessage->SetInReplyTo($sInReplyTo);
|
$oMessage->SetInReplyTo($sInReplyTo);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (\strlen($sReferences))
|
$sReferences = $this->GetActionParam('References', '');
|
||||||
{
|
if (\strlen($sReferences)) {
|
||||||
$oMessage->SetReferences($sReferences);
|
$oMessage->SetReferences($sReferences);
|
||||||
}
|
}
|
||||||
|
|
||||||
$aFoundCids = array();
|
$aFoundCids = array();
|
||||||
$mFoundDataURL = array();
|
$aFoundDataURL = array();
|
||||||
$aFoundContentLocationUrls = array();
|
$aFoundContentLocationUrls = array();
|
||||||
|
|
||||||
$sTextToAdd = $bTextIsHtml ?
|
$sText = $this->GetActionParam('Text', '');
|
||||||
\MailSo\Base\HtmlUtils::BuildHtml($sText, $aFoundCids, $mFoundDataURL, $aFoundContentLocationUrls) : $sText;
|
if ($bTextIsHtml) {
|
||||||
|
$sText = \MailSo\Base\HtmlUtils::BuildHtml($sText, $aFoundCids, $aFoundDataURL, $aFoundContentLocationUrls);
|
||||||
|
}
|
||||||
$this->Plugins()->RunHook($bTextIsHtml ? 'filter.message-html' : 'filter.message-plain',
|
$this->Plugins()->RunHook($bTextIsHtml ? 'filter.message-html' : 'filter.message-plain',
|
||||||
array($oAccount, $oMessage, &$sTextToAdd));
|
array($oAccount, $oMessage, &$sText));
|
||||||
|
if ($bTextIsHtml && \strlen($sText)) {
|
||||||
if ($bTextIsHtml && \strlen($sTextToAdd))
|
$sTextConverted = \MailSo\Base\HtmlUtils::ConvertHtmlToPlain($sText);
|
||||||
{
|
|
||||||
$sTextConverted = \MailSo\Base\HtmlUtils::ConvertHtmlToPlain($sTextToAdd);
|
|
||||||
$this->Plugins()->RunHook('filter.message-plain', array($oAccount, $oMessage, &$sTextConverted));
|
$this->Plugins()->RunHook('filter.message-plain', array($oAccount, $oMessage, &$sTextConverted));
|
||||||
$oMessage->AddText($sTextConverted, false);
|
$oMessage->AddText($sTextConverted, false);
|
||||||
}
|
}
|
||||||
|
$oMessage->AddText($sText, $bTextIsHtml);
|
||||||
|
|
||||||
$oMessage->AddText($sTextToAdd, $bTextIsHtml);
|
$aAttachments = $this->GetActionParam('Attachments', null);
|
||||||
|
|
||||||
if (\is_array($aAttachments))
|
if (\is_array($aAttachments))
|
||||||
{
|
{
|
||||||
foreach ($aAttachments as $sTempName => $aData)
|
foreach ($aAttachments as $sTempName => $aData)
|
||||||
|
|
@ -1056,30 +1033,27 @@ trait Messages
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($mFoundDataURL && \is_array($mFoundDataURL) && \count($mFoundDataURL))
|
foreach ($aFoundDataURL as $sCidHash => $sDataUrlString)
|
||||||
{
|
{
|
||||||
foreach ($mFoundDataURL as $sCidHash => $sDataUrlString)
|
$aMatch = array();
|
||||||
|
$sCID = '<'.$sCidHash.'>';
|
||||||
|
if (\preg_match('/^data:(image\/[a-zA-Z0-9]+);base64,(.+)$/i', $sDataUrlString, $aMatch) &&
|
||||||
|
!empty($aMatch[1]) && !empty($aMatch[2]))
|
||||||
{
|
{
|
||||||
$aMatch = array();
|
$sRaw = \MailSo\Base\Utils::Base64Decode($aMatch[2]);
|
||||||
$sCID = '<'.$sCidHash.'>';
|
$iFileSize = \strlen($sRaw);
|
||||||
if (\preg_match('/^data:(image\/[a-zA-Z0-9]+);base64,(.+)$/i', $sDataUrlString, $aMatch) &&
|
if (0 < $iFileSize)
|
||||||
!empty($aMatch[1]) && !empty($aMatch[2]))
|
|
||||||
{
|
{
|
||||||
$sRaw = \MailSo\Base\Utils::Base64Decode($aMatch[2]);
|
$sFileName = \preg_replace('/[^a-z0-9]+/i', '.', $aMatch[1]);
|
||||||
$iFileSize = \strlen($sRaw);
|
$rResource = \MailSo\Base\ResourceRegistry::CreateMemoryResourceFromString($sRaw);
|
||||||
if (0 < $iFileSize)
|
|
||||||
{
|
|
||||||
$sFileName = \preg_replace('/[^a-z0-9]+/i', '.', $aMatch[1]);
|
|
||||||
$rResource = \MailSo\Base\ResourceRegistry::CreateMemoryResourceFromString($sRaw);
|
|
||||||
|
|
||||||
$sRaw = '';
|
$sRaw = '';
|
||||||
unset($sRaw);
|
unset($sRaw);
|
||||||
unset($aMatch);
|
unset($aMatch);
|
||||||
|
|
||||||
$oMessage->Attachments()->append(
|
$oMessage->Attachments()->append(
|
||||||
new \MailSo\Mime\Attachment($rResource, $sFileName, $iFileSize, true, true, $sCID)
|
new \MailSo\Mime\Attachment($rResource, $sFileName, $iFileSize, true, true, $sCID)
|
||||||
);
|
);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue