How to set up WordPress SMTP without a plugin

Setting up WordPress without a plugin requires a little technical understanding, but provides several advantages over installing a plugin:

  • One less plugin in what might be a long list of them
  • Sensitive SMTP credentials are stored in a safe location – the wp-config.php file
  • It just feels elegant

Of course, there are certain disadvantages to avoiding using a mailer plugin. For example a well-designed plugin might offer useful features such as log retention, being able to use a backup mail server, or being able to connect to mail services’ APIs instead of SMTP ports. If those are the kind of features you might need, check out our roundup of WordPress mailer plugins.

Here’s how to do it

WordPress SMTP without a plugin involves setting up code in two locations:

1. Wherever you store your code snippets

Add the script below to wherever you store code snippets. If you’re not sure where this is, see Beginner-friendly ways to add custom code to WordPress.

add_action( 'phpmailer_init', 'send_smtp_email' );
function send_smtp_email( $phpmailer ) {
	$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;

This code snippet tells WP’s internal PHP mail library, phpmailer, where to get its credentials, that is, to go look up some constants (normally defined in WP config).

2. WordPress SMTP parameters in wp-config.php

Add the configuration values below to your site’s wp-config.php file somewhere BEFORE the constant ABSPATH is defined, replacing the placeholders appropriately.

define( 'SMTP_USER',   '' );    // Username to use for SMTP authentication
define( 'SMTP_PASS',   'smtp password' );       // Password to use for SMTP authentication
define( 'SMTP_HOST',   '' );    // The hostname of the mail server
define( 'SMTP_FROM',   '' ); // SMTP From email address
define( 'SMTP_NAME',   'e.g Website Name' );    // SMTP From name
define( 'SMTP_PORT',   '25' );                  // SMTP port number - e.g. 25, 465 or 587
define( 'SMTP_SECURE', 'tls' );                 // 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

These are simply the constants that are expected by the first code snippet. Placing SMTP configuration values in wp-config.php makes them universally accessible to any script in WordPress core.

The actual values you need to enter here will have been given to your by your email provider. For example, Microsoft provide their Outlook SMTP connection values, which look like this (see the “SMTP Settings” column):

Microsoft Outlook SMTP Settings

If our WordPress site used Microsoft Outlook as its SMTP email server, we would set our site’s wp-config.php values like this:

define( 'SMTP_USER',   '' );     // Our username on Microsoft Outlook
define( 'SMTP_PASS',   'Ms66o3wd|YmQs>k' );      // Our password on Microsoft Outlook
define( 'SMTP_HOST',   '' );   // The hostname of the mail server, provided above
define( 'SMTP_FROM',   '' ); // Sent emails will have this as their "From" email address
define( 'SMTP_NAME',   'Cool Digs Website' );    // Sent emails will have this as their "From" name
define( 'SMTP_PORT',   '587' );                  // SMTP port number, provided above
define( 'SMTP_SECURE', 'tls' );                  // Encryption system to use, provided above
define( 'SMTP_AUTH',    true );                  // Likely to always be 'true'
define( 'SMTP_DEBUG',   0 );                     // For debugging purposes only set to 1 or 2

That’s it! With these two snippets in place, your WordPress site is now able to send emails. If you want to verify this, check out the companion article Test if WordPress can send emails.

Questions? Post them below and we’ll update this article for clarity.

Leave a Reply

Your email address will not be published. Required fields are marked *