well i have (not precisely) created a form which accepts manily

1.name
2.subject
3.Message

well i have written the php code for that ,well the data sent by the user is supposed to come to my mail id since i am getting the message fail error i have , i guess i have written something wrong so , i dunno where i went wrong

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
	"http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<head>
<title>Contact Form</title>
<meta http-equiv="Content-Type" 
   content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
   if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>
   <form name="form1" method="post"
         action="<?php echo $me;?>">
      <table border="0" cellspacing="0" cellpadding="2">
         <tr>
            <td>Name:</td>
            <td><input type="text" name="Name"></td>
         </tr>
         <tr>
            <td>Subject</td>
            <td><input type="text" name="Subject"></td>
         </tr>
         <tr>
            <td valign="top">Message:</td>
            <td><textarea name="MsgBody"></textarea></td>
         </tr>
         <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="Submit"
               value="Send"></td>
         </tr>
      </table>
   </form>
<?php
   } else {
      error_reporting(0);
      $recipient = 'rahul8590@yahoo.com';
      $subject = stripslashes($_POST['Subject']);
      $from = stripslashes($_POST['Name']);
      $msg = "Message from: $from\n\n".stripslashes($_POST['MsgBody']);
      if (mail($recipient, $subject, $msg))
         echo nl2br("<b>Message Sent:</b>
         To: $recipient
         Subject: $subject
         Message:
         $msg");
      else
         echo "Message failed to send";
}
?>
</body>
</html>

i would be glad if u could improvise it.

Recommended Answers

All 3 Replies

I just double-checked the PHP syntax for if/else statements. It seems like you would benefit from using curly braces here, like so:

if (mail($recipient, $subject, $msg)) {
	echo nl2br("<b>Message Sent:</b>
	To: $recipient
	Subject: $subject
	Message:
	$msg");
} else {
	echo "Message failed to send";
}

You may also need to add a "From" header, as some mail servers will not send without it being set. You would modify your code to be the following, in that case:

$recipient = 'rahul8590@yahoo.com';
      $subject = stripslashes($_POST['Subject']);
      $from = 'From:' . stripslashes($_POST['Name']) . '\r\n';
      $msg = "Message from: $from\n\n".stripslashes($_POST['MsgBody']);

if (mail($recipient, $subject, $msg, $from)) {
	echo nl2br("<b>Message Sent:</b>
	To: $recipient
	Subject: $subject
	Message:
	$msg");
} else {
	echo "Message failed to send";
}

The PHP manual for 'mail' is a good reference

i did that .. but i am still getting "Message Failed To send "
is there any other way to do it or so...

Try changing this line:

$from = 'From:' . stripslashes($_POST['Name']) . '\r\n';

to this:

$from = 'From:' . stripslashes($_POST['Name']) . '\n';

Also, why don't you change the error_reporting(0); to error_reporting(E_ALL); so that you can see exactly what the error is.

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.