我有一个 html 表单,我正在使用 PHPMailer 通过电子邮件发送表单内容。在表格中,我有一个文件附件部分。我的要求是文件必须小于 2mb,并且必须是 pdf、doc 或 docx 文件。当我在验证 if 语句中添加时,我的表单停止工作:
if ((isset($_FILES['fileUpload']) && $_FILES['fileUpload']['error'] == UPLOAD_ERR_OK) && filesize($file) < $maxsize && in_array(mime_content_type($file), $types)) {
$mail->AddAttachment($_FILES['fileUpload']['tmp_name'],
$_FILES['fileUpload']['name']);
}
这是完整的 PHP 脚本:
<?php
require_once('assets/PHPMailer/class.phpmailer.php');
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$job = $_POST['job'];
$file = $_FILES['fileUpload']['name'];
$mail = new PHPMailer();
$mail->From = "xxxxx";
$mail->FromName = "xxxxx";
$mail->AddAddress("xxxxx","xxxxx");
$mail->WordWrap = 50;
$mail->Subject = "Job form";
$mail->Body="
First Name: $first_name
Last Name: $last_name
Email: $email
Job: $job";
$maxsize = 2 * 1024 * 1024;
$types = array('application/pdf', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
if (isset($_FILES['fileUpload']) && $_FILES['fileUpload']['error'] == UPLOAD_ERR_OK) && filesize($file) < $maxsize && in_array(mime_content_type($file), $types) {
$mail->AddAttachment($_FILES['fileUpload']['tmp_name'],
$_FILES['fileUpload']['name']);
}
if(!$mail->Send())
{
echo 'Message was not sent.';
echo 'Mailer error: ' . $mail->ErrorInfo;
}
else
{
echo 'Thank you for your feedback.';
}
?>
这是 HTML 表单:
<form enctype="multipart/form-data" action="jobform.php" method="POST">
<input type="text" name="first_name" value="First Name">
<input type="text" name="last_name" value="Last Name">
<input type="text" name="phone" value="Phone"><br />
<input type="text" name="email" value="Email">
<select name="job"><br />
<option>Graphic Design Intern</option>
<option>ASP.Net Developer</opttion>
<option>Marketing Coordinator</option>
<option>Social Media Content Writer</option>
</select><br />
<div><h4>Résumé</h4><input type="file" name="fileUpload" /><br /></div>
<p></p>
<input class="btn btn-large btn-success" type="submit" value="Submit">
</form>