* @license PHP License * @package Wombat * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * Event_Handler_Mail * * Send an e-mail... * * @version 0.3.1 * @package Wombat * @subpackage base */ class WBEvent_Handler_Mail extends WBEvent_Handler { /** * Handler config * * Parameters * - "from" e-mail address in from header * - "fromindex" load e-mail address for from header from index * - "replyto" e-mail address in reply-to header * - "replytoindex" load e-mail address for from header reply-toindex * - "rcpt" e-mail address of recipient * - "rcptindex" e-mail address of recipient loaded from index * - "addrcpt" list of additional rcpts * - "bcc" list of rcpt in blind copy * - "lang" locale settings * - "langindex" index for locale settings * - "tmpl" template for mail body * - "x-sender": optional sender address * * @var array */ protected $config = array( 'from' => '', 'fromindex' => '', 'replyto' => '', 'replytoindex' => '', 'rcpt' => '', 'rcptindex' => 'email', 'addrcpt' => '', 'addbcc' => '', 'lang' => '', 'langindex' => '', 'tmpl' => 'email', 'x-sender' => '' ); /** * Actual Mailer * @var WBMailer */ private $mailer; /** * Current Locale Settings * @var string */ private $langCurrent = ''; /** * Dictiobary * @var WBDictionary_URL */ private $dictUrl; /** * send an email * * Use Mailer to prepare and send e-mail. * * @param WBEvent $e * @return bool true to continue, false to stop processing * @throws Exception in case of error */ public function process(WBEvent $e) { $this->setLocale($e); // now as locale settings are settled, start mailer $this->prepareMailer($e); // actually send E-Mail $this->sendMail(); $this->resetLocale(); return true; } /** * Set Locale * * @param WBEvent $e */ private function setLocale(WBEvent $e) { // switch to recipient's language first WBClass::load('patI18n'); $this->langCurrent = patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE); if (!empty($this->config['lang'])) { patI18n::setLocale($this->config['lang']); } else if (!empty($this->config['langindex'])) { patI18n::setLocale($e->get($this->config['langindex'], $this->langCurrent)); } } private function resetLocale() { // undo changes in locale settings patI18n::setLocale($this->langCurrent); } /** * Prepare Mailer * * Create Mailer object, load template and set global data * @param WBEvent $e */ private function prepareMailer($e) { $this->mailer = WBClass::create('WBMailer'); if (!empty($this->config['x-sender'])) { $this->mailer->setHeader('X-Sender', $this->config['X-Sender']); } $this->mailer->setTemplate($this->config['tmpl']); $this->mailer->setData($e->get()); $event = array( 'name' => $e->getName(), 'timestamp' => $e->getTimestamp(), 'msg' => $e->getMessage(), 'lang' => patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT) ); $this->mailer->addData($event, 'event_'); $rcpt = $this->config2EMailAdresse('rcpt', $e); if (!empty($rcpt)) { $this->mailer->addRcpt($rcpt); } // additional RCPT if (!empty($this->config['addrcpt'])) { $this->mailer->addRcpt($this->config['addrcpt']); } // additional Bcc if (!empty($this->config['addbcc'])) { if (!is_array($this->config['addbcc'])) { $this->config['addbcc'] = array($this->config['addbcc']); } foreach ($this->config['addbcc'] as $bcc) { $this->mailer->addBcc($bcc); } } // replay-to $replyTo = $this->config2EMailAdresse('replyto', $e); if (!empty($replyTo)) { $this->mailer->addReplyTo($replyTo); } $from = $this->config2EMailAdresse('from', $e); if (!empty($from)) { $this->mailer->setFrom($from); } } private function sendMail() { $this->mailer->send(); } private function config2EMailAdresse($name, WBEvent $e) { $adr = $this->config[$name]; if (empty($adr) && !empty($this->config[$name . 'index'])) { $adr = $e->get($this->config[$name . 'index']); } // no address found if (empty($adr)) { return ''; } // not numeric? It must be OK then. if (!preg_match('/^\d+$/', $adr)) { return $adr; } // numeric addresses must be dictionary records if (empty($this->dictUrl)) { $this->dictUrl = WBClass::create('WBDictionary_URL'); } $this->dictUrl->load($adr); return $this->dictUrl->getWord(); } }