php - 代码审查 - 在电子邮件脚本上不断出现 500 错误
问题描述
我使用 PHP/AJAX 从表单中通过电子邮件发送一些信息。下面的基本表单,AJAX 和 PHP 脚本,不断收到 500 错误,我不知道为什么!我经常使用相同的设置或非常相似的设置,我已经在服务器上测试了“邮件”功能,并且工作正常。
任何帮助都是极好的!
谢谢
// quickquote form send
$(function() {
// Get the form.
var form = $('#quickquote');
// Get the messages div.
var formMessages = $('#form-messages');
// Set up an event listener for the contact form.
$(form).submit(function(e) {
// Stop the browser from submitting the form.
e.preventDefault();
// Serialize the form data.
var formData = $(form).serialize();
// Submit the form using AJAX.
$.ajax({
type: 'POST',
url: $(form).attr('action'),
data: formData
})
.done(function(response) {
// Make sure that the formMessages div has the 'success' class.
$(formMessages).removeClass('alert alert-danger');
$(formMessages).addClass('alert alert-success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#quickquote')[0].reset();
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('alert alert-success');
$(formMessages).addClass('alert alert-danger');
// Set the message text.
if (data.responseText !== '') {
$(formMessages).text(data.responseText);
} else {
$(formMessages).text('Oops! An error occured and your message could not be sent.');
}
});
});
});
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection
// Get the form fields and remove whitespace.
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST["phone"];
$address = $_POST["address"];
$length = $_POST["length"];
$width = $_POST["width"];
// Set the recipient email address.
// Update this to your desired email address.
$recipient = "email@email.com";
// Set the email subject.
$subject = "Contact Form Request from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$email_content .= "Telephone: $tel\n";
$email_content .= "Address: $address\n";
$email_content .= "Length: $length\n";
$email_content .= "Width: $width\n\n";
// Build the email headers.
$email_headers = "From: $name <$email>";
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// Set a 200 (okay) response code.
http_response_code(200);
echo "Thank You! Your message has been sent.";
} else {
// Set a 500 (internal server error) response code.
http_response_code(500);
echo "Oops! Something went wrong and we couldn't send your message.";
}
} else {
// Not a POST request, set a 403 (forbidden) response code.
http_response_code(403);
echo "There was a problem with your submission, please try again.";
}
解决方案
推荐阅读
- javascript - Webcam.js 错误:尚未在模式上加载网络摄像头
- mysql - Mysql 触发器创建语法错误
- ios - 将 alamofire 响应解析为 JSON 返回 nil
- javascript - for循环中的jquery hasClass不是函数
- css - 对齐 font-variant:smallcaps 与其他元素
- jquery - 从数据库中检索数据并使用 jquery 将数据加载到表中
- java - 项目构建错误:org.springframework.cloud:spring-cloud-starter-sleuth-zipkin:jar 的“dependencies.dependency.version”缺失
- python - 多处理和慢速文件系统
- reactjs - React - 为映射数组中的每个元素打开模态,无论单击哪个元素
- javascript - 添加搜索栏时 mat select 的默认行为发生变化