How to setting localhost XAMMP for php send mail

How to setting localhost XAMMP for php send mail

To send mail using the PHP mail() function in a local development environment like XAMPP, you need to configure the PHP php.ini file and the sendmail configuration file. Here’s a step-by-step guide:

Step 1: Locate the php.ini File

  1. Open the XAMPP Control Panel.
  2. Click on the Config button next to Apache.
  3. Select PHP (php.ini) from the dropdown list. This will open the php.ini file in a text editor.
  4. Find the [mail function] section. You can use the search function (Ctrl+F) to locate it quickly.

[mail function]
; For Win32 only.
; http://php.net/smtp
SMTP=smtp.gmail.com
; http://php.net/smtp-port
smtp_port=587

; For Win32 only.
; http://php.net/sendmail-from
sendmail_from = your-email@gmail.com

; For Unix only. You may supply arguments as well (default: "sendmail -t -i").
 
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

 

Step 2: Configure the sendmail.ini File

  1. Go to the sendmail folder in the XAMPP installation directory, typically located at C:\xampp\sendmail.
  2. Open the sendmail.ini file in a text editor.
  3. Update the settings as follows (again, this example uses Gmail):

[sendmail]

smtp_server=smtp.gmail.com
smtp_port=587
smtp_ssl=auto
error_logfile=error.log
debug_logfile=debug.log

auth_username=your-email@gmail.com
auth_password=your-email-password

; or the path to the sendmail executable, if you have one.
sendmail_path="C:\xampp\sendmail\sendmail.exe -t"

Step 3: Save and Restart

  1. Save the changes to both php.ini and sendmail.ini.
  2. Restart Apache from the XAMPP Control Panel for the changes to take effect.
     

Step 4: Test the Configuration

Create a PHP script to test the mail functionality:

<?php
$to = "recipient@example.com";
$subject = "Test Mail from XAMPP";
$message = "Hello! This is a test email message sent from XAMPP.";
$headers = "From: your-email@gmail.com\r\n" .
          "Reply-To: your-email@gmail.com\r\n" .
          "X-Mailer: PHP/" . phpversion();

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

Save this script as test_mail.php in your XAMPP htdocs directory (e.g., C:\xampp\htdocs\test_mail.php).

Step 6: Run the Test Script

  1. Open a web browser.
  2. Navigate to http://localhost/test_mail.php.
  3. If the configuration is correct, you should see a message indicating whether the email was sent successfully.

Notes

  • Gmail Less Secure Apps: If you are using a Gmail account, you may need to enable access for less secure apps. This can be done in your Google account settings.
  • Two-Factor Authentication: If you have two-factor authentication enabled on your Gmail account, you might need to create an app-specific password.
  • Firewall and Antivirus: Ensure that your firewall or antivirus software is not blocking the connection to the SMTP server.

By following these steps, you should be able to configure XAMPP to send emails using the PHP mail() function.
 

 

 


Share this article







Related Posts




0 Comments



Load more Comments

Post a Comment


helllo
Ocec Copyright text of dont't copyright our content