-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from itk-dev/feature/779-force-only-aarhus-em…
…ails #779: Added pattern checks and error emails to email handler
- Loading branch information
Showing
7 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,26 @@ Configure organisation API endpoint on `/admin/os2forms_organisation/settings` t | |
http://organisation_api:8080/api/v1/ | ||
``` | ||
|
||
### OS2Forms Email | ||
|
||
Overrides default webform email handler | ||
adding the ability to do an extra check on email recipient. | ||
Allows for sending emails to webform owner if recipient email is not valid. | ||
|
||
By default, none of these extra steps are done. | ||
Enable and configure them by setting the following in `settings.local.php`: | ||
|
||
```php | ||
// OS2Forms email | ||
$config['os2forms_email']['pattern_enable'] = TRUE; | ||
$config['os2forms_email']['pattern'] = '/.*@aarhus\.dk$/'; | ||
$config['os2forms_email']['error_message_enable'] = TRUE; | ||
$config['os2forms_email']['error_message_from_email'] = '[email protected]'; | ||
$config['os2forms_email']['error_message_from_name'] = 'Selvbetjening'; | ||
``` | ||
|
||
Remember to translate the error message email subject and body. | ||
|
||
## Production | ||
|
||
```sh | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name: 'OS2Forms email' | ||
type: module | ||
description: 'Overrides email handler adding extra checks and functionality' | ||
package: 'OS2Forms' | ||
core_version_requirement: ^9 | ||
dependencies: | ||
- 'webform:webform' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Module file for the os2forms_email module. | ||
*/ | ||
|
||
use Drupal\os2forms_email\Plugin\WebformHandler\OS2FormsEmailWebformHandler; | ||
|
||
/** | ||
* Implements hook_ELEMENT_info_alter(). | ||
* | ||
* Overrides default webform email handler with OS2FormsEmailWebformHandler. | ||
*/ | ||
function os2forms_email_webform_handler_info_alter(array &$handlers): void { | ||
$handlers['email']['class'] = OS2FormsEmailWebformHandler::class; | ||
} |
134 changes: 134 additions & 0 deletions
134
web/modules/custom/os2forms_email/src/Plugin/WebformHandler/OS2FormsEmailWebformHandler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
|
||
namespace Drupal\os2forms_email\Plugin\WebformHandler; | ||
|
||
use Drupal\webform\Plugin\WebformHandler\EmailWebformHandler; | ||
use Drupal\webform\WebformSubmissionInterface; | ||
|
||
/** | ||
* Emails a webform submission. | ||
* | ||
* @WebformHandler( | ||
* id = "email", | ||
* label = @Translation("OS2Forms email"), | ||
* category = @Translation("Notification"), | ||
* description = @Translation("Sends a webform submission via an email."), | ||
* cardinality = \Drupal\webform\Plugin\WebformHandlerInterface::CARDINALITY_UNLIMITED, | ||
* results = \Drupal\webform\Plugin\WebformHandlerInterface::RESULTS_PROCESSED, | ||
* tokens = TRUE, | ||
* ) | ||
*/ | ||
class OS2FormsEmailWebformHandler extends EmailWebformHandler { | ||
|
||
/** | ||
* Adds extra check to to_mail before sending message. | ||
* | ||
* @param \Drupal\webform\WebformSubmissionInterface $webform_submission | ||
* A webform submission. | ||
* @param array $message | ||
* An array of message parameters. | ||
* | ||
* @throws \Drupal\Core\Entity\EntityMalformedException | ||
*/ | ||
public function sendMessage(WebformSubmissionInterface $webform_submission, array $message) { | ||
|
||
if (!$this->configFactory->get('os2forms_email')->get('pattern_enable')) { | ||
return parent::sendMessage($webform_submission, $message); | ||
} | ||
|
||
$pattern = $this->configFactory->get('os2forms_email')->get('pattern'); | ||
$emailAddresses = explode(',', $message['to_mail']); | ||
|
||
$validEmails = []; | ||
|
||
foreach ($emailAddresses as $emailAddress) { | ||
if (!filter_var($emailAddress, FILTER_VALIDATE_EMAIL) || !preg_match($pattern, $emailAddress)) { | ||
|
||
$context = [ | ||
'@form' => $this->getWebform()->label(), | ||
'@handler' => $this->label(), | ||
'@email' => $emailAddress, | ||
'link' => ($webform_submission->id()) ? $webform_submission->toLink($this->t('View'))->toString() : NULL, | ||
'webform_submission' => $webform_submission, | ||
'handler_id' => $this->getHandlerId(), | ||
'operation' => 'failed sending email', | ||
]; | ||
|
||
if ($webform_submission->getWebform()->hasSubmissionLog()) { | ||
// Log detailed message to the 'webform_submission' log. | ||
$this->getLogger('webform_submission')->notice("Email not sent for '@handler' handler because the email (@email) is not valid.", $context); | ||
} | ||
|
||
$this->sendMessageToWebformAuthor($webform_submission, $message, $context); | ||
|
||
} | ||
else { | ||
$validEmails[] = $emailAddress; | ||
} | ||
|
||
} | ||
|
||
if (!empty($validEmails)) { | ||
$message['to_mail'] = implode(',', $validEmails); | ||
|
||
return parent::sendMessage($webform_submission, $message); | ||
} | ||
|
||
return FALSE; | ||
|
||
} | ||
|
||
/** | ||
* Sends message to webform author. | ||
* | ||
* @param \Drupal\webform\WebformSubmissionInterface $webform_submission | ||
* A webform submission. | ||
* @param array $message | ||
* An array of message parameters. | ||
* @param array $context | ||
* An array with context. | ||
*/ | ||
private function sendMessageToWebformAuthor(WebformSubmissionInterface $webform_submission, array $message, array $context): void { | ||
|
||
if (!$this->configFactory->get('os2forms_email')->get('error_message_enable')) { | ||
return; | ||
} | ||
|
||
$pattern = $this->configFactory->get('os2forms_email')->get('pattern'); | ||
$authorEmail = $webform_submission->getWebform()->getOwner()->getEmail(); | ||
|
||
if (!filter_var($authorEmail, FILTER_VALIDATE_EMAIL) || !preg_match($pattern, $authorEmail)) { | ||
// Cannot send email to author email. Log it and give up. | ||
if ($webform_submission->getWebform()->hasSubmissionLog()) { | ||
|
||
$authorMessageContext = $context; | ||
$authorMessageContext['@email'] = $authorEmail; | ||
|
||
$this->getLogger('webform_submission')->notice("Email not sent for '@handler' handler because the email (@email) is not valid.", $authorMessageContext); | ||
} | ||
|
||
return; | ||
} | ||
|
||
$errorMessage = $this->defaultConfiguration(); | ||
|
||
$errorMessage['to_mail'] = $authorEmail; | ||
$errorMessage['subject'] = $this->t('Sending email failed'); | ||
|
||
$errorMessage['body'] = $this->t( | ||
"<p>Dear @name</p><p>Email not sent for handler (@handler) on form (@form) because the email (@email) is not valid.</p>", [ | ||
'@name' => $webform_submission->getWebform()->getOwner()->getDisplayName(), | ||
'@handler' => $context['@handler'] ?? '', | ||
'@form' => $context['@form'] ?? '', | ||
'@email' => $context['@email'] ?? '', | ||
]); | ||
|
||
$errorMessage['from_mail'] = $this->configFactory->get('os2forms_email')->get('error_message_from_email'); | ||
$errorMessage['from_name'] = $this->configFactory->get('os2forms_email')->get('error_message_from_name'); | ||
$errorMessage['webform_submission'] = $message['webform_submission']; | ||
$errorMessage['handler'] = $message['handler']; | ||
|
||
parent::sendMessage($webform_submission, $errorMessage); | ||
} | ||
|
||
} |