0

I am new at this, and while searching, I could find the problem I am facing. My question is: I have this form, which is supposed to upload the file, and send it as a mail. How do I display the status of the file being uploaded. Because, people are simply submitting the form without letting the file being uploaded. here's my php code:

<?php

if(isset($_POST['Email'])) 
{   
    $to = "<<email id>>";
    $fromEmail = $_POST['Email']; 
    $fromName = $_POST['Name']; 
    $subject = $_POST['Subject'];
    $mess= $_POST['Description'];
    $message ="\n From: $fromName
                \n Email: $fromEmail
                \n Topic Name: $subject
                \n Article Details: $mess
                \n------------------------------------------------------------
                ";    
    /* Start of headers */ 
    $headers = "From: $fromName"; 
    if(isset($_FILES['attachment']['tmp_name'])) 
    {
            /* GET File Variables */ 
            if($_FILES['attachment']['size']<25000000)
            {
                $tmpName = $_FILES['attachment']['tmp_name']; 
                $fileType = $_FILES['attachment']['type']; 
                $fileName = $_FILES['attachment']['name']; 


                if($_FILES['attachment']['size']>0)
                { 
                    $message .="\n There is an attachment along with this mail.\n";
                    /* Reading file ('rb' = read binary)  */
                    $file = fopen($tmpName,'rb'); 
                    $data = fread($file,filesize($tmpName)); 
                    fclose($file); 

                    /* a boundary string */
                    $randomVal = md5(time()); 
                    $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

                    /* Header for File Attachment */
                    $headers .= "\nMIME-Version: 1.0\n"; 
                    $headers .= "Content-Type: multipart/mixed;\n" ;
                    $headers .= " boundary=\"{$mimeBoundary}\""; 

                    /* Multipart Boundary above message */
                    $message  = "This is a multi-part message in MIME format.\n\n" . 
                    "--{$mimeBoundary}\n" . 
                    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
                    "Content-Transfer-Encoding: 7bit\n\n" . 
                    $message . "\n\n"; 

                    /* Encoding file data */
                    $data = chunk_split(base64_encode($data)); 

                    /* Adding attchment-file to message*/
                    $message .= "--{$mimeBoundary}\n" . 
                    "Content-Type: {$fileType};\n" . 
                    " name=\"{$fileName}\"\n" . 
                    "Content-Transfer-Encoding: base64\n\n" . 
                    $data . "\n\n" . 
                    "--{$mimeBoundary}--\n"; 
                }
            }
            else
            {

            }
        }
    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 

    if($flgchk){
        echo "<script type='text/javascript'>alert('Success');</script>";

    }
    else{
        echo "Error in Email sending";
    }
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="Contact.css">
</head>
<body>
    <div id="form-main">
        <div id="form-div">
            <form action="emailFileArticle.php" method="post" name="mainform" enctype="multipart/form-data" class="form">

                <p class="title">
                    <input name="Subject" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Topic Name" id="Subject" required/>
                </p>

                <p class="text">
                    <textarea name="Description" class="validate[required,length[6,300]] feedback-input" id="Description" placeholder="Please mention somthing about the Video/image being uploaded" required></textarea>
                </p>
                <p class="name">
                    <input name="Name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="Name" required/>
                </p>

                <p class="email">
                    <input name="Email" type="email" class="validate[required,custom[email]] feedback-input" id="Email" placeholder="Email" required/>
                </p>

                <p class="upload">
                    <input name="attachment" type="file" id="attachment" multiple>
                </p>

                <div class="submit">
                    <input type="submit" value="send" name="Submit" id="button-blue"/>
                <div class="ease"></div>            

            </form>
        </div>
    </div>
</body>

Edit: I was looking for a progressbar kind of solution, which I got here, but ended up with the following code:

<?php

    if(isset($_POST['Email'])) 
    {   
        $to = "masterpratiksaboo@gmail.com,";
        $fromEmail = $_POST['Email']; 
        $fromName = $_POST['Name']; 
        $subject = $_POST['Subject'];
        $mess= $_POST['Description'];
        $message ="\n From: $fromName
                    \n Email: $fromEmail
                    \n Topic Name: $subject
                    \n Article Details: $mess
                    \n------------------------------------------------------------
                    ";    
        /* Start of headers */ 
        $headers = "From: $fromName"; 
        if(isset($_FILES['file1']['tmp_name'])) 
        {
?>
    <script>
        function _(el){
            return document.getElementById(el);
        }
        function uploadFile(){
            var file = _("file1").files[0];
            if(file.name=="" || file.name==null)
            {alert("step break II");}
            alert(file.name+" | "+file.size+" | "+file.type);
            var formdata = new FormData();
            formdata.append("file1", file);
            var ajax = new XMLHttpRequest();
            ajax.upload.addEventListener("progress", progressHandler, false);
            ajax.addEventListener("load", completeHandler, false);
            ajax.addEventListener("error", errorHandler, false);
            ajax.addEventListener("abort", abortHandler, false);
            ajax.open("POST", "fin.php");
            ajax.send(formdata);
        }
        function progressHandler(event){
            _("loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
            var percent = (event.loaded / event.total) * 100;
            _("progressBar").value = Math.round(percent);
            _("status").innerHTML = Math.round(percent)+"% uploaded... please wait";
        }
        function completeHandler(event){
            _("status").innerHTML = event.target.responseText;
            _("progressBar").value = 0;
        }
        function errorHandler(event){
            _("status").innerHTML = "Upload Failed";
        }
        function abortHandler(event){
            _("status").innerHTML = "Upload Aborted";
        }
    </script>
<?php
            echo "step break";
            /* GET File Variables */ 
            if($_FILES['file1']['size']<25000000)
            {
                $tmpName = $_FILES['file1']['tmp_name']; 
                $fileType = $_FILES['file1']['type']; 
                $fileName = $_FILES['file1']['name']; 
                if($_FILES['file1']['size']>0)
                { 
                    $message .="\n There is an attachment along with this mail.\n";
                    /* Reading file ('rb' = read binary)  */
                    $file = fopen($tmpName,'rb'); 
                    $data = fread($file,filesize($tmpName)); 
                    fclose($file); 

                    /* a boundary string */
                    $randomVal = md5(time()); 
                    $mimeBoundary = "==Multipart_Boundary_x{$randomVal}x"; 

                    /* Header for File Attachment */
                    $headers .= "\nMIME-Version: 1.0\n"; 
                    $headers .= "Content-Type: multipart/mixed;\n" ;
                    $headers .= " boundary=\"{$mimeBoundary}\""; 

                    /* Multipart Boundary above message */
                    $message  = "This is a multi-part message in MIME format.\n\n" . 
                    "--{$mimeBoundary}\n" . 
                    "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . 
                    "Content-Transfer-Encoding: 7bit\n\n" . 
                    $message . "\n\n"; 

                    /* Encoding file data */
                    $data = chunk_split(base64_encode($data)); 

                    /* Adding attchment-file to message*/
                    $message .= "--{$mimeBoundary}\n" . 
                    "Content-Type: {$fileType};\n" . 
                    " name=\"{$fileName}\"\n" . 
                    "Content-Transfer-Encoding: base64\n\n" . 
                    $data . "\n\n" . 
                    "--{$mimeBoundary}--\n"; 
                }
            }
            else
            {

            }
        }


    $flgchk = mail ("$to", "$subject", "$message", "$headers"); 

    if($flgchk){
        echo "<script type='text/javascript'>alert('Thank you for the Article. Our Content Quality team shall review the post ASAP');</script>";

    }
    else{
        echo "Error in Email sending";
    }
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../Contact.css">
</head>
<body>
    <div id="form-main">
        <div id="form-div">
            <form action="fin.php" method="post" name="mainform" enctype="multipart/form-data" class="form">

                <p class="title">
                    <input name="Subject" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Topic Name" id="Subject" required/>
                </p>

                <p class="text">
                    <textarea name="Description" class="validate[required,length[6,300]] feedback-input" id="Description" placeholder="Please mention somthing about the Video/image being uploaded" required></textarea>
                </p>
                <p class="name">
                    <input name="Name" type="text" class="validate[required,custom[onlyLetter],length[0,100]] feedback-input" placeholder="Name" id="Name" required/>
                </p>

                <p class="email">
                    <input name="Email" type="email" class="validate[required,custom[email]] feedback-input" id="Email" placeholder="Email" required/>
                </p>

                <p class="upload">
                    <input type="file" name="file1" id="file1"><br>

                    <progress id="progressBar" value="0" max="100" style="width:300px;"></progress>
                    <h3 id="status"></h3>
                    <p id="loaded_n_total"></p>
                </p>
                <div class="submit">
                    <input type="submit" value="send" name="Submit" id="button-blue"/>
                <div class="ease"></div>            
            </form>
        </div>
    </div>
</body>
</html>

I have tried a lot of stuff, still am clueless.

BlueLeaf
  • 210
  • 3
  • 15

1 Answers1

0

If you want to prevent to send an email without an attachment you should change your 'attachment check'

Change this line:

if(isset($_FILES['attachment']['tmp_name'])) 

To:

if(!empty($_FILES['attachment']['tmp_name']))
S.Pols
  • 3,414
  • 2
  • 21
  • 42