jQuery文件上传为电子邮件附件

时间:2016-12-15 11:00:31

标签: php email jquery-file-upload

我想知道如何使用jquery文件上传发送电子邮件附件

来自此插件https://blueimp.github.io/jQuery-File-Upload/

有没有办法做到这一点。或者我必须使用常规输入文件

这是我发送电子邮件的脚本

<?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>";
    }
}

&GT;

这是我要修改的插件的html

    <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>

0 个答案:

没有答案