Two Ways To Send WordPress Emails With SMTP

Whether you noticed it or not, your WordPress website sends quite a lot of emails. Obviously, it does when someone fills a contact form on your website. However, it sends a lot more emails, from the login confirmation to the account creation confirmation, password reset, or comment notification. As you can notice, it’s mostly related to notifications, and there can be many more depending on the plugins you are using.

Why You Should Send the WordPress Emails With SMTP

If you don’t change the default settings, WordPress will use the wp_mail() function to send emails. This can be problematic, especially for larger installs, because it highly increases the chance of your emails being blocked by most popular email providers.

In fact, using the wp_Mail() function to send email doesn’t allow for proper authentication of the email sender, which is often interpreted as spam by email servers. For that reason, setting up SMTP (Simple Mail Transfer Protocol) is the proper way to follow industry standards and increase the deliverability of your emails.

The Easy Way: Using A WordPress plugin

If you are a frequent WordPress user, you know that the solution often comes in the form of a plugin. In this case, it’s a plugin developed by the makers of the popular WP Forms plugin that will help us quickly set up our SMTP sending from WordPress.

Their WP Mail SMTP plugin, appropriately named, does exactly what its name suggests: it helps you send emails with SMTP for your WordPress install. This plugin resets the wp_mail() function to use SMTP instead of PHP to send emails. You can use your own web host’s SMTP settings to send the emails or go with an external provider, such as SendGrid or Gmail.

The plugin also comes as a paid Pro version, which extends the functionalities way further. It gives you access to email logs, to track email opens and clicks, to resend emails, and many other useful features. You can check the Pro version on the dedicated website.

In either case, you will just need to follow the plugin’s instructions and fill your SMTP settings in order to get started.

The Harder Way: Using A Custom Function

If you want to keep more control over your install and what’s going on in it, you can use these simple instructions to send emails with SMTP for your WordPress install.

You will only need to change two files.

wp-config.php

Unless you did a special type of install, your wp-config.php file should be at the root of your WordPress install. In this file, you should add the following code:

// Setting for sending emails with SMTP
define( 'SMTP_username', 'yourname@email.com' );  // username
define( 'SMTP_password', 'password' );   // password
define( 'SMTP_server', 'smtp.email.com' );     // SMTP server
define( 'SMTP_FROM', 'yourname@email.com' );   // "From" Email Address
define( 'SMTP_NAME', 'Your Name' );   // "From" Name
define( 'SMTP_PORT', '587' );     // Server Port Number, usually 25, 465 or 587
define( 'SMTP_SECURE', 'tls' );   // Encryption - ssl or tls
define( 'SMTP_AUTH', true );  // Use SMTP authentication (true or false)
define( 'SMTP_DEBUG',   0 );  // for debugging purposes only

As you can see, we are defining all the settings for SMTP emails sending by entering the server and login data.

functions.php (function file in your theme)

If there is not functions.php file in your theme, you should create a new one for this purpose. Most of the time, there will be one already. The best practice would be to use a child theme for this kind of change if you are using an external theme that you must update.

You should include the following code in this file:

add_action( 'phpmailer_init', 'my_phpmailer_smtp' );
function my_phpmailer_smtp( $phpmailer ) {
    $phpmailer->isSMTP();     
    $phpmailer->Host = SMTP_server;  
    $phpmailer->SMTPAuth = SMTP_AUTH;
    $phpmailer->Port = SMTP_PORT;
    $phpmailer->Username = SMTP_username;
    $phpmailer->Password = SMTP_password;
    $phpmailer->SMTPSecure = SMTP_SECURE;
    $phpmailer->From = SMTP_FROM;
    $phpmailer->FromName = SMTP_NAME;
}

In this function, we are overriding the wp_mail() function settings, so the emails are sent using the data provided in your wp-config.php file.

Categories: Tutorials