wp_mail() function to use Gmail/Mailgun/SendGrid/SMTP instead of the default mail() and creates an options page to manage the settings.
* Author: WPForms
* Author URI: https://wpforms.com/
* Network: false
* Text Domain: wp-mail-smtp
* Domain Path: /languages
*/
/**
* @author WPForms
* @copyright WPForms, 2007-18, All Rights Reserved
* This code is released under the GPL licence version 3 or later, available here
* https://www.gnu.org/licenses/gpl.txt
*/
define( 'WPMS_PLUGIN_VER', '1.4.2' );
define( 'WPMS_PHP_VER', '5.3.6' );
/**
* Setting options in wp-config.php
*
* Specifically aimed at WP Multisite users, you can set the options for this plugin as
* constants in wp-config.php. Copy the code below into wp-config.php and tweak settings.
* Values from constants are NOT stripslash()'ed.
*/
/*
define( 'WPMS_ON', true ); // True turns on the whole constants support and usage, false turns it off.
define( 'WPMS_MAIL_FROM', 'mail@example.com' );
define( 'WPMS_MAIL_FROM_FORCE', true ); // True turns it on, false turns it off.
define( 'WPMS_MAIL_FROM_NAME', 'From Name' );
define( 'WPMS_MAIL_FROM_NAME_FORCE', true ); // True turns it on, false turns it off.
define( 'WPMS_MAILER', 'smtp' ); // Possible values: 'mail', 'gmail', 'mailgun', 'sendgrid', 'smtp'.
define( 'WPMS_SET_RETURN_PATH', true ); // Sets $phpmailer->Sender if true.
define( 'WPMS_SMTP_HOST', 'localhost' ); // The SMTP mail host.
define( 'WPMS_SMTP_PORT', 25 ); // The SMTP server port number.
define( 'WPMS_SSL', '' ); // Possible values '', 'ssl', 'tls' - note TLS is not STARTTLS.
define( 'WPMS_SMTP_AUTH', true ); // True turns it on, false turns it off.
define( 'WPMS_SMTP_USER', 'username' ); // SMTP authentication username, only used if WPMS_SMTP_AUTH is true.
define( 'WPMS_SMTP_PASS', 'password' ); // SMTP authentication password, only used if WPMS_SMTP_AUTH is true.
define( 'WPMS_SMTP_AUTOTLS', true ); // True turns it on, false turns it off.
define( 'WPMS_GMAIL_CLIENT_ID', '' );
define( 'WPMS_GMAIL_CLIENT_SECRET', '' );
define( 'WPMS_MAILGUN_API_KEY', '' );
define( 'WPMS_MAILGUN_DOMAIN', '' );
define( 'WPMS_MAILGUN_REGION', 'US' ); // or 'EU' for Europe.
define( 'WPMS_SENDGRID_API_KEY', '' );
*/
/**
* Newer PHP version 5.3+ will be handled a lot differently,
* with better code and newer logic.
*
* @since 1.0.0
*/
if ( version_compare( phpversion(), WPMS_PHP_VER, '>=' ) ) {
require_once dirname( __FILE__ ) . '/wp-mail-smtp.php';
return;
}
/**
* Array of options and their default values.
* This is horrible, should be cleaned up at some point.
*/
global $wpms_options;
$wpms_options = array(
'mail_from' => '',
'mail_from_name' => '',
'mailer' => 'smtp',
'mail_set_return_path' => 'false',
'smtp_host' => 'localhost',
'smtp_port' => '25',
'smtp_ssl' => 'none',
'smtp_auth' => false,
'smtp_user' => '',
'smtp_pass' => '',
'pepipost_user' => '',
'pepipost_pass' => '',
'pepipost_port' => '2525',
'pepipost_ssl' => 'none',
'wp_mail_smtp_am_notifications_hidden' => '',
);
/**
* Activation function. This function creates the required options and defaults.
*/
if ( ! function_exists( 'wp_mail_smtp_activate' ) ) :
/**
* What to do on plugin activation.
*/
function wp_mail_smtp_activate() {
global $wpms_options;
// Create the required options...
foreach ( $wpms_options as $name => $val ) {
add_option( $name, $val );
}
}
endif;
if ( ! function_exists( 'wp_mail_smtp_whitelist_options' ) ) :
/**
* Whitelist plugin options.
*
* @param array $whitelist_options
*
* @return mixed
*/
function wp_mail_smtp_whitelist_options( $whitelist_options ) {
global $wpms_options;
// Add our options to the array.
$whitelist_options['email'] = array_keys( $wpms_options );
return $whitelist_options;
}
endif;
/**
* To avoid any (very unlikely) clashes, check if the function already exists.
*/
if ( ! function_exists( 'phpmailer_init_smtp' ) ) :
/**
* This code is copied, from wp-includes/pluggable.php as at version 2.2.2.
*
* @param PHPMailer $phpmailer It's passed by reference, so no need to return anything.
*/
function phpmailer_init_smtp( $phpmailer ) {
/*
* If constants are defined, apply them.
* We should have defined all required constants before using them.
*/
if (
defined( 'WPMS_ON' ) && WPMS_ON &&
defined( 'WPMS_MAILER' )
) {
$phpmailer->Mailer = WPMS_MAILER;
if ( defined( 'WPMS_SET_RETURN_PATH' ) && WPMS_SET_RETURN_PATH ) {
$phpmailer->Sender = $phpmailer->From;
}
if (
WPMS_MAILER === 'smtp' &&
defined( 'WPMS_SSL' ) &&
defined( 'WPMS_SMTP_HOST' ) &&
defined( 'WPMS_SMTP_PORT' )
) {
$phpmailer->SMTPSecure = WPMS_SSL;
$phpmailer->Host = WPMS_SMTP_HOST;
$phpmailer->Port = WPMS_SMTP_PORT;
if (
defined( 'WPMS_SMTP_AUTH' ) && WPMS_SMTP_AUTH &&
defined( 'WPMS_SMTP_USER' ) &&
defined( 'WPMS_SMTP_PASS' )
) {
$phpmailer->SMTPAuth = true;
$phpmailer->Username = WPMS_SMTP_USER;
$phpmailer->Password = WPMS_SMTP_PASS;
}
}
} else {
$option_mailer = get_option( 'mailer' );
$option_smtp_host = get_option( 'smtp_host' );
$option_smtp_ssl = get_option( 'smtp_ssl' );
// Check that mailer is not blank, and if mailer=smtp, host is not blank.
if (
! $option_mailer ||
( 'smtp' === $option_mailer && ! $option_smtp_host )
) {
return;
}
// If the mailer is pepipost, make sure we have a username and password.
if ( 'pepipost' === $option_mailer && ( ! get_option( 'pepipost_user' ) && ! get_option( 'pepipost_pass' ) ) ) {
return;
}
// Set the mailer type as per config above, this overrides the already called isMail method.
$phpmailer->Mailer = $option_mailer;
// Set the Sender (return-path) if required.
if ( get_option( 'mail_set_return_path' ) ) {
$phpmailer->Sender = $phpmailer->From;
}
// Set the SMTPSecure value, if set to none, leave this blank.
$phpmailer->SMTPSecure = $option_smtp_ssl;
if ( 'none' === $option_smtp_ssl ) {
$phpmailer->SMTPSecure = '';
$phpmailer->SMTPAutoTLS = false;
}
// If we're sending via SMTP, set the host.
if ( 'smtp' === $option_mailer ) {
// Set the other options.
$phpmailer->Host = $option_smtp_host;
$phpmailer->Port = get_option( 'smtp_port' );
// If we're using smtp auth, set the username & password.
if ( get_option( 'smtp_auth' ) === 'true' ) {
$phpmailer->SMTPAuth = true;
$phpmailer->Username = get_option( 'smtp_user' );
$phpmailer->Password = get_option( 'smtp_pass' );
}
} elseif ( 'pepipost' === $option_mailer ) {
// Set the Pepipost settings.
$phpmailer->Mailer = 'smtp';
$phpmailer->Host = 'smtp.pepipost.com';
$phpmailer->Port = get_option( 'pepipost_port' );
$phpmailer->SMTPSecure = get_option( 'pepipost_ssl' ) === 'none' ? '' : get_option( 'pepipost_ssl' );
$phpmailer->SMTPAuth = true;
$phpmailer->Username = get_option( 'pepipost_user' );
$phpmailer->Password = get_option( 'pepipost_pass' );
}
}
// You can add your own options here, see the phpmailer documentation for more info: http://phpmailer.sourceforge.net/docs/.
/** @noinspection PhpUnusedLocalVariableInspection It's passed by reference. */
$phpmailer = apply_filters( 'wp_mail_smtp_custom_options', $phpmailer );
}
endif;
if ( ! function_exists( 'wp_mail_smtp_options_page' ) ) :
/**
* This function outputs the plugin options page.
*/
function wp_mail_smtp_options_page() {
global $phpmailer;
// Make sure the PHPMailer class has been instantiated
// (copied verbatim from wp-includes/pluggable.php)
// (Re)create it, if it's gone missing.
if ( ! is_object( $phpmailer ) || ! is_a( $phpmailer, 'PHPMailer' ) ) {
require_once ABSPATH . WPINC . '/class-phpmailer.php';
$phpmailer = new PHPMailer( true );
}
// Send a test mail if necessary.
if (
isset( $_POST['wpms_action'] ) &&
esc_html__( 'Send Test', 'wp-mail-smtp' ) === sanitize_text_field( $_POST['wpms_action'] ) &&
is_email( $_POST['to'] )
) {
check_admin_referer( 'test-email' );
// Set up the mail variables.
$to = sanitize_text_field( $_POST['to'] );
/* translators: %s - email address where test mail will be sent to. */
$subject = 'WP Mail SMTP: ' . sprintf( esc_html__( 'Test mail to %s', 'wp-mail-smtp' ), $to );
$message = esc_html__( 'This is a test email generated by the WP Mail SMTP WordPress plugin.', 'wp-mail-smtp' );
// Set SMTPDebug level, default is 2 (commands + data + connection status).
$phpmailer->SMTPDebug = apply_filters( 'wp_mail_smtp_admin_test_email_smtp_debug', 2 );
// Start output buffering to grab smtp debugging output.
ob_start();
// Send the test mail.
$result = wp_mail( $to, $subject, $message );
// Grab the smtp debugging output.
$smtp_debug = ob_get_clean();
// Output the response.
?>
' . sprintf( /* translators: %1$s - WP Mail SMTP plugin name; %2$s - opening a link tag; %3$s - closing a link tag. */ esc_html__( 'Your site is running an outdated version of PHP that is no longer supported and may cause issues with %1$s. %2$sRead more%3$s for additional information.', 'wpforms' ), 'WP Mail SMTP', '', '' ) . '
' . '