If you have installed WordPress on your Ubuntu instance, and choose to use a plugin such as Contact Form 7 (highly recommended), you might notice that your emails are not sending – because you have to enable the PHP mail() function on your system.
To do so, initialize puTTy or any other SSH client you use.
Step 1 – Install sendmail
The first thing you need to do is install the sendmail package on your system with the following command:
$ sudo apt-get install sendmail
Step 2 – Configure sendmail
Once the package installs, you need to configure it. To do so, run the following command:
$ sudo sendmailconfig
Choose Y
for every question to follow, unless you have your own configurations to apply.
Step 3 – Check host file
$ sudo nano /etc/hosts
Make sure your hosts file contains 127.0.0.1 localhost
If it does not, add it and Save.
Step 4 – Restart Apache
The server does not need to be restarted, but I like to do so for good measure. The PHP mail() function should be working now, and we can test it in the following step.
To restart Apache:
$ sudo service apache2 restart
To restart Nginx:
$ sudo service nginx restart
Step 5 – Test the mail() function
The final step is to test your mail configuration.
To test with command line:
$ php -a php > mail('you@example.com', "Test Subject", "Test message"); php > exit();
Note: If after running php -a
, you get Interactive mode enabled
message but no php >
prompt, then your PHP is not compiled with readline support.
If you run into that issue, you can create a testmail.php file with the following code inside:
<?php mail('you@example.com', "Test Subject", "Test message"); ?>
and run it with
$ php -f testmail.php
If you can’t see any email form PHP, then that means its PHP’s fault.
Check value of sendmail_path
in php.ini config by running
$ which sendmail
You should get back a path similar to /usr/sbin/sendmail
.
To test with WordPress
If PHP can send mails, but WordPress cannot, it must be an issue on WordPress’s end.
- If you have not already, install the WP Mail SMTP plugin.
- In the plugin, enter wordpress@yourdomain.com for the ‘From’ address, and your preferred ‘From’ name below it.
- Leave the Mailer setting at the default PHP, and save.
- Proceed to the Email Test tab and enter your email address.
If you receive your email, you are good to go.
If you get an error, check out the support forum for WP Mail SMTP to troubleshoot your issue!