Reply to for email notifications (#152)

* Reply to for email notifications

* Polish Code
This commit is contained in:
formsdev 2023-07-26 13:20:27 +05:30 committed by GitHub
parent e28d80e7da
commit 524d4db56e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 1 deletions

View File

@ -51,7 +51,7 @@ class FormSubmissionNotification extends Notification implements ShouldQueue
->outputStringsOnly(); ->outputStringsOnly();
return (new MailMessage) return (new MailMessage)
->replyTo($notifiable->routes['mail']) ->replyTo($this->getReplyToEmail($notifiable->routes['mail']))
->from($this->getFromEmail(), config('app.name')) ->from($this->getFromEmail(), config('app.name'))
->subject('New form submission for "'.$this->event->form->title.'"') ->subject('New form submission for "'.$this->event->form->title.'"')
->markdown('mail.form.submission-notification', [ ->markdown('mail.form.submission-notification', [
@ -66,4 +66,36 @@ class FormSubmissionNotification extends Notification implements ShouldQueue
return $originalFromAddress->first(). '+' . time() . '@' . $originalFromAddress->last(); 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);
}
} }

View File

@ -33,6 +33,7 @@
</h2> </h2>
<toggle-switch-input name="notifies" :form="form" class="mt-4" <toggle-switch-input name="notifies" :form="form" class="mt-4"
label="Receive email notifications on submission" label="Receive email notifications on submission"
:help="notifiesHelp"
/> />
<text-area-input v-if="form.notifies" name="notification_emails" :form="form" class="mt-4" <text-area-input v-if="form.notifies" name="notification_emails" :form="form" class="mt-4"
label="Notification Emails" help="Add one email per line" label="Notification Emails" help="Add one email per line"
@ -62,6 +63,19 @@ export default {
set (value) { set (value) {
this.$store.commit('open/working_form/set', value) this.$store.commit('open/working_form/set', value)
} }
},
replayToEmailField () {
const emailFields = this.form.properties.filter((field) => {
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.'
} }
}, },