Answers.Body: <p>I'm not sure this is going to be possible with the current version (1.8.1) of Contact Form.</p>
  
  <p>The only thing passed into the <code>beforeSend</code> event is the message, so it doesn't really give you a chance to modify other properties of the email.</p>
  
  <p>Maybe you could pull it off by <a href="https://github.com/pixelandtonic/ContactForm#dynamically-adding-email-recipients-requires-craft-25" rel="nofollow">dynamically adding these addresses</a> with some JavaScript that checked selected countries and added to post, but those values are expected to be <code>|hash</code>ed.</p>
  
  <p>You could always just fork the plugin and modify it for your specific needs, too.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-17T15:07:36.770Z
Answers.Text: 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.
Answers.UserDisplayName: Lettie
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-09-16T23:30:08.387Z
Answers.LastActivityDate: 2016-09-16T23:30:08.387Z
Answers.UserDisplayName: Brad Bell
Body: <p>I'm trying to build a plugin that fires on the Contact Form Plugins <code>contactForm.beforeSend</code>what 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 <code>$toEmails</code> 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?</p>
  
  <pre><code>public function init() {
      craft()-&gt;on('contactForm.beforeSend', function(ContactFormEvent $event) {
          $message = $event-&gt;params['message'];
          $ukArray = array('United Kingdom','Wales','Scotland','Ireland');
          $country = '';
          foreach ($message as $key =&gt; $field) {
              if($key == 'messageFields') {
                  $country = $field['Country'];
              }
          }
          if (in_array($country, $ukArray)) {
              // add an email address to the contact form $toEmails array
          } 
      });
  }
  </code></pre>
  
Tags: <plugin-contactform>
Title: Contact Form Plugin how to send to different recipients based on country selected?
ThreadText: # Contact Form Plugin how to send to different recipients based on country selected?
  
  (Asked by: username_0 on 2016-09-16)
  
  I'm trying to build a plugin that fires on the Contact Form Plugins `contactForm.beforeSend`what 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
          } 
      });
  }
  
  ```
  
  **Tags:** plugin-contactform
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-09-16. Score: 0 | Accepted answer
  
  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](https://github.com/pixelandtonic/ContactForm#dynamically-adding-email-recipients-requires-craft-25) with some JavaScript that checked selected countries and added to post, but those values are expected to be `|hash`ed.
  
  You could always just fork the plugin and modify it for your specific needs, too.
  
  > **Comments:**
  > **username_0**: Cheers @username_1. 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.  
  
  ---
  
  