gform_pre_send_email

Description

Use this filter to modify the email before a notification has been sent. You may also use this to prevent an email from being sent.

Usage

add_filter( 'gform_pre_send_email', 'your_function_name', 10, 4 );

Parameters

  • $email array
        Array
        (
            [to] => contactus@rocketgenius.com
            [subject] => New submission from A
            [message] => This is the email body.
            [headers] => Array
                (
                    [From] => From: "test@rocketgenius.com" <test@rocketgenius.com>
                    [Content-type] => Content-type: text/html; charset=UTF-8
                )
    
            [attachments] => Array
                (
                )
    
            [abort_email] => FALSE
        )
        

  • $message_format string

    The message format, html or text.

  • $notification array

    An array of properties which make up a notification object. See Notifications Object for possible properties. Available from 1.9.15.6.

  • $entry array

    The current Entry Object. Available from 2.2.3.8.

Examples

1. Abort emails.

This example prevents the email from being sent.

add_filter( 'gform_pre_send_email', 'before_email' );
function before_email( $email ) {
    //cancel sending emails
    $email['abort_email'] = true;
    return $email;
}

2. Update email meta.

This example updates the to address, subject, and message.

add_filter( 'gform_pre_send_email', 'before_email' );
function before_email( $email ) {
    $email['to'] = 'test@somedomain.com';
    $email['message'] = 'This is my new message.';
    $email['subject'] = 'This is a new subject.';
    return $email;
}

3. Wrap message in HTML tags.

add_filter( 'gform_pre_send_email', function ( $email, $message_format ) {
    if ( $message_format != 'html' ) {
        return $email;
    }

    $email['message'] = '<html>' . $email['message'] . '</html>';

    return $email;
}, 10, 2 );

4. Copy To to Bcc.

add_filter( 'gform_pre_send_email', function ( $email, $message_format, $notification ) {
    if ( $notification['name'] != 'User Email' ) {
        return $email;
    }

    $email['headers']['Bcc'] = 'Bcc: ' . $email['to'];
    $email['to']  = 'name@somedomain.com';

    return $email;
}, 10, 3 );

5. Change default font sizes for a notification using {all_fields}

add_filter( 'gform_pre_send_email', function ( $email, $message_format ) {
	if ( $message_format != 'html' ) {
		return $email;
	}

	// Change default 12px to 18px
	$email['message'] = str_replace( 'font-size:12px', 'font-size:18px', $email['message'] );

	// Change default 14px to 20px
	$email['message'] = str_replace( 'font-size:14px', 'font-size:20px', $email['message'] );

	return $email;
}, 10, 2 );

Placement

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

Source Code

This action hook is located in GFCommonn::send_email() in common.php