PHP联系表格的配置问题 [英] Configuration issue with PHP contact form

查看:67
本文介绍了PHP联系表格的配置问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在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的表格,然后按send message键,然后显示此消息将被发送到我的电子邮件.

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>\r\n";
    $headers .= 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

    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.

推荐答案

在进行更多研究并四处询问的同时,我发现了

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.

这篇关于PHP联系表格的配置问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆