fixed default SMTP account was not returned during setup and deprecated old send class in favor of using egw_mailer direct

This commit is contained in:
Ralf Becker 2015-04-24 10:02:49 +00:00
parent 540b6e9e28
commit eea811d46f
4 changed files with 56 additions and 126 deletions

View File

@ -1300,11 +1300,14 @@ class emailadmin_account implements ArrayAccess
if ($only_current_user)
{
$account_id = $only_current_user === true ? $GLOBALS['egw_info']['user']['account_id'] : $only_current_user;
if (!is_numeric($account_id))
// no account_id happens eg. for notifications during login
if ($account_id && !is_numeric($account_id))
{
throw new egw_exception_wrong_parameter(__METHOD__."(".array2string($only_current_user).") is NO valid account_id");
}
$where[] = self::$db->expression(self::VALID_TABLE, self::VALID_TABLE.'.', array('account_id' => self::memberships($account_id)));
$where[] = self::$db->expression(self::VALID_TABLE, self::VALID_TABLE.'.', array(
'account_id' => $account_id ? self::memberships($account_id) : 0
));
}
if (empty($order_by) || !preg_match('/^[a-z_]+ (ASC|DESC)$/i', $order_by))
{
@ -1402,6 +1405,7 @@ class emailadmin_account implements ArrayAccess
*/
static function get_default($smtp=false, $return_id=false)
{
if ($smtp) return null;
try
{
foreach(emailadmin_account::search(true, 'params') as $acc_id => $params)

View File

@ -422,102 +422,6 @@ class egw_mailer extends Horde_Mime_Mail
return $this->addMimePart($part);
}
/**
* static method to send messages e.g. when login failed to configured admins.
*
* this method is to replace $GLOBALS['egw']->send->msg(...)
*
* @param string $service String must be email for compatibility with send->msg
* @param mixed $to address to send to; array or string
* @param string $subject subject to use
* @param string $body body to use for message. plain text
* @param string $msgtype text to use for X-eGW-Type
* @param mixed $cc address to cc to; array or string
* @param mixed $bcc address to bcc to; array or string
* @param string $from from address to use
* @param string $sender sender information to use
* @param string $content_type ignored; compatibility; could cause issues regarding html content
* @param string $boundary ignored; compatibility
* @return boolean or exeption
*/
public static function sendWithDefaultSmtpProfile($service, $to, $subject, $body, $msgtype='', $cc='', $bcc='', $from='', $sender='', $content_type='', $boundary='Message-Boundary')
{
unset($content_type); // not used
if ($service != 'email')
{
return False;
}
unset($boundary); // not used, but required by function signature
try
{
$smtpAcc = emailadmin_account::get_default(true,false,false);
if (!$smtpAcc)
{
error_log(__METHOD__.__LINE__.'#'." Error: no SMTP Account available for ".__METHOD__);
return false;
}
//error_log(__METHOD__.__LINE__.'#'.array2string($smtpAcc));
$mail = new egw_mailer($smtpAcc);
$method = array();
foreach(array('to','cc','bcc') as $adr)
{
if ($$adr && !is_array($$adr))
{
$matches = null;
if (is_string($$adr) && preg_match_all('/"?(.+)"?<(.+)>,?/',$$adr,$matches))
{
$names = $matches[1];
$addresses = $matches[2];
}
else
{
$addresses = is_string($$adr) ? explode(',',trim($$adr)) : explode(',',trim(array_shift($$adr)));
$names = array();
}
foreach($addresses as $address)
{
$method[$adr][] =$address;
}
}
}
if (is_array($method['to'])&& !empty($method['to'])) $to = $method['to'];
foreach ((array)$to as $toElem)
{
if (!empty($toElem)) $mail->addAddress($toElem, $toElem);
}
if (is_array($method['cc'])&& !empty($method['cc'])) $cc = $method['cc'];
foreach ((array)$cc as $ccElem)
{
if (!empty($ccElem)) $mail->addCc($ccElem);
}
if (is_array($method['bcc'])&& !empty($method['bcc'])) $bcc = $method['bcc'];
foreach ((array)$bcc as $bccElem)
{
if (!empty($bccElem)) $mail->addBcc($bccElem);
}
//error_log(__METHOD__.__LINE__."preparing notification message via email.".array2string($mail));
if ($from)
{
$matches = null;
if (preg_match('/"?(.+)"?<(.+)>/',$from,$matches))
{
list(,$FromName,$from) = $matches;
}
}
$mail->setFrom($from, $FromName);
$mail->addHeader('Subject', trim($subject)); // trim the subject to avoid strange wrong encoding problem
if ($sender) $mail->addHeader('Return-Path', '<'.$sender.'>', true);
if ($msgtype) $mail->addHeader('X-eGW-Type',$msgtype);
$mail->setBody($body);
$mail->send();
} catch(Exception $e) {
throw $e;
}
return True;
}
/**
* Send mail, injecting mail transport from account
*

View File

@ -748,23 +748,23 @@ class egw_session
if ($blocked && $GLOBALS['egw_info']['server']['admin_mails'] &&
$GLOBALS['egw_info']['server']['login_blocked_mail_time'] < time()-5*60) // max. one mail every 5mins
{
// notify admin(s) via email
$from = 'eGroupWare@'.$GLOBALS['egw_info']['server']['mail_suffix'];
$subject = lang("eGroupWare: login blocked for user '%1', IP %2",$login,$ip);
$body = lang("Too many unsucessful attempts to login: %1 for the user '%2', %3 for the IP %4",$false_id,$login,$false_ip,$ip);
$admin_mails = explode(',',$GLOBALS['egw_info']['server']['admin_mails']);
foreach($admin_mails as $to)
{
try {
//$GLOBALS['egw']->send->msg('email',$to,$subject,$body,'','','',$from,$from); // deprecated old method
egw_mailer::sendWithDefaultSmtpProfile('email',$to,$subject,$body,'','','',$from);
} catch(Exception $e) {
$mailer = new egw_mailer();
// notify admin(s) via email
$mailer->setFrom('eGroupWare@'.$GLOBALS['egw_info']['server']['mail_suffix']);
$mailer->addHeader('Subject', lang("eGroupWare: login blocked for user '%1', IP %2",$login,$ip));
$mailer->setBody(lang("Too many unsucessful attempts to login: %1 for the user '%2', %3 for the IP %4",$false_id,$login,$false_ip,$ip));
foreach(preg_split('/,\s*/',$GLOBALS['egw_info']['server']['admin_mails']) as $mail)
{
$mailer->addAddress($mail);
}
$mailer->send();
}
catch(Exception $e) {
// ignore exception, but log it, to block the account and give a correct error-message to user
error_log(__METHOD__."('$login', '$ip') ".$e->getMessage());
}
}
// save time of mail, to not send to many mails
$config = new config('phpgwapi');
$config->read_repository();

View File

@ -11,9 +11,7 @@
*/
/**
* New eGW send-class. It implements the old interface (msg-method) on top of PHPMailer.
*
* The configuration is read via emailadmin_account::get_default_acc_id(true); // true=SMTP
* @deprecated use egw_mailer class direct
*/
class send extends egw_mailer
{
@ -25,7 +23,7 @@ class send extends egw_mailer
*/
function send()
{
if ($this->getHeader('Subject') || $this->Body || count($this->getAddresses('to', true)))
if ($this->Subject || $this->Body || count($this->to))
{
return parent::send();
}
@ -55,13 +53,14 @@ class send extends egw_mailer
* Emulating the old send::msg interface for compatibility with existing code
*
* You can either use that code or the PHPMailer variables and methods direct.
* @deprecated use egw_mailer::sendWithDefaultSmtpProfile
*/
function msg($service, $to, $subject, $body, $msgtype='', $cc='', $bcc='', $from='', $sender='', $content_type='', $boundary='Message-Boundary')
{
//error_log(__METHOD__." to='$to',subject='$subject',,'$msgtype',cc='$cc',bcc='$bcc',from='$from',sender='$sender'");
if ($this->debug) error_log(__METHOD__." to='$to',subject='$subject',,'$msgtype',cc='$cc',bcc='$bcc',from='$from',sender='$sender'");
unset($boundary); // not used, but required by function signature
//echo "<p>send::msg(,to='$to',subject='$subject',,'$msgtype',cc='$cc',bcc='$bcc',from='$from',sender='$sender','$content_type','$boundary')<pre>$body</pre>\n";
$this->ClearAll(); // reset everything to its default, we might be called more then once !!!
if ($service != 'email')
{
return False;
@ -71,8 +70,17 @@ class send extends egw_mailer
$matches = null;
if (preg_match('/"?(.+)"?<(.+)>/',$from,$matches))
{
list(,$FromName,$from) = $matches;
list(,$this->FromName,$this->From) = $matches;
}
else
{
$this->From = $from;
$this->FromName = '';
}
}
if ($sender)
{
$this->Sender = $sender;
}
foreach(array('to','cc','bcc') as $adr)
{
@ -88,21 +96,35 @@ class send extends egw_mailer
$addresses = is_string($$adr) ? explode(',',trim($$adr)) : explode(',',trim(array_shift($$adr)));
$names = array();
}
$method = 'Add'.($adr == 'to' ? 'Address' : $adr);
foreach($addresses as $n => $address)
{
$method[$adr][] =$address;
$this->$method($address,$names[$n]);
}
}
}
if (!empty($msgtype))
{
$this->AddCustomHeader('X-eGW-Type: '.$msgtype);
}
if ($content_type)
{
$this->ContentType = $content_type;
}
$this->Subject = $subject;
$this->Body = $body;
try {
egw_mailer::sendWithDefaultSmtpProfile('email',$method['to'],$subject,$body,'',$method['cc'],$method['bcc'],$from,$sender);
} catch(Exception $e) {
// ignore exception, but log it, to block the account and give a correct error-message to user
return false;
//echo "PHPMailer = <pre>".print_r($this,True)."</pre>\n";
if (!$this->Send())
{
$this->err = array(
'code' => 1, // we dont get a numerical code from PHPMailer
'msg' => $this->ErrorInfo,
'desc' => $this->ErrorInfo,
);
return False;
}
return True;
}