2007-11-22 09:29:16 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2016-05-06 11:19:36 +02:00
|
|
|
* EGroupware - Notifications
|
2007-11-22 09:29:16 +01:00
|
|
|
*
|
|
|
|
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
|
|
|
|
* @package notifications
|
2007-12-18 16:53:38 +01:00
|
|
|
* @subpackage backends
|
2007-11-22 09:29:16 +01:00
|
|
|
* @link http://www.egroupware.org
|
|
|
|
* @author Christian Binder <christian@jaytraxx.de>
|
2008-07-30 08:49:55 +02:00
|
|
|
* @version $Id$
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
|
|
|
|
2016-05-06 11:19:36 +02:00
|
|
|
use EGroupware\Api;
|
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* User notification via email.
|
|
|
|
*/
|
2008-01-30 19:58:00 +01:00
|
|
|
class notifications_email implements notifications_iface {
|
2007-11-22 09:29:16 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Appname
|
|
|
|
*/
|
|
|
|
const _appname = 'notifications';
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user who sends the message
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $sender;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds account object for user to notify
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $recipient;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds config object (sitewide application config)
|
|
|
|
*
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
private $config;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* holds preferences object of user to notify
|
|
|
|
*
|
2016-05-06 11:19:36 +02:00
|
|
|
* @var Api\Preferences
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
|
|
|
private $preferences;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* holds mail object
|
|
|
|
*
|
2011-06-23 11:05:25 +02:00
|
|
|
* @var send
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
|
|
|
private $mail;
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
2008-01-30 19:58:00 +01:00
|
|
|
* constructor of notifications_email
|
2007-11-22 09:29:16 +01:00
|
|
|
*
|
2007-12-10 09:26:29 +01:00
|
|
|
* @param object $_sender
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param object $_recipient
|
2007-12-10 09:26:29 +01:00
|
|
|
* @param object $_config
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param object $_preferences
|
|
|
|
*/
|
2024-04-17 15:01:18 +02:00
|
|
|
public function __construct($_sender, $_recipient, $_config = null, $_preferences = null)
|
|
|
|
{
|
2007-12-18 16:53:38 +01:00
|
|
|
if(!is_object($_sender)) { throw new Exception("no sender given."); }
|
|
|
|
if(!is_object($_recipient)) { throw new Exception("no recipient given."); }
|
|
|
|
$this->sender = $_sender;
|
|
|
|
$this->recipient = $_recipient;
|
|
|
|
$this->config = $_config;
|
|
|
|
$this->preferences = $_preferences;
|
2011-02-21 17:30:27 +01:00
|
|
|
if(is_object($this->mail))
|
2011-02-21 13:12:29 +01:00
|
|
|
{
|
|
|
|
unset($this->mail);
|
|
|
|
}
|
2021-06-08 18:22:48 +02:00
|
|
|
|
2024-04-17 15:01:18 +02:00
|
|
|
// only pass account_id of sender, if different from current user, to be able to use current users
|
|
|
|
// personal mail account (requiring his password!) and not the global SMTP only account
|
|
|
|
$called_for = null;
|
|
|
|
if (!empty($_sender->account_id) && $_sender->account_id != $GLOBALS['egw_info']['user']['account_id'])
|
|
|
|
{
|
|
|
|
$called_for = $_sender->account_id;
|
|
|
|
}
|
2024-02-29 08:56:40 +01:00
|
|
|
// Use configured mail account
|
2024-03-06 19:34:13 +01:00
|
|
|
if(!empty($this->config->async_identity))
|
2021-06-08 18:22:48 +02:00
|
|
|
{
|
2024-04-17 15:01:18 +02:00
|
|
|
$ident = Api\Mail\Account::read($this->config->async_identity, $called_for);
|
2022-02-09 21:48:16 +01:00
|
|
|
}
|
2024-04-17 15:01:18 +02:00
|
|
|
$this->mail = new Api\Mailer($ident ?? null, $called_for);
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
/**
|
|
|
|
* sends notification
|
|
|
|
*
|
|
|
|
* @param array $_messages
|
2007-12-18 16:53:38 +01:00
|
|
|
* @param string $_subject
|
|
|
|
* @param array $_links
|
2007-11-22 09:29:16 +01:00
|
|
|
* @param array $_attachments
|
2019-09-13 14:30:55 +02:00
|
|
|
* @param array $_data value for key "reply_to" to use a custom ReplyTo address
|
2007-11-22 09:29:16 +01:00
|
|
|
*/
|
2018-01-24 19:02:35 +01:00
|
|
|
public function send(array $_messages, $_subject = false, $_links = false, $_attachments = false, $_data = false)
|
2014-11-25 15:22:16 +01:00
|
|
|
{
|
2022-04-26 21:04:16 +02:00
|
|
|
$body_plain = $_messages['plain'].$this->render_links($_links, false, $this->preferences->external_mailclient??false);
|
|
|
|
$body_html = "<html><body>\n".$_messages['html'].$this->render_links($_links, true, $this->preferences->external_mailclient??false)."</body>\n</html>\n";
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-11-22 09:29:16 +01:00
|
|
|
$this->mail->ClearAddresses();
|
|
|
|
$this->mail->ClearAttachments();
|
2014-11-25 15:22:16 +01:00
|
|
|
$this->mail->addAddress($this->recipient->account_email, $this->recipient->account_fullname);
|
|
|
|
$this->mail->addHeader('X-EGroupware-Type', 'notification-mail');
|
|
|
|
$this->mail->addHeader('X-EGroupware-Install', $GLOBALS['egw_info']['server']['install_id'].'@'.$GLOBALS['egw_info']['server']['default_domain']);
|
|
|
|
//$this->mail->AddHeader('X-EGroupware-URL', 'notification-mail');
|
|
|
|
//$this->mail->AddHeader('X-EGroupware-Tracker', 'notification-mail');
|
2014-11-18 11:50:35 +01:00
|
|
|
//error_log(__METHOD__.__LINE__."preparing notification message via email.".array2string($this->mail));
|
2014-11-25 15:22:16 +01:00
|
|
|
|
2019-09-13 14:30:55 +02:00
|
|
|
if ( $_data && !empty( $_data['reply_to'] ) )
|
|
|
|
{
|
|
|
|
$this->mail->addReplyTo($_data['reply_to']);
|
|
|
|
}
|
|
|
|
// do NOT set sender as From, as this might not be allowed, set it instead as ReplyTo, if that one it not explicitly set already
|
|
|
|
elseif ($this->mail->getHeader('From') != Api\Mailer::add_personal($this->sender->account_email, $this->sender->account_fullname))
|
|
|
|
{
|
|
|
|
$this->mail->addReplyTo($this->sender->account_email, $this->sender->account_fullname);
|
2019-03-12 22:26:35 +01:00
|
|
|
}
|
|
|
|
|
2015-02-26 15:56:23 +01:00
|
|
|
$this->mail->addHeader('Subject', trim($_subject)); // trim the subject to avoid strange wrong encoding problem
|
2017-10-27 19:03:54 +02:00
|
|
|
if ($_messages['html'])
|
|
|
|
{
|
|
|
|
// embed images as inline
|
|
|
|
\EGroupware\Api\Mail::processURL2InlineImages($this->mail, $body_html, null);
|
|
|
|
}
|
2014-11-25 15:22:16 +01:00
|
|
|
$this->mail->setHtmlBody($body_html, null, false); // no automatic alternativ
|
|
|
|
$this->mail->setBody($body_plain);
|
|
|
|
|
2011-06-23 11:05:25 +02:00
|
|
|
if(is_array($_attachments) && count($_attachments) > 0)
|
|
|
|
{
|
|
|
|
foreach($_attachments as $attachment)
|
|
|
|
{
|
|
|
|
if ($attachment->string)
|
|
|
|
{
|
|
|
|
$this->mail->AddStringAttachment($attachment->string, $attachment->filename, $attachment->encoding, $attachment->type);
|
|
|
|
}
|
|
|
|
elseif($attachment->path)
|
|
|
|
{
|
|
|
|
$this->mail->AddAttachment($attachment->path, $attachment->filename, $attachment->encoding, $attachment->type);
|
|
|
|
}
|
2011-05-26 15:26:09 +02:00
|
|
|
}
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
2014-11-18 11:50:35 +01:00
|
|
|
//error_log(__METHOD__.__LINE__."about sending notification message via email.".array2string($this->mail));
|
2014-11-25 15:22:16 +01:00
|
|
|
$this->mail->send();
|
2007-11-22 09:29:16 +01:00
|
|
|
}
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
/**
|
|
|
|
* renders plaintext/html links from given link array
|
|
|
|
*
|
|
|
|
* @param array $_links
|
|
|
|
* @param boolean $_render_html
|
|
|
|
* @param boolean $_render_external
|
|
|
|
* @return plain or html rendered link(s) as complete string
|
|
|
|
*/
|
2024-04-17 15:01:18 +02:00
|
|
|
private function render_links($_links = false, $_render_html = false, $_render_external = true)
|
|
|
|
{
|
2007-12-18 16:53:38 +01:00
|
|
|
if(!is_array($_links) || count($_links) == 0) { return false; }
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2007-12-18 16:53:38 +01:00
|
|
|
// provide defaults if given arguments are null
|
|
|
|
// php distinguishes between missing and present(null) arguments
|
|
|
|
if(is_null($_render_html)) { $_render_html = false; }
|
|
|
|
if(is_null($_render_external)) { $_render_external = true; }
|
2008-01-30 19:58:00 +01:00
|
|
|
$newline = $_render_html ? "<br />" : "\n";
|
2016-05-06 11:19:36 +02:00
|
|
|
$hruler = $_render_html ? Api\Html::hr() : '';
|
2008-07-30 08:49:55 +02:00
|
|
|
|
2008-01-30 19:58:00 +01:00
|
|
|
$rendered_links = array();
|
2007-12-18 16:53:38 +01:00
|
|
|
foreach($_links as $link) {
|
2008-01-30 19:58:00 +01:00
|
|
|
if($_render_external || ! $link->popup) { $link->view['no_popup'] = 1; }
|
|
|
|
// do not expose sensitive data
|
2016-05-06 11:19:36 +02:00
|
|
|
$url = preg_replace('/(sessionid|kp3|domain)=[^&]+&?/','',Api\Html::link('/index.php', $link->view));
|
2008-01-30 19:58:00 +01:00
|
|
|
// complete missing protocol and domain part if needed
|
2018-03-21 12:40:59 +01:00
|
|
|
if ($url[0] == '/' && $_render_external) $url = Api\Framework::getUrl($url);
|
2009-10-09 21:51:25 +02:00
|
|
|
$a_href = '<a href="'.$url.'" target="_blank">'.$link->text.'</a>';
|
|
|
|
$rendered_links[] = $_render_html ? $a_href : $url;
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
|
|
|
|
2008-01-30 19:58:00 +01:00
|
|
|
return $hruler.$newline.lang('Linked entries:').$newline.implode($newline,$rendered_links);
|
2007-12-18 16:53:38 +01:00
|
|
|
}
|
2007-11-22 09:29:16 +01:00
|
|
|
|
2022-03-28 18:47:56 +02:00
|
|
|
}
|