allow to add all sorts of attachments as stream not just as strings

This commit is contained in:
Ralf Becker 2015-09-11 08:39:00 +00:00
parent a67b3c24df
commit 342b717a04

View File

@ -305,14 +305,14 @@ class egw_mailer extends Horde_Mime_Mail
* "text/calendar; method=..." get automatic detected and added as highes priority alternative, * "text/calendar; method=..." get automatic detected and added as highes priority alternative,
* overwriting evtl. existing html body! * overwriting evtl. existing html body!
* *
* @param string $file The path to the file. * @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.
* @param string $type The content type of the file. * @param string $type The content type of the file.
* @param string $charset The character set of the part, only relevant for text parts. * @param string $charset The character set of the part, only relevant for text parts.
* @return integer part-number * @return integer part-number
* @throws egw_exception_not_found if $file could not be opened for reading * @throws egw_exception_not_found if $file could not be opened for reading
*/ */
public function addAttachment($file, $name = null, $type = null, $charset = 'us-ascii') public function addAttachment($data, $name = null, $type = null, $charset = 'us-ascii')
{ {
// deprecated PHPMailer::AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') call // deprecated PHPMailer::AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') call
if ($type === 'base64') if ($type === 'base64')
@ -322,19 +322,23 @@ class egw_mailer extends Horde_Mime_Mail
} }
// pass file as resource to Horde_Mime_Part::setContent() // pass file as resource to Horde_Mime_Part::setContent()
if (!($resource = fopen($file, 'r'))) if (is_resource($data))
{ {
throw new egw_exception_not_found("File '$file' not found!"); $resource = $data;
}
elseif (!($resource = fopen($data, 'r')))
{
throw new egw_exception_not_found("File '$data' not found!");
} }
$part = new Horde_Mime_Part(); $part = new Horde_Mime_Part();
$part->setType($type ? $type : egw_vfs::mime_content_type($file)); if ($type || !is_resource($data)) $part->setType($type ? $type : egw_vfs::mime_content_type($data));
$matches = null; $matches = null;
if (preg_match('/^([^;]+);\s*([^=]+)=([^;]+)$/', $type, $matches)) if (preg_match('/^([^;]+);\s*([^=]+)=([^;]+)$/', $type, $matches))
{ {
$part->setContentTypeParameter($matches[2], $matches[3]); $part->setContentTypeParameter($matches[2], $matches[3]);
} }
$part->setContents($resource); $part->setContents($resource);
$part->setName($name ? $name : egw_vfs::basename($file)); if ($name || !is_resource($data)) $part->setName($name ? $name : egw_vfs::basename($data));
// store "text/calendar" as _htmlBody, to trigger "multipart/alternative" // store "text/calendar" as _htmlBody, to trigger "multipart/alternative"
if (stripos($type,"text/calendar; method=") !== false) if (stripos($type,"text/calendar; method=") !== false)
@ -353,14 +357,14 @@ class egw_mailer extends Horde_Mime_Mail
/** /**
* Adds an embedded image or other inline attachment * Adds an embedded image or other inline attachment
* *
* @param string $path Path to the attachment. * @param string|resource $data Path to the attachment or open file-descriptor
* @param string $cid Content ID of the attachment. Use this to identify * @param string $cid Content ID of the attachment. Use this to identify
* the Id for accessing the image in an HTML form. * the Id for accessing the image in an HTML form.
* @param string $name Overrides the attachment name. * @param string $name Overrides the attachment name.
* @param string $type File extension (MIME) type. * @param string $type File extension (MIME) type.
* @return integer part-number * @return integer part-number
*/ */
public function addEmbeddedImage($path, $cid, $name = '', $type = 'application/octet-stream') public function addEmbeddedImage($data, $cid, $name = '', $type = 'application/octet-stream')
{ {
// deprecated PHPMailer::AddEmbeddedImage($path, $cid, $name='', $encoding='base64', $type='application/octet-stream') call // deprecated PHPMailer::AddEmbeddedImage($path, $cid, $name='', $encoding='base64', $type='application/octet-stream') call
if ($type === 'base64' || func_num_args() == 5) if ($type === 'base64' || func_num_args() == 5)
@ -368,8 +372,8 @@ class egw_mailer extends Horde_Mime_Mail
$type = func_get_arg(4); $type = func_get_arg(4);
} }
$part_id = $this->addAttachment($path, $name, $type); $part_id = $this->addAttachment($data, $name, $type);
error_log(__METHOD__."('$path', '$cid', '$name', '$type') added with (temp.) part_id=$part_id"); error_log(__METHOD__."(".array2string($data).", '$cid', '$name', '$type') added with (temp.) part_id=$part_id");
$part = $this->_parts[$part_id]; $part = $this->_parts[$part_id];
$part->setDisposition('inline'); $part->setDisposition('inline');
@ -384,7 +388,7 @@ class egw_mailer extends Horde_Mime_Mail
* "text/calendar; method=..." get automatic detected and added as highest priority alternative, * "text/calendar; method=..." get automatic detected and added as highest priority alternative,
* overwriting evtl. existing html body! * overwriting evtl. existing html body!
* *
* @param string $content String attachment data. * @param string|resource $content String attachment data or open file descriptor
* @param string $filename Name of the attachment. We assume that this is NOT a path * @param string $filename Name of the attachment. We assume that this is NOT a path
* @param string $type File extension (MIME) type. * @param string $type File extension (MIME) type.
* @return int part-number * @return int part-number