Problems with Email Form

This is where you share YOUR scripts with others

Moderators: macek, egami, gesf

andyrichin
New php-forum User
New php-forum User
Posts: 22
Joined: Fri Apr 29, 2011 5:02 pm

Problems with Email Form

Postby andyrichin » Fri Apr 29, 2011 5:09 pm

I am a noob when it comes to PHP and I am trying to build an email form. I ripped some code off and built it out and now I am trying to debug why it won't actually send to my email. Once I fill in the required fields it says "your message has been sent" but I don't receive the email. Any help would be wonderful! Much thanks in advance!

Here is the link to the page I am trying to make work. http://www.andyrichin.com/DEV/htmlform.htm

This is my HTML code:

Code: Select all

<form name="htmlform" method="post" action="html_form_send.php">
<table width="550px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name" style="font-family:Century Gothic; font-size:12px;">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" style="font-family:Century Gothic; font-size:12px;">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" style="font-family:Century Gothic; font-size:12px;">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" style="font-family:Century Gothic; font-size:12px;">Cell Phone Number *</label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="age" style="font-family:Century Gothic; font-size:12px;">Age *</label>
 </td>
 <td valign="top">
  <input  type="text" name="age" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="occupation" style="font-family:Century Gothic; font-size:12px;">Occupation *</label>
 </td>
 <td valign="top">
  <input  type="text" name="occupation" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="location" style="font-family:Century Gothic; font-size:12px;">Requested Location *</label>
 </td>
 <td valign="top">
  <input  type="text" name="location" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="city" style="font-family:Century Gothic; font-size:12px;">City Where You Live *</label>
 </td>
 <td valign="top">
  <input  type="text" name="city" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="state" style="font-family:Century Gothic; font-size:12px;">State Where You Live *</label>
 </td>
 <td valign="top">
  <input  type="text" name="state" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="preferred_date" style="font-family:Century Gothic; font-size:12px;">Preferred Date *</label>
 </td>
 <td valign="top">
  <select name="preferred_date">
  <option>01</option>
  <option>02</option>
  <option>03</option>
  <option>04</option>
  <option>05</option>
  <option>06</option>
  <option>07</option>
  <option>08</option>
  <option>09</option>
  <option>10</option>
  <option>11</option>
  <option>12</option>
  <option>13</option>
  <option>14</option>
  <option>15</option>
  <option>16</option>
  <option>17</option>
  <option>18</option>
  <option>19</option>
  <option>20</option>
  <option>21</option>
  <option>22</option>
  <option>23</option>
  <option>24</option>
  <option>25</option>
  <option>26</option>
  <option>27</option>
  <option>28</option>
  <option>29</option>
  <option>30</option>
  <option>31</option>
  </select>
 
  <select name="preferred_date">
  <option>January</option>
  <option>February</option>
  <option>March</option>
  <option>April</option>
  <option>May</option>
  <option>June</option>
  <option>July</option>
  <option>August</option>
  <option>September</option>
  <option>October</option>
  <option>November</option>
  <option>December</option>
  </select> 

  <select name="preferred_date">
  <option>2011</option>
  <option>2012</option>
  </select>
 </td>
</tr>
 
<tr>
 <td valign="top">
  <label for="preferred_time" style="font-family:Century Gothic; font-size:12px;">Preferred Time *</label>
 </td>
 <td valign="top">
  <select name="preferred_time">
  <option>10am</option>
  <option>11am</option>
  <option>12pm</option>
  <option>1pm</option>
  </select>
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="meeting_type" style="font-family:Century Gothic; font-size:12px;">Length Of Meeting *</label>
 </td>
 <td valign="top">
  <select name="meeting_type">
  <option>One Hour Date (incall only) $300.00</option>
  <option>Two Hour Date (incall only) $500.00</option>
  </select>
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="reference_1" style="font-family:Century Gothic; font-size:12px;">Reference #1 *</label>
 </td>
 <td valign="top">   
  <input  type="text" name="reference_1" maxlength="30" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="reference_1number" style="font-family:Century Gothic; font-size:12px;">Reference #1 Phone Number *</label>
 </td>
 <td valign="top">   
  <input  type="text" name="reference_1number" maxlength="30" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="reference_2" style="font-family:Century Gothic; font-size:12px;">Reference #2 *</label>
 </td>
 <td valign="top">   
  <input  type="text" name="reference_2" maxlength="30" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="reference_2number" style="font-family:Century Gothic; font-size:12px;">Reference #2 Phone Number *</label>
 </td>
 <td valign="top">   
  <input  type="text" name="reference_2number" maxlength="30" size="30">
 </td>
</tr>

<tr>
 <td valign="top">
  <label for="comments" style="font-family:Century Gothic; font-size:12px;">Comments or Requests *</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">
  <!-- We are grateful to you for keeping this link in place. thank you. -->
  <input type="submit" value="Submit">   <!--( <a href="http://www.freecontactform.com/html_form.php">HTML Form</a> )-->
 </td>
</tr>
</table>
</form>



And this is my PHP code:

Code: Select all

<?php
if(isset($_POST['email'])) {
   
   // CHANGE THE TWO LINES BELOW
   $email_to = "andy.richin@gmail.com";
   
   $email_subject = "website html form submissions";
   
   
   function died($error) {
      // your error code can go here
      echo "We're sorry, but there's errors found with the form you submitted.<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['age']) ||
      !isset($_POST['occupation']) ||
      !isset($_POST['location']) ||
      !isset($_POST['city']) ||
      !isset($_POST['state']) ||
      !isset($_POST['preferred_date']) ||
      !isset($_POST['preferred_time']) ||
      !isset($_POST['meeting_type']) ||
      !isset($_POST['reference_1']) ||
      !isset($_POST['reference_1number']) ||
      !isset($_POST['reference_2']) ||
      !isset($_POST['reference_2number']) ||

      !isset($_POST['comments'])) {
      died('We are sorry, but there appears to be a problem with the form you submitted.');      
   }
   
   $first_name = $_POST['first_name']; // required
   $last_name = $_POST['last_name']; // required
   $email_from = $_POST['email']; // required
   $telephone = $_POST['telephone']; // required
   $age = $_POST['age']; // required
   $occupation = $_POST['occupation']; // required
   $location = $_POST['location']; // required
   $city = $_POST['city']; // required
   $state = $_POST['state']; // required
   $preferred_date = $_POST['preferred_date']; // required
   $preferred_time = $_POST['preferred_time']; // required
   $meeting_type = $_POST['meeting_type']; // required
   $reference_1 = $_POST['reference_1']; // required
   $reference_1number = $_POST['reference_1number']; // required
   $reference_2 = $_POST['reference_2']; // required
   $reference_2number = $_POST['reference_2number']; // required

   $comments = $_POST['comments']; // required
   
   $error_message = "";
   $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
     $error_message .= 'The Email Address you entered does not appear to be valid.<br />';
  }
   $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$first_name)) {
     $error_message .= 'The First Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$last_name)) {
     $error_message .= 'The Last Name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$telephone)) {
     $error_message .= 'The Telephone Number you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$age)) {
     $error_message .= 'The Age you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$occupation)) {
     $error_message .= 'The Occupation you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$location)) {
     $error_message .= 'The Requested Location you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$city)) {
     $error_message .= 'The City you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$state)) {
     $error_message .= 'The State you entered does not appear to be valid.<br />';
  } 
  if(strlen($preferred_date) < 2) {
     $error_message .= 'The Preferred Date you entered does not appear to be valid.<br />';
  }
  if(strlen($preferred_time) < 2) {
     $error_message .= 'The Preferred Time you entered do not appear to be valid.<br />';
  }
  if(strlen($meeting_type) < 2) {
     $error_message .= 'The Meeting type you entered do not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$reference_1)) {
     $error_message .= 'The First Reference you entered do not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$reference_1number)) {
     $error_message .= 'The First Reference number you entered do not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$reference_2)) {
     $error_message .= 'The Second Reference you entered do not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$reference_2number)) {
     $error_message .= 'The Second Reference number you entered do not appear to be valid.<br />';
  } 
 
  if(strlen($comments) < 2) {
     $error_message .= 'The Comments you entered do 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 .= "Age: ".clean_string($age)."\n";
   $email_message .= "Occupation: ".clean_string($occupation)."\n";
   $email_message .= "Requested Location: ".clean_string($location)."\n";
   $email_message .= "City You Live In: ".clean_string($city)."\n";
   $email_message .= "State You Live In: ".clean_string($state)."\n";
   $email_message .= "Preferred Date: ".clean_string($preferred_date)."\n";
   $email_message .= "Preferred Time: ".clean_string($preferred_time)."\n";
   $email_message .= "Meeting Type: ".clean_string($meeting_type)."\n";
   $email_message .= "Reference 1: ".clean_string($reference_1)."\n";
   $email_message .= "Reference 1 Phone Number: ".clean_string($reference_1number)."\n";
   $email_message .= "Reference 2: ".clean_string($reference_2)."\n";
   $email_message .= "Reference 2 Phone Number: ".clean_string($reference_2number)."\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); 
?>

<!-- place your own success html below -->

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

<?php
}
die();
?>

johnj
php-forum Super User
php-forum Super User
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Problems with Email Form

Postby johnj » Mon May 02, 2011 8:52 am

a).Good if you do not use a function named 'died'

b).Do not suppress error...by using @mail...not a good programming practice

c).Make sure all form variables (like sender,receiver) etc are present before using mail().

d).Look into your spam folder

e).Take a look at your error log


Return to “Your Scripts”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest