I want to send mail using php mail() function. but it is not working. tell me the mistake in code?if any correction in php.ini file should be there then also tell that.

here is my code:-

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
if($_POST['submit'])
{
    $subject = $_POST['subject'];
    $to = $_POST['receiver'];
    $message = $_POST['body'];
    $sentmail = mail($to, $subject, $message);
if($sentmail == 'true')
{
    echo 'Email successfully sent';
}
else
{
    echo 'Email is not successfully sent';
}
}
else
{
?>
<form action="mail_trial3.php" method="POST">
Enter the Receiver email ID:-
<input type="text" name="receiver" /><br /><br />
Enter the subject:-
<input type="text" name="subject" /><br /><br />
Enter the message body:-
<input type="text" name="body" /><br /><br />
<input type="submit" value="Send the mail" name='submit'/>
</form>
<?php
}
?>
</body>
</html>

1. Use code tags.
2. What is not working? Explain what happens?
3. Running on local or remote server?
4. Running on *nix or windows?

you are also use this condition and try in server not in localhost and use $header also

if(mail($to, $subject, $message))
{
echo 'Email successfully sent';
}
else
{
echo 'Email is not successfully sent';
}

reply for your orders:-
2. when I am running the code this error is occuring:-
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:\wamp\www\prj2\mail_trial3.php on line 16

3.I am running this on wamp server that is local server.

4. my operating system is window xp(in VMware workstation).

copy and past this code and try


<?php

if(isset($_POST) && !empty ($_POST["subject"]) ){
$subject = $_POST;
$to = $_POST;
$message = $_POST;

$from = 'mail@mail.com';

$headers = "From: ".$from."\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= "\r\nX-Mailer: PHP/" . phpversion();

if(mail($to , $subject , $message , $headers )){

echo 'Email successfully sent';
}
else
{
echo 'Email is not successfully sent';
}


}
?>

Can i try mail function without the headers parameter? I think it is optional to use header.

yes , but you are try all in server in not localhost

Even I'm getting that error....... :(

do you have a mail server setup on your pc(localhost)?

you can download something like hmail - http://www.hmailserver.com/ if you dont have a mail server on your pc.

OR

just setup php.ini to connect to some mail account that you use, hopefully you have some email account around like a mail@ company email address?

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.