Add a Reply-To Header to Outgoing Emails
Adds the site admin address as Reply-To on emails that do not set one, so replies to no-reply senders reach a real inbox.
Opens WPIDE on
Code
add_filter( 'wp_mail', function ( $args ) {
$reply_to = (string) get_option( 'admin_email' );
$headers = isset( $args['headers'] ) ? $args['headers'] : array();
if ( ! is_array( $headers ) ) {
$headers = '' === trim( (string) $headers ) ? array() : explode( "\n", str_replace( "\r\n", "\n", (string) $headers ) );
}
foreach ( $headers as $header ) {
if ( 0 === stripos( trim( $header ), 'reply-to:' ) ) {
return $args;
}
}
if ( is_email( $reply_to ) ) {
$headers[] = 'Reply-To: ' . $reply_to;
$args['headers'] = $headers;
}
return $args;
} );
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.