PHP Contact Form Send Email File Attachment Example

Today, We want to share with you PHP Contact Form Send Email File Attachment Example.In this post we will show you PHP Based Email Form With File Attachment, hear for Send Email with Attachment on Form Submission using PHP we will give you demo and example for implement.In this post, we will learn about How to create PHP based email form with file attachment with an example.

PHP Contact Form Send Email File Attachment Example

There are the Following The simple About PHP Contact Form Send Email File Attachment Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop Send Email with Attachment on Form Submission using PHP, so the some major files and Directory structures for this example is following below.

  • index.php
  • do_submit.php

PHP Based Email Form With File Attachment


This is where I will make a simple HTML form and PHP server side source code for our web application. To make the forms simply all souce code copy and write it into your any text editor Like Notepad++, then save file it as index.php for Web Form HTML with File Upload Field.

Read Also:  Angular 7|8|9 Validate Mobile number With 10 Digits Example

<!-- Display submission status -->
<?php if(!empty($commentStatus)){ ?>
    <p class="commentStatus <?php echo !empty($sendMsgCls)?$sendMsgCls:''; ?>"><?php echo $commentStatus; ?></p>
<?php } ?>
<h1>Send Email with Attachment on Form Submission using PHP</h1>
<!-- Display View contact form Send Email-->
<form method="post" action="do_submit.php" enctype="multipart/form-data">
    <div class="form-group">
        <input type="text" name="name" class="form-control" value="<?php echo !empty($allDataPost['name'])?$allDataPost['name']:''; ?>" placeholder="Name" required="">
    <div class="form-group">
        <input type="email" name="email" class="form-control" value="<?php echo !empty($allDataPost['email'])?$allDataPost['email']:''; ?>" placeholder="Email address" required="">
    <div class="form-group">
        <input type="text" name="mailSub" class="form-control" value="<?php echo !empty($allDataPost['mailSub'])?$allDataPost['mailSub']:''; ?>" placeholder="EmailSubject" required="">
    <div class="form-group">
        <textarea name="comment" class="form-control" placeholder="Write your comment here" required=""><?php echo !empty($allDataPost['comment'])?$allDataPost['comment']:''; ?></textarea>
    <div class="form-group">
        <input type="file" name="file_attach" class="form-control">
    <div class="submit">
        <input type="submit" name="submit" class="btn" value="SUBMIT">

PHP Send Email Comment with attachment files on Form Submission

$allDataPost = $liveMainFlImg = $commentStatus = '';
$sendMsgCls = 'errordiv';
    // Retrive the submitted form data
    $allDataPost = $_POST;
    $email = $_POST['email'];
    $name = $_POST['name'];
    $mailSub = $_POST['mailSub'];
    $comment = $_POST['comment'];
    if(!empty($email) && !empty($name) && !empty($mailSub) && !empty($comment)){
        if(filter_var($email, FILTER_VALIDATE_EMAIL) === false){
            $commentStatus = 'Please enter your valid email.';
            $fileStatusUpload = 1;
                // File path config
                $targetDir = "uploads/";
                $myflname = basename($_FILES["file_attach"]["name"]);
                $targetFilePath = $targetDir . $myflname;
                $fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);
                // Allow certain file formats
                $allowTypes = array('pdf', 'doc', 'docx', 'jpg', 'png', 'jpeg');
                if(in_array($fileType, $allowTypes)){
                    // Upload file to the server
                    if(move_uploaded_file($_FILES["file_attach"]["tmp_name"], $targetFilePath)){
                        $liveMainFlImg = $targetFilePath;
                        $fileStatusUpload = 0;
                        $commentStatus = "Sorry, there some was an error Your uploading your Img or files.";
                    $fileStatusUpload = 0;
                    $commentStatus = 'Sorry, only PDF,JPEG, & PNG, DOC, JPG, files are allowed to upload.';
            if($fileStatusUpload == 1){
                //Send Email Recipient
                $tosendEmail = '[email protected]';

                // Sender
                $from = '[email protected]';
                $fromName = 'Pakainfo';
                // EmailSubject
                $sendMailSub = 'Contact Request Submitted by '.$name;
                // PHP send Email Comment 
                $htmlContent = '<h2>Contact Request Submitted</h2>
                    <p><b>Name:</b> '.$name.'</p>
                    <p><b>Email:</b> '.$email.'</p>
                    <p><b>EmailSubject:</b> '.$mailSub.'</p>
                // PHP send Email Header for sender info
                $headers = "From: $fromName"." <".$from.">";

                if(!empty($liveMainFlImg) && file_exists($liveMainFlImg)){
                    // PHP send Email Boundary 
                    $semi_rand = md5(time()); 
                    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; 
                    // PHP send Email Headers for file_attach 
                    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; 
                    // PHP send Email Multipart boundary 
                    $comment = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" .
                    "Content-Transfer-Encoding: 7bit\n\n" . $htmlContent . "\n\n"; 
                    // PPHP send Email reparing file_attach
                        $comment .= "--{$mime_boundary}\n";
                        $fp =    @fopen($liveMainFlImg,"rb");
                        $data =  @fread($fp,filesize($liveMainFlImg));
                        $data = chunk_split(base64_encode($data));
                        $comment .= "Content-Type: application/octet-stream; name=\"".basename($liveMainFlImg)."\"\n" . 
                        "Content-Description: ".basename($liveMainFlImg)."\n" .
                        "Content-Disposition: attachment;\n" . " filename=\"".basename($liveMainFlImg)."\"; size=".filesize($liveMainFlImg).";\n" . 
                        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
                    $comment .= "--{$mime_boundary}--";
                    $backURL = "-f" . $email;
                    // Send email
                    $mail = mail($tosendEmail, $sendMailSub, $comment, $headers, $backURL);
                     // Config or Set here content-type header for sending HTML email
                    $headers .= "\r\n". "MIME-Version: 1.0";
                    $headers .= "\r\n". "Content-type:text/html;charset=UTF-8";
                    // Send email
                    $mail = mail($tosendEmail, $sendMailSub, $htmlContent, $headers); 
                // If mail sent
                    $commentStatus = 'Your contact request has been submitted successfully !';
                    $sendMsgCls = 'divofsuccess';
                    $allDataPost = '';
                    $commentStatus = 'Your Main contact request as well as submission failed, please try again.';
        $commentStatus = 'Please Full fill all the fields.';

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  php send mail attachment with Pear Mail


You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about PHP Contact Form Send Email File Attachment Example.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PayPal Standard Payment Gateway Integration in Vuejs
  2. Read Also:  PHP SEND mail with Multiple Attachments
  3. Read Also:  PHP Mail Attachment - Php Send Email File-Images Attachments
  4. Read Also:  Laravel 7 Custom Pagination View Example Tutorial
  5. Read Also:  Form Input Bindings in vue.js
  6. Read Also:  Export data mysql table to csv file using PHP
  7. Read Also:  php send email with attachment example
  8. Read Also:  PHP Contact Form Send Email Add More File Attachment
  9. Read Also:  jquery empty/null json object Example
  10. Read Also:  PHP Multiple Email Attachments with Sending Email AJAX Multiple File upload