Jump to content

I need some code to send email please...


richnb1974

Recommended Posts

Hi guys, I have a booking form on my website for potential customers to request a booking. The form asks for their Name, Address, Email and Tel No. I need the code to be able to send the form by Php. I also need it to show an error message if they haven't completed any of the fields. The fields on my form are: Name, Address, District, City, Postcode, Tel No, Email. Please help.

Link to comment
Share on other sites

I got the following code from About.com and adjusted it to suit my needs, but even though it sends my emails perfectly well, it also shows an error message after the email has sent.

 

<html><body><font face=Arial size=2> <form method="post" action="contact.php"> <table bgcolor=#ffffcc align=center> <tr><td colspan=2><strong>Contact us using this form:</strong></td></tr> <tr><td>Department:</td><td><select name="sendto"> <option value="info@mycompany.com">General</option> <option value="support@mycompany.com">Support</option> <option value="sales@mycompany.com">Sales</option> </select></td></tr> <tr><td><font color=red>*</font> Name:</td><td><input size=25 name="Name"></td></tr> <tr><td><font color=red>*</font> Email:</td><td><input size=25 name="Email"></td></tr> <tr><td>Company:</td><td><input size=25 name="Company"></td></tr> <tr><td>Phone:</td><td><input size=25 name="Phone"></td></tr> <tr><td>Subscribe to<br> mailing list:</td><td><input type="radio" name="list" value="No"> No Thanks<br> <input type="radio" name="list" value="Yes" checked> Yes, keep me informed<br></td></tr> <tr><td colspan=2>Message:</td></tr> <tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr> <tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr> <tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr> </table> </form> </body> </html>

Link to comment
Share on other sites

<html>
<head>
<title>contact form</title>
<style type='text/css'>
input{
background:#FEFEF0;
color:#ff0000;
border: 1px dashed #ff0000;
font-weight:bold;
}
select{
background:#FEFEF0;
color:#ff0000;
border: 1px dashed #ff0000;
font-weight:bold;
}
textarea{
background:#FEFEF0;
color:#ff0000;
border: 1px dashed #ff0000;
font-weight:bold;
}
#form{
margin: 0 auto;
padding: 10px;
border: 1px solid #ff0000;
background:#FFFFCC;
width: 400px;
}
</style>
<head>
<body>



<?php
if(isset($_POST['submit'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "youremail@yourdomain.com";//add your email address
$email_subject = "Your email subject line";//whatever subject you want


function died($error) {
	// your error code can go here
	echo "We are very sorry, but there were error(s) found with the form your submitted. ";
	echo "These errors appear below.<br /><br />";
	echo $error."<br /><br />";
	echo "Please go back and fix these errors.<br /><br />";
	die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
	!isset($_POST['last_name']) ||
	!isset($_POST['email']) ||
	!isset($_POST['telephone']) ||
	!isset($_POST['comments'])) {
	died('We are sorry, but there appears to be a problem with the form your submitted.');		
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = "^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$";
  if(!eregi($email_exp,$email_from)) {
  	$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
$string_exp = "^[a-z .'-]+$";
  if(!eregi($string_exp,$first_name)) {
  	$error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!eregi($string_exp,$last_name)) {
  	$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(strlen($comments) < 2) {
  	$error_message .= 'The Comments you entered do not appear to be valid.<br />';
  }
  $string_exp = "^[0-9 .-]+$";
  if(!eregi($string_exp,$telephone)) {
  	$error_message .= 'The Telphone Number you entered does not appear to be valid.<br />';
  }
  if(strlen($error_message) > 0) {
  	died($error_message);
  }
$email_message = "Form details below.\n\n";

function clean_string($string) {
  $bad = array("content-type","bcc:","to:","cc:","href");
  return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>

<form name="contactform" method="post" action="email.php">
<table width="450px">
</tr>
<tr>
<td valign="top">
  <label for="first_name">First Name *</label>
</td>
<td valign="top">
  <input  type="text" name="first_name" maxlength="50" size="30">
</td>
</tr>

<tr>
<td valign="top"">
  <label for="last_name">Last Name *</label>
</td>
<td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
</td>
</tr>
<tr>
<td valign="top">
  <label for="email">Email Address *</label>
</td>
<td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
</td>

</tr>
<tr>
<td valign="top">
  <label for="telephone">Telephone Number</label>
</td>
<td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
</td>
</tr>
<tr>
<td valign="top">
  <label for="comments">Comments *</label>
</td>
<td valign="top">
  <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>
</td>

</tr>
<tr>
<td colspan="2" style="text-align:center">
  <input type="submit" name="submit" value="Submit">  
</td>
</tr>
</table>



<!-- include your own success html here -->

Thank you for contacting us. We will be in touch with you very soon.

<?
}
?>

</body>
</html>

 

if solved mark this topic as solved and close it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.