Email form not working

The php code used to send the email from the website is not working. Please let me know what am I doing here. Attached is the code.

<?php $EmailFrom=" "; $EmailTo = "[email protected]; $Subject = "Website Contact Form Submission"; $Name = Trim(stripslashes($_POST['Name'])); $Email = Trim(stripslashes($_POST['Email'])); $Comments = Trim(stripslashes($_POST['Comments'])); // prepare email body text $Body = ""; $Body .= "Name: "; $Body .= $Name; $Body .= "\n"; $Body .= "Email: "; $Body .= $Email; $Body .= "\n"; $Body .= "Message: "; $Body .= $Comments; $Body .= "\n"; $success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>"); if ($success){ print ""; } else{ print ""; } ?>

After clicking the submit button, I got this error message.
"); // redirect to success page // CHANGE THE URL BELOW TO YOUR “THANK YOU” PAGE if ($success){ print “”; } else{ print “”; } ?>

thank you

it could be due to the missing quotes on the end of $EmailTo:

[php]$EmailTo = "[email protected]";[/php]

Thanks for the reply Dave. However, I did put the quote on the email to line. I am still getting the same error message. What about the URL path? The URL is pointing to the directory where the contact thanks and error html are located. Is it possible that I will not add this code and just leave a message that says " You will be contacted by the administrator as soon as possible".

don’t think it’s that since you would still get an email.

Here’s a contact form script I use a lot so I know it works great feel free to use it it has validation when there’s an error it adds it to an array then loops through all errors and show’s them.

at the least it might give you some ideas

[php]
//This code runs if the contact form has been submitted
if (isset($_POST[‘maincontsubmit’]))
{

// check feilds are not empty
$name = trim($_POST[‘name’]);
if (strlen($name) < 3) {
$mainerror[] = ‘Name Must be more then 3 charactors.’;
}
if (strlen($name) > 20) {
$mainerror[] = ‘Name Must be less then 20 charactors.’;
}

// check for valid email address
$email = $_POST[‘email’];
$pattern = ‘/^[^@][email protected][^\s\r\n’";,@%]+$/’;
if (!preg_match($pattern, trim($email))) {
$mainerror[] = ‘Please enter a valid email address’;
}

// check feilds are not empty
$message = trim($_POST[‘message’]);
if (strlen($message) < 3) {
$mainerror[] = ‘Message Must be more then 3 charactors.’;
}

// if valadation is okay then carry on
if (!$mainerror ) {

//send email
$to = ‘[email protected]’;
$subject = “Contact form”;
$body = “on “.date(‘M j, Y’).” Information from contact form: \n\n Name: $name \n\n Email: $email \n\n Message: \n\n $message \n\n”;
$additionalheaders = “From: <$email>\r\n”;
$additionalheaders .= “Replt-To: $email”;

if(mail($to, $subject, $body, $additionalheaders))
{
$_SESSION[‘psuccess’] = ‘Message sent successfully’;
header('Location: '.$_SERVER[‘HTTP_REFERER’]);
exit();
}

} // end valadation
}// end submit

//show any errors
if (!empty($mainerror))
{
$i = 0;
while ($i < count($mainerror)){
$showError.= “<div class=“msg-error hidethis”>”.$mainerror[$i]."";
$i ++;}
$output.= $showError;
}// close if empty errors

$cmessage = ‘’;
if($_SESSION[‘psuccess’] != ‘’) {
$cmessage = ‘

’.$_SESSION[‘psuccess’].’
’;
$_SESSION[‘psuccess’] = ‘’;
}
if($_SESSION[‘perror’] != ‘’) {
$cmessage = ‘
’.$_SESSION[‘perror’].’
’;
$_SESSION[‘perror’] = ‘’;
}
$output.= $cmessage;

$output.= “<form action=”" method=“post”>\n";
$output.= “

Name:<input name=“name” type=“text” maxlength=“20” value=”$name"/>

\n";
$output.= "

Email:<input name=“email” type=“text” maxlength=“255” value="$email"/>

\n";
$output.= “

Message:
<textarea name=“message” cols=“50” rows=“10”>$message

\n”;
$output.= “

<input type=“submit” name=“maincontsubmit” value=“Send Message” />

\n”;

echo $output;[/php]

Dave
Thank you very much for the reply. I will try it and see how it goes. I will keep you posted.

Dave
Is this a php code? My Contact form is on html format and in that form the action is the php file which is named as contactengine.php. I copied your code and save it as contactengine.php. When I ran the html, the message printed some portions of the code.
thanks again

yes it php, you’ve got 2 choices here you can rename you .html page to be .php or sepeerate the php into another page that the form sends to.

I prefer to process my forms on the same page keeps things easier to manage plus you can do what’s called sticky forms if there has been an error the data that has been put into the form so far will be remembered so the user does not have to enter it all again.

Sponsor our Newsletter | Privacy Policy | Terms of Service