【问题标题】:Contact form won't send the email联系表格不会发送电子邮件
【发布时间】:2017-03-05 02:15:54
【问题描述】:

说明

我从themefisher 下载此网站模板,但我无法使用联系表。我将php文件中的$to部分更改为我的电子邮件地址,这样我就可以收到了;但是在填写表格并发送之后,即使我收到了成功消息,我的收件箱或垃圾箱中也没有任何内容。


代码

可以看到直播预览here;或下载模板here

contact_me.js

$(function() {

    $("input,textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // get values from FORM
            var name = $("input#name").val();
            var email = $("input#email").val();
            var phone = $("input#phone").val();
            var message = $("textarea#message").val();
            var firstName = name; // For Success/Failure Message
            // Check for white space in name for Success/Fail message
            if (firstName.indexOf(' ') >= 0) {
                firstName = name.split(' ').slice(0, -1).join(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                cache: false,
                success: function() {
                    // Success message
                    $('#success').html("<div class='alert alert-success'>");
                    $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-success')
                        .append("<strong>Your message has been sent. </strong>");
                    $('#success > .alert-success')
                        .append('</div>');

                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
                error: function() {
                    // Fail message
                    $('#success').html("<div class='alert alert-danger'>");
                    $('#success > .alert-danger').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                        .append("</button>");
                    $('#success > .alert-danger').append("<strong>Sorry " + firstName + ", it seems that my mail server is not responding. Please try again later!");
                    $('#success > .alert-danger').append('</div>');
                    //clear all fields
                    $('#contactForm').trigger("reset");
                },
            })
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

contact_me.php

<?php
// Check for empty fields
if(empty($_POST['name'])        ||
   empty($_POST['email'])       ||
   empty($_POST['phone'])       ||
   empty($_POST['message']) ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
    echo "No arguments Provided!";
    return false;
   }

$name = $_POST['name'];
$email_address = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];

// Create the email and send the message
$to = 'myemail@gmail.com'; // Add your email address inbetween the '' replacing yourname@yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form:  $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";
$headers = "From: noreply@yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply@yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>

【问题讨论】:

  • mail() 函数返回什么?你能检查一下true 还是false?这表明邮件是否已成功投递。
  • 我在 Chrome 上打开网站,打开 JavaScript 控制台,填写联系表并发送。现在它显示一条错误消息,如您所见here(控制台日志也在屏幕截图中)。
  • 如果弄错了对不起,我对js真的很陌生,所以对它了解不多。
  • 控制台中的此错误是因为您从文件系统而不是本地服务器加载页面。在浏览器上打开localhost,然后重试。
  • 如果我做对了,控制台保持空白,如您所见here。我使用 CodeKit 来生成这个 url。

标签: javascript php


【解决方案1】:

您需要外部服务器来发送电子邮件。无法使用 PHP 在本地服务器上发送电子邮件。

【讨论】:

  • 你能告诉我如何创建它吗?或者一些链接,以便我可以查找它?
  • 也许你可以寻找免费的 PHP 托管,只是为了托管这个页面,看看它是否有效。我不知道有什么平台提供免费服务,但我知道有一些。
  • 只有我的电脑就没有办法吗?我的意思是,将此页面托管在我的计算机上运行的服务器中(我不知道是否可行)。
  • 然后我将您重定向到此帖子:stackoverflow.com/questions/5342822/…
猜你喜欢
  • 2015-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-30
  • 2013-12-29
  • 2012-02-25
相关资源
最近更新 更多