Table of Content
This post teaches you how you can make a PHP based email form that supports file attachment. We will also explain to you how you can confirm the form and size of the uploaded document.
What is a file attachment?
When speaking to email, an attachment is a document delivered with an email message. An attachment may be an image, a record, a picture, a sound file, a spreadsheet, or another document which requires a different application to open it.
Email is mostly utilized to send text messages to other users online. Should you have to send images, videos, files, PDF’s, or another document, you would have to attach that document to an email. As an instance, if you’re applying for a job which you might create your resume at a word processor and deliver a short email message together with all the resume attached to the company.
How to create an HTML5 form with file attachment?
<form method="POST" name="email_form_with_php" action="php-form-action.php" enctype="multipart/form-data"> <label for='name'>Name: </label> <input type="text" name="name" > <label for='email'>Email: </label> <input type="text" name="email" > <label for='message'>Message:</label> <textarea name="message"></textarea> <label for='uploaded_file'>Select A File To Upload:</label> <input type="file" name="uploaded_file"> <input type="submit" value="Submit" name='submit'> </form>
How to validate a file with PHP?
From the PHP script, we’ll first validate the entry, and whether the validation succeeds, then we’ll send the data by email.
We can get the uploaded file and its various characteristics using the $_FILES array. This array will include the title, size, route and other aspects of the uploaded document. The code below has got the title, type and magnitude of the uploaded document:
//Get the uploaded file information $name_of_uploaded_file = basename($_FILES['uploaded_file']['name']); //get the file extension of the file $type_of_uploaded_file = substr($name_of_uploaded_file, strrpos($name_of_uploaded_file, '.') + 1); $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;//size in KBs
After this step, you need to validate the size of the file:
//Settings $max_allowed_file_size = 100; // size in KB $allowed_extensions = array("jpg", "jpeg", "gif", "bmp"); //Validations if($size_of_uploaded_file > $max_allowed_file_size ) { $errors .= "\n Size of file should be less than $max_allowed_file_size"; } //------ Validate the file extension ----- $allowed_ext = false; for($i=0; $i<sizeof($allowed_extensions); $i++) { if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0) { $allowed_ext = true; } } if(!$allowed_ext) { $errors .= "\n The uploaded file is not supported by the file type. ". " Only the following file types are supported: ".implode(',',$allowed_extensions); }
Then, we copy the file and send the email.
//copy the temp. uploaded file to uploads folder $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file; $tmp_path = $_FILES["uploaded_file"]["tmp_name"]; if(is_uploaded_file($tmp_path)) { if(!copy($tmp_path,$path_of_uploaded_file)) { $errors .= '\n error while copying the uploaded file'; } } $message = new Mail_mime(); $message->setTXTBody($text); $message->addAttachment($path_of_uploaded_file); $body = $message->get(); $extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email); $headers = $message->headers($extraheaders); $mail = Mail::factory("mail"); $mail->send($to, $headers, $body);
Now you have a fully functional PHP form with a file attachment.