2011-08-26 99 views
0

我想同時發送多個電子郵件。它在某些時候不起作用。多個電子郵件附件不起作用

幫助我....

# 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 
+0

不完全是你的問題的答案,但我想你可能想用http://swiftmailer.org/ – swordfish

+0

做這樣的事情爲什麼'@'使用'mail()'函數?將其移除以查看發送時發生的警告/錯誤 - 這應該有助於您理解問題的原因。 – kpower

+0

郵件()已通電,不建議使用此功能。有些東西讓自己的套接字連接到像phpmailer這樣的郵件服務器是一個更好的主意。 – 2011-08-26 05:14:21

回答

-2

我不知道PHP,但它看起來像你想一次添加多個附件。在Visual Basic中,您需要一次附加一個文件。嘗試使用循環一次附加一個文件,直到連接所有文件。同樣,我不知道PHP的,雖然如此,這是純粹的猜測從我所知道的在Visual Basic

+0

Uhm,看起來像他*是*使用循環一次附加一個文件...?! – deceze

+0

再一次,我不知道PHP,但對我來說,他看起來像他正在使用該爲了建立多個電子郵件。他的代碼中有一個循環,看起來像它做的更多,而不僅僅是添加附件。但是,唉,我不知道PHP。 – Tony318

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

如果你想知道確切的工作,然後請閱讀MIME規範發送電子郵件herehere