1

I'm developing a site in Craft 3. I have added Freeform PRO to it. I am just wondering if this is possible:

I have created a form, and I want all the notifications of submissions sent to the admin (which is doing good), but I also want to send a notification to the user that is completing the form in the frontend based on a field's value.

Basically, I have added a checkbox labelled as "I want to receive a copy of this enquiry.", so if this is checked, then send an email to the person who is submitting the form.

I'm thinking of different options to do this, like listening to the submission event and check if the checkbox is ticked, but I am looking first at the docs of Freeform but I think that scenario is not supported (yet) ?

1

Okay I got a response from the Solspace team, mentioning that they dont have this feature.

I have made a workaround and build a module for Craft, listening to the "After a submission is submitted" event.

This is what I did:

 Event::on(
        SubmissionsService::class,
        SubmissionsService::EVENT_AFTER_SUBMIT,
        function (SubmitEvent $event) {
            $form = $event->getForm();

            if ($form->getLayout()->getFieldByHandle('00N6F00000EZWfQ')->getValue()) { // check if checkbox is checked
                $email = $form->getLayout()->getFieldByHandle('Email')->getValueAsString();

                $html = '<h2>We have received your email</h2>';
                $html .= "<p>We would like to acknowledge that we have received your enquiry. A member of our staff will be reviewing your enquiry and will send you a personal response.</p>";
                $html .= "<p>Thank you for your patience.</p>";
                $html .= "<p>Sincerely,</p>";
                $html .= "<p>" . getenv('DEFAULT_SITE_NAME') . "</p>";
                $html .= "<hr>";

                $html .= "<p><i>Copy of your enquiry attached </i></p>";
                $html .= '<ul>';
                foreach ($form->getLayout()->getFields() as $field) {

                    if ($field->getType() == 'hidden' || $field->getType() == 'submit') {
                        // Dont include them
                    } else {
                        $html .= '<li><i>' . $field->getLabel() . ": " . $field->getValueAsString() . "</i></li>";
                    }

                }

                $html .= '</ul>';

                $subject = "Enquiry received - [" . $form->getName() . "]";

                $this->sendMail($html, $subject, $email);
            }

        }
    );

And my function to send emails:

 private function sendMail($html, $subject, $mail = null): bool
{
    $message = new Message();

    $message->setFrom(getenv('SYSTEM_EMAIL_ADDRESS'));
    $message->setTo($mail);
    $message->setSubject($subject);
    $message->setHtmlBody($html);
    

    return Craft::$app->mailer->send($message);
}

Hope this is useful for someone

Happy coding :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.