任何人都可以查看我的代码并告诉我为什么电子邮件没有发送?我正在尝试编写一个jQuery Ajax方法,将联系表单数据发布到PHP电子邮件脚本。
以下是联系表单页面的正文:
<body>
<?php require 'Includes/Header.php' ?>
<div id="container1">
<h1>Contact Us</h1>
<form id="form" onSubmit="return validate(this);" method="post" action="customerMsg2.php" >
<table>
<tr>
<td>First Name: </td>
<td><input type="text" name="FirstName" id="FirstName" autofocus></td>
</tr>
<tr>
<td>Last Name: </td>
<td><input type="text" name="LastName" id="LastName"></td>
</tr>
<tr>
<td>Email: </td>
<td><input type="text" name="Email" id="Email"></td>
</tr>
<tr>
<td>Leave a message: </td>
<td><textarea name="Message" id="Message"></textarea></td>
</tr>
<tr>
<td><input type="submit" id="sendEmail" value="Send Email"></td>
</tr>
<input type="hidden" name="submitted" value="true">
</table>
</form>
<div id="ajax-message"></div>
</div>
<div id="container2">
<ul id="slideshow">
<li id="services"><h2>Make a Donation!</h2><br>
Our pets need lots of love and attention while they wait for new homes. Why not give today?<br><input type="button" value="Donate"></li>
<li id="services"><h2>Check Out Our Podcast!</h2><br>
Tune in weekly as Josh gives his unique perspective on pet-ownership and dutifully regulated capitalism.</li>
</ul>
</div>
<script src="slideshow.js"></script>
<script src="ajaxsubmit.js"></script>
<?php require 'Includes/Footer.php' ?>
</body>
</html>
这是jQuery,ajax代码(ajaxsubmit.js):
$(document).ready(function () {
var $form = $('#form');
var ajaxmessage = $('#ajax-message');
$form.submit(function (event) {
event.preventDefault();
var serializedData = $form.serialize();
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: serializedData
})
.done(function (response) {
$(ajaxmessage).removeClass('error');
$(ajaxmessage).addClass('success');
$(ajaxmessage).text(response);
$('#FirstName').val('');
$('#LastName').val('');
$('#Email').val('');
$('#Message').val('');
})
.fail(function (data) {
$(ajaxmessage).removeClass('success');
$(ajaxmessage).addClass('error');
if (data.responseText !== '') {
$(ajaxmessage).text(data.responseText);
} else {
$(ajaxmessage).text('Oops! An error occured and your message could not be sent.');
}
});
});
});
这是PHP脚本:
<?php
ini_set('SMTP', 'smtp.gmail.com');
$to = '(i would put my email address here)';
$firstname = strip_tags(trim($_POST["FirstName"]));
$lastname = strip_tags(trim($_POST["LastName"]));
$message = trim($_POST["Message"]);
$from = trim($_POST['Email']);
$subject = "New contact from $firstname $lastname";
if(mail($to, $subject, $message, $from))
{
echo "Message sent.";
}
else
{
echo "Message not sent.";
}
?>
有什么想法吗?
答案 0 :(得分:0)
这是来自php doc的一个例子:
`<?php
$to = 'nobody@example.com';
$subject = 'the subject';
$message = 'hello';
$headers = 'From: webmaster@example.com' . "\r\n" .
'Reply-To: webmaster@example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
?>
您的标题信息不正确。
再见
答案 1 :(得分:0)
我没有检查你的所有代码,但PHP脚本有几个问题:
首先,邮件功能定义如下:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
您不能只将电子邮件地址字符串设为$additional_headers
。
例如:
$header = "From: " . $from . "\r\n";
mail($to, $subject, $message, $header);
此外,我猜您不能通过gmail服务器发送带有不同标题的电子邮件作为您的电子邮件地址。通常,您的托管服务提供商已经配置了使用php邮件功能发送电子邮件所需的设置。