0

I use to have an email form using PHP. It seems that it stopped working and I can't find any working script. It claims the email got sent but I never receive it.

I've tried both Yahoo and Gmail accounts as well as accounts on my server. Maybe there is something wrong with the code? I feel like it's not making sense but there doesn't seem to be a solution anywhere.

Thank you.

   <?php

   if(isset($_POST['email'])) {

    $email_to = "somemail@someserver.com";

   $email_subject = "Your email subject line";


    function died($error) {


    echo "We are very 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();

   }



// 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'];

   $last_name = $_POST['last_name'];

   $email_from = $_POST['email'];

   $comments = $_POST['comments'];



$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 .= "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
    } 
    ?>
Jorge Campos
  • 22,647
  • 7
  • 56
  • 87
  • What error are you receiving ? Use PHPMailer https://github.com/PHPMailer/PHPMailer Use SMTP Authentication. This is better way than simple mail function – Ish Sep 02 '16 at 04:00
  • Don't post real email address in a public site. – Jorge Campos Sep 02 '16 at 04:02
  • I'm not receiving any errors, it submits the form. I just never get the email. – sharkwave540 Sep 02 '16 at 04:02
  • Do you have E_ALL activated for your error_reporting configuration on your server? It is on php.ini file. Or you can add on the top of your code: `error_reporting( E_ALL );` – Jorge Campos Sep 02 '16 at 04:05
  • Make sure it doesn't block **mail** function ..try to send emails using **smtp** and **phpmailer**. – Mittul Chauhan Sep 02 '16 at 04:06
  • Use mail($to, $subject, $message, $headers) you use @mail in your code? And if doing this on your local server it will not work – Sunny Sep 02 '16 at 04:12
  • You are suppressing the error with this line ` @mail($email_to, $email_subject, $email_message, $headers);`. Remove `@` from the beginning. And try to send the message again. You will get much better idea of what is going on. I have restructured your code here http://pastebin.com/G032XUDN so it's easier to debug. – Samundra Sep 02 '16 at 04:26

0 Answers0