Skip to content

Instantly share code, notes, and snippets.

@gadiener
Created June 20, 2017 16:04
Show Gist options
  • Save gadiener/aec09c77a336edc705405ef6f3053fa6 to your computer and use it in GitHub Desktop.
Save gadiener/aec09c77a336edc705405ef6f3053fa6 to your computer and use it in GitHub Desktop.
Reconfigures the wp_mail() function with SMTP.
<?php
/*
Plugin Name: WP Mail Proxy
Version: 1.0.0
Description: Reconfigures the wp_mail() function.
Author: Gabriele Diener
Author URI: https://gdiener.com
*/
/**
* ADD THESE IN wp-config.php
*
* define('MAIL_PROXY_FROM', 'g.diener@me.com');
* define('MAIL_PROXY_FROM_NAME', 'Gabriele Diener');
* define('MAIL_PROXY_MAILER', 'smtp'); // Possible values 'smtp', 'mail', or 'sendmail'
* define('MAIL_PROXY_SET_RETURN_PATH', false); // Sets $phpmailer->Sender if true
* define('MAIL_PROXY_SMTP_HOST', 'localhost'); // The SMTP mail host
* define('MAIL_PROXY_SMTP_PORT', 25); // The SMTP server port number
* define('MAIL_PROXY_SSL', ''); // Possible values '', 'ssl', 'tls'
* define('MAIL_PROXY_SMTP_USER', 'username'); // SMTP authentication username
* define('MAIL_PROXY_SMTP_PASS', 'password'); // SMTP authentication password
**/
add_action('phpmailer_init', function(&$phpmailer) {
!defined('MAIL_PROXY_MAILER') ?: $phpmailer->Mailer = MAIL_PROXY_MAILER;
if (defined('MAIL_PROXY_SET_RETURN_PATH') && MAIL_PROXY_SET_RETURN_PATH) {
$phpmailer->Sender = $phpmailer->From;
}
if (defined('MAIL_PROXY_MAILER') && MAIL_PROXY_MAILER == 'smtp') {
!defined('MAIL_PROXY_SSL') ?: $phpmailer->SMTPSecure = MAIL_PROXY_SSL;
!defined('MAIL_PROXY_SMTP_HOST') ?: $phpmailer->Host = MAIL_PROXY_SMTP_HOST;
!defined('MAIL_PROXY_SMTP_PORT') ?: $phpmailer->Port = MAIL_PROXY_SMTP_PORT;
if (defined('MAIL_PROXY_SMTP_USER') && defined('MAIL_PROXY_SMTP_PASS')) {
$phpmailer->SMTPAuth = true;
$phpmailer->Username = MAIL_PROXY_SMTP_USER;
$phpmailer->Password = MAIL_PROXY_SMTP_PASS;
}
}
$phpmailer = apply_filters('wp_mail_proxy_options', $phpmailer);
});
add_filter('wp_mail_from', function($orig) {
return (defined('MAIL_PROXY_FROM')) ? MAIL_PROXY_FROM : $orig;
});
add_filter('wp_mail_from_name', function($orig) {
return (defined('MAIL_PROXY_FROM_NAME')) ? MAIL_PROXY_FROM_NAME : $orig;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment