//mail
$mail = new CUserMail();
$mail->load($mail_id);
$mail->loadRefsFwd();
$mail->checkHprim();
//HprimMedecin
$mail->checkApicrypt();
//HprimMedecin
//pop account
$log_pop = new CSourcePOP();
$log_pop->load($mail->account_id);
//if not read email, send the seen flag to server
if (!$mail->date_read && !CAppUI::pref("markMailOnServerAsRead")) {
    $pop = new CPop($log_pop);
    $pop->open();
    $pop->setflag($mail->uid, "\\Seen");
    $pop->close();
}
$mail->date_read = CMbDT::dateTime();
$mail->store();
//get the CFile attachments
$nbAttachPicked = 0;
$nbAttach = count($mail->_attachments);
foreach ($mail->_attachments as $_att) {
    $_att->loadRefsFwd();
    if ($_att->_file->_id) {
        $nbAttachPicked++;
    }
}
$mail->checkInlineAttachments();
$headers = preg_split("/(\r\n|\n)/", $mail->_text_plain->content);
 */
CCanDo::checkRead();
CPop::checkImapLib();
$account_id = CValue::get("account_id");
$user = CMediusers::get();
//get account
$account_pop = new CSourcePOP();
$account_pop->load($account_id);
//get the list
$mail = new CUserMail();
$where = array();
$where[] = "date_read IS NULL AND account_id = '{$account_id}' AND account_class = 'CSourcePOP'";
$mails = $mail->loadList($where);
$pop = new CPop($account_pop);
$pop->open();
$count = 0;
/** @var $mails CUserMail[] */
foreach ($mails as $_mail) {
    if ($pop->setflag($_mail->uid, "\\Seen")) {
        $_mail->date_read = CMbDT::dateTime();
        if (!($msg = $_mail->store())) {
            $count++;
        }
    }
}
$pop->close();
if ($count > 0) {
    CAppUI::stepAjax("CUserMail-markedAsRead", UI_MSG_OK, $count);
} else {
    CAppUI::stepAjax("CUserMail-markedAsRead-none", UI_MSG_OK);
}