多个电子邮件附件无法正常工作

时间:2011-08-26 03:56:15

标签: php

我正在尝试同时发送多封电子邮件。它不会有效。

帮助我......

# DYNAMICALLY GET ALL ATTACHMENTS   
    for($i=1;$i<count($_FILES)+1; $i++){
        if(!empty($_FILES['atachment'.$i]['tmp_name'])){
            $file_name = $_FILES['atachment'.$i]['name'];
            $temp_name = $_FILES['atachment'.$i]['tmp_name'];
            $file_location = $_FILES['atachment'.$i]['tmp_name'];
            $filetype = $_FILES['atachment'.$i]['type'];
            move_uploaded_file($temp_name,'temp_upload/'.$file_name);   
            $files[] = 'temp_upload/'.$file_name;
        }
    }

    # EMAIL SETTING
    $headers = "From:$from";

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

    // headers for attachment
    $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\"";

    // multipart 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"; 

    # PREPARE ATTACMENTS
    for($x=0;$x<count($files);$x++){
        $file = fopen($files[$x],"rb");
        $data = fread($file,filesize($files[$x]));
        fclose($file);
        $data = chunk_split(base64_encode($data));
        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" .
        "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" .
        "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n";
        $message .= "–{$mime_boundary}–\n";
    }

    # CHECK IF SEND FUNCTION
    $ok = @mail($to, $subject, $message, $headers); 
    if ($ok) { 
        echo "<p>mail sent to $to!</p>"; 
    } else { 
        echo "<p>mail could not be sent!</p>"; 
    } enter code here

2 个答案:

答案 0 :(得分:0)

在这里做错事:

// multipart 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"; 

必须用作:

// multipart boundary
$message = "–{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content- Transfer-Encoding: 7bit\n\n" . "This is a multi-part message in MIME format.\n\n" . 

如果您想知道确切的工作情况,请仔细阅读发送电子邮件herehere

的MIME规范

答案 1 :(得分:-2)

我不知道PHP,但看起来你正试图一次附加多个文件。在Visual Basic中,您需要一次附加一个文件。尝试使用循环一次附加一个文件,直到连接所有文件。同样,我不知道PHP,所以这是我在Visual Basic中所知道的纯粹推测