Ciao a tutti. Ho creato un form per l'invio di mail dal sito, e mi hanno chiesto di implementare anche la possibilità di allegare dei file.
Allo scopo ho usato phpmailer, già utilizzato con successo in altre occasioni, ma per qualche strana ragione non mi allega il file alla mail. Sicuramente ho sbagliato qualche passaggio. L'upload del file non dà alcun errore, ma poi l'email arriva senza allegato!
Posto il codice:
Codice PHP:
<?php
if (count($_POST)>0) { //l'array $_POST esiste, quindi vado ad inserire l'utente
$check=1;
$error='';
$checkfile=1;
$errfile='';
if (trim($_POST['mittente'])!='') {
$mittente=addslashes(stripslashes(trim($_POST['mittente'])));
$mittente=ucfirst($mittente);
} else {
$error.="Attenzione! Mittente mancante.
";
$check=0;
}
if (trim($_POST['emailmittente'])!='' && eregi("^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9_-])+.)+[a-z]{2,6}$", trim($_POST['emailmittente']))) {
$emailmittente=addslashes(stripslashes(trim($_POST['emailmittente'])));
$emailmittente=strtolower($emailmittente);
} else {
$error.="Attenzione! Email del mittente mancante o non corretta.
";
$check=0;
}
if (trim($_POST['destinatario'])!='') {
$destinatario=addslashes(stripslashes(trim($_POST['destinatario'])));
$destinatario=ucfirst($destinatario);
} else {
$error.="Attenzione! Destinatario mancante.
";
$check=0;
}
if (trim($_POST['emaildestinatario'])!='' && eregi("^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9_-])+.)+[a-z]{2,6}$", trim($_POST['emaildestinatario']))) {
$emaildestinatario=addslashes(stripslashes(trim($_POST['emaildestinatario'])));
$emaildestinatario=strtolower($emaildestinatario);
} else {
$error.="Attenzione! Email del destinatario mancante o non corretta.
";
$check=0;
}
if (trim($_POST['oggetto'])!='') {
$oggetto=addslashes(stripslashes(trim($_POST['oggetto'])));
} else {
$error.="Attenzione! Oggetto mancante.
";
$check=0;
}
if (trim($_POST['testo'])!='') {
$testo=stripslashes(trim($_POST['testo']));
} else {
$error.="Attenzione! Messaggio mancante.
";
$check=0;
}
if ($_FILES['allegato']['name']!='') {
$file_content = $_FILES['allegato']['tmp_name'];
$file_name = $_FILES['allegato']['name'];
$file_type = $_FILES['allegato']['type'];
$file_size = $_FILES['allegato']['size'];
switch ($_FILES['allegato']['error']) {
case UPLOAD_ERR_INI_SIZE:
$errfile = 'La dimensione del file eccede il valore impostato in php.ini.';
$checkfile = 0;
break;
case UPLOAD_ERR_FORM_SIZE:
$errfile = 'La dimensione del file eccede il valore impostato nel campo MAX_FILE_SIZE.';
$checkfile = 0;
break;
case UPLOAD_ERR_PARTIAL:
$errfile = 'Il file è stato caricato parzialmente.';
$checkfile = 0;
break;
case UPLOAD_ERR_NO_FILE:
$errfile = 'Nessun file selezionato per il caricamento.';
$checkfile = 0;
break;
case UPLOAD_ERR_NO_TMP_DIR:
$errfile = 'Non esiste la cartella temporanea in cui caricare il file.';
$checkfile = 0;
break;
case UPLOAD_ERR_CANT_WRITE:
$errfile = 'Impossibile scrivere il file sul disco.';
$checkfile = 0;
break;
case UPLOAD_ERR_EXTENSION: // Solo da PHP 5!
$errfile = 'Il caricamento è stato impedito dal tipo di estensione del file.';
$checkfile = 0;
break;
case UPLOAD_ERR_OK:
if (!is_uploaded_file($_FILES['allegato']['tmp_name'])) {
$errfile = 'Problemi nell\'upload del file.';
$checkfile = 0;
}
break;
}
}
if ($check==1 && $checkfile==1) {
require_once("class.phpmailer.php");
$mail = new PHPMailer();
$mail->From = $emailmittente;
$mail->FromName = $mittente;
$mail->AddAddress($emaildestinatario,$destinatario);
$mail->IsHTML(true);
$mail->Subject = $oggetto;
$mail->Body = $testo;
$mail->AltBody = strip_tags($testo);
$mail->AddAttachment("$file_content","$file_name");
if(!$mail->Send()){
echo "Si è verificato un errore nell'invio della mail.
";
}else{
echo "Mail inviata con successo!
";
}
} else {
print "<p class=\"errore\">".$error."
".$errfile."</p>";
}
}
?>
//di seguito il form
non riesco a capire dov'è l'errore :master: