Buonasera a tutti!
Ho questo codice per l'invio di un email con allegato. L'email mi arriva i campi da compilare pure l'allegato anche, l'unico problema è che l'allegato arriva 0kb cioè vuoto. sapete dirmi come mai?
Codice PHP:
<?php
// RENDIAMO LO SCRIPT COMPATIBILE CON LE VERSIONI DI PHP < 4.1.0
if(!isset($_POST)) $_POST = $HTTP_POST_VARS;
if(!isset($_FILES)) $_FILES = $HTTP_POST_FILES;
// RIPULIAMO I VARI CAMPI DEL MODULO
$Nome = trim($_POST["nome"]);
$Cognome = trim(stripslashes($_POST["cognome"]));
$Email = trim(stripslashes($_POST["email"]));
$to="prova@prova.it";
$oggetto="Curriculum Vitae";
$messaggio="
Nome: $Nome
Cognome: $Cognome
Email: $Email";
// ASSEGNIAMO A VARIABILI PIU' LEGGIBILI, LE PROPRIETA' DELL'ALLEGATO
$attach = $_FILES["allegato"]["tmp_name"];
$file_name = $_FILES["allegato"]["name"];
$file_type = $_FILES["allegato"]["type"];
$file_size = $_FILES["allegato"]["size"];
// DELIMITATORE
$boundary = md5(uniqid(microtime()));
// APRIAMO L'ALLEGATO PER LEGGERLO E CODIFICARLO
$file = @fopen($attach, "r");
$contents = @fread($file, $file_size);
$encoded_attach = chunk_split(base64_encode($contents));
@fclose($file);
// INTESTAZIONI DELLA MAIL
$mail_headers .= "MIME-version: 1.0\n";
$mail_headers .= "Content-type: multipart/mixed; boundary=\"$boundary\"";
$mail_headers .= "X-attachments: $file_name\n";
$mail_headers .= "From: $Nome $Cognome <$Email>\r\n";
// COSTRUIAMO IL CORPO DELLA MAIL
$mail_body = "--$boundary\n";
$mail_body .= "Content-Type: text/plain; charset=us-ascii\n";
$mail_body .= "Content-Transfer-Encoding: 7bit\n\n";
$mail_body .= "$messaggio\n\n";
$mail_body .= "--$boundary\n";
$mail_body .= "Content-type: $file_type; name=\"$file_name\"\n";
$mail_body .= "Content-Transfer-Encoding: base64\n";
$mail_body .= "$encoded_attach\n";
$mail_body .= "--$boundary--\n";
// INVIO DELLA MAIL
if(@mail($to, $oggetto, $mail_body, $mail_headers)) { // SE L'INVIO È ANDATO A BUON FINE...
echo "La mail è stata inoltrata con successo.";
} else {// ALTRIMENTI...
echo "Si sono verificati dei problemi nell'invio della mail.";
}
?>
Ovviamente c'è anche il form:
codice:
<p class="var"><FORM ACTION="invia.php" enctype="multipart/form-data" METHOD="POST">
<p class="form"><label>Nome:</label> <input type="text" name="nome" size="20" /></p>
<p class="form"><label>Cognome:</label><input type="text" name="cognome" size="20" /></p>
<p class="form"><label>Email:</label><input type="text" name="email" size="20" /></p>
<p class="form">
<label>Allegato:</label>
<input type="file" name="allegato" size="20" /></p>
<p class="form">
<input type="submit" style="background-color:#454FA4" value="Invia" /></p>
</form></p>