我正在使用php邮件程序填写联系表单。虽然我没有收到电子邮件,但我能够收到发送电子邮件的成功消息。 这是我的代码:
contact.php
<form method="post" id="contactForm" action="contactProcess.php">
<div class="clearfix">
<div class="grid_6 alpha fll"><input type="text" name="senderName" id="senderName" placeholder="Name *" class="requiredField" /></div>
<div class="grid_6 omega flr"><input type="text" name="senderEmail" id="senderEmail" placeholder="Email Address *" class="requiredField email" /></div>
</div>
<div><textarea name="message" id="message" placeholder="Message *" class="requiredField" rows="8"></textarea></div>
<input type="submit" id="sendMessage" name="sendMessage" value="Send Email" />
<span> </span>
</form>
contactProcess.php
<?php
include 'library.php'; // include the library file
include "classes/class.phpmailer.php"; // include the class name
if(isset($_POST["sendMessage"])){
$name = $_POST['senderName'];
$email = $_POST['senderEmail'];
$message = $_POST['message'];
$mail = new PHPMailer; // call the class
$mail->IsSMTP();
$mail->SMTPDebug = 2;
$mail->Host = SMTP_HOST; //Hostname of the mail server
$mail->Port = SMTP_PORT; //Port of the SMTP like to be 25, 80, 465 or 587
$mail->SMTPAuth = true; //Whether to use SMTP authentication
$mail->Username = SMTP_UNAME; //Username for SMTP authentication any valid email created in your domain
$mail->Password = SMTP_PWORD; //Password for SMTP authentication
$mail->From = $email; //From address of the mail
$mail->FromName = $name;
$mail->Subject = ("Mail From Contact Form"); //Subject of your mail
$mail->IsHTML(true);
$mail->AddAddress("me@add.com");//To address who will receive this email
$mail->AddCC("add@add.com");
$mail->AddReplyTo("add2@add.com", "Me");
$mail->Body = $message;
$mail->AltBody = $message;
$send = $mail->Send(); //Send the mails
if($send){ ?>
<script language="javascript" type="text/javascript">
alert('sent');
window.location = 'contact.php';
</script>
<?php
}
else{ ?>
<script language="javascript" type="text/javascript">
alert('Message failed');
window.location = 'contact.php';
</script>
<?php
}
}
?>
最后是ajax验证脚本: main.js
// Ajax Contact
if ($("#contactForm")[0]) {
$('#contactForm').submit(function () {
$('#contactForm .error').remove();
$('.requiredField').removeClass('fielderror');
$('.requiredField').addClass('fieldtrue');
$('#contactForm span strong').remove();
var hasError = false;
$('#contactForm .requiredField').each(function () {
if (jQuery.trim($(this).val()) === '') {
var labelText = $(this).prev('label').text();
$(this).addClass('fielderror');
$('#contactForm span').html('<strong>*Please fill out all fields.</strong>');
hasError = true;
} else if ($(this).hasClass('email')) {
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
if (!emailReg.test(jQuery.trim($(this).val()))) {
var labelText = $(this).prev('label').text();
$(this).addClass('fielderror');
$('#contactForm span').html('<strong>Your email address is incorrect</strong>');
hasError = true;
}
}
});
if (!hasError) {
$('#contactForm').slideDown('normal', function () {
$("#contactForm #sendMessage").addClass('load-color');
$("#contactForm #sendMessage").attr("disabled", "disabled").val('Sending message. Please wait...');
});
var formInput = $(this).serialize();
$.post($(this).attr('action'), formInput, function (data) {
$('#contactForm').slideUp("normal", function () {
$(this).before('<div class="notification-box notification-box-success"><p><i class="icon-ok"></i>Thanks!</strong> Your email was successfully sent. We will get back to you soonest!.</p></div>');
});
});
}
return false;
});
}
我试图设置$ mail-&gt; SMTPDebug = 2;还包括error_reporting(E_ALL);希望我能得到任何错误,但没有任何反应。
请提出任何想法!
答案 0 :(得分:0)
我在服务器上尝试了您的代码,相应地设置了SMTP_ *值。就我而言,主机是&#34; localhost&#34;,端口25,以及SMTP Auth&#34; false&#34;。消息已正确排队。
所以我的猜测是邮件设置存在问题。检查所有SMTP_ *值(我假设您在library.php或其中包含的文件中设置)是正确的。
在评论window.location()
电话时,我能够从SMTP调试中看到此输出。
SMTP -> FROM SERVER: 220 <server name edited> ESMTP Postfix (Debian/GNU)
SMTP -> FROM SERVER: 250-<server name edited> 250-PIPELINING 250-SIZE 10240000 250-VRFY 250-ETRN 250-ENHANCEDSTATUSCODES 250-8BITMIME 250 DSN
SMTP -> FROM SERVER: 250 2.1.0 Ok
SMTP -> FROM SERVER: 250 2.1.5 Ok
SMTP -> FROM SERVER: 354 End data with .
SMTP -> FROM SERVER: 250 2.0.0 Ok: queued as C39A6440AA
SMTP -> FROM SERVER: 221 2.0.0 Bye
总而言之,您的代码似乎没问题,您可能遇到配置问题。
希望这有帮助!