Send all WPMU emails via SMTP

If I understand the question correctly, you’re having trouble with SMTP settings in a Multisite environment; specifically, your SMTP settings are not propagating throughout your sites from the root site.

It’s possible that you need to configure the SMTP settings for every site individually, even if you’ve network activated the plugin. Some plugins aren’t written with WPMU / Multisite in mind.

You might be able to programmatically set your SMTP settings, though.

Note: The following code is untested and may or may not work for you. I also can’t speak as to its performance, though I don’t think there should be any significant issues.

add_action( 'plugins_loaded', 'wpse101829_set_smtp' );
function wpse101829_set_smtp() {
    $default_smtp = array(
        'host' => 'smtp.example.com',
        'port' => 25,
    );
    $option_name="_smtp_settings";
    if ( is_multisite() ) {
        if ( ! get_site_option( $option_name ) ) {
            update_site_option( $option_name, $default_smtp );
        }
    } else {
        if ( ! get_option( $option_name ) ) {
            update_option( $option_name, $default_smtp );
        }
    }
}

Then, in your code sample above, you would use something like this:

add_action( 'phpmailer_init', 'wpse8170_phpmailer_init' );
function wpse8170_phpmailer_init( PHPMailer $phpmailer ) {
    // get the SMTP defaults
    if ( is_multisite() ) {
        $default_smtp = get_site_option( '_smtp_settings' );
    } else {
        $default_smtp = get_option( '_smtp_settings' );
    }

    $phpmailer->Host = $default_smtp['host'];
    $phpmailer->Port = $default_smtp['port']; // could be different
    $phpmailer->Username="[email protected]"; // if required
    $phpmailer->Password = 'mypassword'; // if required
    $phpmailer->SMTPAuth = true; // if required
    $phpmailer->SMTPSecure="ssl"; // enable if required, 'tls' is another possible value

    $phpmailer->IsSMTP();
}

References

Codex pages for:

Leave a Comment