Alternate Notification "To" Email Address

Hello GF Experts,

I’ve started using Gravity Forms and I’m trying to alternate the email address that notifications are sent to, so that each second emails is sent to an alternative address.

It’s for a sales team of 2 people and they want to divide the notifications 50/50 as each submission will be a sales lead.

I’ve tried the two following code snippets but neither will work as the are both setting the email to the same address for every submission.

Snippet Version 1 (sends to 1st email address every time)

add_filter( 'gform_pre_send_email', 'update_meta_before_send', 10, 2 );

function update_meta_before_send( $email, $notification ) {

	$entry_id = $notification->entry_id;

  if ( $entry_id % 2 == 0 ) {
    $email['to'] = '1_email_address@mydomain.com';
  } else {
    $email['to'] = '2_email_address@mydomain.com';  // Original recipient
  }

Snippet Version 2 (sends to 2nd email address every time)

add_filter( 'gform_pre_send_email', 'update_meta_before_send' );
function update_meta_before_send( $email ) {
	
	$entry_id = $entry['entry_id'];
	
    $email['to'] = '1_email_address@mydomain.com';
	
	if ($entry_id %2 == 0){
    $email['to'] = '2_email_address@mydomain.com';
	}
    return $email;
}

It seems that I’m not accessing the submission id correctly and I can’t figure out what I’m doing wrong. Any help would be appreciated.

Thanks in Advance,
Donal

function update_meta_before_send( $email, $notification ) {

	$entry_id = $notification->entry_id;

This isn’t working because $notification only contains properties related to how the notification is configured, it does not contain an entry_id property. Also, it should be accessed as an array. You can find out what $notification contains here: Notifications Object - Gravity Forms Documentation

function update_meta_before_send( $email ) {
	
	$entry_id = $entry['entry_id'];

This isn’t working because $entry isn’t available.

I recommend using the gform_notification filter:

add_filter( 'gform_notification', 'change_notification_email', 10, 3 );
function change_notification_email( $notification, $form, $entry ) {
    if ( $notification['name'] == 'Admin Notification' ) {
        $notification['toType'] = 'email';
        $notification['to'] = ( rgar( $entry, 'id' ) %2 == 0 ) ? '1_email_address@mydomain.com' : '2_email_address@mydomain.com';
    }
 
    return $notification;
}

Hi Richard,

Thanks for your great help and explanations with this and I guess I’ve got a lot to learn.

Can you recommend any good learning sources?

Have a great day.
Donal

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