2010-09-15 11:10:12 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* eGroupWare API: Sending mail via PHPMailer
|
|
|
|
*
|
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package api
|
|
|
|
* @subpackage mail
|
|
|
|
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
|
|
|
|
* @version $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Log mails to log file specified in $GLOBALS['egw_info']['server']['log_mail']
|
|
|
|
* or regular error_log for true (can be set either in DB or header.inc.php).
|
2011-02-28 22:10:16 +01:00
|
|
|
*
|
2014-11-25 00:38:37 +01:00
|
|
|
* New egw_mailer object uses Horde Mime Mail class with compatibility methods for
|
|
|
|
* old PHPMailer methods and class variable assignments.
|
|
|
|
*
|
2010-09-15 11:10:12 +02:00
|
|
|
* This class does NOT use anything EGroupware specific, it acts like PHPMail, but logs.
|
|
|
|
*/
|
2014-11-25 00:38:37 +01:00
|
|
|
class egw_mailer extends Horde_Mime_Mail
|
2010-09-15 11:10:12 +02:00
|
|
|
{
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Mail account used for sending mail
|
|
|
|
*
|
|
|
|
* @var emailadmin_account
|
|
|
|
*/
|
|
|
|
protected $account;
|
|
|
|
|
|
|
|
/**
|
2014-11-25 10:06:46 +01:00
|
|
|
* Header / recipients set via Add(Address|Cc|Bcc|Replyto)
|
2014-11-25 00:38:37 +01:00
|
|
|
*
|
|
|
|
* @var Horde_Mail_Rfc822_List
|
|
|
|
*/
|
|
|
|
protected $to;
|
|
|
|
protected $cc;
|
|
|
|
protected $bcc;
|
|
|
|
protected $replyto;
|
2014-11-26 15:08:52 +01:00
|
|
|
/**
|
|
|
|
* Translates between interal Horde_Mail_Rfc822_List attributes and header names
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
static $type2header = array(
|
|
|
|
'to' => 'To',
|
|
|
|
'cc' => 'Cc',
|
|
|
|
'bcc' => 'Bcc',
|
|
|
|
'replyto' => 'Reply-To',
|
|
|
|
);
|
2014-11-25 00:38:37 +01:00
|
|
|
|
2010-09-15 11:10:12 +02:00
|
|
|
/**
|
|
|
|
* Constructor: always throw exceptions instead of echoing errors and EGw pathes
|
2014-11-25 00:38:37 +01:00
|
|
|
*
|
|
|
|
* @param int|emailadmin_account $account =null mail account to use, default use emailadmin_account::get_default($smtp=true)
|
|
|
|
*/
|
|
|
|
function __construct($account=null)
|
|
|
|
{
|
2014-11-25 15:22:16 +01:00
|
|
|
// Horde use locale for translation of error messages
|
|
|
|
common::setlocale(LC_MESSAGES);
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
parent::__construct();
|
|
|
|
$this->_headers->setUserAgent('EGroupware API '.$GLOBALS['egw_info']['server']['versions']['phpgwapi']);
|
|
|
|
|
|
|
|
$this->setAccount($account);
|
|
|
|
|
|
|
|
$this->is_html = false;
|
|
|
|
|
2014-11-26 15:08:52 +01:00
|
|
|
$this->clearAllRecipients();
|
|
|
|
$this->clearReplyTos();
|
2014-11-25 00:38:37 +01:00
|
|
|
|
|
|
|
$this->clearParts();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-26 15:08:52 +01:00
|
|
|
* Clear all recipients: to, cc, bcc (but NOT reply-to!)
|
2014-11-25 00:38:37 +01:00
|
|
|
*/
|
2014-11-26 15:08:52 +01:00
|
|
|
function clearAllRecipients()
|
2014-11-25 00:38:37 +01:00
|
|
|
{
|
|
|
|
// clear all addresses
|
2014-11-26 15:08:52 +01:00
|
|
|
$this->clearAddresses();
|
|
|
|
$this->clearCCs();
|
|
|
|
$this->clearBCCs();
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set mail account to use for sending
|
|
|
|
*
|
|
|
|
* @param int|emailadmin_account $account =null mail account to use, default use emailadmin_account::get_default($smtp=true)
|
|
|
|
* @throws egw_exception_not_found if account was not found (or not valid for current user)
|
|
|
|
*/
|
|
|
|
function setAccount($account=null)
|
|
|
|
{
|
|
|
|
if (is_a($account, 'emailadmin_account'))
|
|
|
|
{
|
|
|
|
$this->account = $account;
|
|
|
|
}
|
|
|
|
elseif ($account > 0)
|
|
|
|
{
|
|
|
|
$this->account = emailadmin_account::read($account);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-04-24 10:23:35 +02:00
|
|
|
if (!($this->account = emailadmin_account::get_default(true))) // true = need an SMTP (not just IMAP) account
|
|
|
|
{
|
|
|
|
throw new egw_exception_not_found('SMTP: '.lang('Account not found!'));
|
|
|
|
}
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
2015-04-21 14:52:25 +02:00
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
$identity = emailadmin_account::read_identity($this->account->ident_id, true, null, $this->account);
|
2015-04-24 10:23:35 +02:00
|
|
|
}
|
|
|
|
catch(Exception $e)
|
|
|
|
{
|
|
|
|
unset($e);
|
2015-04-21 14:52:25 +02:00
|
|
|
error_log(__METHOD__.__LINE__.' Could not read_identity for account:'.$account['acc_id'].' with IdentID:'.$account['ident_id']);
|
|
|
|
$identity['ident_email'] = $this->account->ident_email;
|
|
|
|
$identity['ident_realname'] = $this->account->ident_realname ? $this->account->ident_realname : $this->account->ident_email;
|
|
|
|
}
|
2014-11-25 12:32:25 +01:00
|
|
|
|
|
|
|
// use smpt-username as sender/return-path, if available, but only if it is a full email address
|
2014-11-25 00:38:37 +01:00
|
|
|
$sender = $this->account->acc_smtp_username && strpos($this->account->acc_smtp_username, '@') !== false ?
|
2014-11-25 12:32:25 +01:00
|
|
|
$this->account->acc_smtp_username : $identity['ident_email'];
|
|
|
|
$this->addHeader('Return-Path', '<'.$sender.'>', true);
|
|
|
|
|
|
|
|
$this->setFrom($identity['ident_email'], $identity['ident_realname']);
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-25 12:32:25 +01:00
|
|
|
* Set From header
|
2014-11-25 00:38:37 +01:00
|
|
|
*
|
|
|
|
* @param string $address
|
|
|
|
* @param string $personal =''
|
|
|
|
*/
|
|
|
|
public function setFrom($address, $personal='')
|
|
|
|
{
|
|
|
|
$this->addHeader('From', self::add_personal($address, $personal));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add one or multiple addresses to To, Cc, Bcc or Reply-To
|
|
|
|
*
|
|
|
|
* @param string|array|Horde_Mail_Rfc822_List $address
|
|
|
|
* @param string $personal ='' only used if $address is a string
|
|
|
|
* @param string $type ='to' type of address to add "to", "cc", "bcc" or "replyto"
|
|
|
|
*/
|
|
|
|
function addAddress($address, $personal='', $type='to')
|
|
|
|
{
|
2014-11-26 15:08:52 +01:00
|
|
|
if (!isset(self::$type2header[$type]))
|
2014-11-25 00:38:37 +01:00
|
|
|
{
|
|
|
|
throw new egw_exception_wrong_parameter("Unknown type '$type'!");
|
|
|
|
}
|
|
|
|
if ($personal) $address = self::add_personal ($address, $personal);
|
|
|
|
|
|
|
|
// add to our local list
|
|
|
|
$this->$type->add($address);
|
|
|
|
|
|
|
|
// add as header
|
2014-11-26 15:08:52 +01:00
|
|
|
$this->addHeader(self::$type2header[$type], $this->$type, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove all addresses from To, Cc, Bcc or Reply-To
|
|
|
|
*
|
|
|
|
* @param string $type ='to' type of address to add "to", "cc", "bcc" or "replyto"
|
|
|
|
*/
|
|
|
|
function clearAddresses($type='to')
|
|
|
|
{
|
|
|
|
$this->$type = new Horde_Mail_Rfc822_List();
|
|
|
|
|
|
|
|
$this->removeHeader(self::$type2header[$type]);
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 18:37:46 +01:00
|
|
|
/**
|
|
|
|
* Get set to addressses
|
|
|
|
*
|
|
|
|
* @param string $type ='to' type of address to add "to", "cc", "bcc" or "replyto"
|
|
|
|
* @param boolean $return_array =false true: return array of string, false: Horde_Mail_Rfc822_List
|
|
|
|
* @return array|Horde_Mail_Rfc822_List supporting arrayAccess and Iterable
|
|
|
|
*/
|
|
|
|
function getAddresses($type='to', $return_array=false)
|
|
|
|
{
|
|
|
|
if ($return_array)
|
|
|
|
{
|
|
|
|
$addresses = array();
|
2015-04-22 16:26:52 +02:00
|
|
|
foreach($this->$type as $addr)
|
2014-12-19 18:37:46 +01:00
|
|
|
{
|
|
|
|
$addresses[] = (string)$addr;
|
|
|
|
}
|
|
|
|
return $addresses;
|
|
|
|
}
|
|
|
|
return $this->$type;
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Write Bcc as header for storing in sent or as draft
|
|
|
|
*
|
2014-11-25 10:06:46 +01:00
|
|
|
* Bcc is normally only add to recipients while sending, but not added visible as header.
|
|
|
|
*
|
|
|
|
* This function is should only be called AFTER calling send, or when NOT calling send at all!
|
2014-11-25 00:38:37 +01:00
|
|
|
*/
|
|
|
|
function forceBccHeader()
|
|
|
|
{
|
|
|
|
$this->_headers->removeHeader('Bcc');
|
2014-11-25 10:06:46 +01:00
|
|
|
|
|
|
|
// only add Bcc header, if we have bcc's
|
|
|
|
if (count($this->bcc))
|
|
|
|
{
|
|
|
|
$this->_headers->addHeader('Bcc', $this->bcc);
|
|
|
|
}
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add personal part to email address
|
|
|
|
*
|
|
|
|
* @param string $address
|
|
|
|
* @param string $personal
|
|
|
|
* @return string Rfc822 address
|
|
|
|
*/
|
|
|
|
static function add_personal($address, $personal)
|
|
|
|
{
|
|
|
|
if (is_string($address) && !empty($personal))
|
|
|
|
{
|
|
|
|
//if (!preg_match('/^[!#$%&\'*+/0-9=?A-Z^_`a-z{|}~-]+$/u', $personal)) // that's how I read the rfc(2)822
|
|
|
|
if ($personal && !preg_match('/^[0-9A-Z -]*$/iu', $personal)) // but quoting is never wrong, so quote more then necessary
|
|
|
|
{
|
|
|
|
$personal = '"'.str_replace(array('\\', '"'),array('\\\\', '\\"'), $personal).'"';
|
|
|
|
}
|
|
|
|
$address = ($personal ? $personal.' <' : '').$address.($personal ? '>' : '');
|
|
|
|
}
|
|
|
|
return $address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add one or multiple addresses to Cc
|
|
|
|
*
|
|
|
|
* @param string|array|Horde_Mail_Rfc822_List $address
|
|
|
|
* @param string $personal ='' only used if $address is a string
|
|
|
|
*/
|
2014-11-27 10:10:43 +01:00
|
|
|
function addCc($address, $personal=null)
|
2014-11-25 00:38:37 +01:00
|
|
|
{
|
2014-11-27 10:10:43 +01:00
|
|
|
$this->addAddress($address, $personal, 'cc');
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-26 15:08:52 +01:00
|
|
|
/**
|
|
|
|
* Clear all cc
|
|
|
|
*/
|
|
|
|
function clearCCs()
|
|
|
|
{
|
|
|
|
$this->clearAddresses('cc');
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Add one or multiple addresses to Bcc
|
|
|
|
*
|
|
|
|
* @param string|array|Horde_Mail_Rfc822_List $address
|
|
|
|
* @param string $personal ='' only used if $address is a string
|
|
|
|
*/
|
2014-11-27 10:10:43 +01:00
|
|
|
function addBcc($address, $personal=null)
|
2014-11-25 00:38:37 +01:00
|
|
|
{
|
2014-11-27 10:10:43 +01:00
|
|
|
$this->addAddress($address, $personal, 'bcc');
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-26 15:08:52 +01:00
|
|
|
/**
|
|
|
|
* Clear all bcc
|
|
|
|
*/
|
|
|
|
function clearBCCs()
|
|
|
|
{
|
|
|
|
$this->clearAddresses('bcc');
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Add one or multiple addresses to Reply-To
|
|
|
|
*
|
|
|
|
* @param string|array|Horde_Mail_Rfc822_List $address
|
|
|
|
* @param string $personal ='' only used if $address is a string
|
|
|
|
*/
|
2014-11-27 10:10:43 +01:00
|
|
|
function addReplyTo($address, $personal=null)
|
2014-11-25 00:38:37 +01:00
|
|
|
{
|
2014-11-27 10:10:43 +01:00
|
|
|
$this->addAddress($address, $personal, 'replyto');
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-26 15:08:52 +01:00
|
|
|
/**
|
|
|
|
* Clear all reply-to
|
|
|
|
*/
|
|
|
|
function clearReplyTos()
|
|
|
|
{
|
|
|
|
$this->clearAddresses('replyto');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get set ReplyTo addressses
|
|
|
|
*
|
|
|
|
* @return Horde_Mail_Rfc822_List supporting arrayAccess and Iterable
|
|
|
|
*/
|
|
|
|
function getReplyTo()
|
|
|
|
{
|
|
|
|
return $this->replyto;
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Adds an attachment
|
|
|
|
*
|
|
|
|
* "text/calendar; method=..." get automatic detected and added as highes priority alternative,
|
|
|
|
* overwriting evtl. existing html body!
|
|
|
|
*
|
|
|
|
* @param string $file The path to the file.
|
|
|
|
* @param string $name The file name to use for the attachment.
|
|
|
|
* @param string $type The content type of the file.
|
|
|
|
* @param string $charset The character set of the part, only relevant for text parts.
|
|
|
|
* @return integer part-number
|
|
|
|
* @throws egw_exception_not_found if $file could not be opened for reading
|
2010-09-15 11:10:12 +02:00
|
|
|
*/
|
2014-11-25 00:38:37 +01:00
|
|
|
public function addAttachment($file, $name = null, $type = null, $charset = 'us-ascii')
|
2010-09-15 11:10:12 +02:00
|
|
|
{
|
2014-11-25 00:38:37 +01:00
|
|
|
// deprecated PHPMailer::AddAttachment($path, $name = '', $encoding = 'base64', $type = 'application/octet-stream') call
|
|
|
|
if ($type === 'base64')
|
|
|
|
{
|
|
|
|
$type = $charset;
|
|
|
|
$charset = 'us-ascii';
|
|
|
|
}
|
|
|
|
|
|
|
|
// pass file as resource to Horde_Mime_Part::setContent()
|
|
|
|
if (!($resource = fopen($file, 'r')))
|
|
|
|
{
|
|
|
|
throw new egw_exception_not_found("File '$file' not found!");
|
|
|
|
}
|
|
|
|
$part = new Horde_Mime_Part();
|
|
|
|
$part->setType($type ? $type : egw_vfs::mime_content_type($file));
|
2015-03-31 11:49:57 +02:00
|
|
|
$matches = null;
|
|
|
|
if (preg_match('/^([^;]+);\s*([^=]+)=([^;]+)$/', $type, $matches))
|
|
|
|
{
|
|
|
|
$part->setContentTypeParameter($matches[2], $matches[3]);
|
|
|
|
}
|
2014-11-25 00:38:37 +01:00
|
|
|
$part->setContents($resource);
|
2011-02-28 22:10:16 +01:00
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
// store "text/calendar" as _htmlBody, to trigger "multipart/alternative"
|
|
|
|
if (stripos($type,"text/calendar; method=") !== false)
|
2010-09-15 11:10:12 +02:00
|
|
|
{
|
2014-11-25 00:38:37 +01:00
|
|
|
$this->_htmlBody = $part;
|
|
|
|
return;
|
2010-09-15 11:10:12 +02:00
|
|
|
}
|
2015-10-05 16:27:07 +02:00
|
|
|
// setting name, also sets content-disposition attachment (!), therefore we have to do it after "text/calendar; method=" handling
|
|
|
|
if ($name || !is_resource($data)) $part->setName($name ? $name : egw_vfs::basename($data));
|
|
|
|
|
2014-11-25 10:06:46 +01:00
|
|
|
// this should not be necessary, because binary data get detected by mime-type,
|
|
|
|
// but at least Cyrus complains about NUL characters
|
|
|
|
$part->setTransferEncoding('base64', array('send' => true));
|
2014-11-25 00:38:37 +01:00
|
|
|
$part->setDisposition('attachment');
|
|
|
|
|
|
|
|
return $this->addMimePart($part);
|
|
|
|
}
|
|
|
|
|
2014-11-27 13:29:16 +01:00
|
|
|
/**
|
|
|
|
* Adds an embedded image or other inline attachment
|
|
|
|
*
|
|
|
|
* @param string $path Path to the attachment.
|
|
|
|
* @param string $cid Content ID of the attachment. Use this to identify
|
|
|
|
* the Id for accessing the image in an HTML form.
|
|
|
|
* @param string $name Overrides the attachment name.
|
|
|
|
* @param string $type File extension (MIME) type.
|
|
|
|
* @return integer part-number
|
|
|
|
*/
|
|
|
|
public function addEmbeddedImage($path, $cid, $name = '', $type = 'application/octet-stream')
|
|
|
|
{
|
|
|
|
// deprecated PHPMailer::AddEmbeddedImage($path, $cid, $name='', $encoding='base64', $type='application/octet-stream') call
|
|
|
|
if ($type === 'base64' || func_num_args() == 5)
|
|
|
|
{
|
|
|
|
$type = func_get_arg(4);
|
|
|
|
}
|
|
|
|
|
|
|
|
$part_id = $this->addAttachment($path, $name, $type);
|
|
|
|
error_log(__METHOD__."('$path', '$cid', '$name', '$type') added with (temp.) part_id=$part_id");
|
|
|
|
|
|
|
|
$part = $this->_parts[$part_id];
|
|
|
|
$part->setDisposition('inline');
|
|
|
|
$part->setContentId($cid);
|
|
|
|
|
|
|
|
return $part_id;
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Adds a string or binary attachment (non-filesystem) to the list.
|
|
|
|
*
|
2015-04-17 16:17:17 +02:00
|
|
|
* "text/calendar; method=..." get automatic detected and added as highest priority alternative,
|
2014-11-25 00:38:37 +01:00
|
|
|
* overwriting evtl. existing html body!
|
|
|
|
*
|
|
|
|
* @param string $content String attachment data.
|
|
|
|
* @param string $filename Name of the attachment. We assume that this is NOT a path
|
|
|
|
* @param string $type File extension (MIME) type.
|
|
|
|
* @return int part-number
|
|
|
|
*/
|
2014-11-27 10:10:43 +01:00
|
|
|
public function addStringAttachment($content, $filename, $type = 'application/octet-stream')
|
2014-11-25 00:38:37 +01:00
|
|
|
{
|
|
|
|
// deprecated PHPMailer::AddStringAttachment($content, $filename = '', $encoding = 'base64', $type = 'application/octet-stream') call
|
|
|
|
if ($type === 'base64' || func_num_args() == 4)
|
|
|
|
{
|
|
|
|
$type = func_get_arg(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
$part = new Horde_Mime_Part();
|
|
|
|
$part->setType($type);
|
2015-03-31 11:49:57 +02:00
|
|
|
$matches = null;
|
|
|
|
if (preg_match('/^([^;]+);\s*([^=]+)=([^;]+)$/', $type, $matches))
|
|
|
|
{
|
|
|
|
$part->setContentTypeParameter($matches[2], $matches[3]);
|
|
|
|
}
|
2014-11-25 00:38:37 +01:00
|
|
|
$part->setCharset('utf-8');
|
|
|
|
$part->setContents($content);
|
|
|
|
|
|
|
|
// store "text/calendar" as _htmlBody, to trigger "multipart/alternative"
|
|
|
|
if (stripos($type,"text/calendar; method=") !== false)
|
2010-09-15 11:10:12 +02:00
|
|
|
{
|
2014-11-25 00:38:37 +01:00
|
|
|
$this->_htmlBody = $part;
|
|
|
|
return;
|
2010-09-15 11:10:12 +02:00
|
|
|
}
|
2015-04-17 16:17:17 +02:00
|
|
|
// this should not be necessary, because binary data get detected by mime-type,
|
|
|
|
// but at least Cyrus complains about NUL characters
|
|
|
|
$part->setTransferEncoding('base64', array('send' => true));
|
|
|
|
$part->setName($filename);
|
2014-11-25 00:38:37 +01:00
|
|
|
$part->setDisposition('attachment');
|
|
|
|
|
|
|
|
return $this->addMimePart($part);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send mail, injecting mail transport from account
|
|
|
|
*
|
2014-12-19 18:37:46 +01:00
|
|
|
* Log mails to log file specified in $GLOBALS['egw_info']['server']['log_mail']
|
|
|
|
* or regular error_log for true (can be set either in DB or header.inc.php).
|
|
|
|
*
|
2014-11-25 15:22:16 +01:00
|
|
|
* @throws egw_exception_not_found for no smtp account available
|
|
|
|
* @throws Horde_Mime_Exception
|
2014-11-25 00:38:37 +01:00
|
|
|
*/
|
|
|
|
function send()
|
|
|
|
{
|
2014-12-19 18:58:44 +01:00
|
|
|
if (!($message_id = $this->getHeader('Message-ID')) &&
|
|
|
|
class_exists('Horde_Mime_Headers_MessageId')) // since 2.5.0
|
2014-12-19 18:37:46 +01:00
|
|
|
{
|
2014-12-19 18:58:44 +01:00
|
|
|
$message_id = Horde_Mime_Headers_MessageId::create('EGroupware');
|
2014-12-19 18:37:46 +01:00
|
|
|
$this->addHeader('Message-ID', $message_id);
|
|
|
|
}
|
|
|
|
$body_sha1 = null; // skip sha1, it requires whole mail in memory, which we traing to avoid now
|
|
|
|
|
|
|
|
$mail_id = $GLOBALS['egw']->hooks->process(array(
|
|
|
|
'location' => 'send_mail',
|
|
|
|
'subject' => $subject=$this->getHeader('Subject'),
|
|
|
|
'from' => $this->getHeader('Return-Path') ? $this->getHeader('Return-Path') : $this->getHeader('From'),
|
|
|
|
'to' => $to=$this->getAddresses('to', true),
|
|
|
|
'cc' => $cc=$this->getAddresses('cc', true),
|
|
|
|
'bcc' => $bcc=$this->getAddresses('bcc', true),
|
|
|
|
'body_sha1' => $body_sha1,
|
|
|
|
'message_id' => (string)$message_id,
|
|
|
|
), array(), true); // true = call all apps
|
|
|
|
|
2015-06-19 16:21:18 +02:00
|
|
|
// check if we are sending an html mail with inline images
|
|
|
|
if (!empty($this->_htmlBody) && count($this->_parts))
|
|
|
|
{
|
|
|
|
$related = null;
|
|
|
|
foreach($this->_parts as $n => $part)
|
|
|
|
{
|
|
|
|
if ($part->getDisposition() == 'inline' && $part->getContentId())
|
|
|
|
{
|
|
|
|
// we need to send a multipart/related with html-body as first part and inline images as further parts
|
|
|
|
if (!isset($related))
|
|
|
|
{
|
|
|
|
$related = new Horde_Mime_Part();
|
|
|
|
$related->setType('multipart/related');
|
|
|
|
$related[] = $this->_htmlBody;
|
|
|
|
$this->_htmlBody = $related;
|
|
|
|
}
|
|
|
|
$related[] = $part;
|
|
|
|
unset($this->_parts[$n]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-19 18:37:46 +01:00
|
|
|
try {
|
|
|
|
parent::send($this->account->smtpTransport(), true); // 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
|
|
|
|
$GLOBALS['egw']->hooks->process(array(
|
|
|
|
'location' => 'send_mail',
|
|
|
|
'subject' => $subject,
|
|
|
|
'from' => $this->getHeader('Return-Path') ? $this->getHeader('Return-Path') : $this->getHeader('From'),
|
|
|
|
'to' => $to,
|
|
|
|
'cc' => $cc,
|
|
|
|
'bcc' => $bcc,
|
|
|
|
'body_sha1' => $body_sha1,
|
|
|
|
'message_id' => (string)$message_id,
|
|
|
|
'mail_id' => $mail_id,
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
), array(), true); // true = call all apps
|
|
|
|
}
|
|
|
|
|
|
|
|
// log mails to file specified in $GLOBALS['egw_info']['server']['log_mail'] or error_log for true
|
|
|
|
if ($GLOBALS['egw_info']['server']['log_mail'])
|
|
|
|
{
|
|
|
|
$msg = $GLOBALS['egw_info']['server']['log_mail'] !== true ? date('Y-m-d H:i:s')."\n" : '';
|
|
|
|
$msg .= (isset($e) ? 'Mail send' : 'Mail NOT send').
|
|
|
|
' to '.implode(', ', $to).' with subject: "'.$subject.'"';
|
|
|
|
|
|
|
|
$msg .= ' from instance '.$GLOBALS['egw_info']['user']['domain'].' and IP '.egw_session::getuser_ip();
|
|
|
|
$msg .= ' from user #'.$GLOBALS['egw_info']['user']['account_id'];
|
|
|
|
|
|
|
|
if ($GLOBALS['egw_info']['user']['account_id'] && class_exists('common',false))
|
|
|
|
{
|
|
|
|
$msg .= ' ('.common::grab_owner_name($GLOBALS['egw_info']['user']['account_id']).')';
|
|
|
|
}
|
|
|
|
if (isset($e))
|
|
|
|
{
|
|
|
|
$msg .= $GLOBALS['egw_info']['server']['log_mail'] !== true ? "\n" : ': ';
|
|
|
|
$msg .= 'ERROR '.$e->getMessage();
|
|
|
|
}
|
|
|
|
$msg .= ' cc='.implode(', ', $cc).', bcc='.implode(', ', $bcc);
|
|
|
|
if ($GLOBALS['egw_info']['server']['log_mail'] !== true) $msg .= "\n\n";
|
|
|
|
|
|
|
|
error_log($msg,$GLOBALS['egw_info']['server']['log_mail'] === true ? 0 : 3,
|
|
|
|
$GLOBALS['egw_info']['server']['log_mail']);
|
|
|
|
}
|
|
|
|
// rethrow error
|
|
|
|
if (isset($e)) throw $e;
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
2014-12-19 18:37:46 +01:00
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Reset all Settings to send multiple Messages
|
|
|
|
*/
|
2014-11-27 10:10:43 +01:00
|
|
|
function clearAll()
|
2014-11-25 00:38:37 +01:00
|
|
|
{
|
|
|
|
$this->__construct($this->account);
|
|
|
|
}
|
|
|
|
|
2014-11-25 15:22:16 +01:00
|
|
|
/**
|
|
|
|
* Get value of a header set with addHeader()
|
|
|
|
*
|
|
|
|
* @param string $header
|
2014-11-27 10:10:43 +01:00
|
|
|
* @return string|array
|
2014-11-25 15:22:16 +01:00
|
|
|
*/
|
|
|
|
function getHeader($header)
|
|
|
|
{
|
2014-11-27 10:10:43 +01:00
|
|
|
return $this->_headers ? $this->_headers->getValue($header) : null;
|
2014-11-25 15:22:16 +01:00
|
|
|
}
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
/**
|
|
|
|
* Get the raw email data sent by this object.
|
|
|
|
*
|
|
|
|
* Reimplement to be able to call it for saveAsDraft by calling
|
|
|
|
* $this->send(new Horde_Mail_Transport_Null()),
|
|
|
|
* if no base-part is set, because send is not called before.
|
|
|
|
*
|
|
|
|
* @param boolean $stream If true, return a stream resource, otherwise
|
|
|
|
* @return stream|string The raw email data.
|
|
|
|
*/
|
|
|
|
function getRaw($stream=true)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->getBasePart();
|
|
|
|
}
|
|
|
|
catch(Horde_Mail_Exception $e)
|
|
|
|
{
|
|
|
|
unset($e);
|
|
|
|
parent::send(new Horde_Mail_Transport_Null(), true); // true: keep Message-ID
|
|
|
|
}
|
2014-11-26 16:05:59 +01:00
|
|
|
// code copied from Horde_Mime_Mail::getRaw(), as there is no way to inject charset in
|
|
|
|
// _headers->toString(), which is required to encode headers containing non-ascii chars correct
|
2014-11-26 15:08:52 +01:00
|
|
|
if ($stream) {
|
|
|
|
$hdr = new Horde_Stream();
|
2014-11-26 16:05:59 +01:00
|
|
|
$hdr->add($this->_headers->toString(array('charset' => 'utf-8', 'canonical' => true)), true);
|
2014-11-26 15:08:52 +01:00
|
|
|
return Horde_Stream_Wrapper_Combine::getStream(
|
|
|
|
array($hdr->stream,
|
|
|
|
$this->getBasePart()->toString(
|
2014-11-26 16:05:59 +01:00
|
|
|
array('stream' => true, 'canonical' => true, 'encode' => Horde_Mime_Part::ENCODE_7BIT | Horde_Mime_Part::ENCODE_8BIT | Horde_Mime_Part::ENCODE_BINARY))
|
2014-11-26 15:08:52 +01:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-11-26 16:05:59 +01:00
|
|
|
return $this->_headers->toString(array('charset' => 'utf-8', 'canonical' => true)) .
|
|
|
|
$this->getBasePart()->toString(array('canonical' => true));
|
2014-11-26 15:08:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find body: 1. part with mimetype "text/$subtype"
|
|
|
|
*
|
|
|
|
* Use getContents() on non-null return-value to get string content
|
|
|
|
*
|
|
|
|
* @param string $subtype =null
|
|
|
|
* @return Horde_Mime_Part part with body or null
|
|
|
|
*/
|
|
|
|
function findBody($subtype=null)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$base = $this->getBasePart();
|
2014-11-27 10:10:43 +01:00
|
|
|
if (!($part_id = $base->findBody($subtype))) return null;
|
|
|
|
return $base->getPart($part_id);
|
2014-11-26 15:08:52 +01:00
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
unset($e);
|
|
|
|
return $subtype == 'html' ? $this->_htmlBody : $this->_body;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-12 09:57:37 +02:00
|
|
|
/**
|
|
|
|
* Parse base-part into _body, _htmlBody and _parts to eg. add further attachments
|
|
|
|
*/
|
|
|
|
function parseBasePart()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$base = $this->getBasePart();
|
|
|
|
$plain_id = $base->findBody('plain');
|
|
|
|
$html_id = $base->findBody('html');
|
|
|
|
|
|
|
|
$this->_body = $this->_htmlBody = null;
|
|
|
|
$this->clearParts();
|
|
|
|
|
|
|
|
foreach($base->partIterator() as $part)
|
|
|
|
{
|
|
|
|
$id = $part->getMimeId();
|
|
|
|
//error_log(__METHOD__."() plain=$plain_id, html=$html_id: $id: ".$part->getType());
|
|
|
|
switch($id)
|
|
|
|
{
|
|
|
|
case '0': // base-part itself
|
|
|
|
continue 2;
|
|
|
|
case $plain_id:
|
|
|
|
$this->_body = $part;
|
|
|
|
break;
|
|
|
|
case $html_id:
|
|
|
|
$this->_htmlBody = $part;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$this->_parts[] = $part;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->setBasePart(null);
|
|
|
|
}
|
|
|
|
catch (Exception $e) {
|
|
|
|
// ignore that there is no base-part yet, so nothing to do
|
|
|
|
unset($e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a MIME message part.
|
|
|
|
*
|
|
|
|
* Reimplemented to add parts / attachments if message was parsed / already has a base-part
|
|
|
|
*
|
|
|
|
* @param Horde_Mime_Part $part A Horde_Mime_Part object.
|
|
|
|
* @return integer The part number.
|
|
|
|
*/
|
|
|
|
public function addMimePart($part)
|
|
|
|
{
|
|
|
|
if ($this->_base) $this->parseBasePart();
|
|
|
|
|
|
|
|
return parent::addMimePart($part);
|
|
|
|
}
|
|
|
|
|
2014-11-26 15:08:52 +01:00
|
|
|
/**
|
|
|
|
* Clear all non-standard headers
|
|
|
|
*
|
|
|
|
* Used in merge-print to remove headers before sending "new" mail
|
|
|
|
*/
|
2014-11-27 10:10:43 +01:00
|
|
|
function clearCustomHeaders()
|
2014-11-26 15:08:52 +01:00
|
|
|
{
|
|
|
|
foreach($this->_headers->toArray() as $header => $value)
|
|
|
|
{
|
|
|
|
if (stripos($header, 'x-') === 0 || $header == 'Received')
|
|
|
|
{
|
|
|
|
$this->_headers->removeHeader($header);
|
|
|
|
}
|
|
|
|
unset($value);
|
|
|
|
}
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deprecated PHPMailer compatibility methods
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get header part of mail
|
|
|
|
*
|
|
|
|
* @deprecated use getRaw($stream=true) to get a stream of whole mail containing headers and body
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getMessageHeader()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->getBasePart();
|
|
|
|
}
|
|
|
|
catch(Horde_Mail_Exception $e)
|
|
|
|
{
|
|
|
|
unset($e);
|
|
|
|
parent::send(new Horde_Mail_Transport_Null(), true); // true: keep Message-ID
|
|
|
|
}
|
|
|
|
return $this->_headers->toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get body part of mail
|
|
|
|
*
|
|
|
|
* @deprecated use getRaw($stream=true) to get a stream of whole mail containing headers and body
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function getMessageBody()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
$this->getBasePart();
|
|
|
|
}
|
|
|
|
catch(Horde_Mail_Exception $e)
|
|
|
|
{
|
|
|
|
unset($e);
|
|
|
|
parent::send(new Horde_Mail_Transport_Null(), true); // true: keep Message-ID
|
|
|
|
}
|
|
|
|
return $this->getBasePart()->toString(
|
|
|
|
array('stream' => false, 'encode' => Horde_Mime_Part::ENCODE_7BIT | Horde_Mime_Part::ENCODE_8BIT | Horde_Mime_Part::ENCODE_BINARY));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use SMPT
|
|
|
|
*
|
|
|
|
* @deprecated not used, SMTP always used
|
|
|
|
*/
|
|
|
|
function IsSMTP()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @deprecated use AddHeader($header, $value)
|
|
|
|
*/
|
|
|
|
function AddCustomHeader($str)
|
|
|
|
{
|
|
|
|
$matches = null;
|
|
|
|
if (preg_match('/^([^:]+): *(.*)$/', $str, $matches))
|
|
|
|
{
|
|
|
|
$this->addHeader($matches[1], $matches[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @deprecated use clearParts()
|
|
|
|
*/
|
|
|
|
function ClearAttachments()
|
|
|
|
{
|
|
|
|
$this->clearParts();
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @deprecated done by Horde automatic
|
|
|
|
*/
|
|
|
|
function EncodeHeader($str/*, $position = 'text'*/)
|
|
|
|
{
|
|
|
|
return $str;
|
|
|
|
}
|
2014-11-25 10:27:42 +01:00
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
protected $is_html = false;
|
|
|
|
/**
|
|
|
|
* Defines that setting $this->Body should set Body or AltBody
|
|
|
|
* @param boolean $html
|
|
|
|
* @deprecated use either setBody() or setHtmlBody()
|
|
|
|
*/
|
|
|
|
function isHtml($html)
|
|
|
|
{
|
|
|
|
$this->is_html = (bool)$html;
|
|
|
|
}
|
|
|
|
|
2014-11-25 10:27:42 +01:00
|
|
|
/**
|
|
|
|
* Sets the message type
|
|
|
|
*
|
|
|
|
* @deprecated no longer necessary to call, happens automatic when calling send or getRaw($stream=true)
|
|
|
|
*/
|
|
|
|
public function SetMessageType()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assembles message header
|
|
|
|
*
|
|
|
|
* @deprecated use getMessageHeader() or better getRaw($stream=true)
|
|
|
|
* @return string The assembled header
|
|
|
|
*/
|
|
|
|
public function CreateHeader()
|
|
|
|
{
|
|
|
|
return $this->getMessageHeader();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assembles message body
|
|
|
|
*
|
|
|
|
* @deprecated use getMessageBody() or better getRaw($stream=true)
|
|
|
|
* @return string The assembled header
|
|
|
|
*/
|
|
|
|
public function CreateBody()
|
|
|
|
{
|
|
|
|
return $this->getMessageBody();
|
|
|
|
}
|
|
|
|
|
2014-11-25 00:38:37 +01:00
|
|
|
protected $from = '';
|
|
|
|
/**
|
|
|
|
* Magic method to intercept assignments to old PHPMailer variables
|
|
|
|
*
|
|
|
|
* @deprecated use addHeader(), setBody() or setHtmlBody()
|
|
|
|
* @param type $name
|
|
|
|
* @param type $value
|
|
|
|
*/
|
|
|
|
function __set($name, $value)
|
|
|
|
{
|
|
|
|
switch($name)
|
|
|
|
{
|
2015-01-27 08:56:27 +01:00
|
|
|
case '_bcc':
|
|
|
|
$this->_bcc = $value; // this is NOT PHPMailer compatibility, but need for working BCC, if $this->_bcc is NOT set
|
2015-02-03 15:11:55 +01:00
|
|
|
break;
|
2014-11-25 00:38:37 +01:00
|
|
|
case 'Sender':
|
|
|
|
$this->addHeader('Return-Path', '<'.$value.'>', true);
|
|
|
|
break;
|
|
|
|
case 'From':
|
|
|
|
case 'FromName':
|
|
|
|
if (empty($this->from) || $name == 'From' && $this->from[0] == '<')
|
|
|
|
{
|
|
|
|
$this->from = $name == 'From' ? '<'.$value.'>' : $value;
|
|
|
|
}
|
|
|
|
elseif ($name == 'From')
|
|
|
|
{
|
|
|
|
$this->from = self::add_personal($value, $this->from);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->from = self::add_personal(substr($this->from, 1, -1), $value);
|
|
|
|
}
|
|
|
|
$this->addHeader('From', $this->from, true);
|
|
|
|
break;
|
|
|
|
case 'Priority':
|
2014-11-26 14:05:10 +01:00
|
|
|
$this->addHeader('X-Priority', $value);
|
2014-11-25 00:38:37 +01:00
|
|
|
break;
|
|
|
|
case 'Subject':
|
2014-11-26 14:05:10 +01:00
|
|
|
$this->addHeader($name, $value);
|
2014-11-25 00:38:37 +01:00
|
|
|
break;
|
|
|
|
case 'MessageID':
|
2014-11-26 14:05:10 +01:00
|
|
|
$this->addHeader('Message-ID', $value);
|
|
|
|
break;
|
|
|
|
case 'Date':
|
|
|
|
case 'RFCDateToSet':
|
|
|
|
if ($value) $this->addHeader('Date', $value, true);
|
2014-11-25 00:38:37 +01:00
|
|
|
break;
|
|
|
|
case 'AltExtended':
|
|
|
|
case 'AltExtendedContentType':
|
|
|
|
// todo addPart()
|
|
|
|
break;
|
|
|
|
case 'Body':
|
|
|
|
$this->is_html ? $this->setHtmlBody($value, null, false) : $this->setBody($value);
|
|
|
|
break;
|
|
|
|
case 'AltBody':
|
|
|
|
!$this->is_html ? $this->setHtmlBody($value, null, false) : $this->setBody($value);
|
|
|
|
break;
|
|
|
|
default:
|
2014-11-26 17:01:41 +01:00
|
|
|
error_log(__METHOD__."('$name', ".array2string($value).") unsupported attribute '$name' --> ignored ".function_backtrace());
|
2014-11-25 00:38:37 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Magic method to intercept readin old PHPMailer variables
|
|
|
|
*
|
|
|
|
* @deprecated use getHeader(), etc.
|
|
|
|
* @param type $name
|
|
|
|
*/
|
|
|
|
function __get($name)
|
|
|
|
{
|
|
|
|
switch($name)
|
|
|
|
{
|
2014-11-27 13:29:16 +01:00
|
|
|
case '_bcc':
|
|
|
|
return $this->_bcc; // this is NOT PHPMailer compatibility, but quietening below log, if $this->_bcc is NOT set
|
2014-11-25 00:38:37 +01:00
|
|
|
case 'Sender':
|
|
|
|
return $this->getHeader('Return-Path');
|
|
|
|
case 'From':
|
|
|
|
return $this->getHeader('From');
|
2014-11-26 15:08:52 +01:00
|
|
|
case 'Body':
|
|
|
|
case 'AltBody':
|
|
|
|
$body = $this->findBody($name == 'Body' ? 'plain' : 'html');
|
|
|
|
return $body ? $body->getContents() : null;
|
2014-11-25 00:38:37 +01:00
|
|
|
}
|
2014-11-26 17:01:41 +01:00
|
|
|
error_log(__METHOD__."('$name') unsupported attribute '$name' --> returning NULL ".function_backtrace());
|
2014-11-25 00:38:37 +01:00
|
|
|
return null;
|
2010-09-15 11:10:12 +02:00
|
|
|
}
|
|
|
|
}
|