ciao a tutti
ho un problema con l'invio di una mail con allegati, che viene spedita dopo la compilazione di un form.
se la mail � in formato html,
ovvero $body .= "Content-Type: text/html; charset=iso-8859-1\r\n";
non viene spedita,
se � in formato plain,
ovvero $body .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
viene spedita ma perdo la formattazione in html.
come risolvo il compromesso?
ecco il codice usato:
codice:
$recipient_email = $destinatario; //recepient
$noreply = "Noreply bla bla bla";
$subject = $motivo;
$attachments = $_FILES['file'];
$mail_corpo = <<<HTML
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<center><img src="http://immagine.blablabla"></center><br><br><br><br>
Oggetto: $titolo<br><br>
$testo <br><br><br><br>
<br><br><br>L'amministratore. bla bla bla<br><br><br><br><br>
Si prega di non rispondere a questa mail.<br><br>
</body>
</html>
HTML;
$file_count = count($attachments['name']); //count total files attached
$boundary = md5(time());
if($file_count > 0){ //if attachment exists
//header
$headers = "MIME-Version: 1.0\r\n";
$headers .= "From:".$noreply." <" . $noreply . ">\r\n";
$headers .= "Reply-To: ".$noreply."" . "\r\n";
$headers .= "Content-Type: multipart/mixed; boundary = $boundary\r\n\r\n";
//message text
$body = "--$boundary\r\n";
$body .= "Content-Type: text/plain; charset=iso-8859-1\r\n";
$body .= "Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($mail_corpo));
//attachments
for ($x = 0; $x < $file_count; $x++){
if(!empty($attachments['name'][$x])){
if($attachments['error'][$x]>0) //exit script and output error if we encounter any
{
$mymsg = array(
1=>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
2=>"The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",
3=>"The uploaded file was only partially uploaded",
4=>"No file was uploaded",
6=>"Missing a temporary folder" );
die($mymsg[$attachments['error'][$x]]);
}
//get file info
$file_name = $attachments['name'][$x];
$file_size = $attachments['size'][$x];
$file_type = $attachments['type'][$x];
//read file
$handle = fopen($attachments['tmp_name'][$x], "r");
$content = fread($handle, $file_size);
fclose($handle);
$encoded_content = chunk_split(base64_encode($content)); //split into smaller chunks (RFC 2045)
$body .= "--$boundary\r\n";
$body .="Content-Type: $file_type; name=\"{$file_name}\"\r\n";
$body .="Content-Disposition: attachment; filename=\"{$file_name}\"\r\n";
$body .="Content-Transfer-Encoding: base64\r\n";
$body .="X-Attachment-Id: ".rand(1000,99999)."\r\n\r\n";
$body .= $encoded_content;
}
}
}else{ //send plain email otherwise
$headers = "From:".$noreply."\r\n".
"Reply-To: ".$noreply. "\n" .
"X-Mailer: PHP/" . phpversion();
$body = $mail_corpo;
}
$sentMail = @mail($recipient_email, $subject, $body, $headers);
if($sentMail) //output success or failure messages
{
echo 'ok';exit(); }
else{
echo 'no'); }
grazie in anticipo.