Simple Mail Transfer Protocol (SMTP) is the protocol used for reliably sending emails between servers. In this guide, we’ll cover how to set up SMTP in WordPress both with and without using plugins. WordPress uses the PHPMailer library for sending emails, which has built-in SMTP support.
Configuring SMTP in WordPress Without Plugin
There are two main methods for configuring WordPress SMTP settings without a plugin:
- Editing the
wp-config.php
file. - Creating a custom settings page to collect SMTP information.
Method 1: Configuring SMTP via wp-config.php
This approach is simpler but less secure. In this method, we add our SMTP details directly to the wp-config.php
file.
- Open the wp-config.php file: Find it in the root directory of your WordPress installation.
- Add the following code just above the line /* That’s all, stop editing! Happy blogging. */.
- Replace the PHP constant values with your SMTP provider’s information (e.g., Gmail, SendGrid, Mailgun).
Here’s an example setup using Gmail’s SMTP server:
// WordPress free Gmail SMTP server configuration
define( 'SMTP_HOST', 'smtp.gmail.com' ); // SMTP server for Gmail
define( 'SMTP_PORT', '587' ); // SMTP port number
define( 'SMTP_USERNAME', 'youremail@gmail.com' ); // Your email address
define( 'SMTP_PASSWORD', 'your-app-password' ); // App password for Gmail
// It is recommended to use a business email address for the "From" email field,
// e.g., admin@diversifyindia.in. However, Gmail SMTP does not support custom email addresses in the free plan.
define( 'SMTP_FROM', 'youremail@gmail.com' ); // 'From' email address
define( 'SMTP_NAME', 'Your Name or Business' ); // 'From' name
define( 'SMTP_SECURE', 'tls' ); // Encryption: 'ssl' or 'tls'
define( 'SMTP_AUTH', true ); // Enables SMTP authentication
Note: If you’re using Gmail SMTP server, you may need to create an app password. You can generate this by visiting https://myaccount.google.com/apppasswords.
Method 2: Creating a Custom Settings Page
If you’re comfortable coding, you can create a custom settings page in WordPress to gather SMTP configuration details from users. This approach requires additional coding and a more complex setup, but it allows users to input their SMTP information directly in the WordPress dashboard.
For more information, please refer to our article on creating a settings page for SMTP plugins.
Setting Up the phpmailer_init WordPress Hook
WordPress’s wp_mail
function uses the phpmailer_init
hook to set up SMTP before sending emails. This function will configure SMTP each time wp_mail
is used to send an email.
Here’s an example using Gmail’s SMTP settings:
// Configure SMTP with phpmailer_init
// This function receives a PHPMailer instance as an argument.
function setup_wp_smtp( $phpmailer ) {
$phpmailer->isSMTP(); // Send mail using SMTP
$phpmailer->Host = 'smtp.gmail.com'; // Gmail SMTP server
$phpmailer->Port = 587; // Use 587 for TLS
$phpmailer->SMTPAuth = true; // Enable SMTP authentication
$phpmailer->Username = 'youremail@gmail.com'; // Your Gmail email
$phpmailer->Password = 'your-app-password'; // Your Gmail app password
$phpmailer->SMTPSecure = 'tls'; // Secure connection
$phpmailer->From = 'youremail@gmail.com'; // From email address
$phpmailer->FromName = 'Your Name'; // From name
}
add_action( 'phpmailer_init', 'setup_wp_smtp' );
If you are storing your SMTP details in the wp-config.php
file, you can modify the setup_wp_smtp
function to use those constants instead, as shown below:
function setup_wp_smtp( $phpmailer ) {
$phpmailer->isSMTP();
$phpmailer->Host = SMTP_HOST;
$phpmailer->Port = SMTP_PORT;
$phpmailer->SMTPAuth = SMTP_AUTH;
$phpmailer->Username = SMTP_USERNAME;
$phpmailer->Password = SMTP_PASSWORD;
$phpmailer->SMTPSecure = SMTP_SECURE;
$phpmailer->From = SMTP_FROM;
$phpmailer->FromName = SMTP_NAME;
}
add_action( 'phpmailer_init', 'setup_wp_smtp' );
If you are gathering SMTP configuration information through a settings page and storing it in the WordPress database using the update_option
function, you can use the get_option
function to retrieve those settings. Here’s an example:
function setup_wp_smtp( $phpmailer ) {
$smtp_config = get_option( "smtp_config" );
$phpmailer->isSMTP();
$phpmailer->Host = $smtp_config['host'];
$phpmailer->Port = $smtp_config['port'];
$phpmailer->SMTPAuth = $smtp_config['auth'];
$phpmailer->Username = $smtp_config['username'];
$phpmailer->Password = $smtp_config['password'];
$phpmailer->SMTPSecure = $smtp_config['security_type'];
$phpmailer->From = $smtp_config['from_email'];
$phpmailer->FromName = $smtp_config['from_name'];
}
add_action( 'phpmailer_init', 'setup_wp_smtp' );
For more information on sending emails in WordPress, refer to our article on how to send emails using the wp_mail function.
Configuring SMTP in WordPress Using a Plugin
WordPress offers various plugins to set up SMTP for sending emails. Here are some of the most popular options:
- Easy WP SMTP: Developed by SendLayer, this popular plugin includes basic SMTP setup and offers email logging as a premium feature. The plugin size is around 5 MB.
- WP Mail SMTP: Known for being easy to set up, WP Mail SMTP is another popular choice. It also offers email logging as a paid feature, with a plugin size of approximately 6 MB.
- WordPress Simple SMTP: This lightweight plugin (less than 150 KB) provides essential SMTP functionality for WordPress. It’s easy to set up and completely free.
The following steps will guide you through setting up the WP Mail SMTP plugin, a popular option for configuring mail in WordPress.
- Install the WP Mail SMTP Plugin
- In your WordPress dashboard, go to Plugins > Add New Plugin.
- In the search bar, type WP Mail SMTP.
- Click Install Now, then Activate to enable the plugin.
- Configure WordPress SMTP Settings
- Go to WP Mail SMTP > Settings from your dashboard.
- From Email: The email address you want to send emails from.
- From Name: The name that will appear in the “From” field of your emails.
- Mailer: Select your SMTP service provider (e.g., Gmail, SendGrid). If your provider isn’t listed, select Other.
- Return Path: It’s recommended to check this box to match the return path with the “From” email, which helps you receive bounce notifications.
- SMTP Host: Enter your SMTP provider’s server (e.g.,
smtp.gmail.com
for Gmail). - SMTP Port: Enter the port based on your encryption choice, typically
465
for SSL or587
for TLS. - Encryption: Select SSL or TLS based on your provider’s instructions.
- SMTP Username: Enter your email address or SMTP username.
- SMTP Password: Enter your SMTP or email password.
- After filling in these details, click Save Settings to apply your configuration.
Third-Party SMTP Service Providers
There are many third-party SMTP providers available, each offering different features and pricing plans. Here are some popular options:
- SendLayer: Offers a free trial allowing up to 200 emails.
- Brevo: Provides a forever-free plan with a limit of 300 emails per day.
- Mailgun: Offers a “pay-as-you-go” plan with 5,000 free emails during a 30-day trial.
- SMTP.com: Includes a 30-day free trial, allowing up to 50,000 emails.
- Amazon SES: Part of AWS, providing 62,000 free emails per month for websites hosted on Amazon Web Services.
Self-Hosted Email Servers
Using a self-hosted email server provides several benefits, including complete control, unlimited emails, and enhanced privacy and security. Here are some popular options:
- Dovecot: Written in C, first released in 2002. It is primarily a mail delivery agent (MDA) and IMAP/POP3 server.
- Postfix: Written in C, first released in 1998. It is a widely used mail transfer agent (MTA) that routes and delivers email.
- Apache James: An open-source mail server written in Java.
- Mail-in-a-Box: Developed in Python, launched in 2013.
Testing SMTP Settings
To test your SMTP configurations on localhost, you can use the WPOven free SMTP server for testing purposes.
WP Mail SMTP also offers a “Test Email” option under Email Test in its settings, which can help you confirm the setup.
Conclusion
In this article, we learned how to configure SMTP in WordPress both with and without a plugin, as well as the popular plugins used for setting up SMTP. For troubleshooting, refer to our FAQ section to address common issues with SMTP email in WordPress.
Frequently Asked Questions
How to Fix the Error “Your site may not be correctly configured to send emails” When Clicking the “Lost Your Password” Link
Ensure that you entered the correct information while configuring SMTP. The most common issue is an incorrect sender email ID. Make sure the sender email matches your website’s domain. For example, admin@gmail.com
is incorrect, while admin@mydomain.com
is correct.
Is It Necessary to Define SMTP Settings in wp-config.php?
No, it’s not necessary to define SMTP settings in wp-config.php
. You can store SMTP settings in the database using the update_option
function in WordPress and retrieve them within the phpmailer_init
action hook using the get_option
function.
Which is the Best Free SMTP Service Provider?
Brevo is considered one of the best free SMTP service providers, offering a free plan with up to 300 emails per day. Alternatively, you can self-host an SMTP server using tools like Dovecot, Apache James, or Mail-in-a-Box. Self-hosted mail servers are always free and have no limits on the number of emails sent.
How to Reset Your WordPress Password if SMTP is Not Set Up or wp_mail is Not Working
If SMTP isn’t set up or wp_mail
is not working, you can reset your password by logging into the database through phpMyAdmin or using the mysql -u username -p
command. Once logged in, go to the wp_users
table and update the user’s password with this SQL query:
UPDATE wp_users SET user_pass = '$P$B8li7QFlfvxZfHQoEjfApQtv0W.h/N1' WHERE wp_users.ID = 1;
The string '$P$B8li7QFlfvxZfHQoEjfApQtv0W.h/N1'
is the hashed version of the password admin. You can now log in using admin as the password, but be sure to change it to a strong password after logging in.