i m using windows, xampp, with appache sever.
plz tell me from the first how to send mail in php on localhost ...
this is really pizzing me ...
plz its very urgent plz help me
thank you...

mailing in php is very simple, php provide the mail() function that does everything for us -

to      = 'to@test.com';
$subject = 'subject';
$message = 'hello, how are you';
$headers = 'From: test@test.com' . "\r\n" .
    'Reply-To: test@test.com' . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

The header part is optional, so you can exclude , if it bothers you.

i did wht u have written but it gives me error

hey its very urgent plz help me

What error do you get ? Did you install an smtp server, or did you change the httpd.conf to specify an smtp mail server ?

apart from above also post your code, what you did

posting for others too, so that it can get faster.
here is your php -

<?
$to = "moonisha1@gmail.com";
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "enquiry";

$fields = array();
$fields{"name"} = "name";
$fields{"add"} = "address";
$fields{"email"} = "email";
$fields{"ph"} = "phone";
$fields{"mobile"} = "mobile";
$fields{"city"} = "city";
$fields{"zip"} = "zip";
$fields{"country"} = "country";
$fields{"req"} = "req";

$body = "We have received the following information:\n\n";
foreach($fields as $a => $b)
{
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}

if($from == '')
{
print "You have not entered an email, please go back and try again";
}
else
{
if($name == '')
{
print "You have not entered a name, please go back and try again";
}
else
{
$send = mail($to,$subject,$body,$headers);

if($send)
{
echo "<script type='text/javascript'> alert('Your mail has been sent');</script>";
}
else
{
print "We encountered an error sending your mail";
}
}
} 
?>

hey there's nothing wrong with your code. I got this below echo, means you mailed successfully =>

Array
(
    [req] => fsdf
    [name] => dfs
    [add] => dsfs
    [ph] => dsfs
    [mobile] => sdf
    [email] => dfds
    [city] => fdsf
    [zip] => dsfs
    [country] => dsf
    [submit] => submit
)

=>moonisha1@gmail.com
pritam is sending mail
We have received the following information: name: dfs address: dsfs email: dfds phone: dsfs mobile: sdf city: fdsf zip: dsfs country: dsf req: fsdf
From: dfds=>1

for this =>

<?
echo '<pre>';
print_r($_POST);
echo '</pre>';

$to = "moonisha1@gmail.com";
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = " is sending mail";

$fields = array();
$fields{"name"} = "name";
$fields{"add"} = "address";
$fields{"email"} = "email";
$fields{"ph"} = "phone";
$fields{"mobile"} = "mobile";
$fields{"city"} = "city";
$fields{"zip"} = "zip";
$fields{"country"} = "country";
$fields{"req"} = "req";

$body = "We have received the following information:\n\n";
foreach($fields as $a => $b)
{
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}

if($from == '')
{
print "You have not entered an email, please go back and try again";
}
else
{
if($name == '')
{
print "You have not entered a name, please go back and try again";
}
else
{
echo "=>".$to."<br>".$subject."<br>".$body."<br>".$headers;
echo "=>".$send = mail($to,$subject,$body,$headers);

if($send)
{
echo "<script type='text/javascript'> alert('Your mail has been sent');</script>";
}
else
{
print "We encountered an error sending your mail";
}
}
} 
?>

So just check with another email id,because in some organisation the mail to gmail is blocked for some reason or just check with your smtp server settings as above suggested or catch hold of your network admin. he might help in such case.

hey can u tell me wht settings u did.... did
u changed the ini settings

Nothing , just your code with some echoes at the right point to verify it.
You stil didnt get it?

actually it gives me
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\www\blackcatsecuritas\enquiry.php on line 50

so wht to do i tried everything but of no use

actually it gives me
Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\www\blackcatsecuritas\enquiry.php on line 50

so wht to do i tried everything but of no use

post your smtp settings fro the php.ini by phpinfo() here

You will need to edit your smtp settings in php.ini,
Like if you have configured an e-mail account for outlook and you've set your ISP's mail server for mail
delivery (something like smtp.something.net etc). Its the same in PHP.

You don't need to install and configure your own mail server to send mail

You will need to edit your smtp settings in php.ini,
Like if you have configured an e-mail account for outlook and you've set your ISP's mail server for mail
delivery (something like smtp.something.net etc). Its the same in PHP.

You don't need to install and configure your own mail server to send mail

Find php.ini here xampp for windows: C:\Program Files\xampp\apache\bin\php.ini
It seems that the smtp server is not installed on localhost, you need to provide the working smtp server to the php.
Change the below settings
SMTP = localhost
smtp_port = 25
to something like
SMTP = smtp.example.com
smtp_port = 25
In above case smtp.example.com you need to confirm it for you.

Find php.ini here xampp for windows: C:\Program Files\xampp\apache\bin\php.ini
It seems that the smtp server is not installed on localhost, you need to provide the working smtp server to the php.
Change the below settings
SMTP = localhost
smtp_port = 25
to something like
SMTP = smtp.example.com
smtp_port = 25
In above case smtp.example.com you need to confirm it for you.

hello plz tell me wht 2 do..... its urgent ...

Is the receiver getting mail now, after above settings.
Post the problem you facing now

try this -

<?
$to = "info@maharajarollingshuters.com";
$from = $_REQUEST['email'] ;
$name = $_REQUEST['name'] ;
$headers = "From: $from";
$subject = "enquiry";

$fields = array();
$fields{"name"} = "name";
$fields{"add"} = "address";
$fields{"email"} = "email";
$fields{"ph"} = "phone";
$fields{"mobile"} = "mobile";
$fields{"city"} = "city";
$fields{"req"} = "req";

$body = "We have received the following information:\n\n";
foreach($fields as $a => $b)
{
$body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]);
}

if($from == '')
{
print "You have not entered an email, please go back and try again";
exit();
}
else
{
if($name == '')
{
	print "You have not entered a name, please go back and try again";
	exit();
}
else
{
/* echo "=>".$to."<br>".$subject."<br>".$body."<br>".$headers;
echo "=>".$send = mail($to,$subject,$body,$headers);*/
$send = mail($to,$subject,$body,$headers);
}
	if($send)
	{
		echo "<script type='text/javascript'> alert('Your mail has been sent');</script>";
	}
	else
	{
		print "We encountered an error sending your mail";
	}
}
?>

its still not working .............

you have missed the outer check for the submit button's post , like -

If(isset($_POST['submit_btn_name']) && $_POST['submit_btn_name']!='')
{ 
//your code here
}

Put your entire code in this for loop

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.