fix charset/transfer encoding issue when sending plain ascii mails but adding utf-8 signature

This commit is contained in:
leithoff 2016-08-05 15:12:21 +02:00
parent 60c34e5881
commit a41896f802
3 changed files with 49 additions and 30 deletions

View File

@ -6959,13 +6959,20 @@ class Mail
* *
* @param Mailer $mailer instance of SMTP Mailer object * @param Mailer $mailer instance of SMTP Mailer object
* @param string|ressource|Horde_Mime_Part $message string or resource containing the RawMessage / object Mail_mimeDecoded message (part)) * @param string|ressource|Horde_Mime_Part $message string or resource containing the RawMessage / object Mail_mimeDecoded message (part))
* @param boolean $force8bitOnPrimaryPart (default false. force transferEncoding and charset to 8bit/utf8 if we have a textpart as primaryPart)
* @throws Exception\WrongParameter when the required Horde_Mail_Part not found * @throws Exception\WrongParameter when the required Horde_Mail_Part not found
*/ */
function parseRawMessageIntoMailObject(Mailer $mailer, $message) function parseRawMessageIntoMailObject(Mailer $mailer, $message, $force8bitOnPrimaryPart=false)
{ {
if (is_string($message) || is_resource($message)) if (is_string($message) || is_resource($message))
{ {
$structure = Horde_Mime_Part::parseMessage($message); $structure = Horde_Mime_Part::parseMessage($message);
//error_log(__METHOD__.__LINE__.'#'.$structure->getPrimaryType().'#');
if ($force8bitOnPrimaryPart&&$structure->getPrimaryType()=='text')
{
$structure->setTransferEncoding('8bit');
$structure->setCharset('utf-8');
}
$mailer->setBasePart($structure); $mailer->setBasePart($structure);
//error_log(__METHOD__.__LINE__.':'.array2string($structure)); //error_log(__METHOD__.__LINE__.':'.array2string($structure));

View File

@ -2977,6 +2977,12 @@ $filter['before']= date("d-M-Y", $cutoffdate2);
$singleBodyPart['body'] = preg_replace($sar,$rar,$singleBodyPart['body']); $singleBodyPart['body'] = preg_replace($sar,$rar,$singleBodyPart['body']);
} }
//error_log(__METHOD__.__LINE__.'reports:'.$singleBodyPart['charSet']); //error_log(__METHOD__.__LINE__.'reports:'.$singleBodyPart['charSet']);
if ($singleBodyPart['charSet']=='us-ascii')
{
$orgCharSet=$singleBodyPart['charSet'];
$singleBodyPart['charSet'] = Api\Translation::detect_encoding($singleBodyPart['body']);
error_log(__METHOD__.__LINE__.'reports:'.$orgCharSet.' but seems to be:'.$singleBodyPart['charSet']);
}
$singleBodyPart['body'] = Api\Translation::convert_jsonsafe($singleBodyPart['body'],$singleBodyPart['charSet']); $singleBodyPart['body'] = Api\Translation::convert_jsonsafe($singleBodyPart['body'],$singleBodyPart['charSet']);
//error_log(__METHOD__.__LINE__.array2string($singleBodyPart)); //error_log(__METHOD__.__LINE__.array2string($singleBodyPart));
if($singleBodyPart['mimeType'] == 'text/plain') if($singleBodyPart['mimeType'] == 'text/plain')

View File

@ -406,10 +406,38 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail,
$activeMailProfile = Mail::getStandardIdentityForProfile($activeMailProfiles,self::$profileID); $activeMailProfile = Mail::getStandardIdentityForProfile($activeMailProfiles,self::$profileID);
ZLog::Write(LOGLEVEL_DEBUG,__METHOD__."(".__LINE__.")".' ProfileID:'.self::$profileID.' ActiveMailProfile:'.array2string($activeMailProfile)); ZLog::Write(LOGLEVEL_DEBUG,__METHOD__."(".__LINE__.")".' ProfileID:'.self::$profileID.' ActiveMailProfile:'.array2string($activeMailProfile));
// collect identity / signature for later usage, and to determine if we may have to manipulate TransferEncoding and Charset
try
{
$acc = Mail\Account::read($this->mail->icServer->ImapServerId);
//error_log(__METHOD__.__LINE__.array2string($acc));
$_signature = Mail\Account::read_identity($acc['ident_id'],true);
}
catch (Exception $e)
{
$_signature=array();
}
$signature = $_signature['ident_signature'];
if ((isset($preferencesArray['disableRulerForSignatureSeparation']) &&
$preferencesArray['disableRulerForSignatureSeparation']) ||
empty($signature) || trim(Api\Mail\Html::convertHTMLToText($signature)) =='')
{
$disableRuler = true;
}
$beforePlain = $beforeHtml = "";
$beforeHtml = ($disableRuler ?'&nbsp;<br>':'&nbsp;<br><hr style="border:dotted 1px silver; width:90%; border:dotted 1px silver;">');
$beforePlain = ($disableRuler ?"\r\n\r\n":"\r\n\r\n-- \r\n");
$sigText = Mail::merge($signature,array($GLOBALS['egw']->accounts->id2name($GLOBALS['egw_info']['user']['account_id'],'person_id')));
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' Signature to use:'.$sigText);
$sigTextHtml = $beforeHtml.$sigText;
$sigTextPlain = $beforePlain.Api\Mail\Html::convertHTMLToText($sigText);
$force8bit=false;
if (Api\Translation::detect_encoding($sigTextPlain)!='ascii') $force8bit=true;
// initialize the new Api\Mailer object for sending // initialize the new Api\Mailer object for sending
$mailObject = new Api\Mailer(self::$profileID); $mailObject = new Api\Mailer(self::$profileID);
$this->mail->parseRawMessageIntoMailObject($mailObject,$smartdata->mime);
$this->mail->parseRawMessageIntoMailObject($mailObject,$smartdata->mime,$force8bit);
// Horde SMTP Class uses utf-8 by default. as we set charset always to utf-8 // Horde SMTP Class uses utf-8 by default. as we set charset always to utf-8
$mailObject->Sender = $activeMailProfile['ident_email']; $mailObject->Sender = $activeMailProfile['ident_email'];
$mailObject->setFrom($activeMailProfile['ident_email'],Mail::generateIdentityString($activeMailProfile,false)); $mailObject->setFrom($activeMailProfile['ident_email'],Mail::generateIdentityString($activeMailProfile,false));
@ -560,32 +588,7 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail,
$body = str_replace("\r",((preg_match("^text/html^i", $ContentType))?'<br>':""),$body); // what is this for? $body = str_replace("\r",((preg_match("^text/html^i", $ContentType))?'<br>':""),$body); // what is this for?
if ($this->debugLevel>2) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' retrieved Body (modified):'.$body); if ($this->debugLevel>2) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' retrieved Body (modified):'.$body);
*/ */
// add signature!! ----------------------------------------------------------------- // actually use prepared signature --------------------collected earlier--------------------------
ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' ActiveMailProfile:'.array2string($activeMailProfile));
try
{
$acc = Mail\Account::read($this->mail->icServer->ImapServerId);
//error_log(__METHOD__.__LINE__.array2string($acc));
$_signature = Mail\Account::read_identity($acc['ident_id'],true);
}
catch (Exception $e)
{
$_signature=array();
}
$signature = $_signature['ident_signature'];
if ((isset($preferencesArray['disableRulerForSignatureSeparation']) &&
$preferencesArray['disableRulerForSignatureSeparation']) ||
empty($signature) || trim(Api\Mail\Html::convertHTMLToText($signature)) =='')
{
$disableRuler = true;
}
$beforePlain = $beforeHtml = "";
$beforeHtml = ($disableRuler ?'&nbsp;<br>':'&nbsp;<br><hr style="border:dotted 1px silver; width:90%; border:dotted 1px silver;">');
$beforePlain = ($disableRuler ?"\r\n\r\n":"\r\n\r\n-- \r\n");
$sigText = Mail::merge($signature,array($GLOBALS['egw']->accounts->id2name($GLOBALS['egw_info']['user']['account_id'],'person_id')));
if ($this->debugLevel>0) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' Signature to use:'.$sigText);
$sigTextHtml = $beforeHtml.$sigText;
$sigTextPlain = $beforePlain.Api\Mail\Html::convertHTMLToText($sigText);
$isreply = $isforward = false; $isreply = $isforward = false;
// reply --------------------------------------------------------------------------- // reply ---------------------------------------------------------------------------
if ($smartdata_task == 'reply' && isset($smartdata->source->itemid) && if ($smartdata_task == 'reply' && isset($smartdata->source->itemid) &&
@ -717,6 +720,7 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail,
// add signature, in case its not already added in forward or reply // add signature, in case its not already added in forward or reply
if (!$isreply && !$isforward) if (!$isreply && !$isforward)
{ {
//error_log(__METHOD__.__LINE__.'adding Signature');
$Body = $Body.$sigTextPlain; $Body = $Body.$sigTextPlain;
$AltBody = $AltBody.$sigTextHtml; $AltBody = $AltBody.$sigTextHtml;
} }
@ -724,11 +728,13 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail,
if ($AltBody && ($html_body = $mailObject->findBody('html'))) if ($AltBody && ($html_body = $mailObject->findBody('html')))
{ {
if ($this->debugLevel>1) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' -> '.$AltBody); if ($this->debugLevel>1) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' -> '.$AltBody);
//error_log(__METHOD__.__LINE__.' html:'.$AltBody);
$html_body->setContents($AltBody,array('encoding'=>Horde_Mime_Part::DEFAULT_ENCODING)); $html_body->setContents($AltBody,array('encoding'=>Horde_Mime_Part::DEFAULT_ENCODING));
} }
if ($Body && ($text_body = $mailObject->findBody('plain'))) if ($Body && ($text_body = $mailObject->findBody('plain')))
{ {
if ($this->debugLevel>1) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' -> '.$Body); if ($this->debugLevel>1) ZLog::Write(LOGLEVEL_DEBUG,__METHOD__.__LINE__.' -> '.$Body);
//error_log(__METHOD__.__LINE__.' text:'.$Body);
$text_body->setContents($Body,array('encoding'=>Horde_Mime_Part::DEFAULT_ENCODING)); $text_body->setContents($Body,array('encoding'=>Horde_Mime_Part::DEFAULT_ENCODING));
} }
//advanced debugging //advanced debugging