The Simple Mail Transfer Protocol (SMTP) is an application layer protocol which is used for electronic mail transmission. There are many third party SMTP service providers are available.

List of third party SMTP service providers

  • SendLayer – It offers free trial that allows you to send up to 200 emails.
  • Brevo – Brevo has forever free plan with 300 emails per day.
  • Mailgun – Mailgun offers “pay as you go” plan with 5000 free emails in 30 day trial.
  • SMTP.com – SMTP.com offers 30 day free trial which allow you to send 50,000 emails.
  • Amazon SES – AWS offers 62,000 emails/month in their free trial offer for websites hosted on Amazon Web Services (AWS).

It is also possible to use self hosted email server. There are many benefits of using self-hosted email server. Some of the benefits are it is always free, completely secure, 100% privacy, no limit on sending emails, total control over mail server, etc.

List of software’s used for self hosted mail server

  • Dovecot – Initial release in 2002. Completely written in C language.
  • Apache James – It is written in Java.
  • Mail-in-a-Box – Mail in a Box is written in Python language and it was launched in 2013.

After setting up SMTP account we have to note down following details to setup SMTP in WordPress.

  • SMTP Host – smtp.example.com
  • SMTP Port – 25, 465 or 587
  • Username – Username used for SMTP account
  • Password – Your secret password

You will get above information from your smtp service provider or from your self hosted mail server.

WordPress uses the PHPMailer library for setting up SMTP. The wp_mail function of WordPress uses the phpmailer_init action to set up SMTP before sending email. So we have to use this action hook to setup SMTP in WordPress.

Note that phpmailer_init hook works only with wp_mail function which is wrapper function for PHP’s mail function. Directly using PHP’s mail method will require different steps to setup SMTP. It is recommended to use wp_mail method for sending emails in WordPress.

Setting up SMTP

To configure SMTP on WordPress paste following code in theme’s functions.php file or use WP Code plugin to insert following code snippet.

Make sure to replace username, password and other details with your valid SMTP details.

add_action( 'phpmailer_init', 'setup_wp_smtp' );

// This function receives PHPMailer instance as an argument
function setup_wp_smtp( $phpmailer ) {
    $phpmailer->isSMTP();                 // Send mail using SMTP
    $phpmailer->Host = 'smtp.gmail.com';  // Set the google SMTP server to send emails
    $phpmailer->Port = 587;               // Set to 587 for using more secure TLS protocol
    $phpmailer->SMTPAuth = true;          // Enable SMTP authentication
    $phpmailer->Username = 'username';    // SMTP username
    $phpmailer->Password = 'secret';      // SMTP password

    // Optional settings
    $phpmailer->From = 'admin@diversifyindia.in';
    $phpmailer->FromName = 'Admin';
}

How to send email using wp_mail

We can use wp mail function in our contact form or newsletter form to get notification about new subscribers or messages, read our article to create forms in WordPress without plugin. You can also create a html form with following fields on admin side for sending important emails to your clients or subscribers.

  • From Email – e.g. admin@diversifyindia.in
  • Receivers email – e.g. client@gmail.com
  • Cc emails
  • Bcc emails
  • Subject
  • Message

Use data collected from above fields to send email using wp_mail function

if(isset($_POST["submit"])) {
    $email = $_POST['sender-email'];
    $receiver = $_POST['receiver-email'];
    $cc = $_POST['cc-email'];
    $bcc = $_POST['bcc-email'];
    $subject = $_POST['email-subject'];
    $message = $_POST['message'];

    $headers = array(
        "From: {$email}",
        "Cc: {$cc}",
        "Bcc: {$bcc}"
    );

    wp_mail( array( $receiver ), $subject, $message, $headers );
}

Setup SMTP using plugin

On WordPress there are many plugins available to setup SMTP for sending email, following are the most popular plugins to setup SMTP on WordPress website.

  • Easy WP SMTP – Easy WP SMTP is a popular plugin developed by SendLayer. It has email logging functionality in pro feature. This plugin is around 5mb in size.
  • WP Mail SMTP – It is an easy to setup popular SMTP plugin for WordPress. It has email logging functionality as a paid feature. This plugin is around 6mb in size.
  • WordPress Simple SMTP – It is lightweight, easy to setup SMTP plugin. Less than 150kb in size. Provide essential functionality to setup SMTP on WordPress. Completely free.

Test smtp settings

Test your smtp configurations on localhost using WPOven free smtp server for testing.

Conclusion

In this article we learn how to configure SMTP in WordPress with and without using plugin also we learn how to use WordPress mail function to send emails. You can read our FAQ section to fix most commonly face problems while sending emails using SMTP on WordPress website. It is recommended to not use any plugin for configuring SMTP on WordPress.

Frequently Asked Questions

How to fix error “Your site may not be correctly configured to send emails.” when click on lost your password link ?

Make sure you entered correct information while configuring SMTP. Most common issue is incorrect sender email id. Make sure that sender email id should contain your websites domain. e.g. admin@gmail.com is incorrect sender email id and admin@mydomain.com is correct sender email id.

Is it compulsory to define SMTP settings in wp-config.php ?

No. It is not compulsory to define SMTP settings in wp-config.php. You can store smtp settings in database using update_option function of WordPress and retrieve this data inside phpmailer_init action hook using get_option function.

Which is the best free SMTP service provider ?

Brevo is the best free SMTP service provider. Brevo has forever free plan with 300 emails/day. Alternatively we can self host smtp server using tools like Dovecot, Apache James or Mail-in-a-Box. Self hosted mail servers are always free without any limit on number of emails to be send.

How to reset WordPress password if SMTP is not setup or wp mail is not working ?

In this case we have to change password by login to database using phpMyAdmin or mysql -u username -p command. After login go to wp_users table and add new password for the user or use sql query

UPDATE wp_users SET user_pass = '$P$B8li7QFlfvxZfHQoEjfApQtv0W.h/N1' WHERE wp_users.ID = 1;

This string ‘$P$B8li7QFlfvxZfHQoEjfApQtv0W.h/N1’ is a hashed version of password admin. Now you can login using password admin, after login don’t forget to change your password to a strong password.