AUZ Tutorials

PHP Contact Form Tutorial: Sending Email via XAMPP on Localhost

To send an email with PHP from a XAMPP server, you can use PHP’s built-in mail() function, which is capable of sending simple emails. Here are the steps to set up and send an email using XAMPP:

Install and Start XAMPP:

If you haven’t already, download and install XAMPP. Start the XAMPP control panel and ensure that the Apache and MySQL services are running.

Create Your PHP Script:

Create a PHP script that sends the email. This script will use the mail() function to send the email. For example:

    
     <?php
$to = "recipient@example.com"; // Replace with the recipient's email address
$subject = "Test Email";
$message = "This is a test email sent from XAMPP.";

$headers = "From: sender@example.com"; // Replace with the sender's email address

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

Replace “recipient@example.com” with the actual recipient’s email address.
Replace “sender@example.com” with the sender’s email address.
Customize the email subject and message as needed.

Place the PHP Script in Your XAMPP Web Root:

Save the PHP script in the htdocs directory of your XAMPP installation or in a subdirectory within htdocs.

Access the PHP Script Through Your Web Browser:

Access the PHP script through your web browser by visiting http://localhost/your_project_folder/your_script.php, where your_project_folder is the name of the directory where you placed the PHP script.

For example, if you saved the script directly in the htdocs directory and named it send_email.php, you would access it at http://localhost/send_email.php.

Configure the PHP mail function in XAMPP

Edit the php.ini file:

Locate your php.ini configuration file. In XAMPP, you can find it in the php directory. The path is typically xampp/php/php.ini.

Open php.ini in a text editor (e.g., Notepad).

Configure the SMTP settings:

Search for the [mail function] section in the php.ini file.

Configure the SMTP settings according to your local mail server setup. You can use the SMTP server of your email provider or a local SMTP server if available. Below is an example configuration for using a Gmail SMTP server (you would replace with your own SMTP server information):

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

Replace the Gmail-specific values with the SMTP server information from your email provider or your local SMTP server.

Install and configure sendmail (if not already done):

If XAMPP does not come with sendmail, you may need to download and install it. You can download sendmail for Windows from http://glob.com.au/sendmail/.
After installation, configure sendmail.ini (located in the sendmail directory) with your SMTP server settings. Here is an example configuration for Gmail:

    
     smtp_server=smtp.gmail.com
smtp_port=587
auth_username=your-email@gmail.com
auth_password=your-gmail-password
    
   

Replace the Gmail-specific values with your own.

Restart Apache:

After making changes to the php.ini and sendmail.ini files, restart the Apache web server through the XAMPP control panel for the changes to take effect.

Test the configuration:

Open your web browser and navigate to http://localhost/test_mail.php or the appropriate URL to execute the script and test the mail functionality.
Make sure that you’ve provided the correct SMTP server settings and credentials. If you’re using Gmail, you might need to enable “Less secure apps” in your Google Account settings temporarily to allow the PHP mail script to send emails. However, be cautious when using this setting in a production environment, as it may pose a security risk. In a production environment, consider using a dedicated mail server.

Please watch this tutorial for a step by step guide

1 thought on “PHP Contact Form Tutorial: Sending Email via XAMPP on Localhost”

  1. Hello!

    Mine is a problem i need to assisted in something here kindly.

    I am finding it difficult to be redirected to a page i named results. On typing the correct login credentials that’s username and password stored in the database i am still redirected to registration page instead of, to the results page.

    What could be the problem?

Leave a Comment

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