0

I recently set up a contact form using the php mail function and it works great, except if the person submitting their email is using a gmail acount. The form can submit emails from every other provider but if it's from gmail it never sends. I'm hosting on Dreamhost. Here's the code I'm using:

 <?php

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

        include 'contact_settings.php';

        function died($error) {
            echo "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['Full_Name']) ||
            !isset($_POST['Email_Address']) ||
            !isset($_POST['Telephone_Number']) ||
            !isset($_POST['Your_Message']) || 
            !isset($_POST['AntiSpam'])      
            ) {
            died('Sorry, there appears to be a problem with your form submission.');        
        }

        $full_name = $_POST['Full_Name']; // required
        $email_from = $_POST['Email_Address']; // required
        $telephone = $_POST['Telephone_Number']; // not required
        $comments = $_POST['Your_Message']; // required
        $antispam = $_POST['AntiSpam']; // 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)==0) {
        $error_message .= 'The Email Address you entered does not appear to be valid.<br
      }
      if(strlen($full_name) < 2) {
        $error_message .= 'Your Name 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($antispam <> $antispam_answer) {
        $error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
      }

      if(strlen($error_message) > 0) {
        died($error_message);
      }
        $email_message = "Form details below.\r\n";

        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Full Name: ".clean_string($full_name)."\r\n";
        $email_message .= "Email: ".clean_string($email_from)."\r\n";
        $email_message .= "Telephone: ".clean_string($telephone)."\r\n";
        $email_message .= "Message: ".clean_string($comments)."\r\n";

    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $email_message,"From: $email_from ", $headers);
    header("Location: $thankyou");
    ?>
zipbug
  • 1
  • How did you verify that it didn't send? Couldn't it just end up in the spam folder? (As it usually happens when using the bland `mail` function.) – mario Dec 07 '13 at 20:43
  • Are you sure you have correct configuration for gmail? It requires SSL, plus you have to authenticate. See this question: http://stackoverflow.com/questions/712392/send-email-using-gmail-smtp-server-from-php-page – naivists Dec 07 '13 at 20:44
  • Watch out for mail header injection. Always verify that the headers contain valid email addresses, otherwise it's easy for spammers to sent mail to multiple email addresses. BTW, you miss a `'`, leading to syntax errors. @naivists The OP is using standard `mail` function, so authentication is not required. – Marcel Korpel Dec 07 '13 at 20:55
  • Also note that the regex to validate email addresses is incomplete. It does not accept addresses with the .museum TLD and probably misses more. – Arjan Dec 07 '13 at 21:20
  • I changed the validation to FILTER_VALIDATE_EMAIL and I checked the spam folder, every other email work just not gmail. – zipbug Dec 08 '13 at 01:27
  • Okay no worries, I ended up changing over to PHPMailer and now everything works great. – zipbug Dec 09 '13 at 15:49

0 Answers0