Configure SMTP with constants or site options with support for multisite networks.
- Just download or copy this file
- Place it under
wp-content/mu-plugins/smtp-emails.php
- Done.
<?php | |
/** | |
* Plugin Name: SMTP | |
* Plugin URI: https://gist.github.com/lightningspirit/bb51e110d92821b724ab53bf2e07cb87 | |
* Description: Send emails through SMTP server | |
* Author: Move Your Digital, Inc. | |
* Author URI: https://moveyourdigital.com | |
* Version: 0.3.2 | |
* | |
* @package Smtp_Emails | |
*/ | |
/* | |
This program is free software; you can redistribute it and/or modify | |
it under the terms of the GNU General Public License as published by | |
the Free Software Foundation; either version 2 of the License, or | |
(at your option) any later version. | |
This program is distributed in the hope that it will be useful, | |
but WITHOUT ANY WARRANTY; without even the implied warranty of | |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
GNU General Public License for more details. | |
You should have received a copy of the GNU General Public License | |
along with this program; if not, write to the Free Software | |
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
*/ | |
/** | |
* Fires after PHPMailer is initialized. | |
* | |
* @since 0.2.0 | |
* | |
* @param PHPMailer $phpmailer The PHPMailer instance (passed by reference). | |
*/ | |
add_action('phpmailer_init', function ($phpmailer) { | |
if (!is_object($phpmailer)) { | |
$phpmailer = (object) $phpmailer; | |
} | |
if (defined('WP_DEBUG') && constant('WP_DEBUG')) { | |
$phpmailer->SMTPDebug = 2; | |
} | |
$smtp_host = get_option('smtp_host') ?: constant('SMTP_HOST'); | |
if ($smtp_host) { | |
$phpmailer->IsSMTP(); | |
$phpmailer->Host = $smtp_host; | |
} | |
$smtp_auth = get_option('smtp_auth') ?: constant('SMTP_AUTH'); | |
if ($smtp_auth) { | |
$phpmailer->SMTPAuth = $smtp_auth; | |
} | |
$smtp_port = get_option('smtp_port') ?: constant('SMTP_PORT'); | |
if ($smtp_port) { | |
$phpmailer->Port = $smtp_port; | |
} | |
$smtp_username = get_option('smtp_username') ?: constant('SMTP_USERNAME'); | |
if ($smtp_username) { | |
$phpmailer->Username = $smtp_username; | |
} | |
$smtp_password = get_option('smtp_password') ?: constant('SMTP_PASSWORD'); | |
if ($smtp_password) { | |
$phpmailer->Password = $smtp_password; | |
} | |
$smtp_secure = get_option('smtp_secure') ?: constant('SMTP_SECURE'); | |
if ($smtp_secure) { | |
$phpmailer->SMTPSecure = $smtp_secure; | |
} | |
$smtp_email_from = get_option('smtp_from_email') ?: constant('SMTP_FROM'); | |
if ($smtp_email_from) { | |
$phpmailer->From = $smtp_email_from; | |
} | |
$smtp_name_from = get_option('smtp_from_name') ?: constant('SMTP_NAME'); | |
if ($smtp_name_from) { | |
$phpmailer->FromName = $smtp_name_from; | |
} | |
return $phpmailer; | |
}); | |
/** | |
* Filters the email address to send from. | |
* | |
* @since 0.1.0 | |
* | |
* @param string $from_email Email address to send from. | |
* | |
* @uses wp_mail_from | |
*/ | |
add_filter('wp_mail_from', function ($from_email) { | |
$smtp_email_from = get_option('smtp_from_email') ?: constant('SMTP_FROM'); | |
if ($smtp_email_from) { | |
return $smtp_email_from; | |
} | |
return $from_email; | |
}); | |
/** | |
* Filters the name to associate with the "from" email address. | |
* | |
* @since 0.1.0 | |
* | |
* @param string $from_name Name associated with the "from" email address. | |
* | |
* @uses wp_mail_from_name | |
*/ | |
add_filter('wp_mail_from_name', function ($from_name) { | |
$smtp_name_from = get_option('smtp_from_name') ?: constant('SMTP_NAME'); | |
if ($smtp_name_from) { | |
return $smtp_name_from; | |
} | |
return $from_name; | |
}); | |
/** | |
* In every visit to the site settings network, if the options | |
* don't exist, create them so we can change in a per site basis. | |
* | |
* @since 0.2.1 | |
*/ | |
add_action('admin_head', function () { | |
if (! is_multisite()) return; | |
if (get_current_screen()->id !== 'site-settings-network') return; | |
$id = isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0; | |
switch_to_blog($id); | |
if (null === get_option('smtp_host', null)) { | |
$options = [ | |
'smtp_host', 'smtp_auth', 'smtp_port', | |
'smtp_username', 'smtp_password', 'smtp_secure', | |
'smtp_from_email', 'smtp_from_name', | |
]; | |
foreach ($options as $option) { | |
add_option($option, ''); | |
} | |
} | |
restore_current_blog(); | |
}); |