chrishajer

updated multi-form populated email based on accountID

Nov 16th, 2011
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. // updated version of http://pastebin.com/Rws5p0T5 for gmuhr
  2. // http://www.gravityhelp.com/forums/topic/dynamic-population-notification-strategy
  3. // NOTE - change here to apply to all forms; drop the _1
  4. add_filter('gform_notification_email', 'populate_admin_email_via_db', 10, 2);
  5. function populate_admin_email_via_db($email,$lead) {
  6.  
  7.         // setup the array mapping the form ID to the field where the accountID will be available
  8.         // format: form ID => field ID
  9.         $idField = array (
  10.                     1  => 28,
  11.                     8  => 55,
  12.                     3  => 29,
  13.                     7  => 60,
  14.                     11 => 73,
  15.                     5  => 68,
  16.                     4  => 33,
  17.                     9  => 74,
  18.                     13 => 98,
  19.                     14 => 103);
  20.  
  21.         // if the form_id in the entry is NOT in the array of forms we want to process
  22.         // just return the email address
  23.         if (!array_key_exists($lead['form_id'], $idField)) {
  24.             return $email;
  25.         }
  26.  
  27.         // set the email we're going to return equal to the email that was passed
  28.         // in from the admin settings, in case something fails
  29.         $selected_email = $email;
  30.  
  31.         // database connection details
  32.         $dbname  = 'xxxxx';
  33.         $dbuser  = 'xxxxx';
  34.         $dbpass  = 'xxxxx';
  35.         $dbhost  = 'localhost';
  36.         $dbtable = 'xxxxx';
  37.  
  38.         // changed this line to use the form ID and field ID from the idField array
  39.         // continue: if the form_id is in our array, then grab the accountID and pull the email address
  40.         $account = $lead[$idField[$lead['form_id']]];
  41.  
  42.         // establish the connection
  43.         $link = mysql_connect($dbhost, $dbuser, $dbpass);
  44.  
  45.         // if connection was established
  46.         if ($link){
  47.                 $sql = "SELECT email FROM $dbname.$dbtable WHERE activation_key='$account' LIMIT 1;";
  48.                 $result = mysql_query($sql);
  49.  
  50.                 // deal with the results
  51.                 if($result) {
  52.                         // the email address will be the one and only record returned
  53.                         $returned_record = mysql_fetch_row($result);
  54.                         if(!empty($returned_record))
  55.                                 $selected_email = $returned_record[0];
  56.                 }
  57.                 // close the connection if it was established
  58.                 mysql_close($link);
  59.         }
  60.     // return an email address one way or the other
  61.     return $selected_email;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment