• Resolved bibberle

    (@bibberle)


    Hi, I have a real estate website where agents can send properties to people who invest in this kind of properties.

    However, some of the agents want to use their own email address sending the real estate data to the investors. So they have the possibility to store their own SMTP credentials in a woocommerce setting, which I implemented in the frontend this afternoon.

    Now what I am trying to achieve is – in case an agent wants to use his own SMTP settings when distributing a property – to change the SMTP settings for outgoing mails from this specific agent.

    That’s what I got, but unfortunally, the SMTP settings are not being overwritten, although the current_user_id()-agent has the SMTP user meta field filled out correctly using the woocommerce setting I mentioned above:

    What might be an approach / hook / filter that might work to overwrite the phpmailer settings under specific circumstances?

    $agent_id = get_current_user_id();
    
    // returns false, if agent has no own SMTP settings, returns an array from agents user meta data with the SMTP settings if he wants to use custom SMTP setting
    $possiblearray = use_own_mail_account($agent_id);
    
    if(is_array($possiblearray)) 
    {
            // agent wants to use own SMTP settings
    	$smtp_host = $smtpinfos["host"];
    	$smtp_port = $smtpinfos["port"];
    	$smtp_username = $smtpinfos["nutzername"];
    	$smtp_password = $smtpinfos["nutzerpasswort"];
    	$smtpsecure = $smtpinfos["verschluesselung"];
    	$smtpfrom = $smtpinfos["frommail"];
    	$smtpfromname = $smtpinfos["fromname"];;
    
            // filter to overwrite / change the default SMTP settings for the next mail
    	add_filter('wp_mail_smtp_custom_options', function($phpmailer) use ($smtp_host, $smtp_port, $smtp_username, $smtp_password, $smtpsecure, $smtpfrom, $smtpfromname) {
    
    		$phpmailer->isSMTP();
    		$phpmailer->Host       = $smtp_host;
    		$phpmailer->SMTPAuth   = true;
    		$phpmailer->Username   = $smtp_username;
    		$phpmailer->Password   = $smtp_password;
    		$phpmailer->SMTPSecure = $smtpsecure;
    		$phpmailer->Port       = $smtp_port;
    		$phpmailer->From       = $smtpfrom;
    		$phpmailer->FromName   = $smtpfromname;
    	});
    }
    
    // Send a test mail
    wp_mail("[email protected]", "this is a test mail", "");
    
    // remove filter again
    remove_filter('wp_mail_smtp_custom_options', false);


Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Support Victoria Sakal

    (@sakalvictoria)

    Hi, @bibberle!

    Thanks for getting in touch.

    Please kindly know, that you need to use the easy_wp_smtp_custom_options filter instead of the wp_mail_smtp_custom_options.

    If you need further help with custom development, I apologize, however, we aren’t able to provide support for this degree of customization.

    Kind regards.

    Thread Starter bibberle

    (@bibberle)

    Thank you, works like a charm!!!

    What line of code do I need to remove this filter again right after a modified mail has been sent?

    remove_filter(‘easy_wp_smtp_custom_options’, false); does not work, unfortunally

    • This reply was modified 6 months, 1 week ago by bibberle.
    Plugin Support Victoria Sakal

    (@sakalvictoria)

    Hi, @bibberle!

    I’m happy to hear that it’s worked.

    Regarding your last question: in your code, you use an anonymous callback function for the action. In this case, it’s not possible to remove the action. You need to use the named function like in the below example:

    add_filter( 'wp_mail_smtp_custom_options', 'theme_prefix_wp_mail_smtp_custom_options' );
    
    function theme_prefix_wp_mail_smtp_custom_options( $phpmailer ) {
      // SMTP server settings.
    }
    
    remove_filter( 'wp_mail_smtp_custom_options', 'theme_prefix_wp_mail_smtp_custom_options' );

    With such an approach, it’s not allowed to use use, so you need to retrieve all required values in the action function.

    I hope it helps!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Use different phpmailer for some of my customers’ is closed to new replies.