我根据this教程制作了一个联系表单,但我无法使用此表单。 当我点击提交按钮时,没有任何反应(似乎页面正在刷新或在我的实时网站上它返回到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当然是我常规电子邮件地址的占位符。
FIDDLE使用html,js和css。
我做错了什么?
答案 0 :(得分: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邮件功能是明智的,这样你就可以检查它是否成功。