Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smhmic/4a8c74085a9d1721946cd99e96249da4 to your computer and use it in GitHub Desktop.
Save smhmic/4a8c74085a9d1721946cd99e96249da4 to your computer and use it in GitHub Desktop.
Configure WordPress built-in email system (phpmail) to send mail using an SMTP account (like Gmail, Yahoo, etc).
<?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' );

To use, customize the $opts array at the top of pfx_wp_email_config() and place the code into functions.php.

It's recommended to create a new email account for this, since this requires placing credentials in plaintext in the source code.

Includes default configuration for Gmail, but this can be used with any platform that uses SMTP.

CHANGELOG

  • v0.2.1 - 4/11/2012
    • Changed default fromname value to be current domain (was get_option('blogname')).
    • Allow from* values to default to replyto* values.
  • v0.2 - 2011
    • Separated user from default settings into separate array, so they're always available even after customizing for site.
    • Added comments.
  • v0.1 - 2011 - inital version
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment