send-email-via-smtp-in-localhost
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 the 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 the Gmail account to send an email from your WAMP server. 

Note, sendmail works for Windows only. 

Okay, let’s do it…

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 (your Wamp server location. I installed Wampserver in C:\wamp64)
  • Inside the sendmail folder, find the sendmail.ini file and edit with the following setting. As I mentioned earlier, I will use the Gmail account for auth_username and auth_password setting.
smtp_server=smtp.gmail.com
smtp_port=587
auth_username=your_username
auth_password=your_password
  • Save and close the file

Configure php.ini

  • At the wamp icon on the right bottom corner, click on that wamp icon then go to PHP and php.ini. Then follows this setting. Notice that the SMTP and smtp_port settings are the same as we set in sendmail.ini above.
[mail function]
SMTP=smtp.gmail.com
smtp_port=587
sendmail_path ="C:\wamp64\sendmail\sendmail.exe"
sendmail_from ="[email protected]"
  • Save and restart all services in the wamp server

Test sending an email via your wamp server

  • You can use this snippet to test the email sending. Simply creates the send_email_testing.php at your webroot. For Wamp server, the webroot will be “C:\wamp64\www”. Then add the snippet below into the file.

Snippet:

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

$result = mail($to, $subject, $message, $headers);
if( $result ) {
   echo 'Success';
}else{
   echo 'Fail';
}
?>
  • Make sure, your wamp is running (green icon )
  • Then go to the 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
  • From the snippet above, I send a test email to “[email protected]” which is a dummy email. So you will see the “Delivery Status Notification (Failure)” email at your Gmail account which you set at the sendmail.ini. When you change from “[email protected]” to the existing email, you will see the test email in that email.

Can not send an email via the Gmail account? Try MailTrap

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

You can signup 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, they will appear in MailTrap ONLY. For example, you send an email out to [email protected] This email will appear in MailTrap instead of [email protected] account.

Once you signup, you will see the inboxes page.

MailTrap – Inboxes page

Click on Demo box or gear icon under Action column, you will see the messages page.

mailtrap configure
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');

At the sendmail that you download from the link above, you can use the SMTP configuration from MailTrap to replace the Gmail SMTP setting.

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

Now, you can test the email sending by http://localhost/send_email_testing.php you should see the success message on your browser.

WordPress only – use Post SMTP plugin

For WordPress, you try 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 sever within 1 or 2 minutes. Then test by sending a email. Another benefit you get from Post SMTP plugin is the email log. The email log is very handy and help you debug when sending out the email. Post SMTP plugin should work for all operation platform (Windows, Mac and Ubuntu).

If you have any issue with sendmail file, you should copy the error and search on the internet or post on StackOverflow. Also, you may check your junk mail.

That’s it. I hope this post will be useful to you.