Last Updated: February 25, 2016
·
50.4K
· isuruj

Sending emails from a WordPress site on localhost

NOTE: This post is directed to students who took the Ultimate WordPress Plugin Course course Udemy course. But it can be applied for anyone who's in a similar situation.


By the end of Lecture 24, we should be able to send an email to the user confirming his/her subscription. We use the WordPress' built-in wp_mail() function (which actually uses the PHP function mail() under the hood to send the emails).

If you're using a web server in localhost like XAMPP or MAMP, then email sending won't work. You need to take a new approach in order to get this working. So let's get in to it.

First you need to install a new WordPress plugin called WP Mail SMTP. This plugin reconfigures the wp_mail() function to use SMTP instead of mail().

So go ahead and install this plugin and activate it. Then go to the plugin's settings page. You can click the link below the plugin name in the Plugin list or from the sidebar, select Settings and then Email.

Then you'll see a page like this.

Leave the From Email and From Name fields blank. Select the Send all WordPress emails via SMTP. for the Mailer option.
Now in order to send all WordPress emails via a SMTP server, you need to change some settings here. I'm gonna use Gmail's SMTP settings which I have listed below. But you can use any other SMTP server settings.

  • SMTP Host: smtp.gmail.com
  • SMTP Port: 465
  • Encryption: Use SSL encryption.
  • Authentication: Yes: Use SMTP authentication.
  • Username: [your - entire - Gmail or Google Apps email address]
  • Password: [your password for your Gmail or Google Apps email account]

Then click Save Changes to save the settings.

Wait, you think you're done? Not so fast! You need to enable access for less secure apps in your Google account. Don't worry, since you know what you're giving access to. Once you're done with developing and testing on localhost, you can turn this setting back on.

Now you can send a test email to check if everything's working fine. In the Send a Test Email section below, enter an email of your own and click Send Test. You should get an email with the subject WP Mail SMTP: Test mail to [your-email@example.com]. It should work but in any case if it fails, try turning this Google account setting called Allow access to your Google account on as well.

Now open up the page in your WordPress site where you have the New subscription form. Enter a name and an email (make sure to enter an email you own) and click Sign me up button. You should get a success message.

And check your inbox. You should have received the email.

That's it. This is how you send emails when you're on localhost.

Happy coding!

2 Responses
Add your response

Even after following the above said approach i am not yet able to send the mail. It keep showing the popup unable to send mail. Subscription not saved, however strangely subscription getting saved without any subscription list added. Can you please help, let me know in case any input required from my side.

over 1 year ago ·

I am at the same point as P847765. When I test send the email form the WP Mail settings page it works but then I try to subscribe from the wordpress subscription page I get the message that the subscription was not saved, email not sent. The subscriber is created however, just with no lists subscribed to. I don't receive an email either so it appears the email really is not getting sent.

over 1 year ago ·