WordPress has lots of hooks (actions and filters) which make it super easy to extend.

Today, I've set up Orbisius to use Mailgun for outgoing emails. It's a great service to send transactional emails.

I've installed the official Mailgun plugin but the missing element was that the plugin didn't offer to specify Reply-to.

This is important because I want people to be able to reply to the emails the site sends them.

That way  they can report an issue and don't have to go through the main site and do multiple steps.

Most people are super busy nowadays and if it's too hard to report an issue people won't do it.

I've done it too.

 

Here's how to set the reply-to field for WordPress outgoing emails if wp_mail function is used.

// Sets reply-to if it doesn't exist already.
add_filter( 'wp_mail', 'orb_custom_wp_mail_filter' );

function orb_custom_wp_mail_filter( $args ) {
	if (!isset($args['headers'])) {
		$args['headers'] = array();
	}
	
	$headers_ser = serialize($args['headers']);
	
	// Does it exist already?
	if (stripos($headers_ser, 'Reply-To:') !== false) {
		return $args;
	}
	
	$site_name = get_option('blogname');
	$admin_email = get_option('admin_email');
	
	$reply_to_line = "Reply-To: $site_name <$admin_email>";

	if (is_array($args['headers'])) {
		$args['headers'][] = 'h:' . $reply_to_line;
		$args['headers'][] = $reply_to_line . "\r\n";
	} else {
		$args['headers'] .= 'h:' . $reply_to_line . "\r\n";
		$args['headers'] .= $reply_to_line . "\r\n";
	}
	
	return $args;
}

 

Note: WordPress allows that function to be overridden. If that's the case make sure the overridden function calls the apply_filters('wp_mail') properly otherwise the solution above won't wok.

 

 

Disclaimer: The content in this post is for educational purposes only. Always remember to take a backup before doing any of the suggested steps just to be on the safe side.
Referral Note: When you purchase through a referral link (if any) on this page, we may earn a commission.
If you're feeling thankful, you can buy me a coffee or a beer