用于上传多个文件的PHP代码

时间:2014-03-21 08:44:02

标签: php function

我有一个功能代码,您可以使用该代码向附件发送信件。如何修改此代码以发送多个文件?

function xmail($from, $to, $subj, $text, $filename)
{
    $f = fopen($filename, "rb");
    $un = strtoupper(uniqid(time()));
    $head = "From: $from\n";
    $head .= "To: \n";
    $head .= "Subject: \n";
    $head .= "X-Mailer: PHPMail Tool\n";
    $head .= "Reply-To: $from\n";
    $head .= "Mime-Version: 1.0\n";
    $head .= "Content-Type:multipart/mixed;";
    $head .= "boundary=\"----------" . $un . "\"\n\n";
    $zag = "------------" . $un . "\nContent-type: text/plain; charset=utf-8\n";
    $zag .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n";
    $zag .= "------------" . $un . "\n";
    $zag .= "Content-Type: application/octet-stream;";
    $zag .= "name=\"" . basename($filename) . "\"\n";
    $zag .= "Content-Transfer-Encoding:base64\n";
    $zag .= "Content-Disposition:attachment;";
    $zag .= "filename=\"" . basename($filename) . "\"\n\n";
        $zag .= chunk_split(base64_encode(fread($f, filesize($filename)))) . "\n";

    return @mail("$to", "$subj", $zag, $head);
}

2 个答案:

答案 0 :(得分:1)

您已经有两个附件,所以只需添加其他附件:

$zag .= "------------" . $un . "\n";
$zag .= "Content-Type: application/octet-stream;";
$zag .= "name=\"" . basename($otherFilename) . "\"\n";
$zag .= "Content-Transfer-Encoding:base64\n";
$zag .= "Content-Disposition:attachment;";
$zag .= "filename=\"" . basename($otherFilename) . "\"\n\n";
$zag .= chunk_split(base64_encode(fread($otherFile, filesize($otherFilename)))) . "\n";

您也可以通过设置其他Content-Transfer-Encoding来使用其他编码,您所要做的就是设置分隔符(在这种情况下为"------------"),以表示新文件开始。

答案 1 :(得分:-1)

function xmail($from, $to, $subj, $text, $filename, $otherFile)
{
    $f = fopen($filename, "rb");
    $f2 = fopen($otherFile, "rb");
    $un = strtoupper(uniqid(time()));
    $head = "From: $from\n";
    $head .= "To: \n";
    $head .= "Subject: \n";
    $head .= "X-Mailer: PHPMail Tool\n";
    $head .= "Reply-To: $from\n";
    $head .= "Mime-Version: 1.0\n";
    $head .= "Content-Type:multipart/mixed;";
    $head .= "boundary=\"----------" . $un . "\"\n\n";
    $zag = "------------" . $un . "\nContent-type: text/plain; charset=utf-8\n";
    $zag .= "Content-Transfer-Encoding: 8bit\n\n$text\n\n";
    $zag .= "------------" . $un . "\n";
    $zag .= "Content-Type: application/octet-stream;";
    $zag .= "name=\"" . basename($filename) . "\"\n";
    $zag .= "Content-Transfer-Encoding:base64\n";
    $zag .= "Content-Disposition:attachment;";
    $zag .= "filename=\"" . basename($filename) . "\"\n\n";
    $zag .= chunk_split(base64_encode(fread($f, filesize($filename)))) . "\n";
    $zag .= "------------" . $un . "\n";
    $zag .= "Content-Type: application/octet-stream;";
    $zag .= "name=\"" . basename($otherFile) . "\"\n";
    $zag .= "Content-Transfer-Encoding:base64\n";
    $zag .= "Content-Disposition:attachment;";
    $zag .= "filename=\"" . basename($otherFile) . "\"\n\n";
    $zag .= chunk_split(base64_encode(fread($f2, filesize($otherFile)))) . "\n";

    return @mail("$to", "$subj", $zag, $head);
}