如何使用php-mime从两个不同的输入文件标记发送2个附件文件


How do i send 2 attachment file from two different input file tag using php mime?

我想从两个不同的输入标记邮寄两个附件和选择文件。我对只发送一个文件的代码进行罚款。我的代码是

<?php
if(isset($_POST) && !empty($_POST))
{
if(!empty($_FILES['attachment']['name']))
{
    $file_name = $_FILES['attachment']['name'];
    $temp_name = $_FILES['attachment']['tmp_name'];
    $file_type = $_FILES['attachment']['type'];

    $base = basename($file_name);
    $extension = substr($base, strlen($base)-4, strlen($base));
    $allow_extension = array(".jpg",".pdf",".png");
    if(in_array($extension,$allow_extension))
    {
        $form = $_POST['email'];
        $to = "hassanh80@gmail.com";
        $subject = "Subject Here";
        $message = "Message Here";
        $file = $temp_name;
        $content = chunk_split(base64_encode(file_get_contents($file)));
        $uid = md5(uniqid(time()));

        $header = "From: ". $form . "'r'n";
        $header .= "Replay-To: ". $to . "'r'n";
        $header .= "MIME-Version: 1.0'r'n";
        $header .= "Content-Type: multipart/mixed; boundary='"".$uid."'"'r'n'r'n";
        $header .= "This is multi-part message in MIME format. 'r'n";
        $header .= "--".$uid."'r'n";
        $header .= "Content-type:text/plain; charset=iso-8859-1'r'n";
        $header .= "Content-Transfer-Encoding: 7bit'r'n'r'n";
        $header .= $message."'r'n'r'n";
        $header .= "--".$uid."'r'n";
        $header .= "Content-Type: ".$file_type."; name='"".$file_name."'"'r'n";
        $header .= "Content-Transfer-Encoding: base64'r'n";
        $header .= "Content-Disposition: attachment; filename='"".$file_name."'"'r'n'r'n";
        $header .= $content."'r'n'r'n";
        if(mail($to,$subject, "", $header))
        {
            echo "Successfull";
        }
        else
        {
            echo "Fail";
        }
    }   
    else
    {
        echo "File Type Not Allow...!";
    }   
}
else
{
    echo "No File Posted...!";
}
}
?>
<form method="post" action="mail1.php" enctype="multipart/form-data">
<input type="email" name="email" />
<br />
<input type="file" name="attachment" />
<br />
<input type="submit" value="Send">
</form>

现在我想再放一个输入文件标记。我试了很多次,但没有成功。请帮帮我。

这个脚本可以执行您想要的操作。从输入中获取文件,将它们放在一个数组中,然后在文件数组中进行附加时循环。你的代码和这个可以智能地组合

<?php
// array with filenames to be sent as attachment
$files = array("file_1.ext","file_2.ext","file_3.ext",......);
// email fields: to, from, subject, and so on
$to = "mail@mail.com";
$from = "mail@mail.com"; 
$subject ="My subject"; 
$message = "My message";
$headers = "From: $from";
// boundary 
$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"; 
$message .= "--{$mime_boundary}'n";
// preparing attachments
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";
}
// send
$ok = @mail($to, $subject, $message, $headers); 
if ($ok) { 
	echo "<p>mail sent to $to!</p>"; 
} else { 
	echo "<p>mail could not be sent!</p>"; 
} 
?>

希望它能帮助