简体   繁体   中英

Email won't send on contact form submission (PHP)

Can someone help me figure out where I'm wrong on this? The form just goes to the PHP code when click 'send'. I've been messing around with it for hours and think I am just doing/missing something very simple. Going to step away from this for a bit and enter back into the vortex. But in the meantime, if anyone can help me out, please let me know.

Here is the HTML

 <div class="container"> <div class="row form-container"> <div class="col-md-12 contact-form"> <h3 style="text-align:center">Rather have us contact you? No problem!</h3> <h4 style="text-align:center">Just fill out this form.</h4> <form name="contactform" method="post" action="send_form_email.php"> <div class="form-group"> <input type="text" class="form-control" id="name" name="name" placeholder="Your Name..." value="" required> </div> <div class="form-group"> <input type="text" class="form-control" id="email" name="email" placeholder="Your email..." value="" required> </div> <div class="form-group"> <input type="text" class="form-control" id="phone" name="phone" placeholder="Your phone..." value=""> </div> <div class="form-group"> <textarea class="form-control" rows="4" name="message" placeholder="Your message..."></textarea> </div> <div class="form-group"> <button type="submit formnovalidate" name="submit" value="Submit" class="btn btn-default"><i class="fa fa-paper-plane fa-fw"></i> Send</button> </div> </form> </div> </div> </div> 

Here is the PHP

<?php

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





$email_to = "mdevlin14@gmail.com";

$email_subject = "Contact from Hebert Counseling";





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();
}



if(!isset($_POST['name']) ||

    !isset($_POST['email']) ||

    !isset($_POST['phone']) ||

    !isset($_POST['message'])) {

    died('We are sorry, but there appears to be a problem with the form you submitted.');

}



$first_name = $_POST['name']; // required

$email_from = $_POST['email']; // required

$telephone = $_POST['phone']; // not required

$comments = $_POST['message']; // not 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,$name)) {

$error_message .= 'The name you entered does not appear to be valid.<br />';

}

if(strlen($message) < 2) {

$error_message .= 'The message 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 .= "Name: ".clean_string($first_name)."\n";

$email_message .= "Email: ".clean_string($email_from)."\n";

$email_message .= "Phone: ".clean_string($telephone)."\n";

$email_message .= "Message: ".clean_string($comments)."\n";





//  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);

?>





Thank you for reaching out! We will be in touch with you very soon!



<?php

}

?>

As pointed out by Franz, definitely remove the error supression (the @ sign) before the mail() function. Then you will see warnings and errors that are not visible now and you can fix the problem.

You can check if any e-mail is actually sent by setting the mail.log path in php.ini :

; The path to a log file that will log all mail() calls. Log entries include
; the full path of the script, line number, To address and headers.
mail.log = "C:\xampp\mail.log"

You can also redirect all e-mails to a file instead of sending it for real. In XAMPP on Windows, there is a script mailtodisk.exe. Just set it in php.ini:

sendmail_path = "C:\xampp\mailtodisk\mailtodisk.exe"

You will find sent e-mails in the C:\\xampp\\mailoutput folder.

Don't forget to restart the server after any changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM