duanjiao6730 2015-03-06 18:40
浏览 31

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

I want to mail two attachments and choice file from two different input tags. I fine a code that send only one file. my code is

<?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 . "
";
        $header .= "Replay-To: ". $to . "
";
        $header .= "MIME-Version: 1.0
";

        $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"

";
        $header .= "This is multi-part message in MIME format. 
";

        $header .= "--".$uid."
";
        $header .= "Content-type:text/plain; charset=iso-8859-1
";
        $header .= "Content-Transfer-Encoding: 7bit

";
        $header .= $message."

";

        $header .= "--".$uid."
";
        $header .= "Content-Type: ".$file_type."; name=\"".$file_name."\"
";
        $header .= "Content-Transfer-Encoding: base64
";
        $header .= "Content-Disposition: attachment; filename=\"".$file_name."\"

";
        $header .= $content."

";

        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>

Now I want to put one more input file tag. I tried many time but not worked. kindly help me out.

  • 写回答

1条回答 默认 最新

  • douxieshang5577 2015-03-06 19:08
    关注

    This script does what you want. Gets the files from the input, put them in an array then loop through the file array when its time to attach. Your code and this can be combined smartly

    <?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 .= "
    MIME-Version: 1.0
    " . "Content-Type: multipart/mixed;
    " . " boundary=\"{$mime_boundary}\""; 
    
    // multipart boundary 
    $message = "This is a multi-part message in MIME format.
    
    " . "--{$mime_boundary}
    " . "Content-Type: text/plain; charset=\"iso-8859-1\"
    " . "Content-Transfer-Encoding: 7bit
    
    " . $message . "
    
    "; 
    $message .= "--{$mime_boundary}
    ";
    
    // 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\"};
    " . " name=\"$files[$x]\"
    " . 
        "Content-Disposition: attachment;
    " . " filename=\"$files[$x]\"
    " . 
        "Content-Transfer-Encoding: base64
    
    " . $data . "
    
    ";
        $message .= "--{$mime_boundary}
    ";
    }
    
    // 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>"; 
    } 
    
    ?>

    Hope it helps

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常