How to Secure SMTP Settings by Using Constants

Would you like to avoid adding SMTP settings directly to your WordPress admin area? On most sites, email deliverability is critical functionality. But when settings are added to the WordPress admin area, any administrator on the site can see and edit those details (and the values get stored to the site’s database, as well).

In this tutorial, we’ll show you how to avoid adding WP Mail SMTP settings to your WordPress admin area by setting up constants.

  1. Enabling Constants in WordPress
  2. Adding Constants for WP Mail SMTP
  3. Confirming Your Constants

1. Enabling Constants in WordPress

To get started, you’ll need to open your site’s wp-config.php file. If you’re not sure how to locate and edit this file, be sure to check out WPBeginner’s guide.

Once this file is open, scroll down. Look for the line that reads /* That's all, stop editing! Happy publishing. */, and be sure to add any new code above that.

Note: If you feel at all unsure of where to add this code within wp-config.php, then be sure to add it to the top of this file. This will help ensure that the code can be used by your site.

Here’s the first line of code you’ll need to add:

define( 'WPMS_ON', true ); // True turns on constants support and usage, false turns it off.

As described in the code comment, this will enable the usage of WP Mail SMTP constants on your site.

2. Adding Constants for WP Mail SMTP

Now that you’ve enabled constants for WP Mail SMTP, the next step is to add the code for any constants you want to use.

Below, you’ll find the code needed to create a constant for each SMTP value. Go ahead and copy the lines of code you need into your site’s wp-config.php file. After adding them to your file, be sure to check that all code values match what you need on your site.

General Settings

Everything in this section can be used regardless of mailer (not specific to any one mailer option).

define( 'WPMS_LICENSE_KEY', '' ); 
define( 'WPMS_MAIL_FROM', '[email protected]' );
define( 'WPMS_MAIL_FROM_FORCE', true ); // True turns it on, false turns it off.
define( 'WPMS_MAIL_FROM_NAME', 'Example Name' );
define( 'WPMS_MAIL_FROM_NAME_FORCE', true ); // True turns it on, false turns it off.
define( 'WPMS_MAILER', 'smtp' ); // Possible values: 'mail', 'gmail', 'mailgun', 'sendgrid', 'smtp'.
define( 'WPMS_SET_RETURN_PATH', true ); // Sets $phpmailer->Sender if true.
define( 'WPMS_DO_NOT_SEND', true ); // Possible values: true, false.
Pepipost Mailer
define( 'WPMS_MAILER', 'pepipostapi' );
define( 'WPMS_PEPIPOST_API_KEY', '' );
Sendinblue Mailer
define( 'WPMS_MAILER', 'sendinblue' );
define( 'WPMS_SENDINBLUE_API_KEY', '' );
Mailgun Mailer
define( 'WPMS_MAILGUN_API_KEY', '' );
define( 'WPMS_MAILGUN_DOMAIN', '' );
define( 'WPMS_MAILGUN_REGION', 'US' ); // Change to 'EU' for Europe.
SendGrid Mailer
define( 'WPMS_SENDGRID_API_KEY', '' );
Amazon SES
define( 'WPMS_MAILER', 'amazonses' );
define( 'WPMS_AMAZONSES_CLIENT_ID', '' );
define( 'WPMS_AMAZONSES_CLIENT_SECRET', '' );
define( 'WPMS_AMAZONSES_REGION', '' ); // Possible values for region: 'email.us-east-1.amazonaws.com', 'email.us-west-2.amazonaws.com', 'email.eu-west-1.amazonaws.com'.
Google Mailer
define( 'WPMS_GMAIL_CLIENT_ID', '' );
define( 'WPMS_GMAIL_CLIENT_SECRET', '' );
Outlook Mailer
define( 'WPMS_MAILER', 'outlook' );
define( 'WPMS_OUTLOOK_CLIENT_ID', '' );
define( 'WPMS_OUTLOOK_CLIENT_SECRET', '' );
Other SMTP Mailer
define( 'WPMS_SMTP_HOST', 'example' ); // The SMTP mail host.
define( 'WPMS_SMTP_PORT', 587 ); // The SMTP server port number.
define( 'WPMS_SSL', '' ); // Possible values '', 'ssl', 'tls' - note TLS is not STARTTLS.
define( 'WPMS_SMTP_AUTH', true ); // True turns it on, false turns it off.
define( 'WPMS_SMTP_USER', 'username' ); // SMTP authentication username, only used if WPMS_SMTP_AUTH is true.
define( 'WPMS_SMTP_PASS', 'password' ); // SMTP authentication password, only used if WPMS_SMTP_AUTH is true.
define( 'WPMS_SMTP_AUTOTLS', true ); // True turns it on, false turns it off.

Once you’ve copied the code you’d like to use, you’ll need to add the details that are relevant to your specific site configuration.

Note: If you aren’t sure what values are needed for your site, then be sure to check out the tutorial for the specific mailer you’ve chosen. You can find links to all of our mailer tutorials in this guide.

After your code is set up, make sure that you save the file.

3. Confirming Your Constants

The last step is to make sure your constants are working. To do this, you’ll need to open your WordPress admin area and go to WP Mail SMTP ยป Settings. If you check the settings here, you should see that any fields using constants are disabled (they’ll appear slightly greyed out).

Fields using constants will be disabled in the WordPress admin area

That’s it! You can now add your WP Mail SMTP settings to your site’s config file instead of the admin area to make them more secure.

Next, would you like to keep tabs on all emails sent out from your WordPress site? WP Mail SMTP’s email logging option lets you see all emails that have been sent, as well as whether they were able to send successfully.