PHP 联系表单的配置问题

2022-01-14 00:00:00 bitbucket xampp php html contact-form

我在 StackOverflow 上看到了许多与我的问题相似的问题,但我想我还没有找到正确的解决方案.

I have seen that there are many similar to mine questions here on StackOverflow, but I think I was not able to find the correct solution to my question yet.

我遇到的问题与接收电子邮件功能有关 - 基本上是网站的 联系我们 部分,用户可以在其中填写基于 html 的表单并按 <代码>发送消息,此消息将发送到我的电子邮件.

The problem what I am having is connected with receiving emails functionality - basically a contact us section of the website, where user can fill in html based form and press send message and this message will be delivered to my email.

我使用 XAMPP 进行开发,并在 localhost 上运行 Apache 服务器和 Mercury.即使验证显示成功 - 电子邮件也无法通过.

I do my development using XAMPP and running the Apache server and Mercury on localhost. Even though validation shows success - emails do not get through.

  1. 我尝试将 method="post" 添加到 HTML 部分,但发现 type: "POST" 已包含在内.
  2. 仔细检查 Mercury 使用的端口,并将其与 php.ini 中提到的端口进行比较,两者都设置为 25.
  3. 我也尝试部署到 Bitbucket 页面 和 Azure 从本地主机迁移到实时托管.
  1. I have tried to add method="post" to HTML section, but found out that type: "POST" is already included.
  2. Double checked what port Mercury uses and compared it with the one mentioned in php.ini both are set to 25.
  3. I have also tried to deploy to Bitbucket pages and Azure to move from localhost to live hosting.

我已经仔细阅读了这篇非常详细的帖子.将网站上传到实时托管的想法来自这篇文章.

I have carefully read through this very detailed post. The idea to upload the website to live hosting came from this post.

<?php
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);


if( isset($name) && isset($email) ) {

    // Avoid Email Injection and Mail Form Script Hijacking
    $pattern = "/(content-type|bcc:|cc:|to:)/i";
    if( preg_match($pattern, $name) || preg_match($pattern, $email) || preg_match($pattern, $message) ) {
        exit;
    }

    $to = "myemail@gmail.com";
    $sub = "$name from Cv";
    // HTML Elements for Email Body
    $body = <<<EOD
    <strong>Name:</strong> $name <br>
    <strong>Email:</strong> <a href="mailto:$email?subject=feedback" "email me">$email</a> <br> <br>
    <strong>Message:</strong> $message <br>
EOD;

    $headers = "From: $name <$email>
";
    $headers .= 'MIME-Version: 1.0' . "
";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "
";

    mail($to, $sub, $body, $headers);
}

?>

$("#contact-form-1").submit(function(e) {
  e.preventDefault();
  var success = $(this).find('.email-success'),
      failed = $(this).find('.email-failed'),
      loader = $(this).find('.email-loading'),
      postUrl = $(this).attr('action');

  success.fadeOut(100); failed.fadeOut(100); loader.fadeOut(100);

  var data = {
    name: $(this).find('.contact-name').val(),
    email: $(this).find('.contact-email').val(),
    message: $(this).find('.contact-message').val()
  };

  if ( isValidEmail(data['email']) && (data['message'].length > 1) && (data['name'].length > 1) ) {
    $.ajax({
      type: "POST",
      url: postUrl,
      data: data,
      beforeSend: function() {
        loader.fadeIn(500);
      },
      success: function(data) {
        loader.fadeOut(500);
        failed.fadeOut(500);
        success.delay(500).fadeIn(500);
      },
      error: function(xhr) { // if error occured
        loader.fadeOut(500);
        success.fadeOut(500);
        failed.delay(500).fadeIn(500);
      },
      complete: function() {
        loader.fadeOut(500);
      }
    });
  } else {
    loader.fadeOut(500);
    failed.delay(500).fadeIn(500);
    success.fadeOut(500);
  }

  return false;
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form id="contact-form-1" action="sendmail.php" method="POST">
  
  <input class="contact-name form-control" type="text" name="name" placeholder="FULL NAME">
  <input class="contact-email form-control" type="text" name="email" placeholder="EMAIL">
  <textarea class="contact-message form-control" name="message" placeholder="MESSAGE" rows="5"></textarea>
  <button class="btn">SEND MESSAGE</button>

</form>

注意:此代码不是我自己编写的,因为它是由模板创建者提供的.我已经联系了模板创建者并面临绝对糟糕的支持.这是我在这里发帖的主要原因.

NOTE: I did not write this code by myself as it was provided by the template creator. I have contacted the template creator and faced an absolutely terrible support. This is the main reason of my post here.

推荐答案

在进行更多研究和四处询问时,我发现 this 非常有用的答案最终修复了我的本地 XAMPP 配置.所以代码本身没有问题,但我的配置是问题所在.

While looking doing more research and asking around I found this extremely useful answer what finally got my local XAMPP configuration fixed. So there was nothing wrong with code itself but my configuration was the issue.

重要提示: 尽量按照步骤进行操作,简单的 ; 缺失会对最终结果产生很大影响.因此,请仔细检查每一步是否有问题.

IMPORTANT: try to follow the steps as close as possible, simple ; missing can do the big difference in the final result. So double check every step if something seems to be broken.

还发现另一个非常有见地和有趣的阅读 发布.

Also found one other very insightful and interesting to read post.

相关文章