One of the most common ways of sending out emails in PHP projects is through the mail() function.
However, according to the official documentation, PHP mail() is not suitable for larger volumes. Here is what it states:
It is worth noting that the mail() function is not suitable for larger volumes of email in a loop. This function opens and closes an SMTP socket for each email, which is not very efficient.
As you can see, the PHP itself discourages the use of mail() function for sending out a huge amount of emails. This encourages PHP developers to explore other options.
However, by default, the WordPress also uses PHP mail() function to send out the transactional emails such as user registration and forgot password emails.
Table of Contents
Why WordPress Emails Get Marked as Spam?
You might observe the emails from your WordPress website landing in Spam/Junk folders. In most of the cases, it happens if you are hosted on a shared WordPress hosting provider where every user shares the same server IP address. Now, if any ONE of the users sharing the IP block is marked as Spam, the entire server will be marked as spam.
Thus, no matter what, every email originating from this server will be detected and marked as spam email by the email service providers such as Gmail, Outlook, Yahoo, etc.
If your customers/subscribers are not receiving the emails, ask them to check out the Spam/Junk folder. If that’s the case, the most likely cause is that the server IP is marked as spam or the server is not sending out the emails. Another important cause is that the respective port is blocked by the hosting provider.
If your WordPress emails are marked as spam and/or the subscribers aren’t receiving email, this guide is specifically for you.
Introducing SMTP for WordPress
SMTP stands for Simple Mail Transfer Protocol. It is a service used to send out emails from the server to the final recipients or to intermediary server in between.
A number of WordPress SMTP plugins allow you to configure and send all outgoing emails via an SMTP service provider. This will prevent your emails from going into the Spam/Junk folder of the recipients.
As far as WordPress is concerned, I believe that SMTP is a good alternative to PHP mail function. Once configured properly, it will send all outgoing emails via SMTP server ensuring that the email appears to originate from an authorized source.
WordPress SMTP Plugin
First of all, you need to choose which WordPress SMTP plugin you want to use. I have written a detailed guide on Post SMTP mailer earlier, and this time I will use another plugin known as WP Mail SMTP by WPForms.
After you have installed and activated the plugin, you will see a new label titled WP Mail SMTP under the Settings menu.
There are just three steps to set up the WP Mail SMTP plugin. Let’s cover them by one!
Enter Email Address
Generally, businesses prefer to send out emails from the official address, it is important to change this address from the default address, usually firstname.lastname@example.org.
For this enter the email address you wish to use for your outgoing emails and the name which receipts will receive the emails from.
Note that several plugins might conflict with the WP Mail SMTP plugin while choosing the ‘from’ email and name. To avoid this, check the Force From Email and Force From Name checkboxes.
Set up Mailer
The next step is to set up the SMTP service provider. In this example, I will offer a simple guide to set up:
- Gmail SMTP for WordPress
- SendGrid SMTP for WordPress
- MailGun SMTP for WordPress
If your email provider is not on the above list, you don’t need to worry. The steps for setting up your SMTP provider are generally the same.
Configure Gmail SMTP on WordPress
Before setting up the SMTP on Gmail, you need to read these precautions carefully.
- If you have enabled two-factor authentication on your Gmail account. You need to generate a special password and note it down as it will be required for different password fields. Follow Sign in using App Passwords to generate an app password.
- If you have not enabled two-factor authentication, you just need to allow access to less secure apps. Just toggle the Turn On button.
- You need to authorize your server to access your Gmail account. If you have merged more than one account on Gmail → log in with the main account → allow access to your Google account and click on the Continue button.
The next step involves the configuration of Gmail. Log in to the Google developer console with your Gmail ID, create a project and enable the Gmail API for your WordPress site.
Click Go to credentials to get the credentials required for setting up your Gmail SMTP on WordPress.
Choose the options as shown below:
Click What credentials do I need? button to proceed to the final step towards getting your client ID.
- For the Name field, provide a name of your choice
- For Authorized redirect URL, copy the URL from WP Mail SMTP setting page.
Now click Create client ID and on the next page. Check your email address and the Product name to verify your details and then click Continue.
Click on I’ll do this later and on the next screen, click Edit to reveal Client ID and Client Secret.
Copy these and go back to the WP Mail SMTP plugin dashboard and paste it under Gmail settings.
Click Save Settings button. Next, I will authorize the WP Mail SMTP plugin to use my Gmail account for sending out emails.
While authorizing, you may get a warning because my domain name does not has an SSL certificate. To avoid this, you need to add an SSL certificate to your WordPress site.
Once allowed, navigate to Email Test tab at the top to test your newly configured Gmail SMTP email.
Send Test Email
Once successfully installed and configured Gmail SMTP on WordPress, I will now move ahead to the testing stage. Let’s see how the results look like. Simply, enter an email and hit the Send Test button.
Here’s how the test email will look like:
And that’s it! But before I move further, let me warn you that Gmail (the free version) restricts you to only 500 emails or 500 recipients per day.
If you want more, then try out premium SMTP providers that offer fewer limitations. The steps are similar to those described above, and you just need to configure the settings as per the SMTP provider.
Configure Mailgun SMTP for WordPress
Mailgun is an email automation service owned by Rackspace. Mailgun is considered one of the best WordPress SMTP providers. It allows you to send, receive, track and store emails easily, and is used to send newsletters, notifications, or anything you want to distribute among your subscribers.
Another advantage of Mailgun is that it allows you to send 10,000 emails per month for FREE. If you need more emails, they have an excellent pricing model.
First of all, you need to have an account with Mailgun. Once created, log in to your Mailgun account, and visit the Mailgun dashboard. Since I want to use my domain name for sending emails, I need to add my domain name to Mailgun. From the top menu bar, go to Domains and click on Add New Domain.
Once added, Mailgun recommends verifying the domain name to ensure smooth processes. Here is an excellent guide on how you can verify your domain for Mailgun. If you have any questions, ping your DNS register for help.
Here are the few reasons, why you need to verify the domain.
- Prove that you are an authorized sender for the domain.
- Verified domains are not subject to the limit of 300 outgoing emails per day.
- No more sent via Mailgun.org footer text in your emails.
- Mailgun is less suspicious of traffic that is being sent by verified domains.
I assume that you have followed the guide and verified the domain name. Click the domain name and you will be redirected to the Domain Information tab.
Copy the API Key and paste it into the WP Mail SMTP plugin. To enter the correct domain name, log in to Mailgun account and copy the exact domain name.
Don’t forget to click Save Settings at the bottom of the page. Now, navigate to Email Test tab at the top and send a test email to your email address. You should get an email like:
Note: I have not verified my domain name but still I get the email. I would suggest you get your domain verified to get advantages Mailgun offers to a verified domain.
Configure SendGrid SMTP on WordPress
Just like Mailgun, SendGrid is a cloud-based SMTP service provider that allows you to send and receive bulk emails. SendGrid is ideal for marketing campaigns and transactional emails without worrying about the emails being marked as spam.
Here’s a trivia that highlights the importance of SendGrid: Over 58,000 paying customers use SendGrid to send more than 35 billion emails every month.
SendGrid also offers a free to try plan known as Essentials that includes 40,000 emails, free for 30 days and send 100 emails/day for free forever.
First thing first, you need to create an account with SendGrid.
Important Note: Sign up with the same email address you’d like to set the From Email. These two emails will need to match in the later steps.
I assume, you have created an account with SendGrid and are logged into the SendGrid dashboard. In the left pane, open the drop-down menu and get into the Setup Guide. Click Start where it says Integrate using our Web API or SMTP Relay.
Choose SMTP Relay from the available options.
On the next screen, SendGrid will ask the name of your API key. Name it anything you want, and click Create Key.
Copy and paste the above key in WP Mail SMTP plugin and click Save Settings.
Go to the Email Test tab, enter the email address where you want to receive the test email. And, you should get the test email, similar to the following:
Get back to the SendGrid dashboard, check the highlighted box (see the following screenshot), and click the Next: Verify Integration button.
On the next screen, click on Verify Integration and you will get a success message.
Verify Domain & Email Links
Again, just like Mailgun, SendGrid also recommends verifying the domain names as well as Email links to ensure the delivery of your emails. In SendGrid, they are called as Whitelabeling.
Setup SMTP Service on Server
In many cases, users often have several WordPress applications and want to use the same WordPress SMTP service for all of them. In this case, the ideal solution is to set up SMTP on the server instead of setting it up on each WordPress site separately.
If you are a Cloudways customer, you can follow these steps to set up Gmail SMTP and/or any custom SMTP service provider.
In my example, I will use Gmail SMTP as my SMTP service provider that will be set up on the server. As mentioned earlier, before setting up a Gmail SMTP on the server, you need to read these precautions carefully.
- If you have enabled Two-Factor authentication on your Gmail account. You need to generate a special password and note it down as it will be required for different password fields.
- If you have not enabled the Two-Factor authentication, then you just need to allow access to less secure apps from here. Just click on Turn On button.
- You need to authorize your server to access your Gmail account. If you have merged more than one account on Gmail, Login with the main account and open this link, and click on the Continue button.
So without further ado, let’s set up Gmail SMTP on our server.
First of all, you need to authorize your server to access your Gmail account. If you have merged more than one account on Gmail, Login with the main account and open this link, and click on Continue.
Configure SMTP on Your Cloudways Server
Login to your Cloudways account, and click on the Servers tab located in the left corner of the top admin bar and get into your server. From the left pane, go to the SMTP tab, and select Your Own SMTP option from the drop-down list.
You will be asked to select the SMTP provider. In this example, I will use Gmail SMTP which is not on the list. I will select Other to connect my applications with the server. My configurations are:
- Host: smtp.gmail.com
- Port: 587
- Username: My Gmail Email ID
- Password: My Gmail Password (or App Generated Password)
By following these steps, you can configure any third party SMTP service provider.
Why Should You set up SMTP Separately for WordPress?
Since all the applications on the server use the default SMTP setting, you might need to set up separate email configurations to avoid performance issues.
For this, you can use any WordPress SMTP plugin and configure it to override the default SMTP settings and send emails using these customized settings.
We are now done with setting up the SMTP successfully to create an active communication channel between SMTP provider and all applications.
If you have any query or get stuck at any step, feel free to comment below. I would like to help you out!