Trouble with Formit &emailTo and Dropdown Selection

Hi there.

I’m scratching my head a bit with a FormIt issue. I’ve got this form with a dropdown, and I want the selected option to determine which email address the form gets sent to.

am I allowed to use conditional logic (like output modifiers) directly within the &emailTo parameter?

<form action="[[~[[*id]]]]" method="post">
    <label for="recipient">Choose a recipient:</label>
    <select name="recipient">
        <option value="option1">Option 1</option>
        <option value="option2">Option 2</option>
        <option value="option3">Option 3</option>
        <option value="option4">Option 4</option>
    </select>
    <br>
    <label for="message">Your Message:</label>
    <textarea name="message"></textarea>
    <br>
    <input type="submit" value="Send">
</form>

[[!FormIt?
    &hooks=`email`
    &tpl=`emailFormChunk`
    &emailTo=`[[+recipient:is=`option1`:then=`emailone@gmail.com`:else=`
        [[+recipient:is=`option2`:then=`your@gmail.com`:else=`
            [[+recipient:is=`option3`:then=`xxx@gmail.com`:else=`
                [[+recipient:is=`option4`:then=`general@gmail.com`:else=``]]
            ]]
        ]]
    ]]]`
    &emailSubject=`New Message from Website`
    &emailTpl=`emailTemplateChunk`
]]

What is the placeholder [[+recipient]] and where does it get set?

If it gets set in a snippet that is executed before the FormIt snippet, then it could work.
If you mean the placeholder [[!+fi.recipient]] that gets set by FormIt, then you can’t use it here (as FormIt hasn’t been executed).

Personally, I would write a custom hook snippet that runs before the “email” hook and sets the value of “emailTo” ($hook->formit->config['emailTo']) according to the value of the recipient parameter ($hook->getValue('recipient')).

1 Like

Thank you for your reply.
I’m going to try this snippet and see if it works

<?php
// Get the value of the recipient from the submitted form
$recipient = $hook->getValue('recipient');

// Set the email recipient based on the dropdown value
switch ($recipient) {
	case 'option1':
		$emailTo = 'one@example.com';
		break;
	case 'option2':
		$emailTo = 'two@example.com';
		break;
	case 'option3':
		$emailTo = 'three@example.com';
		break;
	default:
		$emailTo = 'other@example.com'; // Default email address
		break;
}

// Set the emailTo parameter for FormIt
$hook->formit->config['emailTo'] = $emailTo;

return true;

emailSelectTo + emailSelectField properties are possible. The usage is described well in the docs.

1 Like

If this is a simple contact form, you might look at SPForm. It has the multiple recipients drop-down built in, and I’ve found it to be more spam-proof than FormIt, even without Captcha.

1 Like

@jako Thank you, I can’t believe I went through the docs and I missed that …

@bobray thanks, I will check out SPForm.

By the way, it seems the snippet worked.