通过php脚本从html表单发送电子邮件

时间:2019-07-08 23:27:38

标签: php html forms

我在使以下html表单与后续php脚本一起运行时遇到问题。

此外,我希望表单可以定向到特定的html页面,如下所示。我将不胜感激,包括将重定向链接放置在php中的任何建议。

标题(https://www.mywebsite/mypage.html

html:

<form action="SendEmail.php" method="post " target="_blank">
          <div class="w3-row-padding" style="margin:0 -16px 8px -16px">
            <div class="w3-half">
              <input class="w3-input w3-border" type="text" placeholder="Name" required name="name">
            </div>
            <div class="w3-half">
              <input class="w3-input w3-border" type="text" placeholder="Email" required name="email">
            </div>
          </div>
          <textarea placeholder="Message" rows="20" cols="43" name="message" wrap="hard"></textarea>

          <button class="w3-button w3-black w3-section w3-right" type="submit" name="submit">SEND</button>



        </form>

php:

<?php
if(isset($_POST['email'])) {

    // EDIT THE 2 LINES BELOW AS REQUIRED
    $email_to = "myemail@mywebsite.com";
    $email_subject = "Form Submission";

    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['name']) ||
        !isset($_POST['email']) ||
        !isset($_POST['message'])) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }



    $name = $_POST['name']; // required
    $email_from = $_POST['email']; // required
    $message = $_POST['message']; // 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 .= '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($name)."\n";
    $email_message .= "Email: ".clean_string($email_from)."\n";
    $email_message .= "Message: ".clean_string($message)."\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);  
?>



<?php

}
?>

我从表单中没有收到任何回复或电子邮件,并且不确定是html还是php(或两者都有)

php脚本(SendEmail.php)在我的Web主机上,因此我正在从那里进行测试。我还故意在此帖子中输入了一封虚拟电子邮件。我在这里看过类似的答案,但找不到完全解决方案。在此先感谢。

0 个答案:

没有答案