How to send mail from localhost in PHP using XAMPP

In this tutorial, we are going to see how to send mail from localhost in PHP using XAMPP. 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 sendmail package is integrated with XAMPP. So if you use XAMPP, you can easily send emails from localhost.

The configuration parameters of the mail service in PHP are:

  • smtp_sever: name of the SMTP host server, for example,
  • 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 XAMPP by following these steps.
1. Open the XAMPP installation directory.

2. Open C:\xampp\php directory and open php.ini file.

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

4. Replace the SMTP configuration settings as below:
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

5. Now open sendmail.ini file located in “c:\xampp\sendmail\sendmail.ini”.

6. Search for [sendmail] by pressing ctrl + f.

7. Find and change the following SMTP configuration settings.
[email protected]
[email protected](optional)

Script to send mail from localhost in PHP using XAMPP

Copy the following script into a PHP file called for example sendmail.php and place it in “htdocs” directory.

	$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

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

One thought on “How to send mail from localhost in PHP using XAMPP

  • Thanks. Finally a clean description on how to setup the email from XAMPP.


Leave a Reply

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