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 14:48:27 +00:00
parent 8f3a4836a8
commit 39c9aab64b

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,41 +505,36 @@ class egw_mailer extends Horde_Mime_Mail
} }
try { try {
// vvv until pull request to Horde_Mime_Mail gets approved vvvvvvvvv $flowed = true;
if (!empty($this->_alternativBody) && empty($this->_htmlBody)) // handling of alternativ body
{
$this->_htmlBody = $this->_alternativBody;
unset($this->_alternativBody);
}
if (!empty($this->_alternativBody)) if (!empty($this->_alternativBody))
{ {
parent::send(new Horde_Mail_Transport_Null, true); // true: keep Message-ID $body = new Horde_Mime_Part();
$body->setType('multipart/alternative');
$this->_base[] = $this->_alternativBody; if (!empty($this->_body))
{
/* Build recipients. */ // Send in flowed format.
$recipients = clone $this->_recipients; if ($flowed)
foreach (array('to', 'cc') as $header) { {
if (($h = $this->_headers[$header])) { $text_flowed = new Horde_Text_Flowed($this->_body->getContents(), $this->_body->getCharset());
$recipients->add($h->getAddressList()); $text_flowed->setDelSp(true);
$this->_body->setContentTypeParameter('format', 'flowed');
$this->_body->setContentTypeParameter('DelSp', 'Yes');
$this->_body->setContents($text_flowed->toFlowed());
} }
$body[] = $this->_body;
} }
if ($this->_bcc) { if (!empty($this->_htmlBody))
$recipients->add($this->_bcc); {
$body[] = $this->_htmlBody;
unset($this->_htmlBody);
} }
$body[] = $this->_alternativBody;
/* Trick Horde_Mime_Part into re-generating the message headers. */ unset($this->_alternativBody);
$this->_headers->removeHeader('MIME-Version'); $this->_body = $body;
$flowed = false;
/* 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); // true: keep Message-ID
} }
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
@ -660,7 +660,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()
{ {
@ -669,7 +669,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)
@ -686,6 +692,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;
} }
@ -713,6 +722,32 @@ class egw_mailer extends Horde_Mime_Mail
return parent::addMimePart($part); return parent::addMimePart($part);
} }
/**
* Sets OpenPGP encrypted body according to rfc3156, section 4
*
* @param string $body The message content.
* @link https://tools.ietf.org/html/rfc3156#section-4
*/
public function setOpenPgpBody($body)
{
$this->_body = new Horde_Mime_Part();
$this->_body->setType('multipart/encrypted');
$this->_body->setContentTypeParameter('protocol', 'application/pgp-encrypted');
$this->_body->setContents('');
$part1 = new Horde_Mime_Part();
$part1->setType('application/pgp-encrypted');
$part1->setContents("Version: 1\r\n", array('encoding' => '7bit'));
$this->_body->addPart($part1);
$part2 = new Horde_Mime_Part();
$part2->setType('application/octet-stream');
$part2->setContents($body, array('encoding' => '7bit'));
$this->_body->addPart($part2);
$this->_base = null;
}
/** /**
* Clear all non-standard headers * Clear all non-standard headers
* *