Visualizzazione dei risultati da 1 a 2 su 2

Visualizzazione discussione

  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2011
    Messaggi
    144

    Messaggio vuoto con funzione mail()

    Salve a tutti,

    ho creato un form contatti nella mia pagina web. Tale form permette di inviare anche allegati. Il problema è che se non allego nulla, il messaggio mi arriva vuoto mentre se allego qualche file il messaggio mi arriva completo. Il codice che sto usando è il seguente:
    codice:
    function process_form() {
        // Read POST request params into global vars
        // FILL IN YOUR EMAIL
        $to = "mia_mail";
        $subject = trim($_POST['subject']);
        $namefrom = trim($_POST['namefrom']);
        // $company = trim($_POST['company']);
        $phone = trim($_POST['phone']);
        $emailfrom = trim($_POST['emailfrom']);
        $comments = trim($_POST['comments']);
        
        // Allowed file types. add file extensions WITHOUT the dot.
        $allowtypes=array("zip", "rar", "txt", "doc", "pdf");
        
        // Maximum file size for attachments in KB NOT Bytes for simplicity. MAKE SURE your php.ini can handel it,
        // post_max_size, upload_max_filesize, file_uploads, max_execution_time!
        // 2048kb = 2MB,       1024kb = 1MB,     512kb = 1/2MB etc..
        $max_file_size="5120";
        
        // Thank you message
        $thanksmessage="La tua mail è stata inviata. La ricontatterò al più presto.";
    
        $errors = array(); //Initialize error array
    
        //checks for a name
        if (empty($_POST['namefrom']) ) {
            $errors[]='Hai dimenticato di inserire il tuo nome';
            }
    
        //checks for an email
        if (empty($_POST['emailfrom']) ) {
            $errors[]='Hai dimenticato di inserire la tua email';
            } else {
    
            if (!eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,4}$', stripslashes(trim($_POST['emailfrom'])))) {
                $errors[]='Inserisci una email valida';
            } // if eregi
        } // if empty email
    
        //checks for a subject
        if (empty($_POST['subject']) ) {
            $errors[]='Hai dimenticato di inserire l\'oggetto del messaggio';
            }
    
        //checks for a message
        if (empty($_POST['comments']) ) {
            $errors[]='Hai dimenticato di inserire il messaggio';
            }
            
        //checks attachment file
        // checks that we have a file
        if((!empty($_FILES["attachment"])) && ($_FILES['attachment']['error'] == 0)) {
                // basename -- Returns filename component of path
                $filename = basename($_FILES['attachment']['name']);
                $ext = substr($filename, strrpos($filename, '.') + 1);
                $filesize=$_FILES['attachment']['size'];
                $max_bytes=$max_file_size*1024;
                
                //Check if the file type uploaded is a valid file type. 
                if (!in_array($ext, $allowtypes)) {
                    $errors[]="Estensione non valida del file: <strong>".$filename."</strong>";
                    
            // check the size of each file
            } elseif($filesize > $max_bytes) {
                    $errors[]= "Il tuo file: <strong>".$filename."</strong> é troppo grande. La dimensione max è ".$max_file_size."kb.";
                }
                
        } // if !empty FILES
    
        if (empty($errors)) { //If everything is OK
            
            // send an email
            // Obtain file upload vars
            $fileatt      = $_FILES['attachment']['tmp_name'];
            $fileatt_type = $_FILES['attachment']['type'];
            $fileatt_name = $_FILES['attachment']['name'];
            
            // Headers
            $headers = "From: $emailfrom";
    
            if (is_uploaded_file($fileatt)) {
              // Read the file to be attached ('rb' = read binary)
              $file = fopen($fileatt,'rb');
              $data = fread($file,filesize($fileatt));
              fclose($file);
    
              // create a boundary string. It must be unique
              $semi_rand = md5(time());
              $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
    
              // Add the headers for a file attachment
              $headers .= "\nMIME-Version: 1.0\n" .
                          "Content-Type: multipart/mixed;\n" .
                          " boundary=\"{$mime_boundary}\"";
    
              // Add a multipart boundary above the plain message
              $message ="This is a multi-part message in MIME format.\n\n";
              $message.="--{$mime_boundary}\n";
              $message.="Content-Type: text/plain; charset=\"iso-8859-1\"\n";
              $message.="Content-Transfer-Encoding: 7bit\n\n";
              $message.="From: ".$namefrom."\n";
              // $message.="Company: ".$company."\n";
              $message.="Phone: ".$phone."\n";
              $message.="Comments: ".$comments."\n\n";
              
              // Base64 encode the file data
              $data = chunk_split(base64_encode($data));
    
              // Add file attachment to the message
              $message .= "--{$mime_boundary}\n" .
                          "Content-Type: {$fileatt_type};\n" .
                          " name=\"{$fileatt_name}\"\n" .
                          //"Content-Disposition: attachment;\n" .
                          //" filename=\"{$fileatt_name}\"\n" .
                          "Content-Transfer-Encoding: base64\n\n" .
                          $data . "\n\n" .
                          "--{$mime_boundary}--\n";
            }
            
            
            // Send the completed message
            if(!mail($to,$subject,$message,$headers)) {
                exit("La email non può essere inviata. Mi dispiace! Si è verificato un errore, per favore segnalano all'amministratore del sito web.\n");
            } else {
                echo '<div id="formfeedback"><h3>Grazie!</h3><p>'. $thanksmessage .'</p></div>';
                print_form();
            } // end of if !mail
            
        } else { //report the errors
            echo '<div id="formfeedback"><h3>Errore!</h3><p>Si sono verificati i seguenti errori:<br />';
            foreach ($errors as $msg) { //prints each error
                    echo " - $msg<br />\n";
                } // end of foreach
            echo '</p><p>Correggi gli errori e riprova</p></div>';
            print_form();
        } //end of if(empty($errors))
    
    } // end of process_form() ?>


    Qualcuno mi saprebbe dire qual'è il problema?

    Grazie in anticipo a chi potrà/vorrà aiutarmi
    Ultima modifica di mbistato; 24-10-2014 a 09:11

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.