PHPMailer 5.2.0 每次发出2封邮件

发布于 2024-12-19 05:56:51 字数 3879 浏览 2 评论 0原文

首先;抱歉,如果之前已经回答过这个问题,我最近几天一直在寻找,但没有运气。

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执手闯天涯 2024-12-26 05:56:51

我认为您的问题是您将 ajaxForm 添加到表单中,该表单本身会自动发送表单。但是,在发送之前,您将其发送到验证它的 validate 函数,然后您还使用 ajaxSubmit 提交表单。

删除ajaxSubmit并且应该修复它,因为ajaxForm将自行提交

我认为这会起作用

$(document).ready(function() { 
    var options = { 
        beforeSubmit:  validate,  // pre-submit callback 
        success:       showResponse  // post-submit callback 

    }; 

    // bind form using 'ajaxForm' 
    $('#myForm').ajaxForm(options); 
}); 



function showResponse(responseText, statusText, xhr, $form){
    $('#results').removeClass('info').addClass('success');
    $('#results').html('<strong><p>Beskeden er sendt!</p></strong>');
    //Får tilbagemelding fra processed.php
    //alert(statusText);
}

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; 
            } 
        }
    }

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 the validate function which validates it, and then you are submitting the form in there also by using ajaxSubmit.

Remove the ajaxSubmit and that should fix it as the ajaxForm will submit it itself

I think this will work

$(document).ready(function() { 
    var options = { 
        beforeSubmit:  validate,  // pre-submit callback 
        success:       showResponse  // post-submit callback 

    }; 

    // bind form using 'ajaxForm' 
    $('#myForm').ajaxForm(options); 
}); 



function showResponse(responseText, statusText, xhr, $form){
    $('#results').removeClass('info').addClass('success');
    $('#results').html('<strong><p>Beskeden er sendt!</p></strong>');
    //Får tilbagemelding fra processed.php
    //alert(statusText);
}

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; 
            } 
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文