gravityflow_notification

Allows the emails to be filtered before they're sent.

Note:
Any changes to the $notification['to'] or $notification['from'] values must result in only a valid email address being defined for the notification to be successful. If you want to modify to also display name details in those fields, you should use the gform_pre_send_email filter of Gravity Forms.

Examples

Example 1: Set the the notification from address and from name to the current user when the current step is approval and the email is an approval or rejection email.

/**
 * Set the the notification from address and from name to the current user when the current step is approval and the email is an approval or rejection email.
 *
 * @param array $notification
 * @param array $form
 * @param array $entry
 * @param Gravity_Flow_Step $step
 *
 * @return array
 */
function sh_gravityflow_notification( $notification, $form, $entry, $step ) {
    if ( $step->get_type() == 'approval' && $step->get_status() !== 'pending' ) {
        $current_user = wp_get_current_user();
        $notification['from']     = $current_user->user_email;
        $notification['fromName'] = $current_user->display_name;
        $notification['subject'] = $current_user->display_name;
	
	// Custom subject
	$notification['subject']  = 'New Status: ' . $status;

	// Uncomment these lines to disable autoformatting
	//$notification['disableAutoformat']  = '1';
	//$notification['message_format']  = 'text';
    }
    return $notification;
}
add_filter( 'gravityflow_notification', 'sh_gravityflow_notification', 10, 4 );

Example 2: Sends the file uploaded in a 'File Upload' field as an attachment to the assignee of an Approval step

/*
Sends the file uploaded in a 'File Upload' field as an  attachment to the assignee of an Approval step
*/

add_filter( 'gravityflow_notification', 'sh_gravityflow_notification', 10, 4 );
function sh_gravityflow_notification( $notification, $form, $entry, $step ) {
 if ( $step->get_type() == 'approval' && $step->get_status() == 'pending' ) {
     
		$fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
		if ( ! is_array( $fileupload_fields ) )
            {
                
                return $notification;
            }
		$notification['attachments'] = ( is_array( rgget('attachments', $notification ) ) ) ? rgget( 'attachments', $notification ) : array();
		$upload_root = RGFormsModel::get_upload_root();
        foreach( $fileupload_fields as $field ) {
            $url = $entry[ $field->id ];
            $attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
            
            if ( $attachment ) {
                $notification['attachments'] = $attachment;
            }
        }	
		
	}
	gravity_flow()->log_debug( 'Attachment for entry_id '.$entry['id']. " ".var_export( $notification, true ) );
	return $notification;
 
 }
Example 3: Exclude users (defined by multi-user field(s) in the form) from receiving notifications for certain step(s).
/*
Sends the file uploaded in a 'File Upload' field as an  attachment to the assignee of an Approval step
*/

add_filter( 'gravityflow_notification', 'sh_gravityflow_notification', 10, 4 );
function sh_gravityflow_notification( $notification, $form, $entry, $step ) {
	// Ensure the filter only applies to specific Step IDs.
	if ( in_array( $step->get_id(), array( '167' ) ) ) {
		return $notification;
	}

	// Identify which field IDs should be checked for users to exclude from the notification.
	$exclude_field_ids = array( '2', '3' );

	// Ensure the notification recipient is a valid WP user.
	if ( ! isset( $notification['to'] ) || $notification['to'] == '' ) {
		return $notification;
	}

	$potential_recipient = get_user_by( 'email', $notification['to'] );
	if ( ! $potential_recipient ) {
		return $notification;
	}

	foreach ( $exclude_field_ids as $field_id ) {

		// Ensure there is atleast 1 user selected in the exclusion field.
		$exclude_users = json_decode( $entry[ $field_id ], true );
		if ( empty( $exclude_users ) ) {
			break;
		}

		if ( in_array( $potential_recipient->ID, $exclude_users ) ) {
			unset( $notification['to'] );
		}
	}

	return $notification;
}

Placement

This code should be placed in the functions.php file of your active theme.