简体   繁体   English

HTML 联系页面未发送 Email

[英]HTML Contact Page Not Sending Email

Hello , I've recently been building a contact page for my html website and it seems to not send my message to my email at all!你好,我最近一直在为我的 html 网站建立一个联系页面,它似乎根本没有将我的消息发送到我的 email!

It continues with ->它继续 -> 在此处输入图像描述

I am a little confused also why its not sending the message, and the default errors don't pop up with saying "this is required to be filled".我也有点困惑为什么它不发送消息,并且默认错误不会弹出说“这需要填写”。

Here is my PHP code (sending the message):这是我的 PHP 代码(发送消息):

<?php
  $name = htmlspecialchars($_POST['name']);
  $email = htmlspecialchars($_POST['email']);
  $phone = htmlspecialchars($_POST['phone']);
  $website = htmlspecialchars($_POST['website']);
  $message = htmlspecialchars($_POST['message']);
  if(!empty($email) && !empty($message)){
    if(filter_var($email, FILTER_VALIDATE_EMAIL)){
      $receiver = "MYEMAILHERE";
      $subject = "From: $name <$email>";
      $body = "Name: $name\nEmail: $email\nPhone: $phone\nWebsite: $website\n\nMessage:\n$message\n\nRegards,\n$name";
      $sender = "From: $email";
      if(mail($receiver, $subject, $body, $sender)){
         echo "Your message has been sent";
      }else{
         echo "Sorry, failed to send your message!";
      }
    }else{
      echo "Enter a valid email address!";
    }
  }else{
    echo "Email and message field is required!";
  }
?>

Here is my JS code (creating the message):这是我的 JS 代码(创建消息):

const form = document.querySelector("form"),
statusTxt = form.querySelector(".button-area span");
form.onsubmit = (e)=>{
  e.preventDefault();
  statusTxt.style.color = "#0D6EFD";
  statusTxt.style.display = "block";
  statusTxt.innerText = "Sending your message...";
  form.classList.add("disabled");
  let xhr = new XMLHttpRequest();
  xhr.open("POST", "src/php/message.php", true);
  xhr.onload = ()=>{
    if(xhr.readyState == 4 && xhr.status == 200){
      let response = xhr.response;
      if(response.indexOf("required") != -1 || response.indexOf("valid") != -1 || response.indexOf("failed") != -1){
        statusTxt.style.color = "red";
      }else{
        form.reset();
        setTimeout(()=>{
          statusTxt.style.display = "none";
        }, 3000);
      }
      statusTxt.innerText = response;
      form.classList.remove("disabled");
    }
  }
  let formData = new FormData(form);
  xhr.send(formData);
}

Here is my HTML code before end of my body (linking the js):这是我身体结束前的 HTML 代码(链接 js):

 <script src="src/js/contact.js"></script>

Is there anything i'm missing?有什么我想念的吗? Could it be not linking correctly?它可能没有正确链接吗? Im also sending this using an online website, not locally.我还使用在线网站发送此信息,而不是在本地发送。

Using Githubpages, which does not support PHP. That is the problem.使用不支持 PHP 的 Githubpages。这就是问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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