1

I'm trying to build a plugin that fires on the Contact Form Plugins contactForm.beforeSendwhat the plugin needs to do is add a recipient email if certain countries are selected. So far I have created the below which checks for the countries and checks against an array to see if they are in it. This works, so far so good. What I need to do now is somehow add to the contact form plugins $toEmails variable if a country is in the array but I'm not sure how to do this from within the event listener. Is this even possible?

public function init() {
    craft()->on('contactForm.beforeSend', function(ContactFormEvent $event) {
        $message = $event->params['message'];
        $ukArray = array('United Kingdom','Wales','Scotland','Ireland');
        $country = '';
        foreach ($message as $key => $field) {
            if($key == 'messageFields') {
                $country = $field['Country'];
            }
        }
        if (in_array($country, $ukArray)) {
            // add an email address to the contact form $toEmails array
        } 
    });
}
0

I'm not sure this is going to be possible with the current version (1.8.1) of Contact Form.

The only thing passed into the beforeSend event is the message, so it doesn't really give you a chance to modify other properties of the email.

Maybe you could pull it off by dynamically adding these addresses with some JavaScript that checked selected countries and added to post, but those values are expected to be |hashed.

You could always just fork the plugin and modify it for your specific needs, too.

| improve this answer | |
  • Cheers @Brad. I've managed to create some jquery instead to do this. But may look at the plugin at a later date when I have a bit more time. – Lettie Sep 17 '16 at 15:07

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.