How to send an email using SMTP in localhost with WAMP
Home » BLOG » Web development » How to send an email using SMTP in localhost with WAMP

How to send an email using SMTP in localhost with WAMP

category:  Web development

When I develop the email function on my localhost, I like to set up a Gmail account for sending an email from my localhost. It is convenient and easy to set up. Today, I will share how to set up a Gmail account to send an email from your WAMP server

Note, that Sendmail works for Windows only. 

Download sendmail.zip

Sendmail.exe is a simple Windows console application that emulates sendmail’s “-t” option to deliver emails piped via stdin. It is intended to ease running Unix code that has <code>/usr/lib/sendmail</code> hardcode as an email delivery means. It doesn’t support deferred delivery and requires an SMTP server to perform the actual delivery of the messages. You can download the Sendmail package from the line below.
download sendmail.zip

Install Sendmail

  • Extract the sendmail.zip then you will see the sendmail folder
  • Copy and paste the sendmail folder at “C:\wamp64\” location where your Wampserver is installed. In my case, I installed Wampserver in C:\wamp64.
  • Inside the sendmail folder, find the sendmail.ini file and edit it with the following setting. As I mentioned earlier, I will use the Gmail account for the mail server setting.
smtp_server=smtp.gmail.com
smtp_port=587
auth_username=your_username
auth_password=your_password
  • Save and close the file

2-factor authentication on Google Account issue (Solved)

Important note, Google recently forced you to enable “2-factor authentication” on your Google account. If your Google account has already enabled “2-factor authentication”, you can not use the regular password of your account for setting in the sendmail.ini. Instead, you need to generate an app-specific password (16 characters) from your Google account and use the new app passwords you just generated in auth_password in sendmail.ini. Below is how to generate the app-specific password from your Google account.

How to generate an app-specific password from your Google account

Note that, the Google account interface is changed from time to time. You may not see the exact steps I show below. But you still want to look for the app-specific password section.

  • Login to your Google account
  • Navigate to the Security menu
  • On the Security page, look for “App passwords” under the “Signing in to Google” section.
  • Click on the “App passwords“, and the page will redirect to the verify page. Just enter your regular password for your Google account.
  • Then you will see the App passwords page.
  • On the App passwords page, select the app and device you want to generate the app password for. If none of the choices match your need, you will choose “Other (Custom name)“. In my case, I choose “Other (Custom name)“. We will continue with the “Other (Custom name)” choice.
  • After that, you need to type the label name of your new app password. Then you will click on the “GENERATE” button.
  • Next, the new popup with the 16 characters will show up. You want to copy those 16 characters and paste them into the config file you want. In this post, we will paste the 16 characters into auth_password in sendmail.ini.

Configure php.ini

  • At the wamp icon (green color), click on that wamp icon then navigate to PHP, and then look for the php.ini menu.
  • Click on the php.ini menu then follow the settings below.
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_path ="C:\wamp64\sendmail\sendmail.exe"
sendmail_from ="yourmail@gmail.com"

NOTE

SMTP and smtp_port settings are the same as we set in sendmail.ini above.
  • Next, save the php.ini and restart all services of Wampserver
  • Lastly, after restarting all services, you should see the wamp icon turn green.

Test by sending an email from your localhost

In order to test our email sending, we will create the new PHP file in the www folder (root). Then copy the code below into the PHP file. Finally, run this PHP file through your browser. Below is the code for testing.

Sending email code for PHP

<?php
/**
 * Sending an email test
 */
$to      = 'YourReadEmailAdddress';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
    'Reply-To: webmaster@example.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

$result = mail($to, $subject, $message, $headers);
if( $result ) {
   echo 'Success';
}else{
   echo 'Fail';
}
?>

Here are the steps:

  • Create the send_email_testing.php at your webroot. For the Wamp, the webroot will be “C:\wamp64\www”. So it will be “C:\wamp64\www\send_email_testing.php.
  • Copy the code above into the send_email_testing.php
  • Make sure, your wamp is running (green icon)
  • Then open your browser and type http://localhost/send_email_testing.php and hit enter
  • If you see the “Success” message on the screen that means your email is sent via your Gmail account successfully.
  • If you see the “Fail” message on the screen that means you need to check your mail settings in sendmail.ini and php.ini again. After changing the settings, don’t forget to restart Wamp.

Note

If the result is “Fail“, you should check the error log file in the sendmail folder.
“c:\wamp64\sendmail\error.log”

Can not send an email via the Gmail account?

If your test shows the “Fail” message on the screen, you should check the error log file in the sendmail folder. The error log file is located in “c:\wamp64\sendmail\error.log”.

BadCredentials or Username and Password not accepted issue

In 2018, I got the error saying “Username and Password not accepted” and something about “BadCredentials“. You may also receive the critical security alert email. The alert email is “Sign-in attempt was blocked“.

In my solution at that time, I had to enable “Less secure app access” from my Gmail account. To enable it, simply go to Settings>Accounts and Import>Other Google Account settings. Then under Security, look for “Less secure app access“. Then just turn on access.

Important note

Once you are done testing the email for your project, you should turn off access for security.

2-factor authentication issue

If your Google account enables “2-factor authentication“, you may receive the error that says “Application-specific password required” in your error log file that can be found in the sendmail folder. To fix it, just follow my link here.

Still can not send an email via the Gmail account?

You try to set up your Gmail account and you can not send an email from your localhost. There is another way you can try.

MailTrap (Sandbox Mail Provider)

You can sign up for MailTrap which is a fake SMTP testing server. There is a free plan with 50 emails limited. All emails that you send out from WordPress, will appear in MailTrap ONLY. For example, you send an email out to abc@gmail.com. This email will appear in MailTrap instead of the abc@gmail.com account.

  • Once you sign up, you will see the inboxes page.
MailTrap – Inboxes page
  • Click on the Demo box or gear icon under the Action column, you will see the messages page.
MailTrap – STMP configuration for WordPress
  • On this page, you will see the SMTP settings tab. Under the Integrations section, choose WordPress. You will see the WordPress SMTP configuration below.
function mailtrap($phpmailer) {
  $phpmailer->isSMTP();
  $phpmailer->Host = 'smtp.mailtrap.io';
  $phpmailer->SMTPAuth = true;
  $phpmailer->Port = 2525;
  $phpmailer->Username = '11111111111111';
  $phpmailer->Password = '11111111111111';
}

add_action('phpmailer_init', 'mailtrap');
  • Now, you can use the SMTP configuration from MailTrap to replace the Gmail SMTP settings in sendmail.ini and php.ini.
  • To replace the Gmail SMTP setting with the MailTrap SMTP setting in sendmail.ini. You will do as below.
smtp_server=smtp.mailtrap.io
smtp_port=2525
auth_username=11111111111111
auth_password=11111111111111

SSL is not available on this server.

If you can not send the email with Mailtrap and see the “SSL is not available on this server.” in the error log file (location: sendmail>error.log). You can try to set the smtp_ssl from auto to none as below.
smtp_ssl=none
Don’t forget to restart Wampserver and then test the sending email again. 🙂

WordPress only – use Post SMTP plugin

For WordPress, you try to use the sendmail with both Gmail account and MailTrap but no luck. You can try Post SMTP plugin instead.

The plugin is very simple and easy to use. You can configure the SMTP mail server within 1 or 2 minutes. Then test by sending an email. Another benefit you get from the Post SMTP plugin is the email log. The email log is very handy and helps you debug when sending out the email. Post SMTP plugin should work for all operation platforms (Windows, Mac, and Ubuntu).

Junk mail

Make sure to check the test email from your junk mail. Sometimes, the test email lands in the junk mail.

Wrap up

Being able to send an email on localhost is handy. You don’t need a staging server or test on the production which is not a good idea to test on the production. You just make sure that you don’t send the test emails out to your client’s email or client’s customers accidentally because you use the same database from the production.

That’s it. If my post is useful and saves you time, please consider buying me a coffee for today. 🙂