Contact or feedback form is utilized for online communication and the submitted form data is sent vie email instantly. At the point when the contact form has a document upload field, the document should be sent with email as a attachment. Using the php mail() function, you can send email with attachment and form data in PHP without much of a stretch. In this instructional tutorials, we will guide you how to send email with attachment on form submission using PHP.
The following code gives a moment capacity to make a contact form with document attachment option and use code on the site. Additionally, an email will be sent to a particular email address with a document file attachment. For the better understanding, we will break the PHP contact form with email and document file attachment code in two sections, HTML (Web Form) and PHP (Form Submission). You can put the two sections of the code together in the site page where you need to use contact form with a document file attachment.
Contact Form HTML with File Upload Field
Most importantly we require a HTML Form through which we will upload the file. The following is the HTML Form code with some basic fields (Name, Email, Subject, and Message) and a file input field.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<!DOCTYPE html> <html> <head><title>How to send attachment in an email on form submission using PHP</title></head> <body> <!-- Display submission status --> <?php if(!empty($statusMsg)){ ?> <p><?php echo $statusMsg; ?></p> <?php } ?> <!-- Display contact form --> <form method="post" action="" enctype="multipart/form-data"> <div class="form-group"> <input type="text" name="name" class="form-control" placeholder="Name" required=""> </div> <div class="form-group"> <input type="email" name="email" class="form-control" placeholder="Email address" required=""> </div> <div class="form-group"> <input type="text" name="subject" class="form-control" placeholder="Subject" required=""> </div> <div class="form-group"> <textarea name="message" class="form-control" placeholder="Write your message here" required=""></textarea> </div> <div class="form-group"> <input type="file" name="file" class="form-control"> </div> <div class="submit"> <input type="submit" name="submit" class="btn" value="SEND MESSAGE"> </div> </form> </body> </html> |
Note the “enctype” attribute in form tag. This is a attribute that determines the encoding type utilized while sending data to server. At whatever point we utilize any file input tag, we have to determine this attribute in form tag with value “multipart/form-data”. This encoding type “multipart/form-data” tells that no character will be encoded while sending. It helps in saving the data of uploaded document file. One more thing to take note that the enctype attribute is used with post method.
Presently let’s take a look at the php code that does the real task of sending uploaded document file to email address.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php $statusMsg=''; if(isset($_FILES["file"]["name"])){ $email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $message = $_POST['message']; $fromemail = $email; $subject="Uploaded file attachment"; $email_message = '<h2>Contact Request Submitted</h2> <p><b>Name:</b> '.$name.'</p> <p><b>Email:</b> '.$email.'</p> <p><b>Subject:</b> '.$subject.'</p> <p><b>Message:</b><br/>'.$message.'</p>'; $email_message.="Please find the attachment"; $semi_rand = md5(uniqid(time())); $headers = "From: ".$fromemail; $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; if($_FILES["file"]["name"]!= ""){ $strFilesName = $_FILES["file"]["name"]; $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"]))); $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message .= "\n\n"; $email_message .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . " name=\"{$strFilesName}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $strContent .= "\n\n" . "--{$mime_boundary}--\n"; } $toemail="info@webcure.co.in"; if(mail($toemail, $subject, $email_message, $headers)){ $statusMsg= "Email send successfully with attachment"; }else{ $statusMsg= "Not sent"; } } ?> |
Complete Code:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
<?php $statusMsg=''; if(isset($_FILES["file"]["name"])){ $email = $_POST['email']; $name = $_POST['name']; $subject = $_POST['subject']; $message = $_POST['message']; $fromemail = $email; $subject="Uploaded file attachment"; $email_message = '<h2>Contact Request Submitted</h2> <p><b>Name:</b> '.$name.'</p> <p><b>Email:</b> '.$email.'</p> <p><b>Subject:</b> '.$subject.'</p> <p><b>Message:</b><br/>'.$message.'</p>'; $email_message.="Please find the attachment"; $semi_rand = md5(uniqid(time())); $headers = "From: ".$fromemail; $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; if($_FILES["file"]["name"]!= ""){ $strFilesName = $_FILES["file"]["name"]; $strContent = chunk_split(base64_encode(file_get_contents($_FILES["file"]["tmp_name"]))); $email_message .= "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type:text/html; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $email_message .= "\n\n"; $email_message .= "--{$mime_boundary}\n" . "Content-Type: application/octet-stream;\n" . " name=\"{$strFilesName}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $strContent .= "\n\n" . "--{$mime_boundary}--\n"; } $toemail="info@webcure.co.in"; if(mail($toemail, $subject, $email_message, $headers)){ $statusMsg= "Email send successfully with attachment"; }else{ $statusMsg= "Not sent"; } } ?> <!DOCTYPE html> <html> <head><title>Send attachment on email</title></head> <body> <!-- Display submission status --> <?php if(!empty($statusMsg)){ ?> <p><?php echo $statusMsg; ?></p> <?php } ?> <!-- Display contact form --> <form method="post" action="" enctype="multipart/form-data"> <div class="form-group"> <input type="text" name="name" class="form-control" placeholder="Name" required=""> </div> <div class="form-group"> <input type="email" name="email" class="form-control" placeholder="Email address" required=""> </div> <div class="form-group"> <input type="text" name="subject" class="form-control" placeholder="Subject" required=""> </div> <div class="form-group"> <textarea name="message" class="form-control" placeholder="Write your message here" required=""></textarea> </div> <div class="form-group"> <input type="file" name="file" class="form-control"> </div> <div class="submit"> <input type="submit" name="submit" class="btn" value="SEND MESSAGE"> </div> </form> </body> </html> |
Conclusion
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co