* @license PHP License * @package Wombat * @subpackage base */ WBClass::load('WBEvent_Handler'); /** * Event_Handler_Mail * * Send an e-mail... * * @version 0.4.0 * @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' => '', 'attachment' => '', 'attachmentindex' => '', ); /** * 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_'); // Mime_Mail required? if ($this->mailer->hasHtmlBody() || !empty($this->config['attachment']) || !empty($this->config['attachmentindex'])) { $this->prepareMailerMime($e); } else { $this->prepareMailerPlain($e); } // 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); } } } /** * Prepare Mailer 4 Mime Mail * * Create featured mime mail with HTML body and attachments * @param WBEvent $e */ private function prepareMailerMime($e) { /** @var WBMail_Mime */ $mm = WBClass::create('WBMail_Mime'); $mm->setSubject($this->mailer->getSubject()); $rcpt = $this->config2EMailAdresse('rcpt', $e); if (!empty($rcpt)) { $this->mailer->addRcpt($rcpt); /** @var WBMail_Mime_Part_Address */ $addrRcpt = WBClass::create('WBMail_Mime_Part_Address'); $addrRcpt->setEmailAddress($rcpt); $mm->setRcpt($addrRcpt); } $replyTo = $this->config2EMailAdresse('replyto', $e); if (!empty($replyTo)) { /** @var WBMail_Mime_Part_Address */ $addrReplyTo = WBClass::create('WBMail_Mime_Part_Address'); $addrReplyTo->setEmailAddress($replyTo); $mm->setReplyTo($addrReplyTo); } $from = $this->config2EMailAdresse('from', $e); if (!empty($from)) { $addrFrom = WBClass::create('WBMail_Mime_Part_Address'); $addrFrom->setEmailAddress($from); $mm->setFrom($addrFrom); } $mm->setPlainBody($this->mailer->getPlainBody()); $mm->setHtmlBody($this->mailer->getHtmlBody()); $this->mailer->setMimeMail($mm); // attachment if (!empty($this->config['attachment'])) { $att = $this->config['attachment']; } else if (!empty($this->config['attachmentindex'])) { $att = $e->get($this->config['attachmentindex']); } if (!is_array($att)) { $att = array(trim($att)); } if (empty($att)) { return; } foreach ($att as $a) { if (!is_array($a)) { $a = array( 'file' => trim($a), 'name' => '' ); } /** @var WBFile */ $file = WBClass::create('WBFile'); // strip basedir $base = $file->getBase(); if (0 == strncmp($base, $a['file'], strlen($base))) { $a['file'] = substr($a['file'], strlen($base)); } if (!$file->exists($a['file'])) { continue; } if (empty($a['name'])) { $a['name'] = $file->basename(); } /** @var WBMail_Mime_Part_Attachment */ $part = WBClass::create('WBMail_Mime_Part_Attachment'); $part->setMime(implode('/', $file->getMimeType())); $part->setData(file_get_contents($file->realpath())); $part->setName($a['name']); $mm->addAttachment($part); } } /** * Prepare Mailer 4 Plain Text Mail * * Create simple text e-mail * @param WBEvent $e */ private function prepareMailerPlain($e) { $rcpt = $this->config2EMailAdresse('rcpt', $e); if (!empty($rcpt)) { $this->mailer->addRcpt($rcpt); } // 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); } } /** * Actually Send E-Mail */ private function sendMail() { $this->mailer->send(); } /** * Config Value 2 E-Mail-Address * * Load E-Mail address from config and event data. * Resolve from dictionary if it is an numeric id * * @param string * @param WBEvent * @return string */ 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(); } }