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