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
The email to be sent.
Array
(
[to] => [email protected]
[subject] => New submission from A
[message] => This is the email body.
[headers] => Array
(
[From] => From: "[email protected]" <[email protected]>
[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
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;
}
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'] = '[email protected]';
$email['message'] = 'This is my new message.';
$email['subject'] = 'This is a new subject.';
return $email;
}
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 );
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'] = '[email protected]';
return $email;
}, 10, 3 );
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 );
Add custom source header
The following shows how you can add a custom header to the email just before it is sent. In this case, the header would identify which form, entry, and notification the email originates from.
add_filter( 'gform_pre_send_email', function ( $email, $message_format, $notification, $entry ) {
$email['headers']['X-gf-source'] = sprintf( 'X-gf-source: %d|%d|%s', rgar( $entry, 'form_id' ), rgar( $entry, 'id' ), rgar( $notification, 'id' ) );
return $email;
}, 10, 4 );
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