From 2f5b40d2d9c4e60241d05d46de9cbcfff5c21d2c Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Wed, 21 Oct 2015 13:10:22 +0000 Subject: [PATCH] fix PHP Fatal on sending with an alternativ body and create multipart/alternativ ourself, if setAlterantivBody() was called --- phpgwapi/inc/class.egw_mailer.inc.php | 82 +++++++++++++-------------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/phpgwapi/inc/class.egw_mailer.inc.php b/phpgwapi/inc/class.egw_mailer.inc.php index 1a265512ce..26423f2692 100644 --- a/phpgwapi/inc/class.egw_mailer.inc.php +++ b/phpgwapi/inc/class.egw_mailer.inc.php @@ -302,8 +302,7 @@ class egw_mailer extends Horde_Mime_Mail /** * Adds an attachment * - * "text/calendar; method=..." get automatic detected and added as highes priority alternative, - * overwriting evtl. existing html body! + * "text/calendar; method=..." get automatic detected and added as highest priority alternative * * @param string|resource $data Path to the attachment or open file-descriptor * @param string $name The file name to use for the attachment. @@ -426,12 +425,17 @@ class egw_mailer extends Horde_Mime_Mail } /** - * Sets alternativ body, eg. text/calendar has highest / last alternativ + * Highest/last alternativ body part. * - * Until pull request to Horde_Mime_Mail gets approved. + * @var Horde_Mime_Part + */ + protected $_alternativBody; + + /** + * Sets an alternativ body, eg. text/calendar has highest / last alternativ * * @param string|resource $content - * @param string $type eg. "text/calendar" or "text/calendar; method=REQUEST" + * @param string $type eg. "text/calendar" * @param array $parameters =array() eg. array('method' => 'REQUEST') * @param string $charset =null default to $this->_charset="utf-8" */ @@ -445,6 +449,7 @@ class egw_mailer extends Horde_Mime_Mail } $this->_alternativBody->setCharset($charset ? $charset : $this->_charset); $this->_alternativBody->setContents($content); + $this->_base = null; } /** @@ -500,44 +505,28 @@ class egw_mailer extends Horde_Mime_Mail } try { + // handling of alternativ body + if (!empty($this->_alternateBody)) + { + $body = new Horde_Mime_Part(); + $body->setType('multipart/alternative'); + if (!empty($this->_body)) + { + $body[] = $this->_body; + } + if (!empty($this->_htmlBody)) + { + $body[] = $this->_htmlBody; + unset($this->_htmlBody); + } + $body[] = $this->_alternativBody; + unset($this->_alternativBody); + $this->setBody($body); + } // no flowed for encrypted messages $flowed = $this->_body && $this->_body->getType() != 'multipart/encrypted'; - // vvv until pull request to Horde_Mime_Mail gets approved vvvvvvvvv - if (!empty($this->_alternativBody) && empty($this->_htmlBody)) - { - $this->_htmlBody = $this->_alternativBody; - unset($this->_alternativBody); - } - if (!empty($this->_alternativBody)) - { - parent::send(new Horde_Mail_Transport_Null, true, $flowed); // true: keep Message-ID - - $this->_base[] = $this->_alternativBody; - - /* Build recipients. */ - $recipients = clone $this->_recipients; - foreach (array('to', 'cc') as $header) { - if (($h = $this->_headers[$header])) { - $recipients->add($h->getAddressList()); - } - } - if ($this->_bcc) { - $recipients->add($this->_bcc); - } - - /* Trick Horde_Mime_Part into re-generating the message headers. */ - $this->_headers->removeHeader('MIME-Version'); - - /* Send message. */ - $recipients->unique(); - $this->_base->send($recipients->writeAddress(), $this->_headers, $this->account->smtpTransport()); - } - else - // ^^^ until pull request to Horde_Mime_Mail gets approved ^^^^^^^^^ - { - parent::send($this->account->smtpTransport(), true, $flowed); // true: keep Message-ID - } + parent::send($this->account->smtpTransport(), true, $flowed); // true: keep Message-ID } catch (Exception $e) { // in case of errors/exceptions call hook again with previous returned mail_id and error-message to log @@ -663,7 +652,7 @@ class egw_mailer extends Horde_Mime_Mail } /** - * Parse base-part into _body, _htmlBody and _parts to eg. add further attachments + * Parse base-part into _body, _htmlBody, _alternativBody and _parts to eg. add further attachments */ function parseBasePart() { @@ -672,7 +661,13 @@ class egw_mailer extends Horde_Mime_Mail $plain_id = $base->findBody('plain'); $html_id = $base->findBody('html'); - $this->_body = $this->_htmlBody = null; + // find further alternativ part + if ($base->getType() == 'multipart/alternativ' && count($base) !== ($html_id ? $html_id : $plain_id)) + { + $alternativ_id = (string)count($base); + } + + $this->_body = $this->_htmlBody = $this->_alternativBody = null; $this->clearParts(); foreach($base->partIterator() as $part) @@ -689,6 +684,9 @@ class egw_mailer extends Horde_Mime_Mail case $html_id: $this->_htmlBody = $part; break; + case $alternativ_id: + $this->_alternativBody = $part; + break; default: $this->_parts[] = $part; }