I’m making an attempt to create a type with an attachment possibility that might be despatched to an e-mail when the shape is submitted on a wordpress web site.
My codes work tremendous and it sends e-mail in a HTMKL desk format to my e-mail handle. Additionally I’m able to ship attachment utilizing the codes. The difficulty arises once I think about the attachment file extensions and file measurement. I have no idea that how one can limit massive measurement of recordsdata and set attachments for some allowed extensions solely.
My code is:
<?php
//Setup an empty array.
$errors = array();
if($_POST["submit"]) {
$to = "myemail@gmail.com";
$topic = "New reservations request";
$resort = $_POST["hotel_url"];
$sender = $_POST["sendername"];
$senderEmail = $_POST["senderEmail"];
//Examine the identify and ensure that it is not a clean/empty string.
if(empty($sender)){
//Clean string, add error to $errors array.
$errors['sendername'] = "Please enter your identify!";
}
/* 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 = "<desk border='1'>
<tr>
<th>No</td>
<th>Merchandise</td>
<th>Description</td>
</tr>
<tr>
<td>01</td>
<td>Lodge</td>
<td>$resort</td>
</tr>
<tr>
<td>02</td>
<td>Title</td>
<td>$sender</td>
</tr>
<tr>
<td>03</td>
<td>E-Mail</td>
<td>$senderEmail</td>
</tr>
</desk>";
$headers = array('From: '.$_POST['sendername'].' <'.$_POST['senderEmail'].'>');
$mail_sent = wp_mail( $to, $topic, $mailBody, $headers, $attachments );
}
}
if ($mail_sent) {
?>
<p>Request despatched</p>
<?php
} else {
?>
<type id="" identify="" motion="<?php echo get_permalink(); ?>" methodology="submit" enctype="multipart/form-data">
<enter sort="hidden" identify="hotel_url" worth="<?php echo get_permalink();?>" />
<div class="section-heading"><h6>Your Particulars</h6></div>
<div class="label-input-wrapper">
<div class="form-label">Title</div>
<div class="form-input">
<enter sort="textual content" identify="sendername"/>
<?php if(isset($errors['sendername'])) { echo '<span type="colour: pink">'.$errors['sendername'].'</span>'; } ?>
</div>
</div>
<div class="label-input-wrapper">
<div class="form-label">E-Mail</div>
<div class="form-input">
<enter sort="e-mail" identify="senderEmail" sample="[a-z0-9._%+-]+@[a-z0-9.-]+.[a-z]{2,4}$" required worth="<?PHP if(!empty($errors)) { echo $senderEmail;} ?>"/>
</div>
</div>
<label for='uploaded_file'>Choose A File To Add:</label>
<enter sort="file" identify="attachment">
<enter sort="submit" worth="Submit" identify="submit">
</type>
<?php
}
?>
The above code ship the attachment to my mail and it saves the file into my uploads dirctory.
I do know I’ve to do one thing round this space /* attachment */ to permit particular extensions and measurement of the file. however how to try this?
eg: if I’ve to permit .png, .jpg, .pdf solely and the utmost file is 1mb how can I do this? the place and what code I’ve to amend into the above codes?