Skip to content

[5.x] Allow conditional sending of form submission emails #11779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed

Conversation

stephensamra
Copy link
Contributor

Overview
Introduces a new FormSendingEmails event that is dispatched prior to SendEmails (the job that sends emails when new form submissions are received).

If a listener of FormSendingEmails returns false, the SendEmails event will not be dispatched (similar to how FormSubmitted works).

This enables the conditional sending of form submission emails.

Motivation
I have a use case where I'd like to examine a form submission and only send the email(s) based on certain criteria.

I think a good example for this would be spam detection: if a submission is flagged as spam, perhaps we don't want to send an email about it. However, we still want the submission to be saved so that it can be manually reviewed at a later date.

Example listener implementation:

<?php

namespace App\Listeners;

use Statamic\Events\FormSendingEmails;

class HandleFormSendingEmails
{
    public function handle(FormSendingEmails $event)
    {
        if (SpamDetector::check($event->submission)) {
            return false; // No emails will be sent if we reach here.
        }
    }
}

Introduces a new `FormSendingEmails` event that is dispatched prior to
`SendEmails` (the job that sends emails when new form submission are
received). If a `FormSendingEmails` listener returns `false`, the
`SendEmails` event will not be dispatched. This enables the conditional
sending of form submission emails.
@edalzell
Copy link
Contributor

You can already set the job used to send emails in the config, can you not use that?

Also, the spam case is already handled
CleanShot 2025-05-12 at 09 42 08@2x

@stephensamra
Copy link
Contributor Author

Thanks @edalzell, I must have an old config file that doesn't have that option. I should be able to use that.

@stephensamra stephensamra deleted the form-sending-emails-event branch May 12, 2025 17:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants