E-mails sent from multisite sites have wrong sender

Hi,

we run multistie installs for ourselves and our clients. When plugins on multisite sends out e-mails the from name and sometimes also from e-mail address is that of the main site and not, as it should be, of the site where the plugin is active.

Until now we have not been able to find a solution and plugin providers just say: then just use on a single site install.

Do you have any experience with this problem? Looking forward to your answer. Thanks.

Best regards,

Carsten Lund Thomsen

  • Carsten from TwelveTrains
    • Flash Drive

    Hi Ari,

    Thanks for your reply, I’m curious if any other staff knows of the problem.

    For us it’s such a common and recurring issue. But maybe most multisite installs are for one client and not as in our case, we use one multisite install for many different clients (like wordpress.com).

    Cheers,

    Carsten

  • aristath
    • Recruit

    I got 2 answers:

    Answer nr 1:

    You can use something like this and put it in a php file in your wp-content/mu-plugins folder:

    <?php

    add_filter('wp_mail_from', 'set_mail_from_email_custom');
    function set_mail_from_email_custom($email) {
    return get_site_option('admin_email');
    }

    add_filter('wp_mail_from_name', 'set_mail_from_name_custom');
    function set_mail_from_name_custom($name) {
    return get_site_option('blogname');
    }

    Answer nr 2:

    Use this code in a similar way as above:

    <?php

    add_action(
    'phpmailer_init', function( $phpmailer ) {
    $phpmailer->From = get_site_option('admin_email');
    $phpmailer->FromName = get_site_option('blogname');
    });

    You can also use this plugin that incorporates the 2nd solution: https://github.com/webtekk/wp-multisite-smtp/

    Thanks @marko and @alexander Rohmann for the solutions they provided!

    Cheers,

    Ari.

  • Alexander
    • DEV MAN’s Mascot

    Hey, just a quick disclaimer on the second one. It uses an anonymous function instead of declaring one explicitly and adding to the filter. This means you’ll need to be using PHP 5.3 or later. (but it could always be adapted to the traditional way)

    Best regards,

  • Carsten from TwelveTrains
    • Flash Drive

    Hi all,

    So sorry for my late reply on this. Thanks for your solutions. As far as I can see in the code you suggest, it’s still the multisite name and e-mail address which is used. Or maybe I don’t understand it correctly.

    We need a solution which uses NOT the network name and e-mail but the site name and admin e-mailaddress of the multisite website from which the mail is sent (so from the multisite network website on which a mail sending plugin is sending mail from).

    I hope I’m more clear now. Or maybe your suggested code does exactly this. Then please let me know. :slight_smile: Thank you!

    Best Carsten

  • Carsten from TwelveTrains
    • Flash Drive

    Hi, that change was actually not enough. I did some more research myself and some testing and found a solution:

    FIRSTLY, this is the correct code I added to a new mu-plugins file:


    add_filter(‘wp_mail_from’, ‘set_mail_from_email_custom’:wink:;

    function set_mail_from_email_custom($email) {

    return get_option(‘admin_email’:wink:;

    }

    add_filter(‘wp_mail_from_name’, ‘set_mail_from_name_custom’:wink:;

    function set_mail_from_name_custom($name) {

    return get_option(‘blogname’:wink:;

    }


    SECONDLY, in the functions.php of the theme I added the following:


    function twelvetrains_wp_mail_from_name( $email_from )

    {

    return get_option(‘blogname’:wink:;

    }

    add_filter( ‘wp_mail_from_name’, ‘twelvetrains_wp_mail_from_name’ );

    function twelvetrains_wp_mail_from( $email )

    {

    return get_option(‘admin_email’:wink:;

    }

    add_filter( ‘wp_mail_from’, ‘twelvetrains_wp_mail_from’ );


    Not sure if I understand :slight_smile: but the one does not work without the other. On my multisite installation the above works.

    Best regards,

    Carsten

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.