Skip to content

Instantly share code, notes, and snippets.

@mickythompson
Last active February 2, 2023 16:08
Show Gist options
  • Save mickythompson/f92509e6e97961591abe67c3adf7e2f0 to your computer and use it in GitHub Desktop.
Save mickythompson/f92509e6e97961591abe67c3adf7e2f0 to your computer and use it in GitHub Desktop.
PHPMailer SMTP Code for use with WordPress
<?php
// The code below will allow you to use SMTP in WordPress with no plugin needed.
//-------------
// Place this code in functions.php inside your theme, in a Custom Plugin (plugins) or a Must Use Plugin (mu-plugins).
// Configures SMTP authentication
add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Port = SMTP_PORT;
$phpmailer->Username = SMTP_USER;
$phpmailer->Password = SMTP_PASS;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
//-------------
// Place this code in wp-config.php
// Configures WordPress to use SMTP server
define( 'SMTP_USER', 'username' ); // Username to use for SMTP authentication
define( 'SMTP_PASS', 'password' ); // Password to use for SMTP authentication
define( 'SMTP_HOST', 'smtp.server.com' ); // The hostname of the mail server
define( 'SMTP_FROM', '[email protected]' ); // SMTP From email address
define( 'SMTP_NAME', 'Jane Doe' ); // SMTP From name
define( 'SMTP_PORT', '587' ); // SMTP port number - likely to be 25, 465 or 587
define( 'SMTP_SECURE', 'PHPMailer::ENCRYPTION_STARTTLS' ); // Encryption system to use - ssl or tls
define( 'SMTP_AUTH', true ); // Use SMTP authentication (true|false)
define( 'SMTP_DEBUG', 0 ); // for debugging purposes only set to 1 or 2
//-------------
/*
I was using the following settings in LocalWP but this caused the site to crash and stop responding.
I recommend you use PHPMailer::ENCRYPTION_STARTTLS and port 587 instead of tls and 25 when using PostMarkApp.com and LocalWP as
tls and port 25 didn't work longterm locally.
define( 'SMTP_PORT', '25' ); // SMTP port number - likely to be 25, 465 or 587
define( 'SMTP_SECURE', 'tls' ); // Encryption system to use - ssl or tls
*/
//-------------
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment