如何解决带附件的文件不发送
我在表单上使用 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 举报,一经查实,本站将立刻删除。