# Trying to populate email variable after submit with variable from query

**URL:** <https://community.gravityforms.com/t/trying-to-populate-email-variable-after-submit-with-variable-from-query/15215>\
**Category:** Get Help\
**Tags:** dynamic-population, custom, notifications, gform\_pre\_send\_email\
**Created:** [May 25, 2023, 2:49pm UTC](https://community.gravityforms.com/t/trying-to-populate-email-variable-after-submit-with-variable-from-query/15215 "2023-05-25T14:49:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkirker](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/jkirker/32/7118_2.png) [@jkirker](https://community.gravityforms.com/u/jkirker)\
**Post date:** [May 25, 2023, 2:49pm UTC](https://community.gravityforms.com/t/trying-to-populate-email-variable-after-submit-with-variable-from-query/15215/1 "2023-05-25T14:49:48Z")

</div>

I’m currently inserting a record into a database after submission.

After the insert I’m setting a variable using wpdb-\>insert\_id, like:  
$customer\_appointment\_id = $wpdb-\>insert\_id;

I’m then trying to pass this into the notifications but I’m not having any luck. I’m stuck. Any help or advice would be appreciated.

What am I doing wrong?

add\_filter( ‘gform\_pre\_send\_email’, ‘before\_email’ );  
function before\_email( $email ) {  
$email[‘message’] = str\_replace( ‘appt\_id=0’, ‘appt\_id=’.$customer\_appointment\_id, $email[‘message’] );  
return $email;  
}

---

<div class="post-metadata">

**Author:** ![faisalahammad](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/faisalahammad/32/13948_2.png) [@faisalahammad](https://community.gravityforms.com/u/faisalahammad)\
**Post date:** [May 28, 2023, 12:01am UTC](https://community.gravityforms.com/t/trying-to-populate-email-variable-after-submit-with-variable-from-query/15215/2 "2023-05-28T00:01:50Z")

</div>

The problem you’re running into is that the `$customer_appointment_id` you’re trying to use in your before\_email function is kind of like a stranger to that function. They haven’t met before, so before\_email doesn’t recognize **$customer\_appointment\_id** when you’re trying to use it.

So how do you introduce them? Well, one way is by using a ‘global’ variable. It’s like making your secret public. After you insert a record and set the insert id, you can do this:

```auto
global $customer_appointment_id;
$customer_appointment_id = $wpdb->insert_id;

```

What this does is it shouts out $customer\_appointment\_id to the whole script, and then anyone (any function) who wants to use it can.

Then in your before\_email function, you can bring it in like this:

```auto
add_filter('gform_pre_send_email', 'before_email');

function before_email($email) {
    global $customer_appointment_id;
    $email['message'] = str_replace('appt_id=0', 'appt_id='.$customer_appointment_id, $email['message']);
    return $email;
}

```

Here you’re saying, “Hey function, remember that $customer\_appointment\_id we made public? We’re going to use it here.”

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex020/uploads/gravityforms1/original/2X/f/fef63f0599c45e0218c30d9f5d7ca8bff9b00189.png) [@system](https://community.gravityforms.com/u/system)\
**Post date:** [June 27, 2023, 12:02am UTC](https://community.gravityforms.com/t/trying-to-populate-email-variable-after-submit-with-variable-from-query/15215/3 "2023-06-27T00:02:23Z")

</div>

Closed on June 27, 2023, 12:02am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
