* Mail/Z-Push: handle charset-problem on sending mails with added EGW-Signature

This commit is contained in:
Klaus Leithoff 2016-11-10 12:50:51 +00:00
parent b5b1ffec65
commit 6fee3b919a
2 changed files with 82 additions and 1 deletions

View File

@ -65,6 +65,7 @@ class Mailer extends Horde_Mime_Mail
*
* @param int|Mail\Account|boolean $account =null mail account to use, default use Mail\Account::get_default($smtp=true)
* false: no NOT initialise account and set other EGroupware specific headers, used to parse mails (not sending them!)
* initbasic: return $this
*/
function __construct($account=null)
{
@ -72,7 +73,14 @@ class Mailer extends Horde_Mime_Mail
Preferences::setlocale(LC_MESSAGES);
parent::__construct();
if ($account ==='initbasic')
{
$this->_headers = new Horde_Mime_Headers();
$this->clearAllRecipients();
$this->clearReplyTos();
error_log(__METHOD__.__LINE__.array2string($this));
return $this;
}
if ($account !== false)
{
$this->_headers->setUserAgent('EGroupware API '.$GLOBALS['egw_info']['server']['versions']['api']);
@ -678,6 +686,72 @@ class Mailer extends Horde_Mime_Mail
$this->getBasePart()->toString(array('canonical' => true));
}
/**
* Convert charset of text-parts of message to utf-8. non static AND include Bcc
*
* @param string|resource $message
* @param boolean $stream =false return stream or string (default)
* @param string $charset ='utf-8' charset to convert to
* @param boolean &$converted =false on return if conversation was necessary
* @return string|stream
*/
function convertMessageTextParts($message, $stream=false, $charset='utf-8', &$converted=false)
{
$headers = Horde_Mime_Headers::parseHeaders($message);
$this->addHeaders($headers);
$base = Horde_Mime_Part::parseMessage($message);
foreach($headers->toArray(array('nowrap' => true)) as $header => $value)
{
foreach((array)$value as $n => $val)
{
switch($header)
{
case 'Bcc':
case 'bcc':
//error_log(__METHOD__.__LINE__.':'.$header.'->'.$val);
$this->addBcc($val);
break;
}
}
}
foreach($base->partIterator() as $part)
{
if ($part->getPrimaryType()== 'text')
{
$charset = $part->getContentTypeParameter('charset');
if ($charset && $charset != 'utf-8')
{
$content = Translation::convert($part->toString(array(
'encode' => Horde_Mime_Part::ENCODE_BINARY, // otherwise we cant recode charset
)), $charset, 'utf-8');
$part->setContents($content, array(
'encode' => Horde_Mime_Part::ENCODE_BINARY, // $content is NOT encoded
));
$part->setContentTypeParameter('charset', 'utf-8');
if ($part === $base)
{
$this->addHeader('Content-Type', $part->getType(true));
// need to set Transfer-Encoding used by base-part, it always seems to be "quoted-printable"
$this->addHeader('Content-Transfer-Encoding', 'quoted-printable');
}
$converted = true;
}
}
elseif ($part->getType() == 'message/rfc822')
{
$mailerWithIn = new Mailer('initbasic');
$part->setContents($mailerWithIn->convertMessageTextParts($part->toString(), $stream, $charset, $converted));
}
}
if ($converted)
{
$this->setBasePart($base);
$this->forceBccHeader();
return $this->getRaw($stream);
}
return $message;
}
/**
* Convert charset of text-parts of message to utf-8
*

View File

@ -418,8 +418,15 @@ class mail_zpush implements activesync_plugin_write, activesync_plugin_sendmail,
$force8bit=false;
if (Api\Translation::detect_encoding($sigTextPlain)!='ascii') $force8bit=true;
// beware. the section below might cause trouble regarding bcc and attachments, so maybe this is to be handeled differently
if ($force8bit)
{
$converterObj = new Api\Mailer('initbasic');
$smartdata->mime = $converterObj->convertMessageTextParts($smartdata->mime,false,'utf-8');
}
// initialize the new Api\Mailer object for sending
$mailObject = new Api\Mailer(self::$profileID);
$this->mail->parseRawMessageIntoMailObject($mailObject,$smartdata->mime,$force8bit);
// Horde SMTP Class uses utf-8 by default. as we set charset always to utf-8
$mailObject->Sender = $activeMailProfile['ident_email'];