diff --git a/.gitignore b/.gitignore index d8d76fc6..d255c37f 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c916a99..be8acd88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index 832bbeec..9f197fd6 100644 --- a/README.md +++ b/README.md @@ -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'] = 'noreply@aarhus.dk'; +$config['os2forms_email']['error_message_from_name'] = 'Selvbetjening'; +``` + +Remember to translate the error message email subject and body. + ## Production ```sh diff --git a/config/sync/core.extension.yml b/config/sync/core.extension.yml index f1e97080..edb04b45 100644 --- a/config/sync/core.extension.yml +++ b/config/sync/core.extension.yml @@ -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 diff --git a/web/modules/custom/os2forms_email/os2forms_email.info.yml b/web/modules/custom/os2forms_email/os2forms_email.info.yml new file mode 100644 index 00000000..176d4d4b --- /dev/null +++ b/web/modules/custom/os2forms_email/os2forms_email.info.yml @@ -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' diff --git a/web/modules/custom/os2forms_email/os2forms_email.module b/web/modules/custom/os2forms_email/os2forms_email.module new file mode 100644 index 00000000..6be96654 --- /dev/null +++ b/web/modules/custom/os2forms_email/os2forms_email.module @@ -0,0 +1,17 @@ +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( + "
Dear @name
Email not sent for handler (@handler) on form (@form) because the email (@email) is not valid.
", [ + '@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); + } + +}