Browse: Home / Snippets /

ProPanel: Filter mail args

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.

// LearnDash Filter ProPanel mail args 
add_filter( 'ld_propanel_email_users_args', function( $mail_args = array() ) {
	// The solution is to rewrite the mail_args['headers'] array with the site specific email account info.
	// $mail_args has the following structure
	/* 
	mail_args Array
	(
	    [to] => 
	    [subject] => Subject
	    [message] => <p>Message</p>

	    [attachments] => 
	    [headers] => Array
	        (
	            [0] => content-type: text/html
	            [1] => From: <sender email address>
	            [2] => Reply-to: <sender email address>
	            [3] => Bcc: <list of destination email addresses>
	        )
	)
	*/

	if ( is_user_logged_in() ) {

		// Start with the current user's email address. And allow some filtering
		$from_address = apply_filters( 'wp_mail_from', wp_get_current_user()->user_email );

		// Then if not empty we set this for the 'To', 'From' and 'Reply-To' headers. 
		if ( !empty( $from_address ) ) {
			if ( empty( $mail_args['to'] ) ) {
				$mail_args['to'] = 	$from_address;
			}	

			foreach( $mail_args['headers'] as $h_idx => $h ) {
				if ( ( strncasecmp(  'From:' , $h, strlen( 'From:' ) ) === 0 ) || ( strncasecmp (  'Reply-to:' , $h, strlen( 'Reply-to:' ) ) === 0 ) ) {
					$mail_header_parts = explode(':', $h, 2 );
					$mail_args['headers'][$h_idx] = $mail_header_parts[0] .': '. $from_address ;
				}
			}
		}
	}
	
	// always return $mail_args
	return $mail_args;
	
}, 1, 1 );