Gform notification

Description

Modify a notification object before it is converted into an email and sent.

Usage

Apply to all forms:

<?php
add_filter("gform_notification", "my_custom_function", 10, 3);
?>

Apply to a specific form:

<?php
// add_filter("gform_notification_{$form_id}", "my_custom_function", 10, 3);
add_filter("gform_notification_5", "my_custom_function", 10, 3);
?>

Parameters

$notification
(array) An array of properties which make up a notification object.
$form
(form object) The form object for which the notification is being sent.
$entry
(entry object) The entry object for which the notification is being sent.

Example 1

This example demonstrates how you can use the gform_notification filter to append a signature to all outgoing Gravity Form notifications.

<?php
add_filter('gform_notification', 'my_gform_notification_signature', 10, 3);
function my_gform_notification_signature( $notification, $form, $entry ) {
       // append a signature to the existing notification 
       // message with .=
	$notification['message'] .= "\n --The Team @ rocketgenius";

    return $notification;
}
?>

Example 2

This example demonstrates how to update legacy code using the deprecated gform_autoresponder_email hook

<?php
add_filter('gform_notification', 'change_autoresponder_email', 10, 3);
function change_autoresponder_email( $notification, $form, $entry ) {

    // There is no concept of user notifications anymore, so 
    // we will need to target notifications based on other 
    // criteria, such as name
    if($notification["name"] == "User Notification"){

        // toType can be routing or email
        $notification['toType'] = "email"; 
        // change the "to" email address
        $notification['to'] = "test@test.com";

    }

    return $notification;
}
?>

Example 3

This example demonstrates how to update legacy code using the deprecated gform_notification_email hook

<?php
add_filter('gform_notification', 'change_notification_email', 10, 3);
function change_notification_email( $notification, $form, $entry ) {

    //There is no concept of admin notifications anymore, so we will need to target notifications based on other criteria, such as name
    if($notification["name"] == "Admin Notification"){

        // toType can be routing or email
        $notification['toType'] = "email"; 
        $notification['to'] = "test@test.com";

    }

    return $notification;
}
?>

Example 4

This example demonstrates how to update legacy code using the deprecated gform_notification_format hook

<?php
add_filter('gform_notification', 'change_notification_format', 10, 3);
function change_notification_format( $notification, $form, $entry ) {

    // change notification format to text from the default html
    $notification['message_format'] = "text";

    return $notification;
}
?>

Example 5

This example demonstrates how to update legacy code using the deprecated gform_user_notification_attachments hook

<?php
add_filter('gform_notification', 'change_user_notification_attachments', 10, 3);
function change_user_notification_attachments( $notification, $form, $entry ) {

    //There is no concept of user notifications anymore, so we will need to target notifications based on other criteria, such as name
    if($notification["name"] == "User Notification"){

        $fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));

        if(!is_array($fileupload_fields))
            return $notification;

        $attachments = 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){
                $attachments[] = $attachment;
            }
        }

        $notification["attachments"] = $attachments;

    }

    return $notification;
}
?>

Use this example if you have the multi-file uploads option selected:

<?php
 
// attach uploaded files to notification, including those from multi-file enabled fields
add_filter( 'gform_notification', 'rw_notification_attachments', 10, 3 );
function rw_notification_attachments( $notification, $form, $entry ) {
    $log = 'rw_notification_attachments() - ';
    GFCommon::log_debug( $log . 'starting.' );
    
    if ( $notification['name'] == 'Admin Notification' ) {
        
        $fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
 
        if ( ! is_array( $fileupload_fields ) ) {
            return $notification;
        }
 
        $attachments = array();
        $upload_root = RGFormsModel::get_upload_root();
        
        foreach( $fileupload_fields as $field ) {
            
            $url = $entry[ $field['id'] ];
            
            if ( empty( $url ) ) {
                continue;
            } elseif ( $field['multipleFiles'] ) {
                $uploaded_files = json_decode( stripslashes( $url ), true );
                foreach ( $uploaded_files as $uploaded_file ) {
                    $attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $uploaded_file );
                    GFCommon::log_debug( $log . 'attaching the file: ' . print_r( $attachment, true  ) );
                    $attachments[] = $attachment;
                }
            } else {
                $attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
                GFCommon::log_debug( $log . 'attaching the file: ' . print_r( $attachment, true  ) );
                $attachments[] = $attachment;
            }
            
        }
 
        $notification['attachments'] = $attachments;
 
    }
    
    GFCommon::log_debug( $log . 'stopping.' );
 
    return $notification;
}
?>

Example 6

This example checks to see if the email address entered by a user matches certain strings to send the notification to a special email address for handling.

<?php
//target form id 203, change to your form id
add_filter('gform_notification_203', 'set_email_to_address', 10, 3);
function set_email_to_address( $notification, $form, $entry ) {
    //There is no concept of user notifications anymore, so we will need to target notifications based on other criteria, 
    //such as name
    if($notification["name"] == "Admin Notification"){
		//get email address value
		$user_email = $entry[1]; //value for field id 1, change to your field id
		        
        //set email to address based on email address user entered
        //gmail.com, hotmail.com, aol.com, yahoo or live.com
        //look for special email addresses in user email string
        if (strpos($user_email, "gmail.com") || strpos($user_email, "hotmail.com") || strpos($user_email,"aol.com") || strpos($user_email, "yahoo.com") || strpos($user_email, "live.com") ){
        	//user entered email address matching free email addresses, send to special address
        	$notification["to"] = "special@example.com";
		}
		else{
			//not a free email address, send to priority email address
			$notification["to"] = "priority@example.com";
		}
    }
	//return altered notification object
    return $notification;
}
?>

Placement

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

Source Code

This filter is located in common.php

Search the Documentation