我正在使用 http://blog.teamtreehouse.com/create-ajax-contact-form 中的 php 邮件脚本连接到我正在构建的网站上的联系表单。
我已将所有内容放在一起,并且似乎运行正常(不显示任何错误),但我没有收到大部分电子邮件。如果我使用我的个人电子邮件帐户之一测试联系表单,我将收到该消息。如果我使用任何其他电子邮件(例如来自 http://www.fakemailgenerator.com 的电子邮件),我将不会收到任何内容。我已附上下面的所有代码。我是否遗漏了任何人都可以发现的东西?
如果有帮助,该网站托管在 AWS LAMP Linux 实例中,域属于 GoDaddy。
提前致谢。
contact.html
<form id="ajax-contact" method="post" action="mailer.php">
<div class="field">
<input type="text" id="name" name="name" placeholder="name*" required>
</div>
<div class="field">
<input type="email" id="email" name="email" placeholder="email*" required>
</div>
<div class="field">
<input type="text" id="school" name="school" placeholder="school" >
</div>
<div class="field">
<input type="tel" id="phone" name="phone" placeholder="phone" >
</div>
<div class="field">
<textarea id="message" name="message" placeholder="message" ></textarea>
</div>
<div class="field">
<button type="submit">Submit</button>
</div>
</form>
mailer.php
<?php
// My modifications to mailer script from:
// http://blog.teamtreehouse.com/create-ajax-contact-form
// Added input sanitizing to prevent injection
// Only process POST reqeusts.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Get the form fields and remove whitespace.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$phone = trim($_POST["phone"]);
$school = trim($_POST["school"]);
$message = trim($_POST["message"]);
// Check that data was sent to the mailer.
if ( empty($name) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Set a 400 (bad request) response code and exit.
http_response_code(400);
echo "There was a problem with your submission. Please ensure all required fields are filled out.";
exit;
}
// Set the recipient email address.
// FIXME: Update this to your desired email address.
$recipient = "sample@gmail.com";
// Set the email subject.
$subject = "Sample subject - New contact from $name";
// Build the email content.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "School: $school\n";
$email_content .= "Phone: $phone\n";
$email_content .= "Message:\n$message\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. We'll get back to you as soon as possible.";
} 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. Please try again later.";
}
} 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.";
}
?>
app.js
$(function() {
// Get the form.
var form = $('#ajax-contact');
// 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('error');
$(formMessages).addClass('success');
// Set the message text.
$(formMessages).text(response);
// Clear the form.
$('#name').val('');
$('#email').val('');
$('#phone').val('');
$('#school').val('');
$('#message').val('');
})
.fail(function(data) {
// Make sure that the formMessages div has the 'error' class.
$(formMessages).removeClass('success');
$(formMessages).addClass('error');
// 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.');
}
});
});
});
最佳答案
我认为亚马逊只是过滤可疑的邮件提供商。大多数 SMTP 服务器都带有过滤器。这些过滤器可防止用户接收垃圾邮件。
正如 Amazon FAQ 所说:
Amazon SES uses in-house anti-spam/anti-virus technologies to filter email messages containing poor-quality content and prevent them from being sent.
关于javascript - PHP 电子邮件脚本仅发送一些消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31803846/