我正在尝试创建一个带有附件选项的表单,当表单在wordpress网站上提交时,它将发送到电子邮件。
我的代码工作正常,它以HTMLL表格格式向我的电子邮件地址发送电子邮件。此外,我能够发送附件使用代码。当我考虑附件文件扩展名和文件大小时,问题就出现了。我不知道如何限制文件的大容量,并只为一些允许的扩展名设置附件。
我的代码是:
<?php
//Setup an empty array.
$errors = array();
if($_POST["submit"]) {
$to = "[email protected]";
$subject = "New reservations request";
$hotel = $_POST["hotel_url"];
$sender = $_POST["sendername"];
$senderEmail = $_POST["senderEmail"];
//Check the name and make sure that it isn\'t a blank/empty string.
if(empty($sender)){
//Blank string, add error to $errors array.
$errors[\'sendername\'] = "Please enter your name!";
}
/* attachment */
move_uploaded_file($_FILES["attachment"]["tmp_name"],WP_CONTENT_DIR .\'/uploads/\'.basename($_FILES[\'attachment\'][\'name\']));
$attachments = array(WP_CONTENT_DIR ."/uploads/".$_FILES["attachment"]["name"]);
if(empty($errors)){
$mailBody = "<table border=\'1\'>
<tr>
<th>No</td>
<th>Item</td>
<th>Description</td>
</tr>
<tr>
<td>01</td>
<td>Hotel</td>
<td>$hotel</td>
</tr>
<tr>
<td>02</td>
<td>Name</td>
<td>$sender</td>
</tr>
<tr>
<td>03</td>
<td>E-Mail</td>
<td>$senderEmail</td>
</tr>
</table>";
$headers = array(\'From: \'.$_POST[\'sendername\'].\' <\'.$_POST[\'senderEmail\'].\'>\');
$mail_sent = wp_mail( $to, $subject, $mailBody, $headers, $attachments );
}
}
if ($mail_sent) {
?>
<p>Request sent</p>
<?php
} else {
?>
<form id="" name="" action="<?php echo get_permalink(); ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="hotel_url" value="<?php echo get_permalink();?>" />
<div class="section-heading"><h6>Your Details</h6></div>
<div class="label-input-wrapper">
<div class="form-label">Name</div>
<div class="form-input">
<input type="text" name="sendername"/>
<?php if(isset($errors[\'sendername\'])) { echo \'<span style="color: red">\'.$errors[\'sendername\'].\'</span>\'; } ?>
</div>
</div>
<div class="label-input-wrapper">
<div class="form-label">E-Mail</div>
<div class="form-input">
<input type="email" name="senderEmail" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$" required value="<?PHP if(!empty($errors)) { echo $senderEmail;} ?>"/>
</div>
</div>
<label for=\'uploaded_file\'>Select A File To Upload:</label>
<input type="file" name="attachment">
<input type="submit" value="Submit" name="submit">
</form>
<?php
}
?>
上面的代码将附件发送到我的邮件,并将文件保存到我的上传目录中。我知道我必须在这附近做点什么
/* attachment */ 允许文件的特定扩展名和大小。但如何做到这一点呢?如果我不得不允许的话。png。jpg。pdf格式,最大文件容量为1mb,我该怎么做?我必须在何处和哪些代码中修改上述代码?