-1

Wanted to send a mail on php form submit. But the below code is not working. Cant figure out where i went wrong.

<input name="add" type="submit" id="add" value="Send Requets">

<?php
    if (isset($POST['add'])) {
        $From ='xxxxx@hotmail.com';
        $To = 'xxxxx@yahoo.com';
        $Subject = 'Leave Request';
        $body = 'You have received a leave request';
        $headers = 'From: $From \r\n';
        mail($To, $Subject, $body, $headers);
    }
?>
Patryk Uszyński
  • 1,069
  • 1
  • 12
  • 20

2 Answers2

0

your are running this function on localhost,Remember! Mailing functions does not work on localhost! if you want to send mail on localhost you have to do SMTP configurations! or you have to upload this code to development server it will work fine there

uneeb meer
  • 882
  • 1
  • 8
  • 21
0

Well since you using localhost this is not gonna work unless you configure localhost to send emails.

You can send mail from localhost with sendmail package , sendmail package is inbuild in XAMPP. So if you are using XAMPP then you can easily send mail from localhost.

for example you can configure C:\xampp\php\php.ini and c:\xampp\sendmail\sendmail.ini for gmail to send mail.

in C:\xampp\php\php.ini find extension=php_openssl.dll and remove the semicolon from the beginning of that line to make SSL working for gmail for localhost.

in php.ini file find [mail function] and change

SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = my-gmail-id@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"

Replace all the existing code in sendmail.ini with following code

smtp_server=smtp.gmail.com
smtp_port=587
error_logfile=error.log
debug_logfile=debug.log
auth_username=your-gmail-id@gmail.com
auth_password=your-gmail-password
force_sender=your-gmail-id@gmail.com

Then you are done :)

remember to restart the server using the XAMMP control panel so the changes take effect.

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34