Skip to content

Commit

Permalink
Merge pull request #281 from itk-dev/feature/779-force-only-aarhus-em…
Browse files Browse the repository at this point in the history
…ails

#779: Added pattern checks and error emails to email handler
  • Loading branch information
jekuaitk authored Feb 21, 2024
2 parents 0451092 + e4f5588 commit c62f36d
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
!/web/modules/custom/os2forms_user_menu
!/web/modules/custom/os2forms_fbs_handler
!/web/modules/custom/os2forms_permission_alterations
!/web/modules/custom/os2forms_email

# Ignore site specific modules.
/web/sites/*/modules
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Nedenfor ses dato for release og beskrivelse af opgaver som er implementeret.
## [Under udvikling]

* Tilføjede mulighed for csv eksport af alle formular konfigurationer.
* Tilføjede mulighed for ekstra tjek på email modtagere (@aarhus.dk).

## [2.7.7] 2024-02-15

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ module:
os2forms_attachment: 0
os2forms_dawa: 0
os2forms_digital_post: 0
os2forms_email: 0
os2forms_failed_jobs: 0
os2forms_fbs_handler: 0
os2forms_forloeb: 0
Expand Down
7 changes: 7 additions & 0 deletions web/modules/custom/os2forms_email/os2forms_email.info.yml
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'
17 changes: 17 additions & 0 deletions web/modules/custom/os2forms_email/os2forms_email.module
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;
}
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);
}

}

0 comments on commit c62f36d

Please sign in to comment.