diff options
-rw-r--r-- | app/frontend/mail/MailModule.php | 38 | ||||
-rw-r--r-- | app/frontend/mail/MailTemplate.php | 10 | ||||
-rw-r--r-- | app/frontend/mail/Mailer.php | 19 |
3 files changed, 61 insertions, 6 deletions
diff --git a/app/frontend/mail/MailModule.php b/app/frontend/mail/MailModule.php index 464f1d0..4d2faeb 100644 --- a/app/frontend/mail/MailModule.php +++ b/app/frontend/mail/MailModule.php @@ -4,17 +4,29 @@ Prado::using('System.I18N.Globalization'); Prado::using('Application.mail.Mailer'); Prado::using('Application.mail.MailTemplate'); Prado::using('Application.mail.MailTemplateTranslator'); +Prado::using('Application.model.MailQueue'); class MailModule extends TModule { private $_configPath; private $_config; + private $_queuedTemplates = []; + private $_templatePath; private static $_templateExtension = 'html'; private $_templateCacheSubpath = 'mail'; private static $_templateTranslationCatalogue = 'messages'; + public function init($xml) { + parent::init($xml); + if ($queueTemplates = $xml->getElementByTagName('mail-queue')) { + foreach ($queueTemplates->getElementsByTagName('template') as $template) { + $this->_queuedTemplates[] = $template->Attributes['id']; + } + } + } + public function setConfigPath(string $configPath) { $this->_configPath = TPropertyValue::ensureString($configPath); $this->_config = json_decode( @@ -24,6 +36,10 @@ class MailModule extends TModule { ); } + public function getConfig() { + return $this->_config; + } + public function setTemplatePath(string $templatePath) { $this->_templatePath = Prado::getPathOfNamespace( TPropertyValue::ensureString($templatePath) @@ -92,14 +108,19 @@ class MailModule extends TModule { return self::$_translator; } - public function getTemplate(string $template, string $language='') { - $template = new MailTemplate($template . '.' . $this->TemplateExtension); + public function getTemplate(string $templateID, string $language='') { + $template = new MailTemplate($templateID . '.' . $this->TemplateExtension); + $template->setOutputMode(PHPTAL::HTML5); $template->setTemplateRepository($this->_templatePath); $template->setPhpCodeDestination($this->CachePath); + + $template->toggleImmediate(!in_array($templateID, $this->_queuedTemplates)); + $translator = self::_getTranslator(); $translator->setLanguage($language); $template->setTranslator($translator); + return $template; } @@ -112,10 +133,21 @@ class MailModule extends TModule { ); } $mailer = new Mailer(); - $mailer->configure($this->_config); + $mailer->configure($this); return $mailer; } + public function queueMail(string $subject, string $body, string $rcptMail, string $rcptName) { + $queue = new MailQueue(); + $queue->Subject = $subject; + $queue->HtmlBody = $body; + $queue->TextBody = strip_tags($body); + $queue->RecipientName = $rcptName; + $queue->RecipientMail = $rcptMail; + $queue->CreateTime = date('Y-m-d H:i:s'); + return $queue->save(); + } + } ?> diff --git a/app/frontend/mail/MailTemplate.php b/app/frontend/mail/MailTemplate.php index ab5c9a5..c31f38b 100644 --- a/app/frontend/mail/MailTemplate.php +++ b/app/frontend/mail/MailTemplate.php @@ -4,6 +4,16 @@ Prado::using('Lib.phptal.PHPTAL'); class MailTemplate extends PHPTAL { + private $_immediate = TRUE; + + public function isImmediate() { + return $this->_immediate; + } + + public function toggleImmediate($value) { + $this->_immediate = TPropertyValue::ensureBoolean($value); + } + } ?> diff --git a/app/frontend/mail/Mailer.php b/app/frontend/mail/Mailer.php index 3b95568..cd08757 100644 --- a/app/frontend/mail/Mailer.php +++ b/app/frontend/mail/Mailer.php @@ -1,16 +1,21 @@ <?php Prado::using('Lib.phpmailer.PHPMailerAutoload'); +Prado::using('Application.mail.MailModule'); Prado::using('Application.mail.MailTemplate'); class Mailer extends PHPMailer { + private $_module; + public function __construct() { $this->isSMTP(); $this->SMTPAuth = TRUE; } - public function configure($config) { + public function configure(MailModule $module) { + $this->_module = $module; + $config = $module->getConfig(); $this->Host = $config->smtp->host; $this->Port = $config->smtp->port; $this->Username = $config->smtp->user; @@ -24,11 +29,10 @@ class Mailer extends PHPMailer { } } - public function sendTemplate(MailTemplate $template, string $subject, string $to, string $name) { + protected function _sendImmediate(string $subject, string $html, string $to, string $name) { $this->addAddress($to, $name); $this->isHTML(TRUE); $this->Subject = $subject; - $html = $template->execute(); $this->Body = $html; $this->AltBody = strip_tags($html); $result = $this->send(); @@ -36,6 +40,15 @@ class Mailer extends PHPMailer { return $result; } + public function sendTemplate(MailTemplate $template, string $subject, string $to, string $name) { + $html = $template->execute(); + if ($template->isImmediate()) { + return $this->_sendImmediate($subject, $html, $to, $name); + } else { + return $this->_module->queueMail($subject, $html, $to, $name); + } + } + } ?> |