Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

.PHP Contact Form not sending to Email


  • This topic is locked This topic is locked
4 replies to this topic

#1 sve

sve

  • Members
  • 19 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Saskatchewan, Canada
  • Local time:01:02 PM

Posted 19 February 2013 - 04:58 PM

Hello there one and all, I have an issue I hope someone can aid me with.
I am helping a local town with a website for their Centennial.  I am working on a Contact form for it, but I can't seem to get the form to email the info to me.

The form works as in I get to the "Thank You" - What am I missing ???


contact.html File:

<form name="contact" method="post" action="html_form_send.php">
<table width="450px">
</tr>
<tr>
 <td valign="top">
  <label for="first_name"><b>First Name</b></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"><b>Last Name</b></label>
 </td>
 <td valign="top">
  <input  type="text" name="last_name" maxlength="50" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="email"><b>Email Address</b></label>
 </td>
 <td valign="top">
  <input  type="text" name="email" maxlength="80" size="30">
 </td>

</tr>
<tr>
 <td valign="top">
  <label for="telephone"><b>Telephone Number</b></label>
 </td>
 <td valign="top">
  <input  type="text" name="telephone" maxlength="30" size="30">
 </td>
</tr>
<tr>
 <td valign="top">
  <label for="comments"><b>Comments:</b></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" value="Submit">
 </td>
</tr>
</table>
</form>


.PHP FILE
<?php
if(isset($_POST['email'])) {
    
    // CHANGE THE TWO LINES BELOW
    $email_to = "starverse@accesscomm.ca";
    
    $email_subject = "Rhien Centennial Comment Form";
    
    
    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['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']; // not 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(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 .= "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();
?>


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:02 PM

Posted 20 February 2013 - 09:04 AM

Does the server have an email server set up on it? Is the firewall open to allow email traffic? Does the IP allow emails to be sent over their network? Is PHP set up to allow emails?

 

There could be any number of things that could go wrong. I think though that you will find that of you take out the line of 'thank you' text, it will probably work fine. When using raw headers, you cannot dump anything to the screen, or the header will get reset.

 

Anyway, unless there is some particular need to set the headers yourself, the follwoing code works just fine for mail:

mail ( string $to , string $subject, string $message)



#3 sve

sve
  • Topic Starter

  • Members
  • 19 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Saskatchewan, Canada
  • Local time:01:02 PM

Posted 20 February 2013 - 11:25 AM

I talked with my Hosting Provider, and said I should have no issue with .PHP.
I have a couple things to try from suggestions from other forums, and of course the removal of the @ (mentioned as well on the other forums)



#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:02:02 PM

Posted 20 February 2013 - 12:32 PM

I think maybe you want to ask your hosting provider if you are allowed to send emails from your account.



#5 sve

sve
  • Topic Starter

  • Members
  • 19 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Saskatchewan, Canada
  • Local time:01:02 PM

Posted 21 February 2013 - 05:21 AM

Hey there, I am glad to let you know that this issue has been Solved smile.png
I removed the @ infront of @mail and now all works fine!!
 

Thank You for your Time and Help !!!
 


Edited by sve, 21 February 2013 - 05:22 AM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users