2001-01-11 10:52:33 +01:00
< ? php
2010-09-15 11:10:12 +02:00
/**
* eGroupWare API : Sending mail via egw_mailer
*
* @ 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 $
*/
/**
* New eGW send - class . It implements the old interface ( msg - method ) on top of PHPMailer .
*
2014-06-05 12:36:13 +02:00
* The configuration is read via emailadmin_account :: get_default_acc_id ( true ); // true=SMTP
2010-09-15 11:10:12 +02:00
*/
class send extends egw_mailer
{
var $err = array ();
var $to_res = array ();
// switching on debug with a numeric value other than 0, switches debug in PHPMailer/SMTP Class on
var $debug = false ;
2004-07-05 12:11:11 +02:00
/**
2010-09-15 11:10:12 +02:00
* eGW specific initialisation of the PHPMailer : charset , language , smtp - host , ...
*
* To be able to call PHPMailer ' s Send function , we check if a subject , body or address is set and call it in that case ,
* else we do our constructors work .
*/
2014-04-09 10:22:59 +02:00
function send ()
2001-03-26 00:24:19 +02:00
{
2010-09-15 11:10:12 +02:00
if ( $this -> debug && is_numeric ( $this -> debug )) $this -> SMTPDebug = $this -> debug ;
if ( $this -> Subject || $this -> Body || count ( $this -> to ))
2010-09-08 18:02:40 +02:00
{
2010-09-15 11:10:12 +02:00
if ( $this -> debug ) error_log ( __METHOD__ . " " . print_r ( $this -> Subject , true ) . " to be send " );
return PHPMailer :: Send ();
2010-09-08 18:02:40 +02:00
}
2010-09-15 11:10:12 +02:00
parent :: __construct (); // calling parent constructor
$this -> CharSet = translation :: charset ();
$this -> IsSmtp ();
2014-04-09 10:22:59 +02:00
// smtp settings from default account of current user
2014-06-05 12:36:13 +02:00
$account = emailadmin_account :: read ( emailadmin_account :: get_default_acc_id ( true )); // true=SMTP
2014-04-09 10:22:59 +02:00
$this -> Host = $account -> acc_smtp_host ;
$this -> Port = $account -> acc_smtp_port ;
switch ( $account -> acc_smtp_ssl )
2011-10-17 10:41:43 +02:00
{
2014-04-09 10:41:57 +02:00
case emailadmin_account :: SSL_TLS : // requires modified PHPMailer, or comment next two lines to use just ssl!
2014-04-09 10:22:59 +02:00
$this -> Host = 'tlsv1://' . $this -> Host ;
break ;
2014-04-09 10:41:57 +02:00
case emailadmin_account :: SSL_SSL :
2014-04-09 10:22:59 +02:00
$this -> Host = 'ssl://' . $this -> Host ;
break ;
2014-04-09 10:41:57 +02:00
case emailadmin_account :: SSL_STARTTLS : // PHPMailer uses 'tls' for STARTTLS, not ssl connection with tls version >= 1 and no sslv2/3
2014-04-09 10:22:59 +02:00
$this -> Host = 'tls://' . $this -> Host ;
2003-05-17 22:41:14 +02:00
}
2014-06-05 12:36:13 +02:00
$this -> SMTPAuth = ! empty ( $account -> acc_smtp_username );
2014-04-09 10:22:59 +02:00
$this -> Username = $account -> acc_smtp_username ;
$this -> Password = $account -> acc_smtp_password ;
$this -> defaultDomain = $account -> acc_domain ;
2014-11-18 12:04:22 +01:00
// we do not want to use the phpmailer defaults, as it is bound to fail anyway
// !from should be connected to the account used!
$this -> From = '' ;
$this -> FromName = '' ;
2014-08-22 15:26:18 +02:00
// use smpt-username as sender, if available, but only if it is a full email address
2014-11-18 12:04:22 +01:00
// we use setFrom as of from now on as it sets From, FromName and Sender
// error_log(__METHOD__.__LINE__.array2string($account));
$Sender = $account -> acc_smtp_username && strpos ( $account -> acc_smtp_username , '@' ) !== false ?
$account -> acc_smtp_username : $account -> ident_email ;
/* emailadmin_account Object has some possible info on the accounts realname
[ acc_name ] => example given ( mail @ domain . suffix )
[ ident_realname ] => example given
[ ident_email ] => mail @ domain . suffix ( maybe this is the content of $Sender ! )
[ ident_org ] => not considered
[ ident_name ] => example
*/
$Name = ( $account [ 'ident_realname' ] ? $account [ 'ident_realname' ] : ( $account [ 'ident_name' ] ? $account [ 'ident_name' ] :
( $account [ 'acc_name' ] ? $account [ 'acc_name' ] : $Sender )));
//error_log(__METHOD__.__LINE__.$Sender.','.$Name);
$this -> setFrom ( $Sender , $Name );
2010-09-15 11:10:12 +02:00
$this -> Hostname = $GLOBALS [ 'egw_info' ][ 'server' ][ 'hostname' ];
2014-04-09 10:22:59 +02:00
2014-04-09 10:49:07 +02:00
if ( $this -> debug ) error_log ( __METHOD__ . " () initialised egw_mailer with " . array2string ( $this ) . " from mail default account " . array2string ( $account -> params ));
2010-09-15 11:10:12 +02:00
}
2005-08-23 19:49:49 +02:00
2010-09-15 11:10:12 +02:00
/**
* Reset all Settings to send multiple Messages
*/
function ClearAll ()
{
$this -> err = array ();
2005-08-23 19:49:49 +02:00
2010-09-15 11:10:12 +02:00
$this -> Subject = $this -> Body = $this -> AltBody = '' ;
$this -> IsHTML ( False );
$this -> ClearAllRecipients ();
$this -> ClearAttachments ();
$this -> ClearCustomHeaders ();
2005-08-23 19:49:49 +02:00
2013-07-04 19:45:20 +02:00
$this -> FromName = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_fullname' ];
$this -> From = $GLOBALS [ 'egw_info' ][ 'user' ][ 'account_email' ];
2005-08-23 19:49:49 +02:00
2010-09-15 11:10:12 +02:00
$this -> AddCustomHeader ( 'X-Mailer:eGroupWare (http://www.eGroupWare.org)' );
}
2005-08-23 19:49:49 +02:00
2010-09-15 11:10:12 +02:00
/**
* Emulating the old send :: msg interface for compatibility with existing code
*
* You can either use that code or the PHPMailer variables and methods direct .
*/
function msg ( $service , $to , $subject , $body , $msgtype = '' , $cc = '' , $bcc = '' , $from = '' , $sender = '' , $content_type = '' , $boundary = 'Message-Boundary' )
{
if ( $this -> debug ) error_log ( __METHOD__ . " to=' $to ',subject=' $subject ',,' $msgtype ',cc=' $cc ',bcc=' $bcc ',from=' $from ',sender=' $sender ' " );
2014-04-09 10:22:59 +02:00
unset ( $boundary ); // not used, but required by function signature
2010-09-15 11:10:12 +02:00
//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 !!!
2005-08-23 19:49:49 +02:00
2010-09-15 11:10:12 +02:00
if ( $service != 'email' )
{
return False ;
}
if ( $from )
{
2014-04-09 10:22:59 +02:00
$matches = null ;
2010-09-15 11:10:12 +02:00
if ( preg_match ( '/"?(.+)"?<(.+)>/' , $from , $matches ))
2005-08-23 19:49:49 +02:00
{
2010-09-15 11:10:12 +02:00
list (, $this -> FromName , $this -> From ) = $matches ;
2005-08-23 19:49:49 +02:00
}
2010-09-15 11:10:12 +02:00
else
2001-03-26 00:24:19 +02:00
{
2010-09-15 11:10:12 +02:00
$this -> From = $from ;
$this -> FromName = '' ;
}
}
if ( $sender )
{
$this -> Sender = $sender ;
}
foreach ( array ( 'to' , 'cc' , 'bcc' ) as $adr )
{
if ( $$adr )
{
if ( is_string ( $$adr ) && preg_match_all ( '/"?(.+)"?<(.+)>,?/' , $$adr , $matches ))
2001-09-28 23:38:29 +02:00
{
2010-09-15 11:10:12 +02:00
$names = $matches [ 1 ];
$addresses = $matches [ 2 ];
2001-09-28 23:38:29 +02:00
}
2004-07-05 12:11:11 +02:00
else
2001-09-28 23:38:29 +02:00
{
2011-03-21 14:13:41 +01:00
$addresses = is_string ( $$adr ) ? explode ( ',' , trim ( $$adr )) : explode ( ',' , trim ( array_shift ( $$adr )));
2010-09-15 11:10:12 +02:00
$names = array ();
2005-08-23 19:49:49 +02:00
}
2010-09-15 11:10:12 +02:00
$method = 'Add' . ( $adr == 'to' ? 'Address' : $adr );
2005-08-23 19:49:49 +02:00
2010-09-15 11:10:12 +02:00
foreach ( $addresses as $n => $address )
{
$this -> $method ( $address , $names [ $n ]);
2001-03-26 00:24:19 +02:00
}
}
2001-09-28 23:38:29 +02:00
}
2010-09-15 11:10:12 +02:00
if ( ! empty ( $msgtype ))
{
$this -> AddCustomHeader ( 'X-eGW-Type: ' . $msgtype );
}
if ( $content_type )
{
$this -> ContentType = $content_type ;
}
$this -> Subject = $subject ;
$this -> Body = $body ;
2005-08-23 19:49:49 +02:00
2010-09-15 11:10:12 +02:00
//echo "PHPMailer = <pre>".print_r($this,True)."</pre>\n";
if ( ! $this -> Send ())
2001-05-30 21:47:13 +02:00
{
2010-09-15 11:10:12 +02:00
$this -> err = array (
'code' => 1 , // we dont get a numerical code from PHPMailer
'msg' => $this -> ErrorInfo ,
'desc' => $this -> ErrorInfo ,
);
return False ;
2001-05-30 21:47:13 +02:00
}
2010-09-15 11:10:12 +02:00
return True ;
}
/**
* encode 8 - bit chars in subject - line
*
* @ deprecated This is not needed any more , as it is done be PHPMailer , but older code depend on it .
*/
function encode_subject ( $subject )
{
return $subject ;
2004-07-05 12:11:11 +02:00
}
2010-09-15 11:10:12 +02:00
}