WordPress – Set Sender – Prevent “Via default@domain.com”

I have been having trouble with the emails I send from my wordpress site. Everything comes from “info@mydomain.co.za via default@hostingprovider.net”

The via part looks really untidy and unprofessional. From past experience I know that this has to do with the Sender not being set. I searched for the setting to change this in WordPress but to no avail. It seems that it can not be set.

Related posts

Leave a Reply

2 comments

  1. You can automatically set the sender by adding this code to functions.php (or putting it in a plugin):

      # set the sender after PHP builds the phpmailer object during a wp_mail call
      add_action( 'phpmailer_init', 'my_phpmailer_init' );
      function my_phpmailer_init( $phpmailer ) {
        $phpmailer->Sender = $phpmailer->From;
      }
    

    This has the same effect as Talon’s answer, but avoids editing core WordPress files. This way the change survives a WordPress upgrade.

    https://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init

  2. So after a bit of searching I managed to find the bit of code that needs to be changed.

    Open the file:
    /wp-includes/pluggable.php

    Find the lines that look like this:

    // Plugin authors can override the potentially troublesome default
    $phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
    $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name  );
    

    And add the following:

    $phpmailer->Sender   = apply_filters( 'wp_mail_from'     , $from_email );
    

    So it should now look like this:

    // Plugin authors can override the potentially troublesome default
    $phpmailer->From     = apply_filters( 'wp_mail_from'     , $from_email );
    $phpmailer->FromName = apply_filters( 'wp_mail_from_name', $from_name  );
    $phpmailer->Sender   = apply_filters( 'wp_mail_from'     , $from_email );
    

    This will prevent the “via unwanted@host.net” and will not just come from you.

    BEWARE:
    This may cause spam issues IF your email address is different from your domain name.
    ie:

    BAD: noreply@example.com and your website is www.mysite.com
    GOOD: noreply@mysite.com and your website is www.mysite.com
    

    I hope this saves someone out there an hour or 2 like I just wasted.