There are several ways that could make PHP mail() function work in localhost. I haven’t tried them except for the solution below:

  1. Download sendmail and extract it on your desired location.
  2. Open sendmail.ini and enter the credentials needed:
    smtp_server=smtp.gmail.com
    smtp_port=465
    auth_username=user@gmail.com
    auth_password=your_password
  3. Open php.ini and uncomment sendmail path and enter the path
    sendmail_path = “\”C:\sendmail\sendmail.exe\” -t”

Use the ff. codes to test if it’s working:

<?php
$headers = 'From: from@gmail.com' . "\r\n" .
'Reply-To: reply@gmail.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
$to = 'to@mail.com';
$subject = "Test";
$message = "Dev Test!";

if (mail($to, $subject, $message, $headers)) {
echo 'sent!';
}
else {
echo 'not sent!';
}

?>

Note: $headers is essential for the function to work.

Leave a Reply