0

我根据教程制作了一个联系表格,但我无法让这个表格工作。当我点击提交按钮时,什么都没有发生(似乎页面正在刷新或在我的实时网站上它返回到 index.html)并且我没有收到电子邮件并且没有回复。我想收到一封包含用户填写的表单内容的电子邮件。

这是 ajaxSubmit.php:

<?php

$name = $_POST['name']; // contain name of person
$email = $_POST['email']; // Email address of sender 
$web = $_POST['web']; // Your website URL
$body = $_POST['text']; // Your message 
$receiver = "xxx@gmail.com" ; // hardcorde your email address here - This is the email address that all your feedbacks will be sent to 
if (!empty($name) & !empty($email) && !empty($body)) {
$body = "Name:{$name}\n\nWebsite :{$web}\n\nComments:{$body}";
$send = mail($receiver, 'Contact Form Submission', $body, "From: {$email}");
if ($send) {
    echo 'true'; //if everything is ok,always return true , else ajax submission won't work
}

}

?>

当然,xxx@gmail.com 只是我常规电子邮件地址的占位符。

使用 html、js 和 css 的FIDDLE 。

我究竟做错了什么?

4

1 回答 1

0

我建议使用 isset 来检查发布的变量是否实际存在,此外:我建议使用三元运算符来检查它们是否实际设置:

$name = isset($_POST['name']) ? $_POST['name'] : false;
$email = isset($_POST['email']) ? $_POST['email'] : false;
$web = isset($_POST['web']) ? $_POST['web'] : false;
$text = isset($_POST['text']) ? $_POST['text'] : false;

$receiver = "xxx@gmail.com"; // You forgot to close a whitespace here.

if(($name && $email && $web && $text) != false)
{
    // It's okay to send email now.
    // Checkpoint.
}

我推荐使用这个垃圾邮件检查过滤功能:

function spamcheck($field)
{
    //address using FILTER_SANITIZE_EMAIL
    $field=filter_var($field, FILTER_SANITIZE_EMAIL);

    //address using FILTER_VALIDATE_EMAIL
    if(filter_var($field, FILTER_VALIDATE_EMAIL))
    {
        return true;
    }
    else
    {
        return false;
    }
}

我喜欢使用这个修改后的邮件功能:

// Usage:   sendMail($toEmail, $fromEmail, $subject, $message);
// Pre:     $toEmail is of type string and is a valid email address, 
//          indicating the receiver.
//          $fromEmail is of type string and is a valid email address, 
//          indicating the sender.
//          $subject is of type string, indicating the email subject.
//          $message is of type string, indicating the message to be send
//          via the email, from $fromEmail's address to $toEmail's address.
// Post:    An email containing $message and the subject $subject from 
//          $fromEmail's address to $toEmail's address if $fromEmail
//          does not indicate a spam email.
function sendMail($toEmail, $fromEmail, $subject, $message)
{
    $validFromEmail = spamcheck($fromEmail);
    if($validFromEmail)
    {
        mail($toEmail, $subject, $message, "From: $fromEmail");
    }
}

所以你可以从我们放样的地方继续。首先:您实际上并不需要花括号。另外..您正在覆盖 $body 变量,将变量分开会更有意义。我还建议将主题存储在变量中。

$subject = 'Contact Form Submission'; // Place where we left of at Checkpoint.
$message = "Name: $name\n\nWebsite :$web\n\nComments:$body"; // Place where we left of at Checkpoint.
sendMail($email, $receiver, $subject, $message); // Place where we left of at Checkpoint.

您还必须确保您可以访问 SMTP(简单邮件传输协议),因此如果您没有要使用的 SMTP 服务器,使用 localhost 可能还不够。如果你有一个,但它不工作,那么你可能需要配置 php ini 设置:

ini_set('SMTP' , 'smtp.yourwebsite.com');
ini_set('smtp_port' , '25');
ini_set('username' , 'example@example.com');
ini_set('password' , 'pass');
ini_set('sendmail_from' , 'example@example.com');

注意:最好把修改后的邮件函数实际设为RETURN邮件函数,这样你就可以检查它是否成功了。

于 2013-04-08T16:07:54.207 回答