2015-04-29 53 views
0

我想在PHP中添加1個附件到一封電子郵件中。我目前的代碼如下。這是一個將多個附件發送到電子郵件的電子郵件表單。帶有foreach的多個數組?

關於如何讓它工作的任何想法?我需要Array $ Anhang中的foreach循環嗎?謝謝!

<?php 
error_reporting(E_ALL); 

function mail_att($to,$subject,$message,$anhang) 
    { 
    $absender = "xxx"; 
    $absender_mail = "[email protected]"; 
    $reply = "[email protected]"; 

    $mime_boundary = "-----=" . md5(uniqid(mt_rand(), 1)); 

    $header ="From:".$absender."<".$absender_mail.">\n"; 
    $header .= "Reply-To: ".$reply."\n"; 

    $header.= "MIME-Version: 1.0\r\n"; 
    $header.= "Content-Type: multipart/mixed;\r\n"; 
    $header.= " boundary=\"".$mime_boundary."\"\r\n"; 

    $content = "This is a multi-part message in MIME format.\r\n\r\n"; 
    $content.= "--".$mime_boundary."\r\n"; 
    $content.= "Content-Type: text/html charset=\"iso-8859-1\"\r\n"; 
    $content.= "Content-Transfer-Encoding: 8bit\r\n\r\n"; 
    $content.= $message."\r\n"; 

    //$anhang ist ein Mehrdimensionals Array 
    //$anhang enthält mehrere Dateien 
    if(is_array($anhang) AND is_array(current($anhang))) 
     { 
     foreach($anhang AS $dat) 
     { 
     $data = chunk_split(base64_encode($dat['data'])); 
     $content.= "--".$mime_boundary."\r\n"; 
     $content.= "Content-Disposition: attachment;\r\n"; 
     $content.= "\tfilename=\"".$dat['name']."\";\r\n"; 
     $content.= "Content-Length: .".$dat['size'].";\r\n"; 
     $content.= "Content-Type: ".$dat['type']."; name=\"".$dat['name']."\"\r\n"; 
     $content.= "Content-Transfer-Encoding: base64\r\n\r\n"; 
     $content.= $data."\r\n"; 
     } 
     $content .= "--".$mime_boundary."--"; 
     } 
    else //Nur 1 Datei als Anhang 
     { 
     $data = chunk_split(base64_encode($anhang['data'])); 
     $content.= "--".$mime_boundary."\r\n"; 
     $content.= "Content-Disposition: attachment;\r\n"; 
     $content.= "\tfilename=\"".$anhang['name']."\";\r\n"; 
     $content.= "Content-Length: .".$dat['size'].";\r\n"; 
     $content.= "Content-Type: ".$anhang['type']."; name=\"".$anhang['name']."\"\r\n"; 
     $content.= "Content-Transfer-Encoding: base64\r\n\r\n"; 
     $content.= $data."\r\n"; 
     } 




    if(@mail($to, $subject, $content, $header)) return true; 
    else return false; 
    } 

echo "Dein Name Bitch ist ". $_POST["name"] . "<br>" . $_FILES["Bild1"]["name"]; 

$anhang = array(); 
$anhang["name"] = $_FILES['Bild1']['name']; 
$anhang["size"] = $_FILES['Bild1']['size']; 
$anhang["type"] = $_FILES['Bild1']['type']; 
$anhang["data"] = implode("",file($_FILES['Bild1']['tmp_name'])); 

mail_att("[email protected]","Email mit Anhang","Im Anhang ist eine Datei",$anhang); 

?> 
+0

我有$ _FILES [ 'Bild2'],$ _FILES [ 'Bild3'],$ _FILES [ 'Bild4']和$ _FILES ['Bild5']並且需要將它存儲在$ anhang數組中...我想這就是問題所在。有任何想法嗎 ? – Tdotcom

回答

0

你也可以遍歷所有上傳的文件:

$anhang = array(); 
foreach ($_FILES as $curfile) { 
    if (!empty($curfile['name'])) { 
     $curfile['data'] = file_get_contents($curfile['tmp_name']); 
     $anhang[] = $curfile; 
    } 
} 
+0

謝謝!我嘗試過那樣。我收到附件的電子郵件,但我不能打開附件.... – Tdotcom

+0

我必須添加\t \t $ anhang [「name」] = file_get_contents($ _ FILES ['name']); \t \t $ anhang [「size」] = file_get_contents($ _ FILES ['size']); \t \t $ anhang [「type」] = file_get_contents($ _ FILES ['type']); – Tdotcom

+0

'$ anhang [] = $ curfile;'用'$ curfile'的副本初始化數組,該數組已經有'name','size'和'type'元素。 – Barmar