fix PHP Fatal on sending with an alternativ body and create multipart/alternativ ourself, if setAlterantivBody() was called

This commit is contained in:
Ralf Becker 2015-10-21 13:10:22 +00:00
parent 798902542e
commit 2f5b40d2d9

View File

@ -302,8 +302,7 @@ class egw_mailer extends Horde_Mime_Mail
/** /**
* Adds an attachment * Adds an attachment
* *
* "text/calendar; method=..." get automatic detected and added as highes priority alternative, * "text/calendar; method=..." get automatic detected and added as highest priority alternative
* overwriting evtl. existing html body!
* *
* @param string|resource $data Path to the attachment or open file-descriptor * @param string|resource $data Path to the attachment or open file-descriptor
* @param string $name The file name to use for the attachment. * @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|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 array $parameters =array() eg. array('method' => 'REQUEST')
* @param string $charset =null default to $this->_charset="utf-8" * @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->setCharset($charset ? $charset : $this->_charset);
$this->_alternativBody->setContents($content); $this->_alternativBody->setContents($content);
$this->_base = null;
} }
/** /**
@ -500,44 +505,28 @@ class egw_mailer extends Horde_Mime_Mail
} }
try { 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 // no flowed for encrypted messages
$flowed = $this->_body && $this->_body->getType() != 'multipart/encrypted'; $flowed = $this->_body && $this->_body->getType() != 'multipart/encrypted';
// vvv until pull request to Horde_Mime_Mail gets approved vvvvvvvvv parent::send($this->account->smtpTransport(), true, $flowed); // true: keep Message-ID
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
}
} }
catch (Exception $e) { catch (Exception $e) {
// in case of errors/exceptions call hook again with previous returned mail_id and error-message to log // 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() function parseBasePart()
{ {
@ -672,7 +661,13 @@ class egw_mailer extends Horde_Mime_Mail
$plain_id = $base->findBody('plain'); $plain_id = $base->findBody('plain');
$html_id = $base->findBody('html'); $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(); $this->clearParts();
foreach($base->partIterator() as $part) foreach($base->partIterator() as $part)
@ -689,6 +684,9 @@ class egw_mailer extends Horde_Mime_Mail
case $html_id: case $html_id:
$this->_htmlBody = $part; $this->_htmlBody = $part;
break; break;
case $alternativ_id:
$this->_alternativBody = $part;
break;
default: default:
$this->_parts[] = $part; $this->_parts[] = $part;
} }