1

OK, so my result is that I submit the form and it does all the validations successfully (like, I entered numbers for the name it catches it and fails with my message). I get the message "Thank you for contacting us. We will be in touch with you very soon." which is at the end of my PHP page. But I am not getting an email to my Gmail account.

At one point I had an issue only while debugging locally, but on my Azure web server, it worked. I am coming back to my website a year later and am trying to remember how it works.

I am having a real hard time figuring out any debugging options to step through code.

Form code:

index.html

<form name="contactform" id="contactform" method="post" action="send_form_email.php">
    <fieldset>

        <div class="form-field">
            <input name="first_name" type="text" id="first_name" placeholder="Name" value="" minlength="2" required="" aria-required="true" class="full-width">
        </div>
        <div class="form-field">
            <input name="email" type="email" id="email" placeholder="Email" value="" required="" aria-required="true" class="full-width">
        </div>
        <div class="form-field">
            <input name="subject" type="text" id="subject" placeholder="Subject" value="" class="full-width">
        </div>
        <div class="form-field">
            <textarea name="comments" id="comments" placeholder="Message" rows="10" cols="50" required="" aria-required="true" class="full-width"></textarea>
        </div>
        <div class="form-field">
            <button class="full-width btn--primary" type="submit">Submit Email</button>
            <div class="submit-loader">
                <div class="text-loader">Sending...</div>
                <div class="s-loader">
                    <div class="bounce1"></div>
                    <div class="bounce2"></div>
                    <div class="bounce3"></div>
                </div>
            </div>
        </div>

    </fieldset>
</form>

AND HERE IS MY PHP FILE:

send_form_email.php

<?php
if(isset($_POST['email'])) {
 
    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "REDACTED@gmail.com";
    $email_subject = "Your email subject line";
 
    function died($error) {
        // your error code can go here
        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['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
    $email_from = $_POST['email']; // required
    $telephone = $_POST['subject']; // 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(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 .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Subject: ".clean_string($subject)."\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);  
?>
 
<!-- include your own success html here -->
 
Thank you for contacting us. We will be in touch with you very soon.
 
<?php
 
}
?>
gecclesinc
  • 190
  • 1
  • 15

1 Answers1

0

Gmail could be marking your email as spam, or blocking it entierly. Try using Pear Mail with SMTP:

require_once "Mail.php";

$from = '<from@gmail.com>';
$to = '<to@gmail.com>';
$subject = 'Subject';
$body = "Body";

$smtp = Mail::factory('smtp', array(
        'host' => 'ssl://smtp.gmail.com',
        'port' => '465',
        'auth' => true,
        'username' => 'johndoe@gmail.com',
        'password' => 'passwordxxx'
    ));

$headers = array(
    'From' => $from,
    'To' => $to,
    'Subject' => $subject
);

$mail = $smtp->send($to, $headers, $body);

if (PEAR::isError($mail)) {
    echo('<p>' . $mail->getMessage() . '</p>');
} else {
    echo('<p>Message successfully sent!</p>');
}
  • Thank you I tried this and I am getting a 405 error now, which is the same as my SendGrid solution. Something is fundamentally wrong with my site it seems like and I may just be giving up. – gecclesinc Nov 17 '19 at 17:15