Jump to content

I'm new at this. Need help with an email form


Chronos21

Recommended Posts

I'm trying to make a webpage for a friend of a friend who is trying to sell his domain (for an outrages amount of money, but hey, his money is as green as anyone's). I have the website designed and with some generous help from the internet have written a PHP script that should send the contents of the form on the page to my email(temporarily). Every time I test it out, though, I get the confirmation page that says the script ran, but no email shows up.

 

Like I said, I'm new at this. I've coded before, but never in PHP. Are there any REALLY common mistakes that people make that could account for this? 

Link to comment
Share on other sites

Ah, I'm sorry. Here it is.

<?php
 
if(isset($_POST['email'])) {
 
    $email_to = "myemail@gmail.com";
 
    $email_subject = "offer";

 
    function died($error) {
 
        echo "We are sorry, but there were error(s) found with the form you submitted. ";
 
        echo "These errors appear below.<br /><br />";
 
        echo $error."<br /><br />";
 
        echo "Please go back and fix these errors.<br /><br />";
 
        die();
 
    }
 
    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']; 
 
    $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";
 

$headers = 'From: '.$email_from."\r\n".
 
'Reply-To: '.$email_from."\r\n" .
 
'X-Mailer: PHP/' . phpversion();
 
@mail($email_to, $email_subject, $email_message, $headers);  
 
?>
 
 
Thank you for contacting us. We will be in touch with you very soon.
 
 
 
<?php
 
}
 
?>
Link to comment
Share on other sites

the code you are attempting to use contains three problems that are either preventing it from working, preventing it from telling you why it isn't working, or is telling you it worked when it may not have.

 

1) the @ error suppressor in front of the mail() function call is preventing the reporting of any errors detected by php or returned to php from the sending mail server. remove the @ and temporarily, for debugging purposes, add the following two lines of code immediately after the first opening <?php tag -

ini_set("display_errors", "1");
error_reporting(-1);

report back with any error messages you get that you cannot solve.

 

2) the mail() function returns a true/false value that you need to test in your code. if it returns a true value, it means that the sending mail server accepted the email and will likely try to send it to the to: email address. you should only display the success message if the mail function returned a true value. this does not mean that the sending mail server will succeed in sending the email or that the receiving mail server accepted the email. it only means that php found a sending mail server at your web hosting and that mail server didn't return any error to php.

 

3) the email is not from the email address that the visitor entered in the form. the email is being sent from the sending mail server at your web hosting to the to: email address. the from address you put into the mail header should be a real mail box at your sending mail server at your web hosting or at least the domain name in the from address must correspond to your web hosting. most receiving mail servers currently test that the domain the mail says it is from can be traced back to the actual sending mail server.

 

in your testing if you entered a from address at @gmail and you are using a to: address @gmail, unless your web hosting is at google, the receiving mail server knows you didn't send this email from a gmail mail server and will discard it.

Edited by mac_gyver
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.