带附件的文件不发送

问题描述

我在表单上使用 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);

?>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...