Send Contact Form 7 Emails From Your Own Domain
When a Contact Form 7 form is set to send "from" the visitor's address, switches the sender to your own domain and moves the visitor's address to Reply-To, so emails stop landing in spam.
Opens WPIDE on
Code
$wpide_cf7_sender = ''; // e.g. 'Website <[email protected]>'; empty = wordpress@yourdomain.
add_filter( 'wpcf7_mail_components', function ( $components ) use ( $wpide_cf7_sender ) {
$site = preg_replace( '/^www\./', '', (string) wp_parse_url( home_url(), PHP_URL_HOST ) );
if ( ! preg_match( '/([^<\s]+@[^>\s]+)/', (string) $components['sender'], $m ) ) {
return $components;
}
$address = strtolower( $m[1] );
$domain = (string) substr( strrchr( $address, '@' ), 1 );
if ( '' === $site || $domain === $site || substr( $domain, -strlen( '.' . $site ) ) === '.' . $site ) {
return $components;
}
if ( false === stripos( (string) $components['additional_headers'], 'Reply-To:' ) && is_email( $address ) ) {
$components['additional_headers'] = trim( $components['additional_headers'] . "\nReply-To: " . $address );
}
$components['sender'] = $wpide_cf7_sender ? $wpide_cf7_sender : wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ) . ' <wordpress@' . $site . '>';
return $components;
} );
Notes
Set the sender address at the top or leave it empty to use wordpress@ your domain; replying still goes to the visitor.
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.