我想在PHP中的电子邮件中添加更多1个附件。我目前的代码如下。这是一个电子邮件表单,可以向电子邮件发送多个附件。
有关如何使其发挥作用的任何想法?我需要在Array $ Anhang中使用foreach循环吗?谢谢!
<?php
error_reporting(E_ALL);
function mail_att($to,$subject,$message,$anhang)
{
$absender = "xxx";
$absender_mail = "xxx@xxx.com";
$reply = "info@xxx.xxx";
$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("info@xxx","Email mit Anhang","Im Anhang ist eine Datei",$anhang);
?>