attached files are wrong... sometimes

40 Views Asked by At

As a beginner i created this code

$o1 = $_POST['pliki'];
            $o12 = $_POST['pliki2'];

            $o2 = $_POST['o2'];
            $o22 = $_POST['o22'];


            $tab1 = explode(',', $o1);
            $tab12 = explode(',', $o12);

            $tab2 = explode(',', $o2);
            $tab22 = explode(',', $o22);

            if(isset($o2) && (bool) $o2) {  


                $format_wydruku1=$_POST['format_wydruku1'];
                $format_wydruku2=$_POST['format_wydruku2'];




                $to = "[email protected]";

                $from = "[email protected]";
                $subject ='Zamówienie od '.$imie.' '.$nazwisko;
                $message = "\n\n";




                $tab111=array_merge($tab1, $tab12);
                $tab222=array_merge($tab2, $tab22);

                for($i = 0; $i<count($tab111); $i++) {
                   $plik2 = "../upload/";
                    $plik2 .= $tab222[$i];


                    $name2 = $tab111[$i];
                    if($i<count($tab1)){
                        $message .= $name2."  ilosc kopii: ".$ile1."   format wydruku:".$format_wydruku1."\n";
                    }else if($i>=count($tab1)){
                        $message .= $name2."  ilosc kopii: ".$ile2."   format wydruku:".$format_wydruku1."\n";
                    }
                }



                /////////////////////////////////////////////////////////////////////////
                $headers = "From: $from";
                $semi_rand = md5(time());
                $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";

                //$headers .="Content-type: text/html; charset=utf-8";
                $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_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";
                $FfilenameCount = 0;


               for($i = 0; $i<count($tab111); $i++) {
                    if($i<count($tab1)){
                        $plik = "../upload/";
                        $plik .= $tab2[$i];

                        $afile = @fopen($plik,"rb");
                        if(empty($afile)){
                            continue;
                        }


                        $data[i] = @fread($afile,filesize($plik));
                        @fclose($afile);

                        $data[i] = chunk_split(base64_encode($data[i]));
                        $name = $tab111[$i];

                        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                            "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                            "Content-Transfer-Encoding: base64\n\n" . $data[i] . "\n\n";
                        $message .= "--{$mime_boundary}\n";
                    }else if($i>=count($tab1)){
                        $plik = "../upload/";
                        $plik .= $tab222[$i];

                        $afile = @fopen($plik,"rb");
                        if(empty($afile)){
                            continue;
                        }


                        $data[i] = @fread($afile,filesize($plik));
                        @fclose($afile);

                        $data[i] = chunk_split(base64_encode($data[i]));
                        $name = $tab111[$i];

                        $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$name\"\n" .
                            "Content-Disposition: attachment;\n" . " filename=\"$name\"\n" .
                            "Content-Transfer-Encoding: base64\n\n" . $data[i] . "\n\n";
                        $message .= "--{$mime_boundary}\n";
                    }
               }

                if(mail($to, $subject, $message, $headers)) {
                    echo '<p>Sent</p>';

                } else {
                    echo "<p>error, try again!</p>";
                }
            }else{

which sends chosen files with some additional informations. It is working, but... sometimes it sends right files, sometime not... have no idea how to fix it, and where exactly problem is. Names of attached are alway right...

0

There are 0 best solutions below