Ciao a tutti, ho scritto questo script per leggere tutte le mail non lette e inoltrarle ma ho problemi nell'Ottenere il corpo del messaggio... Dove sbaglio? Come decodifico correttamente il corpo?
PHP:
<?php
$imap_host_name = '###';
$imap_user_name = '###';
$imap_password = '###';
/* connect to Mailer Server to Read Emails */
$inbox = imap_open($imap_host_name,$imap_user_name,$imap_password) or die('Cannot connect to IMAP: ' . imap_last_error());
//Read all the Emails
$emails = imap_search($inbox,'UNSEEN');
if($emails) {
rsort($emails);
foreach($emails as $email_number) {
$overview = imap_fetch_overview($inbox,$email_number,0);
$message = getBody($inbox,$email_number);
$structure = imap_fetchstructure($inbox,$email_number);
$header = imap_headerinfo($inbox, $email_number);
$mailMittente = $header->from[0]->mailbox . "@" . $header->from[0]->host;
$nomeMittente = $overview[0]->from;
$mailDestinatario = "###";
$oggetto = $overview[0]->subject;
require('gestisciAllegato.php');
invia($mailMittente,$nomeMittente,$mailDestinatario,$oggetto,$message,$file_attachments);
}
}
imap_close($inbox);
function invia($mailMittente,$nomeMittente,$mailDestinatario,$oggetto,$corpoMail,$file_attachments) {
require 'phpmailer/PHPMailerAutoload.php';
$mail = new PHPMailer;
//$mail->SMTPDebug = 3; // Enable verbose debug output
$mail->isSMTP(); // Set mailer to use SMTP
$mail->Host = '###'; // Specify main and backup SMTP servers
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Username = '###'; // SMTP username
$mail->Password = '###'; // SMTP password
$mail->SMTPSecure = 'ssl'; // Enable TLS encryption, `ssl` also accepted
$mail->Port = 465; // TCP port to connect to
$mail->From = $mailMittente;
$mail->FromName = $nomeMittente;
$mail->addAddress($mailDestinatario, $nomeMittente); // Add a recipient
// Aggiungo gli allegati se esistenti
if(!empty($file_attachments)){
foreach($file_attachments as $file){
$mail->AddAttachment($file);
}
}
$mail->isHTML(true); // Set email format to HTML
$mail->Subject = $oggetto;
$mail->Body = $corpoMail;
if(!$mail->send()) {
echo 'Problema nella spedizione'.'<br>';
echo 'Errore: ' . $mail->ErrorInfo;
} else {
echo 'Messaggio spedito!';
}
}
function getBody($uid, $imap) {
$body = get_part($imap, $uid, "TEXT/HTML");
// if HTML body is empty, try getting text body
if ($body == "") {
$body = get_part($imap, $uid, "TEXT/PLAIN");
}
return $body;
}
function get_part($imap, $uid, $mimetype, $structure = false, $partNumber = false) {
if (!$structure) {
$structure = imap_fetchstructure($imap, $uid, FT_UID);
}
if ($structure) {
if ($mimetype == get_mime_type($structure)) {
if (!$partNumber) {
$partNumber = 1;
}
$text = imap_fetchbody($imap, $uid, $partNumber, FT_UID);
switch ($structure->encoding) {
case 3: return imap_base64($text);
case 4: return imap_qprint($text);
default: return $text;
}
}
// multipart
if ($structure->type == 1) {
foreach ($structure->parts as $index => $subStruct) {
$prefix = "";
if ($partNumber) {
$prefix = $partNumber . ".";
}
$data = get_part($imap, $uid, $mimetype, $subStruct, $prefix . ($index + 1));
if ($data) {
return $data;
}
}
}
}
return false;
}
function get_mime_type($structure) {
$primaryMimetype = array("TEXT", "MULTIPART", "MESSAGE", "APPLICATION", "AUDIO", "IMAGE", "VIDEO", "OTHER");
if ($structure->subtype) {
return $primaryMimetype[(int)$structure->type] . "/" . $structure->subtype;
}
return "TEXT/PLAIN";
}
?>