Browse: Home / Snippets /

ProPanel: Change email “From” address

Contents


Snippet #

Important: All snippets are provided as-is without support or guarantees. These snippets are provided as guidelines for advanced users looking to customize LearnDash. For any additional help or support with these snippets, we recommend reaching out to a LearnDash Expert.

add_filter('wp_mail_from', function ( $from_email_address = '' ) {
	// When calling the WP 	wp_mail() function is the 'From' header is not defined 
	// it will set the default 'From' email to be '[email protected]'. where 'domain.com'.
	// matches the site domain
	
	// So our first check is to see of we are using the default
	$sitename = strtolower( $_SERVER['SERVER_NAME'] );
	if ( substr( $sitename, 0, 4 ) == 'www.' ) {
		$sitename = substr( $sitename, 4 );
	}
	$default_from_email = 'wordpress@' . $sitename;
	
	if ( ( $default_from_email == $from_email_address ) && ( is_user_logged_in() ) ) {
		$current_user = wp_get_current_user();
		$from_email_address = $current_user->user_email;
	}

	return $from_email_address;
});

add_filter( 'wp_mail_from_name', function ( $from_name = '' ) {
	// When calling the WP 	wp_mail() function is the 'From' header is not defined 
	// it will set the default 'From' name to be 'WordPress'. 

	if ( ( $from_name === "WordPress" ) && ( is_user_logged_in() ) ) {
		$current_user = wp_get_current_user();
		$from_name = $current_user->display_name;
	}

	return $from_name;
});

/**
 * Filter to modify the outgoing email address of ProPanel emails. By default, uses the current users email. This example shows how to send it using 1 generic email address.
 *
 * @since 3
 * @var $mail_args       wp_mail arguments
 */
 
add_filter( 'ld_propanel_email_users_args', 'ld_change_pp_outgoing_email_address', 10, 1 );

function ld_change_pp_outgoing_email_address($mail_args) {
	$mail_args['headers'] = array(
		'content-type: text/html',
		'From: LearnDash <[email protected]>',
		'Reply-to: [email protected]'
		);
	
	return $mail_args;
}