* @license PHP License * @package WB * @subpackage MailMime */ WBClass::load('WBDatasource_Newsletter' , 'WBString' , 'patI18n'); /** * Wombat Mail Newsletter * * @version 0.2.1 * @package WB * @subpackage MailMime */ class WBMail_Newsletter extends WBStdClass { /** * Mime Mailer * @var WBMail_Mime */ private $mailMime; /** * Template Engine * @var patTemplate */ private $tmpl; /** * Newsletter Issue * @var WBDatasource_Newsletter_Issue */ private $issue; /** * Subscriber Data * @var array */ private $sub; /** * Current Locale Settings * @var string */ private $langCurrent; /** * Standard Constructor * * @param array */ public function __construct($parameter = array()) { $this->langCurrent = patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE); $this->tmpl = WBClass::create('patTemplate'); $this->mailMime = WBClass::create('WBMail_Mime'); } /** * Tell Which Newsletter Issue 2 Mail * * Set issue object * @param WBDatasource_Newsletter_Issue */ public function setIssue(WBDatasource_Newsletter_Issue $issue) { $this->issue = $issue; } /** * Set Subscriber Data As Recipient * * Data must contain: * - email * - forename (may be empty) * - surname (may be empty) * @param array */ public function setSubscriberData($data) { $this->sub = $data; } /** * Prepare Content and Header * * Setup e-mail recipient and from address. Also load e-mail templates and * fill placeholders with content from issue and articles */ public function prepare() { $this->setLocale(); $issue = $this->issue->get(); // $this->mailMime->setDate(strtotime($issue['created'])); $man = $this->issue->getMandatorData(); $from = $this->issue->getConfig('from'); $replyTo = $this->issue->getConfig('replyto'); $tmp = array('issue_id' => $issue['id']); $replyTo['email'] = WBString::populate($replyTo['email'], $tmp); if (!empty($man['shortname'])) { $from['forename'] = $man['shortname']; $replyTo['forename'] = $man['shortname']; } /** @var WBMail_Mime_Part_Address */ $addrReplyTo = WBClass::create('WBMail_Mime_Part_Address'); $addrReplyTo->setEmailAddress($replyTo['email']); $addrReplyTo->setForename($replyTo['forename']); $addrReplyTo->setSurname($replyTo['surname']); $this->mailMime->setReplyTo($addrReplyTo); /** @var WBMail_Mime_Part_Address */ $addrFrom = WBClass::create('WBMail_Mime_Part_Address'); $addrFrom->setEmailAddress($from['email']); $addrFrom->setForename($from['forename']); $addrFrom->setSurname($from['surname']); $this->mailMime->setFrom($addrFrom); /** @var WBMail_Mime_Part_Address */ $addrRcpt = WBClass::create('WBMail_Mime_Part_Address'); $addrRcpt->setEmailAddress($this->sub['email']); $addrRcpt->setForename($this->sub['forename']); $addrRcpt->setSurname($this->sub['surname']); $this->mailMime->setRcpt($addrRcpt); // Return-Path $returnPath = $this->issue->getConfig('returnpath'); if (empty($returnPath)) { $returnPath = $from; } if (!empty($returnPath['email'])) { /** @var WBMail_Mime_Part_Address */ $addrReturnPath = WBClass::create('WBMail_Mime_Part_Address'); $addrReturnPath->setEmailAddress($returnPath['email']); if (!empty($man['forename'])) { $addrReturnPath->setForename($returnPath['forename']); } if (!empty($man['surname'])) { $addrReturnPath->setSurname($returnPath['surname']); } $this->mailMime->setReturnPath($addrReturnPath); } // X-Sender if (!empty($man['email'])) { /** @var WBMail_Mime_Part_Address */ $addrXSender = WBClass::create('WBMail_Mime_Part_Address'); $addrXSender->setEmailAddress($man['email']); if (!empty($man['forename'])) { $addrXSender->setForename($man['forename']); } if (!empty($man['surname'])) { $addrXSender->setSurname($man['surname']); } $this->mailMime->setXSender($addrXSender); } $this->tmpl->clearAllTemplates(); // add locale settings $lang = array( 'short' => patI18n::getLocale(patI18n::LOCALE_TYPE_SHORT), 'lang' => patI18n::getLocale(patI18n::LOCALE_TYPE_LANG), 'complete' => patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE) ); $this->tmpl->addGlobalVars($lang, 'LOCALE_LANG_'); $this->tmpl->addGlobalVars($man, 'MANDATOR_'); $this->tmpl->readTemplatesFromInput('Newsletter/Issue/' . $issue['template'] . '.tmpl'); // add CSS-Template $css = array(); $list = $this->tmpl->getList(); foreach ($list as $name) { if (0 != strncmp('css_', $name, 4)) { continue; } $name = substr($name, 4); $css[$name] = trim($this->tmpl->getParsedTemplate('css_' . $name)); } $this->tmpl->addGlobalVars($css, 'css_'); $this->tmpl->addGlobalVars($issue, 'issue_'); $this->tmpl->addGlobalVars($this->sub, 'sub_'); $art = $this->issue->getArticle(); $this->tmpl->addGlobalVar('article_count', count($art)); if ($this->tmpl->exists('article_list_entry_html')) { $this->tmpl->addRows('article_list_entry_html', $art); } if ($this->tmpl->exists('article_list_entry_plain')) { $this->tmpl->addRows('article_list_entry_plain', $art); } $this->resetLocale(); } /** * Get E-Mail Subject * * @return string */ public function getSubject() { $this->setLocale(); $subject = trim($this->tmpl->getParsedTemplate('email_subject')); $subject = $this->replaceSubscriberInfo($subject); $subject = str_replace("\n", ' ', $subject); $subject = WBString::replaceSuperPlaceholders($subject); $this->resetLocale(); return $subject; } /** * Get E-Mail Plain Body * * @return string */ public function getPlainBody() { $this->setLocale(); $txt = $this->tmpl->getParsedTemplate('email_plain'); $txt = $this->replaceSubscriberInfo($txt); $txt = WBString::replaceSuperPlaceholders($txt); $this->resetLocale(); return $txt; } /** * Get E-Mail HTML Body * * @return string */ public function getHtmlBody($doc = false) { $this->setLocale(); if ($doc) { $html = $this->tmpl->getParsedTemplate('email_html_doc'); } else { $html = $this->tmpl->getParsedTemplate('email_html'); } $html = $this->replaceSubscriberInfo($html); $html = WBString::replaceSuperPlaceholders($html);; $this->resetLocale(); return $html; } /** * Replace String with Subscriber's Data * * @todo consider to replace more data * @param string * @return string */ private function replaceSubscriberInfo($txt) { $search = array(); $search[] = 'OBSCUREID'; $replace = array(); $replace[] = $this->sub['obscureid']; return str_replace($search, $replace, $txt); } /** * Get Complete E-Mail body * * Return Mime-Mail string ready to be send * Also pass e-mail headers using reference header parameter * * @param array $headers * @return string */ public function getMailBody(&$header = array()) { $mm = $this->getMimeMail(); /** @var WBMail_Mime_Storage_String */ $ms = WBClass::create('WBMail_Mime_Storage_String'); $ms->setMimeMail($mm); $header = array(); return $ms->saveToString($header); } /** * Get Prepared Mime Mail * * @return WBMail_Mime */ public function getMimeMail() { // subject must be one line! $this->mailMime->setSubject($this->getSubject()); // body $this->mailMime->setPlainBody($this->getPlainBody()); $this->mailMime->setHtmlBody($this->getHtmlBody(true)); return $this->mailMime; } /** * Set Locale * * Set current locale config to newsletter issue's language setting */ private function setLocale() { // switch to recipient's language first $this->langCurrent = patI18n::getLocale(patI18n::LOCALE_TYPE_COMPLETE); $lang = $this->issue->getLang(); if (!empty($lang)) { patI18n::setLocale($lang); } } /** * Restore Locale Settings * * @see setLocale() */ private function resetLocale() { // undo changes in locale settings patI18n::setLocale($this->langCurrent); } }