Proper management of email in WordPress is very important for our website to run correctly. Not doing so means we lose the main form of communication with our users and visitors.
Mail management in WordPress
WordPress does not include an option to set up mail sending, it instead uses the functionalities included in the web server. The fact that the developers have gone for such a solution is not that surprising if we consider a couple of things.
Usually, mail is a functionality included in the domain, which means that the most logical solution would be to redirect mail to the corresponding servers. Considering the large number of mail service providers available, it really doesn't make much sense to complicate WordPress more by incorporating every configuration setting available. This is why I believe the developers made this choice.
What we must do is linking our web with a mail service adequate to our needs. If the volume of mail delivered is not that large we can just use an account from our own domain, on the contrary we'll have to hire services specialized in massive mail delivery such as MailGun or SendGrid.
Once we decide on what service we'll use, we'll set up our mail in Worpress. We're going to take a look at the two ways of doing so, by coding or by using a plugin.
Sending mail from WordPress without a plugin
Let's look at how to implement mail delivery with a SMTP protocol by adding a few lines of code in our functions.php. First we define a few necessary constants:
define( 'SMTP_USER', 'user@example.com' ); define( 'SMTP_PASS', 'password' ); define( 'SMTP_HOST', 'smtp.example.com' ); define( 'SMTP_FROM', 'website@example.com' ); define( 'SMTP_NAME', 'Name of the site' ); define( 'SMTP_PORT', '25' ); define( 'SMTP_SECURE', 'tls' ); define( 'SMTP_AUTH', true ); define( 'SMTP_DEBUG', 0 );
SMTP_USER, SMTP_PASS and SMTP_HOST are the corresponding values to username, password and the mail server address that we're going to use.
SMTP_PORT, SMTP_SECURE and SMTP_AUTH corresponds to the port used (commonly 25, 465 or 587), the encription (TLS or SSL) and if authentication is obligatory (true) or not (false).
These values are obtained from our mail sever, they're the same ones we use to set up our account in any other email client.
SMTP_FROM and SMTP_NAME correspond to the data the users will see: the set up email address and the name of the sender. In that address we'll recieve the answers from our users.
SMTP_DEBUG controls the registry (0 - disabled, 1 - registers the data we send, 2 - includes answers from the server in the registry, 3 - adds information on the establishment of the first connection) of the communication. This info can be found in the error registry of our server.
The next step is to setup the wp_mail() function to make it use our values.
add_action( 'phpmailer_init', 'send_smtp_email' ); function send_smtp_email( $phpmailer ) { $phpmailer->isSMTP(); $phpmailer->Host = SMTP_HOST; $phpmailer->SMTPAuth = SMTP_AUTH; $phpmailer->Port = SMTP_PORT; $phpmailer->Username = SMTP_USER; $phpmailer->Password = SMTP_PASS; $phpmailer->SMTPSecure = SMTP_SECURE; $phpmailer->From = SMTP_FROM; $phpmailer->FromName = SMTP_NAME; }
By adding these lines of code, we'll have our mail in WordPress properly set up to use the account we've chosen in our server.
Using a plugin to send mail from WordPress
There are many plugins for this task. I'm going to focus on Easy WP SMTP, a simple plugin with over 200,000 installations which does its job perfectly.
Download it from the official repository and enable it. We can set it up in Settings.
In SMTP Settings we find the same parameters we've previously seen Additional Settings allows us to enable the error registry when necessary, it's recommended to leave the rest of the values on default. Save the setup and send a test mail to verify that everything is working fine.
If our website is running on Google Cloud, we won't be able to use SMTP, at least not that easily. The reason being that Google has blocked the usual mail server ports in their instances. This leaves us with two possible solutions:
- Change the port in our mail server to a non-standard one, although not every one is allowed.
- Use a GMail or GSuite account. For this I recommend using the GMail SMTP plugin. Let's look at how do we use it.
Download and enale GMail SMTP, we'll find its configuration in Settings.
First we need to register our server with GMail, to do this we access console.developers.google.com with the credentials of the account we're going to use. Create a new project or choose an already existing one.
If we haven't done so already, enable the GMail API in Library and later create the proper credentials for our web by clicking on the corresponding buttons. Select OAuth as the ID client in the list.
The next step is to select Web, choose an arbitrary name, put our website and paste the url generated by the plugin in its configuration screen.
Click Create and obtain the Client ID and the Secret ID we'll paste on the plugin screen. As OAuth Email Address we'll use our GMail address. Finish the setup with the mail address, the name we want our users to see and port 587, since GMail uses TLS.
Save the changes, a new button will appear: Grant Permissions. Click on it and we'll be presented with a confirmation request from Google. Accept it and, if everything went well, we'll have our web connected to GMail. We can check on the plugin's setup status, which should turn green.
Finally, we'll send ourselves a test mail to verify everything is working as intended..
Conclusions
We've seen many different ways of setting up mail in WordPress. The methods presented share a property in common: they preserve the wp_mail() function. This way we'll reduce compatibility issues to a minimum, while filters and actions are still available at our disposal.
It's essential to use a properly registered mail service. This will prevent the destined server from denying our messages and will save us from a lot of headaches. We can implement our own mail server, but management is not an easy task, I know this based on prior experiences.
We also need to account for the limits of our mail server. If we deliver massive volumes of mail (newsletters, marketing mail, etc.) it's likely that we'll have to resort tocompanie specialized in these services.