* eSync: fix for messages with no text body e.g. just a PDF or an image, returns empty body with attachment(s)

This commit is contained in:
ralf 2024-03-05 15:29:41 +02:00
parent c44e3f08fa
commit c622e6e88e

View File

@ -954,113 +954,122 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail,
{ {
// fetch the body (try to gather data only once) // fetch the body (try to gather data only once)
$css =''; $css ='';
$bodyStruct = $this->mail->getMessageBody($id, 'html_only', '', null, true,$_folderName); try {
if ($this->debugLevel>2) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' html_only Struct:'.array2string($bodyStruct)); $bodyStruct = $this->mail->getMessageBody($id, 'html_only', '', null, true,$_folderName);
$body = $this->mail->getdisplayableBody($this->mail,$bodyStruct,true,false); if ($this->debugLevel>2) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' html_only Struct:'.array2string($bodyStruct));
if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' html_only:'.$body); $body = $this->mail->getdisplayableBody($this->mail,$bodyStruct,true,false);
if ($body != "" && (is_array($bodyStruct) && $bodyStruct[0]['mimeType']=='text/html')) { if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' html_only:'.$body);
// may be html if ($body != "" && (is_array($bodyStruct) && $bodyStruct[0]['mimeType']=='text/html')) {
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "MIME Body".' Type:html (fetched with html_only)'); // may be html
$css = $this->mail->getStyles($bodyStruct); if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "MIME Body".' Type:html (fetched with html_only)');
$output->nativebodytype=2; $css = $this->mail->getStyles($bodyStruct);
} else { $output->nativebodytype=2;
// plain text Message } else {
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "MIME Body".' Type:plain, fetch text (HTML, if no text available)'); // plain text Message
$output->nativebodytype=1; if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "MIME Body".' Type:plain, fetch text (HTML, if no text available)');
$bodyStruct = $this->mail->getMessageBody($id,'never_display', '', null, true,$_folderName); //'only_if_no_text'); $output->nativebodytype=1;
if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' plain text Struct:'.array2string($bodyStruct)); $bodyStruct = $this->mail->getMessageBody($id,'never_display', '', null, true,$_folderName); //'only_if_no_text');
$body = $this->mail->getdisplayableBody($this->mail,$bodyStruct,false,false); if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' plain text Struct:'.array2string($bodyStruct));
if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' never display html(plain text only):'.$body); $body = $this->mail->getdisplayableBody($this->mail,$bodyStruct,false,false);
} if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' never display html(plain text only):'.$body);
// whatever format decode (using the correct encoding) }
if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__."MIME Body".' Type:'.($output->nativebodytype==2?' html ':' plain ').$body); // whatever format decode (using the correct encoding)
//$body = html_entity_decode($body,ENT_QUOTES,$this->mail->detect_encoding($body)); if ($this->debugLevel>3) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__."MIME Body".' Type:'.($output->nativebodytype==2?' html ':' plain ').$body);
// prepare plaintextbody //$body = html_entity_decode($body,ENT_QUOTES,$this->mail->detect_encoding($body));
$plainBody=''; // prepare plaintextbody
if ($output->nativebodytype == 2) $plainBody='';
{ if ($output->nativebodytype == 2)
$bodyStructplain = $this->mail->getMessageBody($id,'never_display', '', null, true,$_folderName); //'only_if_no_text');
if(isset($bodyStructplain[0])&&isset($bodyStructplain[0]['error'])&&$bodyStructplain[0]['error']==1)
{ {
$plainBody = Api\Mail\Html::convertHTMLToText($body); // always display with preserved HTML $bodyStructplain = $this->mail->getMessageBody($id,'never_display', '', null, true,$_folderName); //'only_if_no_text');
if(isset($bodyStructplain[0])&&isset($bodyStructplain[0]['error'])&&$bodyStructplain[0]['error']==1)
{
$plainBody = Api\Mail\Html::convertHTMLToText($body); // always display with preserved HTML
}
else
{
$plainBody = $this->mail->getdisplayableBody($this->mail,$bodyStructplain,false,false);
}
}
//if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "MIME Body".$body);
$plainBody = preg_replace("/<style.*?<\/style>/is", "", (strlen($plainBody)?$plainBody:$body));
// remove all other html
$plainBody = preg_replace("/<br.*>/is","\r\n",$plainBody);
$plainBody = strip_tags($plainBody);
if ($this->debugLevel>3 && $output->nativebodytype==1) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' Plain Text:'.$plainBody);
//$body = str_replace("\n","\r\n", str_replace("\r","",$body)); // do we need that?
if ($bpReturnType==2) //SYNC_BODYPREFERENCE_HTML
{
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "HTML Body with requested pref 2");
// Send HTML if requested and native type was html
$output->asbody->type = 2;
$htmlbody = '<html>'.
'<head>'.
'<meta name="Generator" content="Z-Push">'.
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'.
$css.
'</head>'.
'<body>';
if ($output->nativebodytype==2)
{
if ($css) Api\Mail\Html::replaceTagsCompletley($body,'style');
// as we fetch html, and body is HTML, we may not need to handle this
$htmlbody .= $body;
}
else
{
// html requested but got only plaintext, so fake html
$htmlbody .= str_replace("\n","<BR>",str_replace("\r","<BR>", str_replace("\r\n","<BR>",$plainBody)));
}
$htmlbody .= '</body>'.
'</html>';
if(isset($truncsize) && strlen($htmlbody) > $truncsize)
{
$htmlbody = Utils::Utf8_truncate($htmlbody,$truncsize);
$output->asbody->truncated = 1;
}
// output->nativebodytype is used as marker that the original message was of type ... but is now converted to, as type 2 is requested.
$output->nativebodytype = 2;
$output->asbody->data = StringStreamWrapper::Open($htmlbody);
$output->asbody->estimatedDataSize = strlen($htmlbody);
} }
else else
{ {
$plainBody = $this->mail->getdisplayableBody($this->mail,$bodyStructplain,false,false); // Send Plaintext as Fallback or if original body is plainttext
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "Plaintext Body:".$plainBody);
/* we use plainBody (set above) instead
$bodyStruct = $this->mail->getMessageBody($id,'only_if_no_text'); //'never_display');
$plain = $this->mail->getdisplayableBody($this->mail,$bodyStruct);
$plain = html_entity_decode($plain,ENT_QUOTES,$this->mail->detect_encoding($plain));
$plain = strip_tags($plain);
//$plain = str_replace("\n","\r\n",str_replace("\r","",$plain));
*/
$output->asbody->type = 1;
$output->nativebodytype = 1;
if(isset($truncsize) &&
strlen($plainBody) > $truncsize)
{
$plainBody = Utils::Utf8_truncate($plainBody, $truncsize);
$output->asbody->truncated = 1;
}
$output->asbody->data = StringStreamWrapper::Open((string)$plainBody !== '' ? $plainBody : ' ');
$output->asbody->estimatedDataSize = strlen($plainBody);
}
// In case we have nothing for the body, send at least a blank...
// dw2412 but only in case the body is not rtf!
if ($output->asbody->type != 3 && !isset($output->asbody->data))
{
$output->asbody->data = StringStreamWrapper::Open(" ");
$output->asbody->estimatedDataSize = 1;
} }
} }
//if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "MIME Body".$body); catch(Api\Exception\WrongParameter $e) {
$plainBody = preg_replace("/<style.*?<\/style>/is", "", (strlen($plainBody)?$plainBody:$body)); // we throw, if body is not found, e.g. whole message is a PDF or an image --> return an empty body (1 space)
// remove all other html
$plainBody = preg_replace("/<br.*>/is","\r\n",$plainBody);
$plainBody = strip_tags($plainBody);
if ($this->debugLevel>3 && $output->nativebodytype==1) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' Plain Text:'.$plainBody);
//$body = str_replace("\n","\r\n", str_replace("\r","",$body)); // do we need that?
if ($bpReturnType==2) //SYNC_BODYPREFERENCE_HTML
{
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "HTML Body with requested pref 2");
// Send HTML if requested and native type was html
$output->asbody->type = 2;
$htmlbody = '<html>'.
'<head>'.
'<meta name="Generator" content="Z-Push">'.
'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'.
$css.
'</head>'.
'<body>';
if ($output->nativebodytype==2)
{
if ($css) Api\Mail\Html::replaceTagsCompletley($body,'style');
// as we fetch html, and body is HTML, we may not need to handle this
$htmlbody .= $body;
}
else
{
// html requested but got only plaintext, so fake html
$htmlbody .= str_replace("\n","<BR>",str_replace("\r","<BR>", str_replace("\r\n","<BR>",$plainBody)));
}
$htmlbody .= '</body>'.
'</html>';
if(isset($truncsize) && strlen($htmlbody) > $truncsize)
{
$htmlbody = Utils::Utf8_truncate($htmlbody,$truncsize);
$output->asbody->truncated = 1;
}
// output->nativebodytype is used as marker that the original message was of type ... but is now converted to, as type 2 is requested.
$output->nativebodytype = 2;
$output->asbody->data = StringStreamWrapper::Open($htmlbody);
$output->asbody->estimatedDataSize = strlen($htmlbody);
}
else
{
// Send Plaintext as Fallback or if original body is plainttext
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG, "Plaintext Body:".$plainBody);
/* we use plainBody (set above) instead
$bodyStruct = $this->mail->getMessageBody($id,'only_if_no_text'); //'never_display');
$plain = $this->mail->getdisplayableBody($this->mail,$bodyStruct);
$plain = html_entity_decode($plain,ENT_QUOTES,$this->mail->detect_encoding($plain));
$plain = strip_tags($plain);
//$plain = str_replace("\n","\r\n",str_replace("\r","",$plain));
*/
$output->asbody->type = 1; $output->asbody->type = 1;
$output->nativebodytype = 1; $output->nativebodytype = 1;
if(isset($truncsize) && $output->asbody->data = StringStreamWrapper::Open($error=' '); //$e->getMessage().' ('.$e->getCode().')');
strlen($plainBody) > $truncsize) $output->asbody->estimatedDataSize = strlen($error);
{
$plainBody = Utils::Utf8_truncate($plainBody, $truncsize);
$output->asbody->truncated = 1;
}
$output->asbody->data = StringStreamWrapper::Open((string)$plainBody !== '' ? $plainBody : ' ');
$output->asbody->estimatedDataSize = strlen($plainBody);
}
// In case we have nothing for the body, send at least a blank...
// dw2412 but only in case the body is not rtf!
if ($output->asbody->type != 3 && !isset($output->asbody->data))
{
$output->asbody->data = StringStreamWrapper::Open(" ");
$output->asbody->estimatedDataSize = 1;
} }
} }
} }