|
<?php |
|
|
|
/** |
|
* Configure WP's PHP mailer to send emails from a SMTP account. |
|
* Intended to be used as callback for action `phpmailer_init`. |
|
* @see https://codex.wordpress.org/Plugin_API/Action_Reference/phpmailer_init |
|
* @param PHPMailer &$phpmailer The PHPMailer instance, passed by reference. |
|
* @version 0.2.1 |
|
*/ |
|
function pfx_wp_email_config( $phpmailer ){ |
|
|
|
# v v v v TODO: update these settings for your site v v v v v v |
|
$opts = array( |
|
|
|
# Required - Email account credentials. |
|
'username' => '[email protected]', |
|
'password' => 'TODO', |
|
|
|
# Custom from/reply names and addresses (optional). |
|
'from' => '', # Default: WP admin email |
|
'fromname' => '', # Default: "{HTTP_HOST}"; Alt: get_option('blogname') |
|
'replyto' => '', # Default: WP admin email |
|
'replytoname' => '', # Default: "Support ({`fromname`})" |
|
|
|
# SMTP settings. Defaults to Gmail configuration. |
|
'host' => '', |
|
'smtpsecure' => '', |
|
'port' => '', |
|
'smtpauth' => '', |
|
|
|
# Uncomment to activate PHPMailer debugging. |
|
#'debug' => true |
|
); |
|
# ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ |
|
|
|
// Default settings. |
|
$opts = array_merge( array( |
|
'from' => $opts->username, |
|
'fromname' => $_SERVER['HTTP_HOST'], |
|
'replyto' => get_option( 'admin_email' ), |
|
'host' => 'smtp.gmail.com', |
|
'smtpsecure' => 'ssl', |
|
'port' => '465', |
|
'smtpauth' => true, |
|
'debug' => false |
|
|
|
), array_filter( $opts ) ); |
|
|
|
// Use from* values as defaults for empty relpyto* settings and viceversa. |
|
if( empty($opts['replyto']) ) $opts['replyto'] = $opts['from']; |
|
elseif( empty($opts['from']) ) $opts['from'] = $opts['replyto']; |
|
if( empty($opts['replytoname']) ) $opts['replytoname'] = 'Support ('.$opts['fromname'].')'; |
|
elseif( empty($opts['fromname']) ) $opts['fromname'] = $opts['replytoname']; |
|
|
|
// Apply settings. |
|
$phpmailer->Mailer = "smtp"; |
|
$phpmailer->From = $opts["from"]; |
|
$phpmailer->FromName = $opts["fromname"]; |
|
$phpmailer->Sender = $phpmailer->From; //Return-Path |
|
$phpmailer->AddReplyTo( $opts['replyto'], $opts['replytoname'] ); //Reply-To |
|
$phpmailer->Host = $opts["host"]; |
|
$phpmailer->SMTPSecure = $opts["smtpsecure"]; |
|
$phpmailer->Port = $opts["port"]; |
|
if( $phpmailer->SMTPAuth = !!$opts["smtpauth"] ){ |
|
$phpmailer->Username = $opts["username"]; |
|
$phpmailer->Password = $opts["password"]; |
|
} |
|
if( $opts["debug"] ){ |
|
$phpmailer->SMTPDebug = 2; |
|
} |
|
} |
|
add_action( 'phpmailer_init', 'pfx_wp_email_config' ); |
|
|