diff --git a/include/class.email.php b/include/class.email.php index 0cc04010bf54d85212f3d49f8c05143be9c1c107..e6b10c3892ece68f9d0410a77116f67cf7de61dd 100644 --- a/include/class.email.php +++ b/include/class.email.php @@ -132,7 +132,7 @@ class Email { $info = array ( 'host' => $this->ht['smtp_host'], 'port' => $this->ht['smtp_port'], - 'auth' => $this->ht['smtp_auth'], + 'auth' => (bool) $this->ht['smtp_auth'], 'username' => $this->ht['userid'], 'password' => Mcrypt::decrypt($this->ht['userpass'], SECRET_SALT) ); @@ -141,87 +141,15 @@ class Email { } function send($to, $subject, $message, $attachments=null, $options=null) { - global $cfg, $ost; - - //Get SMTP info IF enabled! - $smtp=array(); - if($this->isSMTPEnabled() && ($info=$this->getSMTPInfo())) { //is SMTP enabled for the current email? - $smtp=$info; - }elseif($cfg && ($email=$cfg->getDefaultSMTPEmail()) && $email->isSMTPEnabled()) { //What about global SMTP setting? - if($email->allowSpoofing() && ($info=$email->getSMTPInfo())) //If spoofing is allowed..then continue. - $smtp=$info; - elseif($email->getId()!=$this->getId()) //No spoofing allowed. Send it via the default SMTP email. - return $email->send($to,$subject,$message,$attachments,$options); - } - - //Get the goodies - require_once ('Mail.php'); // PEAR Mail package - require_once ('Mail/mime.php'); // PEAR Mail_Mime packge - - //do some cleanup - $eol="\n"; - $to=preg_replace("/(\r\n|\r|\n)/s",'', trim($to)); - $subject=stripslashes(preg_replace("/(\r\n|\r|\n)/s",'', trim($subject))); - $body = stripslashes(preg_replace("/(\r\n|\r)/s", "\n", trim($message))); - $fromname=$this->getName(); - $from =sprintf('"%s"<%s>',($fromname?$fromname:$this->getEmail()),$this->getEmail()); - $headers = array ('From' => $from, - 'To' => $to, - 'Subject' => $subject, - 'Date'=>date('D, d M Y H:i:s O'), - 'Message-ID' =>'<'.Misc::randCode(6).''.time().'-'.$this->getEmail().'>', - 'X-Mailer' =>'osTicket v1.7', - 'Content-Type' => 'text/html; charset="UTF-8"' - ); - - $mime = new Mail_mime(); - $mime->setTXTBody($body); - //XXX: Attachments - if($attachments){ - foreach($attachments as $attachment) { - if($attachment['file_id'] && ($file=AttachmentFile::lookup($attachment['file_id']))) - $mime->addAttachment($file->getData(),$file->getType(), $file->getName(),false); - elseif($attachment['file'] && file_exists($attachment['file']) && is_readable($attachment['file'])) - $mime->addAttachment($attachment['file'],$attachment['type'],$attachment['name']); - } - } - - $options=array('head_encoding' => 'quoted-printable', - 'text_encoding' => 'quoted-printable', - 'html_encoding' => 'base64', - 'html_charset' => 'utf-8', - 'text_charset' => 'utf-8'); - //encode the body - $body = $mime->get($options); - //encode the headers. - $headers = $mime->headers($headers); - if($smtp) { //Send via SMTP - $mail = mail::factory('smtp', - array ('host' => $smtp['host'], - 'port' => $smtp['port'], - 'auth' => $smtp['auth']?true:false, - 'username' => $smtp['username'], - 'password' => $smtp['password'], - 'timeout' =>20, - 'debug' => false, - )); - $result = $mail->send($to, $headers, $body); - if(!PEAR::isError($result)) - return true; - //SMTP failed - log error. - $alert=sprintf("Unable to email via %s:%d [%s]\n\n%s\n",$smtp['host'],$smtp['port'],$smtp['username'],$result->getMessage()); - $ost->logError('SMTP Error', $alert, false); //NOTE: email alert overwrite - don't email when having email trouble. - //print_r($result); - } - //No SMTP or it failed....use php's native mail function. - $mail = mail::factory('mail'); - return PEAR::isError($mail->send($to, $headers, $body))?false:true; + $mailer = new Mailer($this); + if($attachments) + $mailer->addAttachments($attachments); + return $mailer->send($to, $subject, $message, $options); } - function update($vars,&$errors) { $vars=$vars; $vars['cpasswd']=$this->getPasswd(); //Current decrypted password. @@ -254,42 +182,8 @@ class Email { /******* Static functions ************/ - - //sends emails using native php mail function Email::sendmail( ......); - //Don't use this function if you can help it. - function sendmail($to,$subject,$message,$from) { - - require_once ('Mail.php'); // PEAR Mail package - require_once ('Mail/mime.php'); // PEAR Mail_Mime packge - - $eol="\n"; - $to=preg_replace("/(\r\n|\r|\n)/s",'', trim($to)); - $subject=stripslashes(preg_replace("/(\r\n|\r|\n)/s",'', trim($subject))); - $body = stripslashes(preg_replace("/(\r\n|\r)/s", "\n", trim($message))); - $headers = array ('From' =>$from, - 'To' => $to, - 'Subject' => $subject, - 'Message-ID' =>'<'.Misc::randCode(10).''.time().'@osTicket>', - 'X-Mailer' =>'osTicket v 1.6', - 'Content-Type' => 'text/html; charset="UTF-8"' - ); - $mime = new Mail_mime(); - $mime->setTXTBody($body); - $options=array('head_encoding' => 'quoted-printable', - 'text_encoding' => 'quoted-printable', - 'html_encoding' => 'base64', - 'html_charset' => 'utf-8', - 'text_charset' => 'utf-8'); - //encode the body - $body = $mime->get($options); - //headers - $headers = $mime->headers($headers); - $mail = mail::factory('mail'); - return PEAR::isError($mail->send($to, $headers, $body))?false:true; - } - - - function getIdByEmail($email) { + + function getIdByEmail($email) { $sql='SELECT email_id FROM '.EMAIL_TABLE.' WHERE email='.db_input($email); if(($res=db_query($sql)) && db_num_rows($res)) @@ -411,7 +305,7 @@ class Email { $smtp = mail::factory('smtp', array ('host' => $vars['smtp_host'], 'port' => $vars['smtp_port'], - 'auth' => $vars['smtp_auth']?true:false, + 'auth' => (bool) $vars['smtp_auth'], 'username' =>$vars['userid'], 'password' =>$passwd, 'timeout' =>20, diff --git a/include/class.mailer.php b/include/class.mailer.php new file mode 100644 index 0000000000000000000000000000000000000000..a469d5528f6598489a8a8ecd63b795594f5db322 --- /dev/null +++ b/include/class.mailer.php @@ -0,0 +1,180 @@ +<?php +/********************************************************************* + class.mailer.php + + osTicket mailer + + It's mainly PEAR MAIL wrapper for now (more improvements planned). + + Peter Rotich <peter@osticket.com> + Copyright (c) 2006-2012 osTicket + http://www.osticket.com + + Released under the GNU General Public License WITHOUT ANY WARRANTY. + See LICENSE.TXT for details. + + vim: expandtab sw=4 ts=4 sts=4: +**********************************************************************/ + +include_once(INCLUDE_DIR.'class.email.php'); + +class Mailer { + + var $email; + + var $ht = array(); + var $attachments = array(); + + var $smtp = array(); + var $eol="\n"; + + function Mailer($email=null, $options=null) { + global $cfg; + + if(is_object($email) && $email->isSMTPEnabled() && ($info=$email->getSMTPInfo())) { //is SMTP enabled for the current email? + $this->smtp = $info; + } elseif($cfg && ($e=$cfg->getDefaultSMTPEmail()) && $e->isSMTPEnabled()) { //What about global SMTP setting? + $this->smtp = $e->getSMTPInfo(); + if(!$e->allowSpoofing() || !$email) + $email = $e; + } elseif(!$email && $cfg && ($e=$cfg->getDefaultEmail())) { + if($e->isSMTPEnabled() && ($info=$email->getSMTPInfo())) + $this->smtp = $info; + $email = $e; + } + + $this->email = $email; + $this->attachments = array(); + } + + function getEOL() { + return $this->eol; + } + + function getEmail() { + return $this->email; + } + + function getSMTPInfo() { + return $this->smtp; + } + /* FROM Address */ + function setFromAddress($from) { + $this->ht['from'] = $from; + } + + function getFromAddress() { + + if(!$this->ht['from'] && $this->getEmail()) + $this->ht['from'] =$this->getEmail()->getAddress(); + + return $this->ht['from']; + } + + /* attachments */ + function getAttachments() { + return $this->attachments; + } + + function addAttachment($attachment) { + $this->attachments[] = $attachment; + } + + function addAttachments($attachments) { + $this->attachments = array_merge($this->attachments, $attachments); + } + + function send($to, $subject, $message, $options=null) { + global $ost; + + //Get the goodies + require_once (PEAR_DIR.'Mail.php'); // PEAR Mail package + require_once (PEAR_DIR.'Mail/mime.php'); // PEAR Mail_Mime packge + + //do some cleanup + $to=preg_replace("/(\r\n|\r|\n)/s",'', trim($to)); + $subject=stripslashes(preg_replace("/(\r\n|\r|\n)/s",'', trim($subject))); + $body = stripslashes(preg_replace("/(\r\n|\r)/s", "\n", trim($message))); + + /* Message ID - generated for each outgoing email */ + $messageId = sprintf('<%s%d-%s>', Misc::randCode(6), time(), + ($this->getEmail()?$this->getEmail()->getEmail():'@osTicketMailer')); + + $headers = array ( + 'From' => $this->getFromAddress(), + 'To' => $to, + 'Subject' => $subject, + 'Date'=> date('D, d M Y H:i:s O'), + 'Message-ID' => $messageId, + 'X-Mailer' =>'osTicket Mailer', + 'Content-Type' => 'text/html; charset="UTF-8"' + ); + + $mime = new Mail_mime(); + $mime->setTXTBody($body); + //XXX: Attachments + if(($attachments=$this->getAttachments())) { + foreach($attachments as $attachment) { + if($attachment['file_id'] && ($file=AttachmentFile::lookup($attachment['file_id']))) + $mime->addAttachment($file->getData(),$file->getType(), $file->getName(),false); + elseif($attachment['file'] && file_exists($attachment['file']) && is_readable($attachment['file'])) + $mime->addAttachment($attachment['file'],$attachment['type'],$attachment['name']); + } + } + + //Desired encodings... + $encodings=array( + 'head_encoding' => 'quoted-printable', + 'text_encoding' => 'quoted-printable', + 'html_encoding' => 'base64', + 'html_charset' => 'utf-8', + 'text_charset' => 'utf-8', + 'head_charset' => 'utf-8' + ); + //encode the body + $body = $mime->get($encodings); + //encode the headers. + $headers = $mime->headers($headers); + if(($smtp=$this->getSMTPInfo())) { //Send via SMTP + $mail = mail::factory('smtp', + array ('host' => $smtp['host'], + 'port' => $smtp['port'], + 'auth' => $smtp['auth'], + 'username' => $smtp['username'], + 'password' => $smtp['password'], + 'timeout' =>20, + 'debug' => false, + )); + + $result = $mail->send($to, $headers, $body); + if(!PEAR::isError($result)) + return $messageId; + + $alert=sprintf("Unable to email via SMTP:%s:%d [%s]\n\n%s\n", + $smtp['host'], $smtp['port'], $smtp['username'], $result->getMessage()); + $this->logError($alert); + } + + //No SMTP or it failed....use php's native mail function. + $mail = mail::factory('mail'); + return PEAR::isError($mail->send($to, $headers, $body))?false:$messageId; + + } + + function logError($error) { + global $ost; + //NOTE: Admin alert overwrite - don't email when having email trouble! + $ost->logError('Mailer Error', $error, false); + } + + /******* Static functions ************/ + + //Emails using native php mail function - if DB connection doesn't exist. + //Don't use this function if you can help it. + function sendmail($to, $subject, $message, $from) { + $mailer = new Mailer(); + $mailer->setFromAddress($from); + return $mailer->send($to, $subject, $message); + } +} +?> diff --git a/include/class.upgrader.php b/include/class.upgrader.php index b08d319d8db65eb1dcd267e8ec43eb037d470bf1..7fa6c331ff542c44e7f4329c4e164f0848cf837e 100644 --- a/include/class.upgrader.php +++ b/include/class.upgrader.php @@ -69,7 +69,7 @@ class Upgrader extends SetupWizard { if($email) { $email->send($thistaff->getEmail(), $subject, $error); } else {//no luck - try the system mail. - Email::sendmail($thistaff->getEmail(), $subject, $error, sprintf('"osTicket Alerts"<%s>', $thistaff->getEmail())); + Mailer::sendmail($thistaff->getEmail(), $subject, $error, sprintf('"osTicket Alerts"<%s>', $thistaff->getEmail())); } } diff --git a/main.inc.php b/main.inc.php index 6d30b7fce585c53f346824aa3a9233bc87311094..abf1eb5ecdfca2d9e93534fbc72c8001c544c89a 100644 --- a/main.inc.php +++ b/main.inc.php @@ -109,6 +109,7 @@ require(INCLUDE_DIR.'class.nav.php'); require(INCLUDE_DIR.'class.format.php'); //format helpers require(INCLUDE_DIR.'class.validator.php'); //Class to help with basic form input validation...please help improve it. + require(INCLUDE_DIR.'class.mailer.php'); require(INCLUDE_DIR.'mysql.php'); #CURRENT EXECUTING SCRIPT. @@ -178,7 +179,7 @@ if($ferror) { //Fatal error //try alerting admin using email in config file $msg=$ferror."\n\n".THISPAGE; - Email::sendmail(ADMIN_EMAIL, 'osTicket Fatal Error', $msg, sprintf('"osTicket Alerts"<%s>', ADMIN_EMAIL)); + Mailer::sendmail(ADMIN_EMAIL, 'osTicket Fatal Error', $msg, sprintf('"osTicket Alerts"<%s>', ADMIN_EMAIL)); //Display generic error to the user die("<b>Fatal Error:</b> Contact system administrator."); exit;