php

How to send mail from localhost in PHP using WAMP server

In this tutorial, we are going to see how to send mail from localhost in PHP using WAMP server. Sometimes we need to test the function of sending emails from our development environment. We can send emails from our local host using a mail server. The SMTP server allows to send messages from localhost and sendmail, it is a mail transport agent which is in php.ini file.

The configuration parameters of the mail service in PHP are:

  • smtp_sever: name of the SMTP host server, for example, smtp.gmail.com
  • smtp_port: the port number (Ex: 465)
  • auth_username: your SMTP username
  • auth_password: your SMTP password

To send a mail from localhost using Gmail, configure WAMP server by following these steps.
 

 

Step 1: Download sendmail

Download sendmail and extract all the files to C:\wamp64\sendmail folder.
 

 

Step 2: Configure sendmail

Open the sendmail.ini file located in “c:\wamp64\sendmail\sendmail.ini”.
 

 
Search for [sendmail] by pressing ctrl + f.
 

 
Find and change the following SMTP configuration settings.

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
[email protected]
auth_password=Your-Gmail-Password
[email protected](optional)

 

 

Step 3: Configure the php.ini file

Open the php.ini file.
 

 
Search for [mail function] by pressing ctrl + f.
 

 
Replace the SMTP configuration settings as below:

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "\"C:\wamp64\sendmail\sendmail.exe\" -t"
Remember to restart WampServer each time you modify php.ini file. Sometimes you have to quit WampServer and run it again, to make sure the changes are executed.

 

 

Script to send mail from localhost in PHP using WAMP server

Copy the following script into a PHP file called for example sendmail.php and place it in ‘www’ directory.

<?php
	$dest = "[email protected]";
	$subjetc = "Test Email";
	$body = "Hi this is a test email send by a php script";
	$headers = "From: [email protected]";

	if (mail($dest, $subjetc, $body, $headers)) {
		echo "Email successfully sent to $dest ...";
	} else {
		echo "Failed to send email...";
	}
?>

Now, navigate to the URL http://localhost/sendmail.php to run the script and send the email. If all goes well, you should see the success message “Email successfully sent to [email protected]” in your browser. If you use your own email address for the recipient’s address, you should also receive the email.
 
If you see the error message “Failed to send email…”, you need to configure GMAIL security settings, by visiting the following link https://myaccount.google.com/security

Scroll down the page until you find Less Secure Application Access, then click on Enable Access as shown in the image below:
 

 
Click to activate the option.
 

 
Now check, if all goes well, you should see the success message “Email sent successfully to [email protected]” in your browser.
 

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

3 thoughts on “How to send mail from localhost in PHP using WAMP server

  • Pablo Gustavo Vidal

    Hi guys, thx for the tutorial.
    But the download link of Sendmail is broken

    Reply
  • Quantic Vision GmbH

    really great – works like a charm – thanks a lot !!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *