How can I programmatically alter Webform "To Email Address"? I want to change email addresses based on postal codes entered by users.
My code looks like below.
function my_webform_tracker_webform_submission_render_alter(&amp;$renderable) {
  $node_id = (int)$renderable['#node']-&gt;nid;
  if ($node_id == 257) {
    $postal_code = (int)$renderable['#submission']-&gt;data[5][0];
    if($postal_code &gt;= 14000 &amp;&amp; $postal_code &lt;= 14990) {
      $renderable['#email'][email] = &quot;thisemailid@mydomain.com&quot;;
      $renderable['#node']-&gt;webform['emails'][1]['email'] = &quot;thisemailid@mydomain.com&quot;;
      $renderable['#node']-&gt;webform['emails'][2]['email'] = &quot;thisemailid@mydomain.com&quot;;
      //print &quot;&lt;pre&gt;&quot;; print_r($renderable); exit;
    }
  }
}

I tried a lot but the email id seems to be not changed. Please help me figure this out.
I think a more apt place to alter the destination email would be hook_mail_alter() where Drupal lets you alter an email before sending it out. The webform module includes all the submission data in the $message['params']['submission'] object.

You can do something like this in your module:

/**
 * Implements hook_mail_alter().
 */
function MYMODULE_mail_alter(&amp;$message) {

  //Catch Webform submissions when we have a valid postal code
  if($message['id'] == 'webform_submission') {
    $postal_code_component = 5;
    if(isset($message['params']['submission']-&gt;data[$postal_code_component][0])) {
      $postal_code = (int) $message['params']['submission']-&gt;data[$postal_code_component][0];

      //Find right addressee to switch to
      if(0 &lt;= $postal_code &amp;&amp; $postal_code &lt;= 13999 ) { 
        $message['to'] = 'addressee.1@example.com'; 
      } elseif(14000 &lt;= $postal_code &amp;&amp; $postal_code &lt;= 14990) { //Etc...
        $message['to'] = 'addressee.2@example.com';
      } else { //Catchall
        $message['to'] = 'addressee.3@example.com';
      }
    }
  }
}