From d96f52cc358722498e948d4d7e25e27cd631ffe5 Mon Sep 17 00:00:00 2001 From: Robbie Averill Date: Fri, 29 Jun 2018 11:34:18 +1200 Subject: [PATCH] FIX Email notification workflow now ignores recipients with invalid email addresses --- src/Actions/NotifyUsersWorkflowAction.php | 39 +++++++++++++---------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Actions/NotifyUsersWorkflowAction.php b/src/Actions/NotifyUsersWorkflowAction.php index 3da32582..08f9f210 100644 --- a/src/Actions/NotifyUsersWorkflowAction.php +++ b/src/Actions/NotifyUsersWorkflowAction.php @@ -15,6 +15,7 @@ use SilverStripe\Security\Security; use SilverStripe\View\ArrayData; use SilverStripe\View\SSViewer; +use Swift_RfcComplianceException; use Symbiote\AdvancedWorkflow\DataObjects\WorkflowAction; use Symbiote\AdvancedWorkflow\DataObjects\WorkflowInstance; @@ -96,7 +97,7 @@ public function execute(WorkflowInstance $workflow) $memberFields = $this->getMemberFields($member); $initiatorFields = $this->getMemberFields($initiator); - $variables = array(); + $variables = []; foreach ($contextFields as $field => $val) { $variables["\$Context.$field"] = $val; @@ -109,28 +110,28 @@ public function execute(WorkflowInstance $workflow) } $pastActions = $workflow->Actions()->sort('Created DESC'); - $variables["\$CommentHistory"] = $this->customise(array( - 'PastActions'=>$pastActions, - 'Now'=>DBDatetime::now() - ))->renderWith('Includes/CommentHistory'); + $variables["\$CommentHistory"] = $this->customise([ + 'PastActions' => $pastActions, + 'Now' => DBDatetime::now() + ])->renderWith('Includes/CommentHistory'); $from = str_replace(array_keys($variables), array_values($variables), $this->EmailFrom); $subject = str_replace(array_keys($variables), array_values($variables), $this->EmailSubject); - if ($this->config()->whitelist_template_variables) { - $item = new ArrayData(array( - 'Initiator' => new ArrayData($initiatorFields), - 'Member' => new ArrayData($memberFields), - 'Context' => new ArrayData($contextFields), + if ($this->config()->get('whitelist_template_variables')) { + $item = ArrayData::create([ + 'Initiator' => ArrayData::create($initiatorFields), + 'Member' => ArrayData::create($memberFields), + 'Context' => ArrayData::create($contextFields), 'CommentHistory' => $variables["\$CommentHistory"] - )); + ]); } else { - $item = $workflow->customise(array( + $item = $workflow->customise([ 'Items' => $workflow->Actions(), 'Member' => $member, - 'Context' => new ArrayData($contextFields), + 'Context' => ArrayData::create($contextFields), 'CommentHistory' => $variables["\$CommentHistory"] - )); + ]); } @@ -141,8 +142,14 @@ public function execute(WorkflowInstance $workflow) foreach ($members as $member) { if ($member->Email) { - $email = new Email; - $email->setTo($member->Email); + $email = Email::create(); + try { + $email->setTo($member->Email); + } catch (Swift_RfcComplianceException $exception) { + // If the email address isn't valid we should skip it rather than break + // the rest of the processing + continue; + } $email->setSubject($subject); $email->setFrom($from); $email->setBody($body);