为什么我的PhpMailer代码两次发送相同的电子邮件?

时间:2019-05-29 12:16:05

标签: php html phpmailer

我已经尝试了StackOverflow社区的几乎所有解决方案,但是不幸的是,它没有帮助。我使用的是PhpMailer,它以联系方式两次发送电子邮件。有人可以帮忙,而不是将我的问题标记为标志吗?

我已经尝试过这些方法

$mail->ClearAddresses();
$mail->ClearAttachments();

HTML表单:

<form class="con-form" method="post" action="/contact-action.php">
// Inputs
</form>

Contact-action.php文件:

<?php
        use PHPMailer\PHPMailer\PHPMailer;
        use PHPMailer\PHPMailer\Exception;

        require 'phpmailer/Exception.php';
        require 'phpmailer/PHPMailer.php';
        require 'phpmailer/SMTP.php';

        $recip_address = "info@yourdomain.com";
        $name       = addslashes(strip_tags($_POST['name'])); 
        $sub        = addslashes(strip_tags($_POST['subject']));
        $email      = addslashes(strip_tags($_POST['email'])); 
        $message    = addslashes(strip_tags($_POST['message']));        

        if ( empty($name) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message)) {
                    http_response_code(400);
                    $msg = 'Please complete the form and try again.';
                    echo $msg;
                    exit;
        }

        $mail = new PHPMailer();
        $mail->SMTPOptions = array(
             'ssl' => array(
                'verify_peer' => false,
                'verify_peer_name' => false,
                'allow_self_signed' => true
             )
         );

        //Recipients
        $mail->setFrom($email, $name);
        $mail->addAddress($recip_address);     // Add a recipient 

        //Content
        $mail->Subject = $sub;
        $mail->Body    = "You have been contacted by: ".$name. "
        <br>E-mail: ".$email. "
        <br>Message: ".$message;
        $mail->AltBody = 'Body text';
        $mail->isHTML(true);                   // Set email format to HTML
        //$mail->send();

        if (!$mail->send()) {
            echo "We couldn't send your message.";
        } else {
            echo "Thank You! Your message has been sent.";
        } 

        $mail->ClearAddresses();
        $mail->ClearAttachments();
?>

JavaScript:

var form = $('.con-form');
    var message = $('.contact-msg');
    var form_data;
    // Success function
    function done_func(response) {
        message.fadeIn().removeClass('alert-danger').addClass('alert-success');
        message.text(response);
        setTimeout(function () {
            message.fadeOut();
        }, 50000);
        form.find('input:not([type="submit"]), textarea').val('');
    }
    // fail function
    function fail_func(data) {
            message.fadeIn().removeClass('alert-success').addClass('alert-danger');
            message.text(data.responseText);
            setTimeout(function () {
                message.fadeOut();
            }, 5000);
        }
    form.submit(function (e) {
            e.preventDefault();
            form_data = $(this).serialize();
            $.ajax({
                type: 'POST',
                url: form.attr('action'),
                data: form_data
            })
            .done(done_func)
            .fail(fail_func);
        });

预期的输出将收到一封电子邮件。它会收到电子邮件,但会收到两次。

0 个答案:

没有答案