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:
[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"
[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"
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).
By following these steps, you should be able to configure XAMPP to send emails using the PHP mail() function.
I'm a dedicated full-stack developer, entrepreneur, and the proud owner of ocec.org.in , hailing from the vibrant country of India. My passion lies in creating informative tutorials and sharing valuable tips that empower fellow artisans in their journey. With a deep-rooted love for technology, I've been an ardent enthusiast of PHP, Laravel, Angular, Vue, Node, JavaScript, jQuery, Codeigniter, and Bootstrap from their earliest days. My philosophy revolves around the values of hard work and unwavering consistency, driving me to continuously explore, create, and share my knowledge with the tech community.
helllo