Skip to content

Commit

Permalink
Merge pull request silverstripe#881 from creative-commoners/pulls/5.4…
Browse files Browse the repository at this point in the history
…/resilient-recipients

Add presence validation for EmailRecipient recipient, add error handling
  • Loading branch information
robbieaverill authored May 5, 2019
2 parents b3dc50d + 2f0aea8 commit 52c0074
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
30 changes: 24 additions & 6 deletions code/Control/UserDefinedFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace SilverStripe\UserForms\Control;

use Exception;
use PageController;
use Psr\Log\LoggerInterface;
use SilverStripe\Assets\File;
use SilverStripe\Assets\Upload;
use SilverStripe\Control\Controller;
use SilverStripe\Control\Email\Email;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleLoader;
use SilverStripe\Forms\Form;
use SilverStripe\i18n\i18n;
Expand All @@ -23,6 +26,7 @@
use SilverStripe\View\ArrayData;
use SilverStripe\View\Requirements;
use SilverStripe\View\SSViewer;
use Swift_RfcComplianceException;

/**
* Controller for the {@link UserDefinedForm} page type.
Expand Down Expand Up @@ -350,16 +354,30 @@ public function process($data, $form)

// check to see if they are a dynamic reciever eg based on a dropdown field a user selected
$emailTo = $recipient->SendEmailToField();
if ($emailTo && $emailTo->exists()) {
$submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name);

if ($submittedFormField && is_string($submittedFormField->Value)) {
$email->setTo(explode(',', $submittedFormField->Value));
try {
if ($emailTo && $emailTo->exists()) {
$submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name);

if ($submittedFormField && is_string($submittedFormField->Value)) {
$email->setTo(explode(',', $submittedFormField->Value));
} else {
$email->setTo(explode(',', $recipient->EmailAddress));
}
} else {
$email->setTo(explode(',', $recipient->EmailAddress));
}
} else {
$email->setTo(explode(',', $recipient->EmailAddress));
} catch (Swift_RfcComplianceException $e) {
// The sending address is empty and/or invalid. Log and skip sending.
$error = sprintf(
'Failed to set sender for userform submission %s: %s',
$submittedForm->ID,
$e->getMessage()
);

Injector::inst()->get(LoggerInterface::class)->notice($error);

continue;
}

// check to see if there is a dynamic subject
Expand Down
6 changes: 6 additions & 0 deletions code/Model/Recipient/EmailRecipient.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,12 @@ public function validate()
if (!$this->EmailFrom && empty(Email::getSendAllEmailsFrom()) && empty(Email::config()->get('admin_email'))) {
$result->addError(_t(__CLASS__.".EMAILFROMREQUIRED", '"Email From" address is required'));
}

// Sending will also fail if there's no recipient defined
if (!$this->EmailAddress && !$this->SendEmailToFieldID) {
$result->addError(_t(__CLASS__.".EMAILTOREQUIRED", '"Send email to" address or field is required'));
}

return $result;
}

Expand Down
11 changes: 11 additions & 0 deletions tests/Model/Recipient/EmailRecipientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,15 @@ public function testShortcodesAreRenderedInEmailPreviewContent()
$result = $recipient->getEmailBodyContent();
$this->assertContains('/about-us/', $result);
}

/**
* @expectedException SilverStripe\ORM\ValidationException
* @expectedExceptionMessage "Send email to" address or field is required
*/
public function testEmptyRecipientFailsValidation()
{
$recipient = new EmailRecipient();
$recipient->EmailFrom = '[email protected]';
$recipient->write();
}
}
4 changes: 3 additions & 1 deletion tests/Model/UserDefinedFormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ public function testGetCMSFieldsShowInSummary()
$this->assertNotContains('SummaryHide', array_keys($summaryFields), 'Summary field showing displayed field');
}


public function testEmailRecipientPopup()
{
$this->logInWithPermission('ADMIN');
Expand All @@ -114,6 +113,7 @@ public function testEmailRecipientPopup()
$popup = new EmailRecipient();
$popup->FormID = $form->ID;
$popup->FormClass = UserDefinedForm::class;
$popup->EmailAddress = '[email protected]';

$fields = $popup->getCMSFields();

Expand Down Expand Up @@ -146,6 +146,7 @@ public function testEmailRecipientPopup()
public function testGetEmailBodyContent()
{
$recipient = new EmailRecipient();
$recipient->EmailAddress = '[email protected]';

$emailBody = 'not html';
$emailBodyHtml = '<p>html</p>';
Expand Down Expand Up @@ -185,6 +186,7 @@ public function testEmailTemplateExists()
$recipient = new EmailRecipient();
$recipient->FormID = $page->ID;
$recipient->FormClass = UserDefinedForm::class;
$recipient->EmailAddress = '[email protected]';

// Set the default template
$recipient->EmailTemplate = current(array_keys($recipient->getEmailTemplateDropdownValues()));
Expand Down

0 comments on commit 52c0074

Please sign in to comment.