If you run this on XAMPP or anoher program that runs on localhost, search on Google for the settings you need to get email service working. You need to edit some files and change some ports/client names.
EDIT:
you only have to modify 2 ini files: php.ini and sendmail.ini
1)look for mail function in php.ini(c:/xampp/php/php.ini)>>[mail function]
change the following::
SMTP=smtp.gmail.com
smtp_port=587
sendmail_from = from@gmail.com
sendmail_path = "\"C:\xampp\sendmail\sendmail.exe\" -t"
NOTE: ensure that the path you specify for sendmail_oath is valid.In my case it is in C.
SAVE your changes.
2) next modify sendmail.ini(c:/xampp/sendmail/sendmail.ini)
comment the mercury as shown below
# Mercury
#account Mercury
#host localhost
#from postmaster@localhost
#auth off
# A freemail service example
#account Hotmail
#tls on
#tls_certcheck off
#host smtp.live.com
#from [exampleuser]@hotmail.com
#auth on
#user [exampleuser]@hotmail.com
#password [examplepassword]
then paste the following lines:
account Gmail
tls on
tls_certcheck off
host smtp.gmail.com
from x@gmail.com
auth on
user x@gmail.com
password x
port 587
# Set a default account
account default : Gmail
Again SAVE your changes.
Use the code below to test if it's working!
<?php
$subject="Test mail";
$to="someone@whatever.com";
$body="This is a test mail";
if (mail($to,$subject,$body))
echo "Mail sent successfully!";
else
echo"Mail not sent!";
?>
NOTE in the above configuration the sender should use gmail email service,for the recipient any email service will do.