Best practice for changing notification's TO address via framework

I’ve created an add-on that looks up a form submitter’s location via their IP address. Using this location, I want to send an email to one of our offices responsible for that region with the form’s information.

I’ve got the IP lookup and region lookup done. I’m looking for best practices regarding generating a new notification email that will go to the email address associated with the user’s region.

I’ve tried using the gform_notification hook. I got a nicely formatted email, but the code felt messy. Based on the value in the “To Email” field, I had to determine if the notification was intended for someone in our company or the external customer–we don’t want these emails going out to someone outside of the organization. Additionally, this left the forms admin putting a company address in the notification’s TO field, which would ultimately not be used because the IP address routing would change it.

Right now, I’m using the feed framework. I’ve got a feed set up, and it’s processing successfully. This feels a lot cleaner, since the forms admin just has to create a new feed for the form. However, I’m left with the question of how best to send an email out to the necessary office from within the process_feed() function. I can add a custom event (, but that requires the forms admin to create a notification in addition to the current feed (and we’re back to them putting in a TO address that will not be used).

Is there a way that I can streamline this? I’d like to minimize the amount of work required by the forms admin to hopefully just a few clicks–if that. If they can create a feed, and I can send out a notification in the background without any further work on their part, that would be great.

Using the gform_notification is the way to go to alter a notification dynamically. If you don’t want to use a notification because the admin will see a setting that is being overridden by your code and you want to handle it using a feed instead, then you can completely forget about notifications events because they’re only for notifications.

Using the feed approach you would need to send the email by your own, for example using the wp_mail() function.

Thank you, Samuel!

Using the wp_mail() approach, is there an easy way to generate a styled email like the ones that go out with notifications? I’d like to mimic the {all_fields} functionality as best as I can.

Hi Steve, to mimic the format of the {all_fields} notification, I recommend taking a look at notification.php in the plugin folder and copying what you need.