Use email address from ACF record for Sent To address in notification [RESOLVED]

I am working on a contact form for a staff directory which allows the user to click a link on the staff member and send an email through the form. I have the form set up and working properly. However, rather than populating a hidden field for the Send To address, I would like to populate the address from an ACF record to prevent email addresses from being collected by bots.

The following are two attempts I’ve made, but neither worked.

add_filter( 'gform_notification_55', 'acf_email', 10, 3);
function acf_email ($notification, $form, $entry){
     $post_id = rgar($entry, '14');
     $notification['to'] = get_field('staff_email', $post_id);
     return $notificaiton;
}
add_filter( 'gform_notification_55', 'acf_email', 10, 3);
function acf_email ($notification, $form, $entry){
	//$post_id = rgar($entry, '14');
	if ($notification['name'] == "Web Contact"){
		$notification['toType'] = 'routing';
		$notificaiton['routing'] = array(
			'fieldID' => 1,
			'operator' => 'is',
			'value' => get_field('full_name', get_post_id()),
			'email' => get_field('staff_email', get_post_id())
		);
	return $notificaiton;
	}
}

This should work. If it’s not working, I recommend adding some additional logging to see why it’s not working.

First, enable Gravity Forms logging:

Then add some custom logging statements to your code:

Try something like this:

<?php
add_filter( 'gform_notification_55', 'acf_email', 10, 3);
function acf_email ($notification, $form, $entry){
    GFCommon::log_debug( __METHOD__ . '(): Running for Form 55.' );
    GFCommon::log_debug( __METHOD__ . '(): The Entry => ' . print_r( $entry, true ) );
    GFCommon::log_debug( __METHOD__ . '(): The Notification => ' . print_r( $notification, true ) );
    $post_id = rgar($entry, '14');
    $email_to = get_field('staff_email', $post_id);
    GFCommon::log_debug( __METHOD__ . '(): Email To => ' . print_r( $email_to, true ) );
    $notification['to'] = $email_to;
    GFCommon::log_debug( __METHOD__ . '(): Modified Notification => ' . print_r( $notification, true ) );
    return $notification;
}

Copy from here if it’s easier: gform_notification ACF to address - Droplr

After updating the code and testing with logging on, check the Gravity Forms Core log (Form → Settings → Logging) for these logging statements. Share a link to the log file if you need help figuring out what is going wrong.

Actually, I just noticed you have a typo here:

return $notificaiton;

Should be:

return $notification;

Thank you for this. I enabled the logging, which should be helpful.

As for the form process, I had to do a little tweaking, but I have it working now using the following:

add_filter( 'gform_notification_55', 'acf_email', 10, 3);
function acf_email ($notification, $form, $entry){
	$post_id = rgar($entry, '14');
    GFCommon::log_debug( __METHOD__ . '(): Running for Form 55.' );
    GFCommon::log_debug( __METHOD__ . '(): The Entry => ' . print_r( $entry, true ) );
    GFCommon::log_debug( __METHOD__ . '(): The Notification => ' . print_r( $notification, true ) );
	if ($notification['name'] == "Web Contact"){
		$notification['to'] = get_field('staff_email', $post_id);
		$notification['toType'] = 'routing';
		$notification['routing'] = array(
			'fieldID' => 1,
			'operator' => 'is',
			'value' => get_field('full_name', $post_id),
			'email' => get_field('staff_email', $post_id)
		);
	GFCommon::log_debug( __METHOD__ . '(): Email To => ' . print_r( $email_to, true ) );
	GFCommon::log_debug( __METHOD__ . '(): Modified Notification => ' . print_r( $notification, true ) );
	return $notification;
	}
}
1 Like

Me and my lazy fingers!