diff --git a/README.md b/README.md index c091bb5..4b9922c 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,34 @@ If you want to add new group, add an extension to Emailing class and implement t { } +Merge vars: +This module supports merge vars using *|MERGETAG|* notation in Emailings +It expects your email gateway to support the X-MC-MergeVars convention. +You can change the email header being used. + + LeKoala\EmailTemplates\Models\Emailing: + mail_merge_header: 'X-Mail-Header-Here' + +By default, we use the mandrill template syntax replacement. If you use other +gateways you may need to replace them. For example, mailgun would be this: + + LeKoala\EmailTemplates\Models\Emailing: + mail_merge_syntax: '%recipient.MERGETAG%' + +Batch sending: +By default, this module will send emails in batch of 1000. You can change this with + + LeKoala\EmailTemplates\Models\Emailing: + batch_count: 1000 + +Sending as bcc: +By default, this module send email as bcc in order to avoid displaying +recipients. If your email gateway (like, mailgun) supports hiding recipients, you can +use a real recipient with the following yml config + + LeKoala\EmailTemplates\Models\Emailing: + send_bcc: false + Finding a good template ================== diff --git a/_config/email-templates.yml b/_config/email-templates.yml index a138503..3e05f0d 100644 --- a/_config/email-templates.yml +++ b/_config/email-templates.yml @@ -29,6 +29,11 @@ LeKoala\EmailTemplates\Models\SentEmail: # possible values : 'time' or 'max' cleanup_method: 'max' cleanup_time: '-7 days' +LeKoala\EmailTemplates\Models\Emailing: + batch_count: 1000 + mail_merge_header: 'X-MC-MergeVars' + mail_merge_syntax: '*|MERGETAG|*' + send_bcc: true SilverStripe\Core\Injector\Injector: SilverStripe\Control\Email\Email: class: LeKoala\EmailTemplates\Email\BetterEmail diff --git a/src/Admin/EmailTemplatesAdmin.php b/src/Admin/EmailTemplatesAdmin.php index 02a9671..566ef2f 100644 --- a/src/Admin/EmailTemplatesAdmin.php +++ b/src/Admin/EmailTemplatesAdmin.php @@ -3,13 +3,14 @@ namespace LeKoala\EmailTemplates\Admin; use Exception; -use LeKoala\EmailTemplates\Helpers\FluentHelper; use SilverStripe\Admin\ModelAdmin; +use SilverStripe\Control\Director; use SilverStripe\View\Requirements; +use SilverStripe\Control\HTTPResponse; use LeKoala\EmailTemplates\Models\Emailing; use LeKoala\EmailTemplates\Models\SentEmail; +use LeKoala\EmailTemplates\Helpers\FluentHelper; use LeKoala\EmailTemplates\Models\EmailTemplate; -use SilverStripe\Control\Director; /** * Manage your email templates @@ -59,39 +60,48 @@ public function SendEmailing() /* @var $Emailing Emailing */ $Emailing = Emailing::get()->byID($id); - $emails = $Emailing->getEmailByLocales(); + $emails = $Emailing->getEmailsByLocales(); $errors = 0; - foreach ($emails as $locale => $email) { - // Wrap with withLocale to make sure any environment variable (urls, etc) are properly set when sending - $res = null; - FluentHelper::withLocale($locale, function () use ($email, &$res) { - try { - $res = $email->send(); - } catch (Exception $ex) { - return $ex->getMessage(); + $messages = []; + foreach ($emails as $locale => $emails) { + foreach ($emails as $email) { + $res = null; + $msg = null; + // Wrap with withLocale to make sure any environment variable (urls, etc) are properly set when sending + FluentHelper::withLocale($locale, function () use ($email, &$res, &$msg) { + try { + $res = $email->send(); + } catch (Exception $ex) { + $res = false; + $msg = $ex->getMessage(); + } + }); + if (!$res) { + $errors++; + $messages[] = $msg; } - return $res; - }); - if (!$res) { - $errors++; } } - $message = _t('EmailTemplatesAdmin.EMAILING_ERROR', 'There was an error sending email'); - if ($errors == 0) { $Emailing->LastSent = date('Y-m-d H:i:s'); $Emailing->write(); - $message = _t('EmailTemplatesAdmin.EMAILING_SENT', 'Emailing sent'); + } else { + $message = _t('EmailTemplatesAdmin.EMAILING_ERROR', 'There was an error sending email'); + $message .= ": " . implode(", ", $messages); } if (Director::is_ajax()) { - return $message; + $this->getResponse()->addHeader('X-Status', rawurlencode($message)); + if ($errors > 0) { + // $this->getResponse()->setStatusCode(400); + } + return $this->getResponse(); } - return $this->redirectBack(); + return $message; } /** diff --git a/src/Models/Emailing.php b/src/Models/Emailing.php index a70eaed..ed0f3f8 100644 --- a/src/Models/Emailing.php +++ b/src/Models/Emailing.php @@ -154,6 +154,9 @@ public function getNormalizedRecipientsList() foreach ($perLine as $line) { $items = explode(',', $line); foreach ($items as $item) { + // Prevent whitespaces from messing up our queries + $item = trim($item); + if (!$item) { continue; } @@ -209,6 +212,17 @@ protected function previewTab() $iframe = new LiteralField('iframe', ''); $tab->push($iframe); + // Merge var helper + $vars = $this->collectMergeVars(); + $syntax = self::config()->mail_merge_syntax; + if (empty($vars)) { + $varsHelperContent = "You can use $syntax notation to use mail merge variable for the recipients"; + } else { + $varsHelperContent = "The following mail merge variables are used : " . implode(", ", $vars); + } + $varsHelper = new LiteralField("varsHelpers", '