From 524d4db56ec00d3e6e107ecf6d4dd51a6b523a51 Mon Sep 17 00:00:00 2001 From: formsdev <136701234+formsdev@users.noreply.github.com> Date: Wed, 26 Jul 2023 13:20:27 +0530 Subject: [PATCH] Reply to for email notifications (#152) * Reply to for email notifications * Polish Code --- .../Forms/FormSubmissionNotification.php | 34 ++++++++++++++++++- .../components/FormNotificationsOption.vue | 14 ++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/app/Notifications/Forms/FormSubmissionNotification.php b/app/Notifications/Forms/FormSubmissionNotification.php index dc57ddb..d5dabf4 100644 --- a/app/Notifications/Forms/FormSubmissionNotification.php +++ b/app/Notifications/Forms/FormSubmissionNotification.php @@ -51,7 +51,7 @@ class FormSubmissionNotification extends Notification implements ShouldQueue ->outputStringsOnly(); return (new MailMessage) - ->replyTo($notifiable->routes['mail']) + ->replyTo($this->getReplyToEmail($notifiable->routes['mail'])) ->from($this->getFromEmail(), config('app.name')) ->subject('New form submission for "'.$this->event->form->title.'"') ->markdown('mail.form.submission-notification', [ @@ -66,4 +66,36 @@ class FormSubmissionNotification extends Notification implements ShouldQueue return $originalFromAddress->first(). '+' . time() . '@' . $originalFromAddress->last(); } + private function getReplyToEmail($default) + { + return $this->getRespondentEmail() ?? $default; + } + + private function getRespondentEmail() + { + // Make sure we only have one email field in the form + $emailFields = collect($this->event->form->properties)->filter(function ($field) { + $hidden = $field['hidden'] ?? false; + + return !$hidden && $field['type'] == 'email'; + }); + if ($emailFields->count() != 1) { + return null; + } + + if (isset($this->event->data[$emailFields->first()['id']])) { + $email = $this->event->data[$emailFields->first()['id']]; + if ($this->validateEmail($email)) { + return $email; + } + } + + return null; + } + + public static function validateEmail($email): bool + { + return (bool)filter_var($email, FILTER_VALIDATE_EMAIL); + } + } diff --git a/resources/js/components/open/forms/components/form-components/components/FormNotificationsOption.vue b/resources/js/components/open/forms/components/form-components/components/FormNotificationsOption.vue index ed05a92..d5ee531 100644 --- a/resources/js/components/open/forms/components/form-components/components/FormNotificationsOption.vue +++ b/resources/js/components/open/forms/components/form-components/components/FormNotificationsOption.vue @@ -33,6 +33,7 @@ { + return field.type === 'email' && !field.hidden + }) + if (emailFields.length === 1) return emailFields[0] + return null + }, + notifiesHelp () { + if (this.replayToEmailField) { + return 'Reply-to for this notification will be the email filled in the field "' + this.replayToEmailField.name + '".' + } + return 'Reply-to for this notification will be your own email. Add a single email field to your form, and it will automatically become the reply to value.' } },