Author Topic: I need some code to send email please...  (Read 277 times)

0 Members and 1 Guest are viewing this topic.

Offline richnb1974Topic starter

  • Irregular
  • Posts: 5
    • View Profile
I need some code to send email please...
« on: July 31, 2010, 01:56:05 PM »
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.

Offline xcoderx

  • Devotee
  • Posts: 816
  • Gender: Male
    • View Profile
    • Free online classifieds listing Solution!
Re: I need some code to send email please...
« Reply #1 on: July 31, 2010, 02:03:26 PM »
google or read php tutorial on mail function. its one of the easiest so ya must do it by ur self if unable then show ur codes when nd whr u r stuck.

Offline richnb1974Topic starter

  • Irregular
  • Posts: 5
    • View Profile
Re: I need some code to send email please...
« Reply #2 on: July 31, 2010, 02:14:54 PM »
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>

Offline dezkit

  • Devotee
  • Posts: 1,223
  • Gender: Male
  • Professor Badass
    • View Profile
Re: I need some code to send email please...
« Reply #3 on: July 31, 2010, 02:33:11 PM »
there is no php in that code.
why hello thar

Offline Pikachu2000

  • I hate everything.
  • Global Moderator
  • Freak!
  • *
  • Posts: 9,063
  • Gender: Male
  • Is it solipsistic in here, or is it just me?
    • View Profile
Re: I need some code to send email please...
« Reply #4 on: July 31, 2010, 02:35:02 PM »
Enclose code in [nobbc]
Code: [Select]
. . . [/nobc][/tt] tags, use appropriate line breaks, and if possible indenting, when posting it. Nobody wants to wade through a mess like that, at least not for free.
"Java" is to "Javascript" about the same as "fun" is to "funeral".

Why $_SERVER['PHP_SELF'] is bad. || Why ORDER BY RAND() is bad || Every problem can be solved with rm -rf * || Linux Help --> linuxforum.com

Offline xcoderx

  • Devotee
  • Posts: 816
  • Gender: Male
    • View Profile
    • Free online classifieds listing Solution!
Re: I need some code to send email please...
« Reply #5 on: July 31, 2010, 06:40:58 PM »
Code: [Select]
<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.
« Last Edit: July 31, 2010, 06:43:49 PM by xcoderx »