How to Configure XAMPP to Send Email from Localhost with PHP

It is often necessary to configure the XAMPP server for sending email from the localhost using PHP. To meet that goal, you need to make changes to two files such as sendmail.ini and php.ini. Here, we will represent to you what steps you need to take to achieve that.

Watch a course Learn object oriented PHP

Steps to Send an Email with the help of XAMPP

Below, you can find the steps to configure XAMPP for sending emails from the local host with PHP.

Step 1: XAMPP Installation Directory

The first step is opening the XAMPP Installation Directory.

Step 2: Go and Open php.ini

The second step is going to C:\xampp\php and opening the php.ini file.

Step 3: Find [mail function]

In the framework of the third step, you should press ctrl + f for finding the [mail function].

Step 4: Pass Values

Now, it is necessary to detect and pass the values, demonstrated below:

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = [email protected]
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Step 5: Open sendmail.ini

The next step is going to C:\xampp\sendmail and opening the sendmail.ini file.

Step 6: Find [sendmail]

Press ctrl + f for finding [sendmail].

Step 7: Search and Pass Values

The seventh step considers searching and passing the values, demonstrated below:

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

Script for Sending Email

Finally, let’s see how the script for sending emails looks like:

<?php

 $to_email = "[email protected]";
 $subject = "Simple Email Testing via PHP";
 $body = "Hello,nn It is a testing email sent by PHP Script";
 $headers = "From: sender\'s email";

 if (mail($to_email, $subject, $body, $headers)) {
   echo "Email successfully sent to $to_email...";
 } else {
   echo "Email sending failed...";
 }

?>

After following the steps above, you will manage to send an email. However, if you still face problems in sending an email, then the reason can be among the following:

  1. A broken port.
  2. A wrong password.
  3. The emails are sent late.
  4. Gmail doesn’t obtain the necessary permissions.
  5. Wrong email configuration.

What is XAMPP?

XAMPP is considered a free and open-source cross-platform web server solution stack package. It is designed and developed by Apache Friends. It consists of the MariaDB database, Apache HTTP Server, as well as interpreters for scripts written in the PHP and Perl programming languages.