Send Gravity Forms Notifications From Your Own Domain
When a Gravity Forms notification uses the visitor's email or another outside domain as the From address, switches it to your domain and keeps that address as Reply-To.
Opens WPIDE on
Code
$wpide_gf_from = ''; // e.g. '[email protected]'; empty = wordpress@yourdomain.
add_filter( 'gform_notification', function ( $notification, $form, $entry ) use ( $wpide_gf_from ) {
$site = preg_replace( '/^www\./', '', (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
$from = trim( GFCommon::replace_variables( (string) rgar( $notification, 'from' ), $form, $entry, false, false, false, 'text' ) );
if ( '' === $site || ! is_email( $from ) ) {
return $notification;
}
$domain = strtolower( (string) substr( strrchr( $from, '@' ), 1 ) );
if ( $domain === $site || substr( $domain, -strlen( '.' . $site ) ) === '.' . $site ) {
return $notification;
}
if ( '' === trim( (string) rgar( $notification, 'replyTo' ) ) ) {
$notification['replyTo'] = $from;
}
$notification['from'] = is_email( $wpide_gf_from ) ? $wpide_gf_from : 'wordpress@' . $site;
return $notification;
}, 20, 3 );
Notes
Merge tags in the From field are resolved first; set your own sender at the top or leave it empty for wordpress@ your domain.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.