Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Non riesco a inviare l'email con allegati usando PHP

    Salve a tutti.
    mi sono appena scritto in questo forum per avere maggiori informazioni riguardo il PHP. Ho provato a costruire un php che fa in modo di inviare il messaggio con allegato ma ho riscontrato alcuni problemi. Qualcuno mi puole aiutare a risolverli?

    Questo è il form che mi sono costruito:

    <form action="../formmail.php" method="POST" enctype="multipart/form-data">
    <input type="hidden" name="destinatario" value="manuel239@interfree.it">
    <table width="100%" cellspacing="0" cellpadding="0">
    <tr>
    <td colspan="6"><div id="striscia"><font color="#006699" size="2">Il Canale del Gusto</font>

    <font color="#FF3300" size="3">Analisi Sensoriali </font></div></td>
    </tr>
    <tr>
    <td colspan="2">VINO </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td colspan="2">
    <table width="100%" cellspacing="0" cellpadding="0">
    <tr>
    <td>Tua Mail</td>
    <td><input type="text" name="mittente" value="" /></td>
    </tr>
    <tr>
    <td>Oggetto</td>
    <td><input type="text" name="oggetto" value="" /></td>
    </tr>
    <tr>
    <td>Cantina</td>
    <td><input type="text" name="cantina" value="" /></td>
    </tr>
    <tr>
    <td>Vitigni</td>
    <td><input type="text" name="vitigni" value="" /></td>
    </tr>
    <tr>
    <td>Gradazione</td>
    <td><input type="text" name="gradazione" value="" /></td>
    </tr>
    <tr>
    <td>Territorio</td>
    <td><input type="text" name="territorio" value="" /></td>
    </tr>
    </table></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>Esame Visivo </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td><table width="100%" cellspacing="0" cellpadding="0">
    <tr>
    <td>Note</td>
    </tr>
    <tr>
    <td><textarea cols="20" rows="4" name="esame_visivo"></textarea></td>
    </tr>

    </table></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>linea</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>Esame Olfattivo </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>Note</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td><textarea cols="20" rows="4" name="esame_olfattivo"></textarea></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>linea</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>Esame Gusto olfattivo </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>Note</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td><textarea cols="20" rows="4" name="esame_gusto_olfattivo"></textarea></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>linea</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td>Se hai una foto clicca qui </td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="file" name="allegato" /></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td></td>
    <td><input type="submit" value="Invia" /></td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    </tr>
    <tr>
    <td colspan="6"></td>
    </tr>
    </table>
    </form>

    è questo è il php che serve per inviare il messaggio + allegato:

    <?php
    // Recupero il valore dei campi del form
    $destinatario = $_POST['destinatario'];
    $mittente = $_POST['mittente'];
    $oggetto = $_POST['oggetto'];
    $cantina = $_POST['cantina'];
    $vitigni = $_POST['vitigni'];
    $gradazione = $_POST['gradazione'];
    $territorio = $_POST['territorio'];
    $esame_visivo = $_POST['esame_visivo'];
    $esame_olfattivo = $_POST['esame_olfattivo'];
    $esame_gusto_olfattivo = $_POST['esame_gusto_olfattivo'];

    // Valorizzo le variabili relative all'allegato
    $allegato = $_FILES['allegato']['tmp_name'];
    $allegato_type = $_FILES['allegato']['type'];
    $allegato_name = $_FILES['allegato']['name'];

    // Creo 2 variabili che riempirò più avanti...
    $headers = "From: " . $mittente;
    $msg = "Cantina: " . $cantina;
    "Vitigni: " . $vitigni;
    "Gradazione: " . $gradazione;
    "Territorio: " . $territorio;
    "Esame visivo: " . $esame_visivo;
    "Territorio: " . $esame_olfattivo;
    "Territorio: " . $esame_gusto_olfattivo;

    // Verifico se il file è stato caricato correttamente via HTTP
    // In caso affermativo proseguo nel lavoro...
    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";

    // Aggiungo le intestazioni necessarie per l'allegato
    $headers .= "\nMIME-Version: 1.0\n";
    $headers .= "Content-Type: multipart/mixed;\n";
    $headers .= " boundary=\"{$mime_boundary}\"";

    // Definisco il tipo di messaggio (MIME/multi-part)
    $msg .= "This is a multi-part message in MIME format.\n\n";

    // Metto il separatore
    $msg .= "--{$mime_boundary}\n";

    // Questa è la parte "testuale" del messaggio
    $msg .= "Content-Type: text/plain; charset=\"iso-8859-1\"\n";
    $msg .= "Content-Transfer-Encoding: 7bit\n\n";
    $msg .= $cantina . "\n\n";

    // Metto il separatore
    $msg .= "--{$mime_boundary}\n";

    // Aggiungo l'allegato al messaggio
    $msg .= "Content-Disposition: attachment;\n";
    $msg .= " filename=\"{$allegato_name}\"\n";
    $msg .= "Content-Transfer-Encoding: base64\n\n";
    $msg .= $data . "\n\n";

    // chiudo con il separatore
    $msg .= "--{$mime_boundary}--\n";
    }
    else
    {
    $msg = $cantina;
    }

    // Invio la mail
    if (mail($destinatario, $oggetto, $msg, $headers))
    {
    echo "

    Mail inviata con successo!</p>";
    }else{
    echo "

    Errore!</p>";
    }
    ?>



  2. #2
    Utente di HTML.it L'avatar di carlo2002
    Registrato dal
    Jun 2002
    Messaggi
    2,746
    Errare humanum est, perseverare ovest

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.