Scusate ho questo script php, invia email con e senza allegati, e c'è l'opzione se formato html o testo.

Quindi si può mandare nei seguenti modi a scelta:

- html senza allegato
- html con allegato
- testo senza allegato
- testo con allegato

funziona ma ha qualche errore:

per esempio non manda gli allegati..

sapete dirmi al volo perchè non funziona ?

grazie anticipatamente.

codice:
    <?php
        function check_email($email) {
        // Controllo le lunghezze e la presenza di una unica "@"
            if (!ereg("[^@]{1,64}@[^@]{1,255}", $email)) {
                // Email non valida a causa di un numero errato di caratteri
                    return false;
                }
                // Divido l'indirizzo in parti per semplicità 
            $email_array = explode("@", $email);
            $local_array = explode(".", $email_array[0]);
            for ($i = 0; $i < sizeof($local_array); $i++) {
                    if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
                            return false;
                        }
                }
            if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // IP
                    $domain_array = explode(".", $email_array[1]);
                    if (sizeof($domain_array) < 2) {
                            return false;
                        }
                    for ($i = 0; $i < sizeof($domain_array); $i++) {
                            if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
                                    return false;
                                }
                        }
                }
                
                // Check if mail exchange exists.
                if ( ! getmxrr ($email_array[1], $mxrecords) ) return false;
            return true;
        }
        
        
        session_start();
        
        if ($_POST['invia'] == 'preview')
        {
        
            $email = $_POST['email'];
            $from = $_POST['from'];
            $subject = stripslashes($_POST['subject']);
            $testo = stripslashes($_POST['testo']);
            $formato = $_POST['formato'];
        
            $allegato = $_FILES['allegato']['tmp_name'];
            $allegato_type = $_FILES['allegato']['type'];
            $allegato_name = $_FILES['allegato']['name'];
            $_SESSION["allegato"] = $allegato;
            $_SESSION["allegato_type"] = $allegato_type;
            $_SESSION["allegato_name"] = $allegato_name;
        
                if ($formato == 'text') { $testo_vis = htmlentities($testo, ENT_NOQUOTES); }
                        else { $testo_vis = $testo; $testo = htmlentities($testo, ENT_QUOTES); }
            $testo_vis = ereg_replace("\n", "
", $testo_vis);
        
            if (check_email($from) && check_email($email)) 
                {                
                    print "
                        


                        Stai per spedire una e-mail a $email

                        Mittente: $from

                        Subject: $subject

                        Testo: $testo_vis

</p>";
                    if (is_uploaded_file($allegato)) 
                        {
                            print   "

Invio allegato: $allegato_name</p>";
                        }
                        else 
                        {
                            print "

Nessun allegato</p>";
                        }
                    print "
                        <form action=\"invio_mail.php\" method=\"post\" enctype=\"multipart/form-data\">
                        

<input type=\"hidden\" name=\"email\" value=\"$email\" />
                        <input type=\"hidden\" name=\"from\" value=\"$from\" />
                        <input type=\"hidden\" name=\"subject\" value=\"$subject\" />
                        <input type=\"hidden\" name=\"testo\" value=\"$testo\" />
                        <input type=\"hidden\" name=\"formato\" value=\"$formato\" />
                        <input type=\"hidden\" name=\"allegato\" value=\"$allegato\" />
                        <input type=\"submit\" name=\"invia\" value=\"invia\" /></p>
                        <input type=\"submit\" name=\"back\" value=\"back\" /></p>
                        </form>";
                }
                else {
                    print "

Non è possibile inviare la mail con mittente $from.</p>
                        

Possibili problemi:</p>
                        <ul>[*]Le mail inserite non sono valide[*]Il dominio delle mail non sono raggiungibili[/list]";
                }
        }
        elseif ($_POST['back'] == 'back')
        {
            header("location: contattaci.php");
        }
        elseif ($_POST['invia'] == 'invia') 
        {                                   
            $email = $_POST['email'];
            $from = $_POST['from'];
            $subject = stripslashes($_POST['subject']);
            $mail_body = stripslashes($_POST['testo']);
            $formato = $_POST['formato'];
        
            $mail_headers = "From: $from\n";
            $mail_headers .= "Reply-To: $from\n";
            $mail_headers .= "X-Priority: 1 (Highest)\n";
            $mail_headers .= "MIME-Version: 1.0\r\n";
        
            $allegato      = $_SESSION["allegato"];
            $allegato_name = $_SESSION["allegato_name"];
        
            if ($formato == 'html' && $allegato!="")
                {
                    $mail_headers .= "Content-Type: multipart/mixed; charset=iso-8859-1\r\n";
                    $mail_headers .= " boundary=\"{$mime_boundary}\"";
                    $mail_body = html_entity_decode($mail_body, ENT_QUOTES);
                
                    if (is_uploaded_file($allegato)) 
                        {
                        // Apro e leggo il file allegato
                            $file = fopen($allegato,'rb');
                            $data = fread($file, filesize($allegato));
                            fclose($file);
                                
                                // Adatto il file al formato MIME base64 usando base64_encode
                            $data = chunk_split(base64_encode($data));
                                
                                // Genero il "separatore"
                                // Serve per dividere, appunto, le varie parti del messaggio.
                                // Nel nostro caso separerà la parte testuale dall'allegato
                            $semi_rand = md5(time());
                            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                                
                                // Metto il separatore
                            $mail_body .= "--{$mime_boundary}\n";
                                
                                // Aggiungo l'allegato al messaggio
                            $mail_body .= "Content-Disposition: attachment;\n";
                        
                            $mail_body .= " filename=\"{$allegato_name}\"\n";
                        
                            $mail_body .= "Content-Transfer-Encoding: base64\n\n";
                            $mail_body .= $data . "\n\n";
                                
                                // chiudo con il separatore
                            $mail_body .= "--{$mime_boundary}--\n";
                        }
                }
                elseif ($formato == 'html' && $allegato=="")
                {
                    $mail_headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
                    $mail_body = html_entity_decode($mail_body, ENT_QUOTES);
                }
                elseif($formato == 'testo' && $allegato!="")
                {
                    $mail_headers .= "Content-Type: multipart/mixed;\n";
                    $mail_headers .= " boundary=\"{$boundary}\"";
                
                    if (is_uploaded_file($allegato)) 
                        {
                        
                        // Apro e leggo il file allegato
                            $file = fopen($allegato,'rb');
                            $data = fread($file, filesize($allegato));
                            fclose($file);
                                
                                // Adatto il file al formato MIME base64 usando base64_encode
                            $data = chunk_split(base64_encode($data));
                                
                                // Genero il "separatore"
                                // Serve per dividere, appunto, le varie parti del messaggio.
                                // Nel nostro caso separerà la parte testuale dall'allegato
                            $semi_rand = md5(time());
                            $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
                                // Metto il separatore
                            $mail_body .= "--{$mime_boundary}\n";
                                
                                // Aggiungo l'allegato al messaggio
                            $mail_body .= "Content-Disposition: attachment;\n";
                            $mail_body .= " filename=\"{$allegato_name}\"\n";
                            $mail_body .= "Content-Transfer-Encoding: base64\n\n";
                            $mail_body .= $data . "\n\n";
                                
                                // chiudo con il separatore
                            $mail_body .= "--{$mime_boundary}--\n";
                        }
                }
        
            $mail_headers .= "X-Mailer: PHP/" . phpversion();
        
            $ris = mail($email, $subject, $mail_body, $mail_headers);
            if ($ris)
                {
                    print "<h2>La mail destinata a $email è stata accettata dal sistema per la spedizione</h2>";
                }
                else 
                {
                    print "<h2>La mail destinata a $email NON è stata accettata dal sistema per la spedizione</h2>";
                    print "
</br/>

<a href=\"contattaci.php\">Riprova</a></p>";
                }
        }               
        unset($_SESSION["allegato"]);
        unset($_SESSION["allegato_type"]);
        unset($_SESSION["allegato_name"]);
        session_destroy();
        ?>