多个文件作为电子邮件附件上传

Multiple File Uploads As Email Attachments

我在文件作为附件通过电子邮件发送之前显示文件。我用这个脚本来显示文件

$(function(){
    var ul = $('#po_award p#file_1');

    $('#po_award').fileupload({
        add: function (e, data) {
            var tpl = $('<li class="dialog"><a style="color: #777777"></a></li>');

            tpl.find('a').text(data.files[0].name)
                .append('<a href="javascript:void(0)"><span style="color: red; float: right">Delete</span></a>');

            data.context = tpl.prependTo(ul);
            tpl.find('span').click(function(){
                if(tpl.hasClass('dialog')){
                    jqXHR.abort();
                }
                tpl.fadeOut(function(){
                    tpl.remove()
                });
            });
            var jqXHR = data.submit();
        },
    });
});

并将此脚本作为索引并调用上面的脚本

<html>
<head>
<script type="text/javascript" src="script.js"></script>
</head>
<body>
    <form action="upload.php" id="form" method="post" enctype="multipart/form-data">
        <div class="fitem">
            <label style="width: 400px">Upload Files :</label>
        </div>
        <div class="fitem" style="float: left">
            <input style="width: 65px; height: 75px; float: right" class="easyui-filebox" name="attachment[]" multiple="true" buttonText="Add Files"/>
        </div>
        <div class="easyui-panel" style="width:440px;height:75px;padding:5px;margin-top:0px">
            <p id="file_1" style="list-style-type: none; margin-top: 0px"></p>
        </div><br>
    </form>

    <button type="submit" name="submit" form="form">Send</button>
</body>
</html>

这是upload.php脚本

<?php
require 'mail/PHPMailerAutoload.php';
include "conn.php";

date_default_timezone_set("Asia/Jakarta");
$id = 1;
$to = 'receiver@email.com';
$subject = 'Test';

if(isset($_POST['submit'])){
$attachment_name = $_FILES['attachment']['name'];
$attachment_type = $_FILES['attachment']['type'];
$attachment = $_FILES['attachment']['tmp_name'];

include 'smtp.php';

$mail->addAddress($to);
$mail->Subject = $subject;
$mail->msgHTML('Tes');

foreach($attachment_name as $key => $att){
    $nama_file = $attachment_name[$key];
    $tmp_file = $attachment[$key];

    $mail->addAttachment($tmp_file, $nama_file);
}

if (!$mail->send()) {
    echo '<script>alert("Failed"); </script>';
} else {
    echo '<script>alert("Success"); </script>';
}
}
?>

我的问题是,当 script.js 包含在索引中时,文件无法出现在电子邮件附件中。但是当 script.js 从索引中删除时,文件可以出现在电子邮件附件中。

有什么解决办法吗?

您没有提到您使用的是哪个文件上传插件,但我所使用的所有插件都是将文件上传到某些服务器端回调,而不是将数据添加到表单本身。 https://github.com/blueimp/jQuery-File-Upload/wiki/Options#add

您需要做的是在单独的脚本中处理上传,例如 return json 以及有关文件的信息。

然后在提交方法的成功回调中,将您需要的数据添加到表单中,以便您可以将其提交给发送电子邮件的脚本