Setting Up Email Contact Form

Hi,

I have added the following contact form to my site however when I use the code it records that an email has been sent but no email is received at the respective email address.

Is there anything I need to set up email system to work other than just the code on the page?


<table width="400" border="0" cellpadding="0" cellspacing="1"  bgcolor="#EDEDED" border="1">
<tr>
<td><form name="form1" method="post" action="contactsend.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td>Subject:</td>
<td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name="detail" cols="50" rows="10" id="detail"></textarea></td>
</tr>
<tr>
<td>Name:</td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Email:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

<?php
// Contact subject
$subject ="$subject";
// Details
$message="$detail";

// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";

// Enter your email address
$to ='*****@****.com';

$send_contact=mail($to,$subject,$message,$header);

// Check, if message sent to your email
// display message "We've recived your information"
if($send_contact){
echo "Thank You for Contacting Us. We will endevour to reply as soon as possible.";
}
else {
echo "ERROR";
}
?>

There is nowhere in your PHP where you extract the values passed from the form in the $_POST array into the variables that the PHP script is using. None of the vaiables your PHP script is using are ever given vallues to use in the subsequent processing.

Note also that you shouldn’t just directly assign the values across - you should validate the content of $_POST[‘customer_email’] using the appropriate validation filter to ensure that it is a valid email address format before moving the value to $customer_email (and similarly for all the other fields).

Thanks,

Is there a template I can use for contact forms. They are widely used and to be honest it would be a good learning exercise.

Here are some handy scripts and tutorias I’ve gethered over time:

Otherwise, here are some links I’ve gathered over time:

http://swiftmailer.org/

http://css-tricks.com/139-nice-and-simple-contact-form/
http://css-tricks.com/examples/NiceSimpleContactForm2/
http://www.helpvid.net/tutorials/dreamweaver/php-script-download.html
http://green-beast.com/gbcf-v3/
http://www.freedback.com/
http://accessify.com/tools-and-wizards/accessibility-tools/quick-form-builder/
http://css-tricks.com/snippets/php/send-email/
http://seo-watch.com/hosting/mail_form.php
http://www.felgall.com/php2.htm
http://www.easyphpcontactform.com/manual.html
http://www.addressmunger.com/contact_form_generator/
http://webgeekworld.com/web_development_resource_details.php?id=42
http://www.webformfactory.com/
http://www.freecontactform.com/email_form.php
http://swiftmailer.org/
http://sourceforge.net/projects/phpmailer/
using Google docs
http://programmingkid.com/google-docs-contact-form-without-coding/

Apart from the missing PHP in the example you posted above, the HTML itself is dreadful … using a table where it doesn’t belong, and using no labels that can be associated with the form inputs. I highly recommend you delete that code and never think of using it again! :slight_smile:

Brilliant thanks,

I used a template and copied it directly. I have added my email address but no email is received.

I have replaced it with email@test.com. I sthere anything else I need to change or set up on my server for emails to work?

<?php

$EmailFrom = "Contact Us";
$EmailTo = "email@test.com";
$Subject = "Contact Form";
$Name = Trim(stripslashes($_POST['Name']));
$Tel = Trim(stripslashes($_POST['Tel']));
$Email = Trim(stripslashes($_POST['Email']));
$Message = Trim(stripslashes($_POST['Message']));

// validation
$validationOK=true;
if (!$validationOK) {
  print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">";
  exit;
}

// prepare email body text
$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\
";
$Body .= "Tel: ";
$Body .= $Tel;
$Body .= "\
";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\
";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\
";

// send email
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

// redirect to success page
if ($success){
  print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=contactthanks.php\\">";
}
else{
  print "<meta http-equiv=\\"refresh\\" content=\\"0;URL=error.htm\\">";
}
?>

Have you changed your HTML? If you have something like $_POST[‘Name’], the work “Name” must correspond to the name=“” attribute in your form code. You had name=“name”, which won’t match because of the capital/lower case. My advice is never to use capitals in code. It leads to lots of unnecessary errors.

And you won’t pick up things like name=“customer_mail”.

Also, make sure $EmailFrom corresponds to an email address.

Hi,

I copied it like for like. I have gone through it all and changed it to lower case.

Not sure what you mean by this "Also, make sure $EmailFrom corresponds to an email address. " Do I need to put an email address in there? I thought was just a title.

Maybe just repost all the code you have—HTML and PHP. It doesn’t look like you’ve got a complete setup there. But yes, the $EmailFrom is expecting an email address.

hi i have a similar issue ( but different in as my php form throws the error up and no email delivered) can i post it here or should i start a new thread?

Probably best to start a new thread. :slight_smile: