00001 <?php
00029 class emailService extends MetaObject
00030 {
00036 public $m_Accounts = null;
00037
00043 private $_errorMessage;
00044
00050 protected $m_UseAccount;
00051
00057 private $_mail;
00058
00064 private $_logEnabled = null;
00065
00072 private $_logType = 'DEFAULT';
00073
00079 private $_logObject;
00080
00087 function __construct(&$xmlArr)
00088 {
00089 $this->readMetadata($xmlArr);
00090 $this->_constructMail();
00091 $acct = $this->m_Accounts->current();
00092 if (! $acct)
00093 return;
00094 $this->useAccount($acct->m_Name);
00095 }
00096
00103 protected function readMetadata(&$xmlArr)
00104 {
00105 parent::readMetaData($xmlArr);
00106 $this->m_Accounts = new MetaIterator($xmlArr["PLUGINSERVICE"]["ACCOUNTS"]["ACCOUNT"], "EmailAccount");
00107 $this->_logEnabled = $xmlArr["PLUGINSERVICE"]["LOGGING"]["ATTRIBUTES"]["ENABLED"];
00108 if ($this->_logEnabled)
00109 {
00110 $this->_logType = $xmlArr["PLUGINSERVICE"]["LOGGING"]["ATTRIBUTES"]["TYPE"];
00111 $this->_logObject = $xmlArr["PLUGINSERVICE"]["LOGGING"]["ATTRIBUTES"]["OBJECT"];
00112 }
00113 }
00114
00120 public function getErrorMsg()
00121 {
00122 return $this->_errorMessage;
00123 }
00124
00130 private function _constructMail ()
00131 {
00132 require_once 'Zend/Mail.php';
00133 $this->_mail = new Zend_Mail('utf-8');
00134 }
00135
00142 public function useAccount($accountName)
00143 {
00144
00145 if ($this->_mail)
00146 $this->_constructMail();
00147
00148 $this->m_UseAccount = $accountName;
00149 $account = $this->m_Accounts->get($accountName);
00150
00151 if ($account->m_IsSMTP == "Y")
00152 {
00153 require_once 'Zend/Mail/Transport/Smtp.php';
00154 if ($account->m_SMTPAuth == "Y")
00155 {
00156 $config = array('auth' =>'login', 'username'=>$account->m_Username , 'password'=>$account->m_Password, "port"=>$account->m_Port, 'ssl'=>$account->m_SSL);
00157 }
00158 else
00159 {
00160 $config = array();
00161 }
00162 $mailTransport = new Zend_Mail_Transport_Smtp($account->m_Host, $config);
00163 $this->_mail->setDefaultTransport($mailTransport);
00164 }
00165 else
00166 {
00167 require_once 'Zend/Mail/Transport/Sendmail.php';
00168 $mailTransport = new Zend_Mail_Transport_Sendmail();
00169 $this->_mail->setDefaultTransport($mailTransport);
00170 }
00171 $this->_mail->setFrom($account->m_FromEmail, $account->m_FromName);
00172 return $this;
00173 }
00174
00188 public function sendEmail ($TOs = null, $CCs = null, $BCCs = null, $subject, $body, $attachments = null, $isHTML = false)
00189 {
00190 $mail = $this->_mail;
00191
00192 if ($TOs)
00193 {
00194 foreach ($TOs as $to)
00195 {
00196 if(is_array($to))
00197 {
00198 $mail->addTo($to['email'],$to['name']);
00199 }
00200 else
00201 {
00202 $mail->addTo($to);
00203 }
00204 }
00205 }
00206
00207 if ($CCs)
00208 {
00209 foreach ($CCs as $cc)
00210 {
00211 if(is_array($cc))
00212 {
00213 $mail->AddCC($cc['email'],$cc['name']);
00214 }
00215 else
00216 {
00217 $mail->AddCC($cc);
00218 }
00219 }
00220 }
00221
00222 if ($BCCs)
00223 {
00224 foreach ($BCCs as $bcc)
00225 {
00226 if(is_array($bcc))
00227 {
00228 $mail->AddBCC($bcc['email'],$bcc['name']);
00229 }
00230 else
00231 {
00232 $mail->AddBCC($bcc);
00233 }
00234 }
00235 }
00236
00237 if ($attachments)
00238 foreach ($attachments as $att)
00239 $mail->CreateAttachment(file_get_contents($att));
00240 $mail->setSubject($subject);
00241 $body = str_replace("\\n", "\n", $body);
00242 if ($isHTML == TRUE)
00243 {
00244 $mail->setBodyHTML($body);
00245 }
00246 else
00247 {
00248 $mail->setBodyText($body);
00249 }
00250
00251 try
00252 {
00253 $result = $mail->Send();
00254 $this->logEmail('Success', $subject, $body, $TOs, $CCs, $BCCs);
00255 return TRUE;
00256 }
00257 catch (Exception $e)
00258 {
00259 $result = "ERROR: ".$e->getMessage();
00260 $this->logEmail($result, $subject, $body, $TOs, $CCs, $BCCs);
00261 return FALSE;
00262 }
00263 }
00264
00275 public function logEmail($result, $subject, $body = NULL, $TOs = NULL, $CCs = NULL, $BCCs = NULL)
00276 {
00277
00278 $recipients = '';
00279
00280 if ($TOs)
00281 {
00282 foreach ($TOs as $to)
00283 {
00284 if(is_array($to))
00285 {
00286 $recipients .= $to['name']."<".$to['email'].">;";
00287 }
00288 else
00289 {
00290 $recipients .= $to.";";
00291 }
00292 }
00293 }
00294
00295 if ($CCs)
00296 {
00297 foreach ($CCs as $cc)
00298 {
00299 if(is_array($cc))
00300 {
00301 $recipients .= $cc['name']."<".$cc['email'].">;";
00302 }
00303 else
00304 {
00305 $recipients .= $cc.";";
00306 }
00307 }
00308 }
00309
00310 if ($BCCs)
00311 {
00312 foreach ($BCCs as $bcc)
00313 {
00314 if(is_array($bcc))
00315 {
00316 $recipients .= $bcc['name']."<".$bcc['email'].">;";
00317 }
00318 else
00319 {
00320 $recipients .= $bcc.";";
00321 }
00322 }
00323 }
00324 if ($this->_logType == 'DB')
00325 {
00326 $account = $this->m_Accounts->get($this->m_UseAccount);
00327 $sender_name = $account->m_FromName;
00328 $sender = $account->m_FromEmail;
00329
00330
00331 $boMessageLog = BizSystem::getObject($this->_logObject);
00332 $mlArr = $boMessageLog->newRecord();
00333 $mlArr["sender"] = $sender;
00334 $mlArr["sender_name"] = $sender_name;
00335 $mlArr["recipients"] = $recipients;
00336 $mlArr["subject"] = $subject;
00337 $mlArr["content"] = $body;
00338 $mlArr["result"] = $result;
00339
00340
00341
00342
00343
00344
00345
00346
00347 $ok = $boMessageLog->insertRecord($mlArr);
00348 if (! $ok)
00349 {
00350 return $boMessageLog->getErrorMessage();
00351 }
00352 else
00353 {
00354 return TRUE;
00355 }
00356 }
00357 else
00358 {
00359 $back_trace = debug_backtrace();
00360 if ($result == 'Success')
00361 {
00362 $logNum = LOG_INFO;
00363 } else
00364 {
00365 $logNum = LOG_ERR;
00366 }
00367 BizSystem::log($logNum, "EmailService", "Sent email with subject - \"$subject\" and body - $body to - $recipients with result $result.", NULL, $back_trace);
00368 }
00369 }
00370 }
00371
00380 class EmailAccount extends MetaObject
00381 {
00382 public $m_Name;
00383 public $m_Host;
00384 public $m_Port;
00385 public $m_SSL;
00386 public $m_FromName;
00387 public $m_FromEmail;
00388 public $m_IsSMTP;
00389 public $m_SMTPAuth;
00390 public $m_Username;
00391 public $m_Password;
00392
00398 public function __construct ($xmlArr)
00399 {
00400 $this->m_Name = $xmlArr["ATTRIBUTES"]["NAME"];
00401 $this->m_Host = $xmlArr["ATTRIBUTES"]["HOST"];
00402 $this->m_Port = $xmlArr["ATTRIBUTES"]["PORT"];
00403 $this->m_SSL = $xmlArr["ATTRIBUTES"]["SSL"];
00404 $this->m_FromName = $xmlArr["ATTRIBUTES"]["FROMNAME"];
00405 $this->m_FromEmail = $xmlArr["ATTRIBUTES"]["FROMEMAIL"];
00406 $this->m_IsSMTP = $xmlArr["ATTRIBUTES"]["ISSMTP"];
00407 $this->m_SMTPAuth = $xmlArr["ATTRIBUTES"]["SMTPAUTH"];
00408 $this->m_Username = $xmlArr["ATTRIBUTES"]["USERNAME"];
00409 $this->m_Password = $xmlArr["ATTRIBUTES"]["PASSWORD"];
00410 }
00411 }