0

I want to know how to send email attachments using jquery file uploads

from this plugin https://blueimp.github.io/jQuery-File-Upload/

is there a way to do that. or i have to use regular input files

here is my script for sending email

<?php
if(isset($_FILES) && (bool) $_FILES) {
    $AllowedExtensions = ["pdf","doc","docx","gif","jpeg","jpg","png","rtf","txt"];
    $files = [];
    $server_file = [];
    foreach($_FILES as $name => $file) {
        $file_name = $file["name"];
        $file_temp = $file["tmp_name"];
        foreach($file_name as $key) {
            $path_parts = pathinfo($key);
            $extension = strtolower($path_parts["extension"]);
            if(!in_array($extension, $AllowedExtensions)) { die("Extension not allowed"); }
            $server_file[] = "uploads/{$path_parts["basename"]}";
        }
        for($i = 0; $i<count($file_temp); $i++) { move_uploaded_file($file_temp[$i], $server_file[$i]); }
    }
    $to = "admin@itc-4u.net";
    $from = "o0medo90o@gmail.com";
    $subject ="test attachment";
    $message = "this is a test message";
    $headers = "From: $from";
    $semi_rand = md5(time());
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";
    $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n";
    $message .= "--{$mime_boundary}\n";
    $FfilenameCount = 0;
    for($i = 0; $i<count($server_file); $i++) {
        $afile = fopen($server_file[$i],"rb");
        $data = fread($afile,filesize($server_file[$i]));
        fclose($afile);
        $data = chunk_split(base64_encode($data));
        $name = $file_name[$i];
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
            "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
            "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "--{$mime_boundary}\n";
    }
    if(mail($to, $subject, $message, $headers)) {
        echo "<p>mail sent to $to!</p>";
    } else {
        echo "<p>mail could not be sent!</p>";
    }
}

?>

and this is the html for the plugin i want to modify

    <form id="fileupload" method="POST" enctype="multipart/form-data">
    <!-- Redirect browsers with JavaScript disabled to the origin page -->
    <noscript><input type="hidden" name="redirect" value="https://blueimp.github.io/jQuery-File-Upload/"></noscript>
    <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload -->
    <div class="row fileupload-buttonbar">
        <div class="col-lg-7">
            <!-- The fileinput-button span is used to style the file input field as button -->
            <span class="btn btn-success fileinput-button">
                <i class="glyphicon glyphicon-plus"></i>
                <span>Add files...</span>
                <input type="file" name="files[]" multiple>
            </span>
            <button type="submit" class="btn btn-primary start">
                <i class="glyphicon glyphicon-upload"></i>
                <span>Start upload</span>
            </button>
            <button type="reset" class="btn btn-warning cancel">
                <i class="glyphicon glyphicon-ban-circle"></i>
                <span>Cancel upload</span>
            </button>
            <button type="button" class="btn btn-danger delete">
                <i class="glyphicon glyphicon-trash"></i>
                <span>Delete</span>
            </button>
            <input type="checkbox" class="toggle">
            <!-- The global file processing state -->
            <span class="fileupload-process"></span>
        </div>
        <!-- The global progress state -->
        <div class="col-lg-5 fileupload-progress fade">
            <!-- The global progress bar -->
            <div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100">
                <div class="progress-bar progress-bar-success" style="width:0%;"></div>
            </div>
            <!-- The extended global progress state -->
            <div class="progress-extended">&nbsp;</div>
        </div>
    </div>
    <!-- The table listing the files available for upload/download -->
    <table role="presentation" class="table table-striped"><tbody class="files"></tbody></table>

    <input type="submit" value="Submit" />
</form>
  • This seems an odd question. The way you upload the files is not related to whether you then use them as email attachments. They are two separate processes - you upload the files, then you process them - you might save them to disk, to a database, throw them away, email them, anything. Completely unrelated. You can use that plugin to upload files, yes. Then you do exactly what you would have done with a regular "file" input, except now you potentially have multiple files. Anyway you have already written lots of code - does it work? What's the problem that you want help with specifically? – ADyson Dec 15 '16 at 11:57
  • I want to know if there is a way i can use jquery with file input so i can make the file input accept dragging files into it or display the files has been chosen in multipart file input instead of showing the number of files attached. – Mohamed Elkashef Dec 16 '16 at 13:35
  • "make the file input accept dragging files into it" . The plugin you mentioned supports drag and drop, yes. And "display the files has been chosen" - it will also do that. Both of those things are mentioned on its home page and visible if you try the demo. You don't really need me in order to answer those questions. And if you look at the Wiki there are loads of examples showing how to integrate it with PHP https://github.com/blueimp/jQuery-File-Upload/wiki. So...have you encoutered a problem using this or something that you need help with? – ADyson Dec 16 '16 at 15:32
  • thanks for your interest but when i use that plugin it send empty attachment to the email that's why i posted the question. – Mohamed Elkashef Dec 16 '16 at 19:33
  • well presumably that's because your PHP code was not handling the uploaded data properly. I doubt that the email part of it is the issue. See this answer which contains a clear example of how to upload the files properly to a PHP page using that plugin. http://stackoverflow.com/questions/17934689/can-someone-explain-how-to-implement-the-jquery-file-upload-plugin Then once you have the files you can attach them to the email. – ADyson Dec 17 '16 at 20:34

0 Answers0