Remove Empty Emails from Notification Email Lists

If you’re passing merge tags as an email list (e.g. {admin_email},{Email:1},{Alternate Email:3}) and one of those emails is empty, Gravity Forms won’t send the email to any of the emails in the list.

This snippet intercepts notifications and processes the to, cc and bcc properties, removing any empty emails.

Code

Filename: gw-remove-empty-emails-from-notifications.php

<?php
/**
 * Gravity Wiz // Gravity Forms // Remove Empty Emails from Notification Email Lists
 * https://gravitywiz.com/
 *
 * If you're passing merge tags as an email list (e.g. {admin_email},{Email:1},{Alternate Email:3}) and one of those
 * emails is empty, Gravity Forms won't send the email to any of the emails in the list.
 *
 * This snippet intercepts notifications and processes the `to`, `cc` and `bcc` properties, removing any empty emails.
 */
add_filter( 'gform_notification', function( $notification, $form, $entry ) {
	foreach ( array( 'to', 'cc', 'bcc' ) as $key ) {
		if ( ! empty( $notification[ $key ] ) ) {
			$notification[ $key ] = GFCommon::replace_variables( $notification[ $key ], $form, $entry, false, false, false, 'text' );
			$emails               = array_filter( explode( ',', $notification[ $key ] ) );
			$notification[ $key ] = implode( ',', $emails );
		}
	}
	return $notification;
}, 10, 3 );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.