Hi everyone i am new to php and have recently adapted a php email form and would like someone to validate it for me to make sure that it is correct.

<?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 name="Name" type="text" size="40" maxlength="40"></input></td>
          </tr>
          <tr>
            <td>Email address:</td>
            <td><input name="Email" type="text" size="40" maxlength="40"></input></td>
          </tr>
          <tr>
            <td valign="top">Message:</td>
            <td><textarea name="MsgBody" cols="40" rows="6"></textarea></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="Submit"
               value="Send"></input></td>
          </tr>
        </table>
    </form>      <?php
   } else {
      error_reporting(0);
      $recipient = [EMAIL="'blahs@blah.com'"]'blahs@blah.com'[/EMAIL];
      $from = stripslashes($_POST['Name']);
   $msg = "Email address of sender: stripslashes($_POST['Email'])";
      $msg = "Message from: $from\n\n".stripslashes($_POST['MsgBody']);
      if (mail($recipient, $subject, $msg))
         echo nl2br("<b>Message Sent:</b>
         To: $recipient
         Message:
         $msg");
      else
         echo "Message failed to send";
}?>

Thanking you in anticipation
Ashjenius

Recommended Answers

All 8 Replies

First let me ask you, did you try to process it? Second, for your action="" just put <?php echo $_SERVER; ?> rather than assigning a variable to it. If you don't need it more than once, don't create a variable for it :)

I'm not sure what you mean by 'process' but i have validated the code in dreamweaver. I have published this code on a page of a website i am designing and as you can see from the attached bmp it hasn't quite worked out, am i missing something like a set of behaviours.

Regards
Ashjenius

I'm no guru, but I have witten some successful scripts.
A few things I noticed,;

I prefer to keep the php together and the html separate as much as possible. The code at the beginning and the end can be integrated.
The trigger would be ; if isset($_POST['submit']) {...
which seems to be missing. How does this program know when to send?

Aslo, just embed the php handler in the html right after <body> like so:

 <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

In the code below, the ?> seems to be in the wrong place.
It terminates befoer the the closing bracket???

Hi everyone i am new to php and have recently adapted a php email form and would like someone to validate it for me to make sure that it is correct.

<?php
   if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>

Thanking you in anticipation
Ashjenius

Non of this has helped i still have the same problem and on top of that it doesn't send to the gesignated email address.

Regards
Ashjenius

Non of this has helped i still have the same problem and on top of that it doesn't send to the gesignated email address.

Regards
Ashjenius

There's an error in one line:

$msg = "Email address of sender: stripslashes($_POST['Email'])";

keep in mind that using when using functions together with strings you have to append it instead of just including it in the string itself as shown above. It should be:

$msg = "Email address of sender: ". stripslashes($_POST['Email']);

And you might wanna change the $recipient to a valid email address because that may be the reason why your mail send keeps failing

Have made the change you suggested but it has made no differrence to the the way the code works because the problem is with the code that sends the email not the code that depicts the content of the message, it might help if you refer to the attached bitmap.

I have not included a valid email address in the script i have posted because of privacy reasons.

Regards
Ashjenius

My original problem shown in the bitmap was a very silly mistake i made by using the wrong type of file so that is now fixed but the email still doesn't send.

<?php
  if ($_SERVER['REQUEST_METHOD'] != 'POST'){
      $me = $_SERVER['PHP_SELF'];
?>
      <form name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
        <table border="0" cellpadding="2" cellspacing="0">
          <tr>
            <td>Name:</td>
            <td><input name="Name" type="text" size="40" maxlength="40"></input></td>
          </tr>
          <tr>
            <td>Email address:</td>
            <td><input name="Email" type="text" size="40" maxlength="40"></input></td>
          </tr>
          <tr>
            <td valign="top">Message:</td>
            <td><textarea name="MsgBody" cols="40" rows="6"></textarea></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td></input>
<label>
              <input name="Submit2" type="submit" value="Submit"/>
              <input type="reset" name="Reset" value="Reset" />
</label>
</td>
          </tr>
        </table>
    </form><?php
   } else {
      error_reporting(0);
      $recipient = [EMAIL="'emanruoy@ereh.com'"]'emanruoy@ereh.com'[/EMAIL];
      $email =stripslashes($_POST['Email']);
      $from = stripslashes($_POST['Name']);
      $msg = "Message from: $from\n\n $email\n\n".stripslashes($_POST['MsgBody']);
      if (mail($recipient, $subject, $msg))
         echo nl2br("<b>Message Sent:</b>
         Message:
         $msg");
      else
         echo "Message failed to send";
}
?>

Here is my adjusted code in its full non working glory for someone to validate and pick errors.

Regards Ashjenius

Try putting a value for the $subject variable you are passing to the mail function. It's all that I can think of right now.

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.