PHPMailer 5.2.0 每次发出2封邮件
首先;抱歉,如果之前已经回答过这个问题,我最近几天一直在寻找,但没有运气。
我正在使用 PHPMailer 5.2.0 从表单发送邮件,该表单由 jQuery 脚本的 AJAX 调用调用 jQuery表单,每次我发送邮件时它都会发送两次。肯定是脚本被调用了两次,但我不知道它发生在哪里。
这是 php 脚本:
<?php
require_once('PHPMailer_5.2.0/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$navn= $_REQUEST['navn'];
$tlf= $_REQUEST['tlf'];
$ring= isset($_REQUEST['ring']) ? $_REQUEST['ring'] : 0;
if($ring == 0)
{
$ring2 = " - og vil gerne ringes tilbage.";
}
else
{
$ring = "";
}
$email= $_REQUEST['email'];
$sendToOwn= isset($_REQUEST['sendToOwn']) ? $_REQUEST['sendToOwn'] : 0;
$besked= $_REQUEST['besked'];
$mail = new PHPMailer();
//$body = "testing 30/11";//file_get_contents('contents.html');
//$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "SMTP.Server.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SetFrom('[email protected]', 'Name');
$mail->ClearReplyTos("");
$mail->AddReplyTo("[email protected]","Name");
$mail->Subject = "Besked fra mjtransportogflyt.dk";
$mail->MsgHTML("TEST FOR HELVEDE");//"<p>Denne mail er sendt af ".$navn." (".$email.") - ".$tlf.$ring2."</p><p>".$besked."</p>");
$mail->AltBody = "Denne mail er sendt af ".$navn." (".$email.") - ".$tlf.$ring2."<br /> ".$besked; // optional, comment out and test
//$mail->MsgHTML($body);
$address = "[email protected]";
$mail->AddAddress($address, "Kasper John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
这是 jQuery:
$(document).ready(function(){
function validate(formData, jqForm, options) {
for (var i=0; i < formData.length; i++) {
if (!formData[i].value) {
$('#results').removeClass('info').addClass('error');
$('#results').html('<p><strong>Udfyld venligst alle felter</strong></p><p>Din besked blev <strong>ikke</strong> sendt</p>');
//Test om det bliver kaldt
//alert('works?');
return false;
}
}
var options = {
//target: '#results',
success: showResponse
};
function showResponse(statusText){
$('#results').removeClass('info').addClass('success');
$('#results').html('<strong><p>Beskeden er sendt!</p></strong>');
//Får tilbagemelding fra processed.php
//alert(statusText);
}
$('#myform').ajaxSubmit(options);
}
$('#myform').ajaxForm({beforeSubmit:validate})
});
最后是表单:
<form id="myform" method="post" action="processed.php">
Lots and lots of inputs and stuff
<!--submit button below-->
<button class="btn action">Send besked</button>
</form>
First of all; sorry if this has been asnwered before, I have searched for the last couple of days with no luck.
I am using PHPMailer 5.2.0 to send a mail from a form, which is called by an AJAX call from a jQuery script with jQuery form, each time I send the mail it sends it out twice. It must be that the script is called twice but I can't figure out where it happens.
Here is the php script:
<?php
require_once('PHPMailer_5.2.0/class.phpmailer.php');
//include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
$navn= $_REQUEST['navn'];
$tlf= $_REQUEST['tlf'];
$ring= isset($_REQUEST['ring']) ? $_REQUEST['ring'] : 0;
if($ring == 0)
{
$ring2 = " - og vil gerne ringes tilbage.";
}
else
{
$ring = "";
}
$email= $_REQUEST['email'];
$sendToOwn= isset($_REQUEST['sendToOwn']) ? $_REQUEST['sendToOwn'] : 0;
$besked= $_REQUEST['besked'];
$mail = new PHPMailer();
//$body = "testing 30/11";//file_get_contents('contents.html');
//$body = eregi_replace("[\]",'',$body);
$mail->IsSMTP(); // telling the class to use SMTP
$mail->Host = "SMTP.Server.com"; // SMTP server
$mail->SMTPDebug = 2; // enables SMTP debug information (for testing)
// 1 = errors and messages
// 2 = messages only
$mail->SetFrom('[email protected]', 'Name');
$mail->ClearReplyTos("");
$mail->AddReplyTo("[email protected]","Name");
$mail->Subject = "Besked fra mjtransportogflyt.dk";
$mail->MsgHTML("TEST FOR HELVEDE");//"<p>Denne mail er sendt af ".$navn." (".$email.") - ".$tlf.$ring2."</p><p>".$besked."</p>");
$mail->AltBody = "Denne mail er sendt af ".$navn." (".$email.") - ".$tlf.$ring2."<br /> ".$besked; // optional, comment out and test
//$mail->MsgHTML($body);
$address = "[email protected]";
$mail->AddAddress($address, "Kasper John Doe");
//$mail->AddAttachment("images/phpmailer.gif"); // attachment
//$mail->AddAttachment("images/phpmailer_mini.gif"); // attachment
if(!$mail->Send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
} else {
echo "Message sent!";
}
?>
And here is the jQuery:
$(document).ready(function(){
function validate(formData, jqForm, options) {
for (var i=0; i < formData.length; i++) {
if (!formData[i].value) {
$('#results').removeClass('info').addClass('error');
$('#results').html('<p><strong>Udfyld venligst alle felter</strong></p><p>Din besked blev <strong>ikke</strong> sendt</p>');
//Test om det bliver kaldt
//alert('works?');
return false;
}
}
var options = {
//target: '#results',
success: showResponse
};
function showResponse(statusText){
$('#results').removeClass('info').addClass('success');
$('#results').html('<strong><p>Beskeden er sendt!</p></strong>');
//Får tilbagemelding fra processed.php
//alert(statusText);
}
$('#myform').ajaxSubmit(options);
}
$('#myform').ajaxForm({beforeSubmit:validate})
});
And last heres the form:
<form id="myform" method="post" action="processed.php">
Lots and lots of inputs and stuff
<!--submit button below-->
<button class="btn action">Send besked</button>
</form>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的问题是您将
ajaxForm
添加到表单中,该表单本身会自动发送表单。但是,在发送之前,您将其发送到验证它的validate
函数,然后您还使用ajaxSubmit
提交表单。删除
ajaxSubmit
并且应该修复它,因为ajaxForm
将自行提交我认为这会起作用
I think what your issue is that you are adding the
ajaxForm
to the form which by itself will automatically send the form. However, before it sends, you are sending it to thevalidate
function which validates it, and then you are submitting the form in there also by usingajaxSubmit
.Remove the
ajaxSubmit
and that should fix it as theajaxForm
will submit it itselfI think this will work