我正在使用HTML表单通过ajax将输入转发到PHP文件,其中使用PHPMailer发送电子邮件。在将数据发送到PHP文件之前,我使用JQuery来验证一些输入(名称和邮件地址)。
这是我的代码:
HTML表单:
<form class="ajax" action="script/send_form.php" method="post">
<input id="name" name="name" type="text" />
<input id="mail" name="mail" type="text" />
<input id="mail" name="note" type="text" />
<button type="submit" id="send">Send</button>
</form>
使用PHPMailer的Autoload功能的PHP文件:
<?
$return = chr(13).chr(10);
$line = "-----------------------------------------------".$return;
$name = '';
$email = '';
if(!empty($_POST['name'])) {
$name = $_POST['name'];
}
$email = $_POST['mail'];
$note = $_POST['note'];
$msg = $note;
require 'phpmailer/PHPMailerAutoload.php';
//Create a new PHPMailer instance
$mail = new PHPMailer;
$mail->CharSet = 'UTF-8';
//Set who the message is to be sent from
$mail->From = $email;
$mail->FromName = $name;
//Set who the message is to be sent to
$mail->addAddress('info@internet.com', 'John Doe');
//Set the subject line
$mail->Subject = 'Some Subject';
$mail->Body = $msg;
//send the message, check for errors
if (!$mail->send()) {
echo "Mail Error: " . $mail->ErrorInfo;
} else {
echo "Sent";
}
?>
Jquery的:
function validateEmail(email) {
var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email);
}
function validate(){
var email = $("#mail").val();
if (validateEmail(email)) {
var mail = $('#mail').val();
} else {
alert ('Bitte gültite E-Mail Adresse eingeben!')
}
}
function dottedButton () {
document.getElementById('send').innerHTML = '...';
}
$('form.ajax').on('submit', function(ev){
ev.preventDefault();
validate ();
var name = $('#name').val();
var mail = $('#mail').val();
if(name && mail){
var that = $(this);
url = that.attr('action');
type = that.attr('method');
contents = that.serialize();
dottedButton ();
$.ajax({
url: url,
type: type,
data: contents,
success: function(response){
document.getElementById('send').innerHTML = 'sent';
}
});
} else {
alert ('pleas fill out all fields!')
};
return false;
});
问题是:在不规则的时间间隔内(每隔一两天)我的收件箱中有一封电子邮件,该邮件是空的,发件人的电子邮件地址是:@。SYNTAX-ERROR。
我不知道为什么会这样。某些机器人可能会自动化PHPMailer吗?
BTW:我的主人是hoststar.at
提前致谢!
答案 0 :(得分:0)
不要这样做:
$mail->From = $email;
$mail->FromName = $name;
您正在尝试伪造发件人地址,但您会遇到投递问题,并且您会对您所看到的问题持开放态度。只需将提交的地址放在邮件正文中,或者如果您需要在标题中进行回复。