php邮件附件奇怪的行为

时间:2010-09-30 14:10:58

标签: php smtp email attachment

我需要一些非常奇怪的帮助。我发了一个webmail表单来发送带有多个附件的电子邮件,它昨天开箱即用,但今天只发送1个文件作为附件而不是多个。 O_O

我还将我的SVN恢复到较旧版本并且问题仍然存在,任何想法?这真的很奇怪..工作从一天到另一天制动,我认为问题必须是SMTP服务器或其他东西。求救!

代码:

        $semi_rand = md5(time()); 
    $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";


            $attachments = TRUE;
        $body = "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" .
        $body . "\n\n";
        $body .= "--{$mime_boundary}\n";
        $attach = array();
        for($i=0; $i<count($_FILES['file']['name']);$i++)
        {
            $tmp_name = $_FILES['file']['tmp_name'][$i];
            $type = $_FILES['file']['type'][$i];
            $name = $_FILES['file']['name'][$i];
            $size = $_FILES['file']['size'][$i];

            if (file_exists($tmp_name)){
                $kb = $size/1024;
                $kbSize = round($kb*100)/100;

                if(is_uploaded_file($tmp_name)){
                    $file = fopen($tmp_name,'rb');
                    $fdata = fread($file,filesize($tmp_name)); //stream file to var and send it in headers
                    fclose($file);
                    $fdata = chunk_split(base64_encode($fdata));
                    $body .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" . 
                        "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" . 
                        "Content-Transfer-Encoding: base64\n\n" . $fdata . "\n\n";
                    $body .= "--{$mime_boundary}-\n";
                }
            }       
        }
        $sent = myMailer::sendAttached($from,$to,$subject,$body,$mime_boundary,$multi);

MyMailer类,sendAttached方法:

    public static function sendAttached($from,$to,$subject,$body,$mime,$multi=FALSE)
{

    $headers = 'From: Website <'.$from.'>'. "\r\n".
    'Reply-To: '.$from. "\r\n".
    "MIME-Version: 1.0\r\n" .
    "Content-Type: multipart/mixed;\r\n" .
    " boundary=\"{$mime}\"";

    if($multi == TRUE && is_array($to)){
        foreach($to as $item)
        {
            mail(trim($item),$subject,$body,$headers);
        }
        return TRUE;
    }else{
        return mail($to,$subject,$body,$headers);
    }
}

1 个答案:

答案 0 :(得分:0)

在某些时候你是否可能会在for循环之外移动theo:

$semi_rand = md5(time()); 
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

我从未试图像这样从头开始创建复杂的多部分消息(我总是使用像Swift Mailer或Zend_Mail这样的现有库),但是每个文件都不必是它自己的部分,因此有自己的边界吗?