Salve,
ho un modulo con il quale l'utente dovrebbe inviare 4 allegati oltre ai propri dati, però non riesco a far allegare all'email gli allegati.
Questo è il codice di invio:
Codice PHP:
$postData = $uploadedFile = $statusMsg = '';$msgClass = 'errordiv';function multi_attach_mail($to, $subject, $message, $senderEmail, $senderName, $files = array()){ $from = $senderName." <".$senderEmail.">"; $headers = "From: $from"; // Boundary $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // Headers for attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // Multipart boundary $message = "--{$mime_boundary}\n" . "Content-Type: text/html; charset=\"UTF-8\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // Preparing attachment if(!empty($files)){ for($i=0;$i<count($files);$i++){ if(is_file($files[$i])){ $file_name = basename($files[$i]); $file_size = filesize($files[$i]); $message .= "--{$mime_boundary}\n"; $fp = @fopen($files[$i], "rb"); $data = @fread($fp, $file_size); @fclose($fp); $data = chunk_split(base64_encode($data)); $message .= "Content-Type: application/octet-stream; name=\"".$file_name."\"\n" . "Content-Description: ".$file_name."\n" . "Content-Disposition: attachment;\n" . " filename=\"".$file_name."\"; size=".$file_size.";\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; } } } $message .= "--{$mime_boundary}--"; $returnpath = "-f" . $senderEmail; // Send email $mail = @mail($to, $subject, $message, $headers, $returnpath); // Return true, if email sent, otherwise return false if($mail){ return true; }else{ return false; } }
$to = 'miaemail@miosito.it'; $subject = "Richiesta informazioni ";
$subject = "Richiesta informazioni"; $azienda = $_POST['azienda']; $name = $_POST['nomecognome']; $telefono = $_POST['telefono']; $email = $_POST['email']; $citta = $_POST['citta']; $attach = $_POST['attachment'];
// Attachment files $files = array();
$htmlContent = ' <h3>Invio documenti</h3> <h2>Richiesta Informazioni</h2> <p><b>Azienda:</b> '.$azienda.'</p> <p><b>Nome e Cognome:</b> '.$fname.'</p> <p><b>Telefono:</b> '.$telefono.'</p> <p><b>E-mail:</b> '.$email.'</p> <p><b>Città:</b> '.$citta.'</p> <p><b>Totale Allegati:</b> '.count($files).'</p>';
$sendEmail = multi_attach_mail($to, $subject, $htmlContent, $email, $name, $files);
if($sendEmail) { header("location:invio-OK.php");
} else { echo 'Il tuo messaggio non è stato inviato.';}
nel form tutti e 4 i campi hanno lo stesso name [
codice HTML:
name="attachment[]"
anche se metto name diversi come posso fare?
Non so come compilare questa parte qui
codice:
// Attachment files
$files = array();
Chi mi può dare una mano?
grazie