首页 > 解决方案 > 带附件的文件不发送

问题描述

我在我的表单上使用 ReCaptcha 3。当我尝试发送带有附件的邮件时,会发送邮件但不会发送附件。但是带有附件的邮件可以在没有 reCaptcha 部分的情况下发送。

我一直在寻找几个希望解决这个问题的线程,但我很困惑。

HTML 表单

<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Mail</title>
</head>
<body>
    <form method="post" id="contact-form" enctype="multipart/form-data" action="testmailer.php">
        <input name="fname" placeholder="Your name" title="Your name" type="text" /></div>
        Select one or more files:
        <input name="userfile[]" type="file" multiple="multiple">
        <input type="hidden" name="recaptcha_response" id="recaptchaResponse">
        <input type="submit" id="submit-button" value="Send Files">
    </form>
    <div id="alertm"></div>

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 
    <script async src="https://www.google.com/recaptcha/api.js?render=******site key******"></script>
     
    
    <script>
    $('#contact_form').submit(function() {
        event.preventDefault();
        $('#alertm').text('Processing...').fadeIn(0); 
        grecaptcha.ready(function() {
            grecaptcha.execute('**site key**', {action: 'contact'}).then(function(token) {
                $('#contact_form').prepend('<input type="hidden" name="g-recaptcha-response" value="' + token + '">');
                $.post("form.php",{Name: name, token: token}, function(result) {
                        console.log(result);
                        if(result.success) {
                                alert('Thanks for posting comment.')
                        } else {
                                alert('You are spammer ! Get the @$%K out.')
                        }
                });
            });
        });
  });
  </script>
</body>
</html>

表格邮寄者

<?php

require $_SERVER['DOCUMENT_ROOT'] . '/PHPMailer/src/PHPMailer.php';
use PHPMailer\PHPMailer\PHPMailer;

$mail = new PHPMailer();
$msg = '';

    if(isset($_POST['fname'])){
        $name=$_POST['fname'];
    }
    if (array_key_exists('userfile', $_FILES)) { 
        $email_to = "toemail@domain.com"; 
        $mail->setFrom('fromemail@somedomain.com', 'From Name');
        $mail->addAddress($email_to);
        $mail->Subject = 'Mail Subject';
        $body = "Name - $name\n";
        $mail->Body = $body;
        $mail->IsHTML(true);
        echo $body;

        for ($ct = 0; $ct < count($_FILES['userfile']['tmp_name']); $ct++) 
        {
            $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name'][$ct]));
            $filename = $_FILES['userfile']['name'][$ct];
            if (move_uploaded_file($_FILES['userfile']['tmp_name'][$ct], $uploadfile)) {
                if (!$mail->addAttachment($uploadfile, $filename)) {
                    $msg .= 'Failed to attach file ' . $filename;
                }
            } else {
                $body .= 'Failed to move file to ' . $uploadfile;
            }
        }
    }
    $error_output = '';
    $success_output = '';

    $recaptcha_url = 'https://www.google.com/recaptcha/api/siteverify';
    $recaptcha_secret = '******secret key******';
    $recaptcha_response = $_POST['recaptcha_response'];

    $recaptcha = file_get_contents($recaptcha_url . '?secret=' . $recaptcha_secret . '&response=' . $recaptcha_response);
    $recaptcha = json_decode($recaptcha);
    
    if ($recaptcha->success == true && $recaptcha->score >= 0.5 && $recaptcha->action == 'contact') {
        if (!$mail->send()) {
            echo 'Mail Sent';
            $msg .= 'Mailer Error: ' . $mail->ErrorInfo;
        } else {
            $msg .= 'Message sent!';
        }
        $success_output = "Your message sent successfully";
    } else {
        $error_output = "Something went wrong. Please try again later";
        echo $msg;
    }
$output = array(
    'error'     =>  $error_output,
    'success'   =>  $success_output
    );
echo json_encode($output);

?>

标签: recaptcha-v3

解决方案


推荐阅读