| CARVIEW |
gform_pre_send_email
- Description
- Usage
- Parameters
- Examples
- Abort emails
- Update email meta
- Wrap message in HTML tags
- Copy To to Bcc
- Change default font sizes for a notification using {all_fields}
- Add custom source header
- Abort email based on the To address domain
- Force From header to use the From Email address only when From Name is empty
- Placement
- Source Code
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 );
Abort email based on the To address domain
This example prevents the email from being sent for a specified domain.
add_filter( 'gform_pre_send_email', function ( $email, $message_format ) {
GFCommon::log_debug( __METHOD__ . "(): To Email Address: {$email['to']}" );
// Abort email if the To address contains example.com
if ( str_contains ( $email['to'], 'example.com' ) ) {
GFCommon::log_debug( __METHOD__ . '(): Email sending aborted due to the domain used for the To field.' );
$email['abort_email'] = true;
}
return $email;
}, 10, 2 );
Force From header to use the From Email address only when From Name is empty
By default, when the From Name is empty for a notification Gravity Forms uses the From Email address as From Name. The following snippet will prevent this by forcing the From header to use the From Email address only.
add_filter(
'gform_pre_send_email',
function ( $email, $message_format, $notification, $entry ) {
GFCommon::log_debug( __METHOD__ . '(): Running custom function for empty From Name...' );
// Skip with no changes if notification has a From Name set.
if ( ! empty( rgar( $notification, 'fromName' ) ) ) {
GFCommon::log_debug( __METHOD__ . '(): From Name is set for this notification, returning without changes.' );
return $email;
}
// Force From header to use the From Email address only.
$form = GFAPI::get_form( rgar( $entry, 'form_id' ) );
$from = GFCommon::replace_variables( rgar( $notification, 'from' ), $form, $entry, false, false, false, 'text', array() );
$email['headers']['From'] = 'From: ' . $from;
GFCommon::log_debug( __METHOD__ . '(): From header set to: ' . $email['headers']['From'] );
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