How to send an email using SMTP in localhost with WAMP

Jyotishgher Astrology
By -
0

 How to send an email using SMTP in localhost with WAMP


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.

https://drive.google.com/file/d/1UTLlOMXsVjrVxcLkcP5-zn5UHr_O8OeW/view?usp=sharing

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 ="yourmail@gmail.com"
  • 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      = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: care@example.com' . "\r\n" .
    'Reply-To: info@example.com' . "\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 “nobody@example.com” 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 “nobody@example.com” to the existing email, you will see the test email in that email.

Can not send an email via the Gmail account?

If you test by using the testing above and get “Fail” message. You should check the error login from the sendmail. The error log file is located in “c:\wamp64\sendmail\error.log”. I got the error saying “Username and Password not accepted” and something about “BadCredentials“. You may also receive the critical security alert in your email. The alert email will alert about “Sign-in attempt was blocked“.

My solution, I have to enable “Less secure app access” from my Gmail account that I set in the Sendmail.

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.

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


Post a Comment

0Comments

Post a Comment (0)