I use next code:
$connection = imap_open("{192.168.1.170:993/imap/ssl/novalidate-cert}INBOX", $send_from_our, $send_from_pass) or die('Cannot connect to your sever: ' . imap_last_error());
$emails = imap_search($connection, 'UNSEEN');
$unread = 0;
$count = imap_num_msg($connection);
for ($msgno = 1; $msgno <= $count; $msgno++) {
$cc='';
$to='';
$headers = imap_headerinfo($connection, $msgno);
if ($headers->Unseen == 'U') {
$unread++;
if (isset($headers->cc) && is_array($headers->cc)) {
foreach ($headers->cc as $cc_extra) {
$mailbox_cc=strtolower($cc_extra->mailbox);
$host_cc=strtolower($cc_extra->host);
$cc .= $mailbox_cc . "@" . $host_cc .',';
}
$cc= rtrim($cc, ',');
}
if (isset($headers->to) && is_array($headers->to)) {
foreach ($headers->to as $to_extra) {
$mailbox_to=strtolower($to_extra->mailbox);
$host_to=strtolower($to_extra->host);
if ($mailbox_to=='$MY_EMAIL_NAME' and $host_to='$MY_HOST_NAME') {
continue;
}
$to .=$mailbox_to . "@" . $host_to.',';
}
$to=rtrim($to, ',');
}
$fromaddr = $headers->from[0]->mailbox . "@" . $headers->from[0]->host;
$full_reply_to=rtrim($fromaddr.','.$cc.','.$to, ',');
This code recieve all unread email from maibox, and put all adress from CC and TO (exclude my self address) for each email to $full_reply_to