function downloadEmails($cronJob = false, $exists = false, $reload = false)
{
    global $GROUPS, $CONFIG, $INTERNAL;
    if (is_array($GROUPS)) {
        foreach ($GROUPS as $group) {
            $gmbout = Mailbox::GetById($group->TicketEmailOut);
            if (is_array($group->TicketEmailIn)) {
                foreach ($group->TicketEmailIn as $mid) {
                    if (!empty($CONFIG["db"]["gl_email"][$mid]) && $CONFIG["db"]["gl_email"][$mid]->LastConnect < time() - $CONFIG["db"]["gl_email"][$mid]->ConnectFrequency * 60) {
                        $CONFIG["db"]["gl_email"][$mid]->SetLastConnect(time());
                        $newmails = downloadFromMailbox($reload, $CONFIG["db"]["gl_email"][$mid]->Type, $CONFIG["db"]["gl_email"][$mid]->Host, $CONFIG["db"]["gl_email"][$mid]->Port, $CONFIG["db"]["gl_email"][$mid]->Password, $CONFIG["db"]["gl_email"][$mid]->Username, $CONFIG["db"]["gl_email"][$mid]->SSL, $CONFIG["db"]["gl_email"][$mid]->Delete);
                        if ($reload) {
                            $CONFIG["db"]["gl_email"][$mid]->SetLastConnect(0);
                        }
                        if (!empty($newmails) && is_array($newmails)) {
                            foreach ($newmails as $temail) {
                                if (TicketEmail::Exists($temail->Id)) {
                                    continue;
                                }
                                $Ticket = null;
                                $temail->MailboxId = $mid;
                                $temail->GroupId = $group->Id;
                                if (preg_match_all("/\\[[a-zA-Z\\d]{12}\\]/", $temail->Subject . $temail->Body, $matches)) {
                                    foreach ($matches[0] as $match) {
                                        $id = $groupid = $language = "";
                                        if ($exists = Ticket::Exists($match, $id, $groupid, $language)) {
                                            $Ticket = new Ticket($id, true);
                                            $Ticket->Group = $groupid;
                                            $Ticket->Language = strtoupper($language);
                                            $Ticket->Messages[0]->Type = $temail->Email == $gmbout->Email || $temail->Email == $CONFIG["db"]["gl_email"][$mid]->Email ? 1 : 3;
                                            $Ticket->Messages[0]->Text = $temail->Body;
                                            $Ticket->Messages[0]->Email = !empty($temail->ReplyTo) ? $temail->ReplyTo : $temail->Email;
                                            $Ticket->Messages[0]->ChannelId = $temail->Id;
                                            $Ticket->Messages[0]->Fullname = $temail->Name;
                                            $Ticket->Messages[0]->Subject = $temail->Subject;
                                            $Ticket->Messages[0]->Hash = strtoupper(str_replace(array("[", "]"), "", $match));
                                            $Ticket->Messages[0]->Edited = $Ticket->Messages[0]->Created = $temail->Created;
                                            if (DEBUG_MODE) {
                                                logit("SAVE EMAIL REPLY: " . $temail->Id . " - " . $temail->Subject);
                                            }
                                            $Ticket->Messages[0]->Save($id, $temail->Created);
                                            $Ticket->Reactivate();
                                            $Ticket->SetLastUpdate(time());
                                        }
                                    }
                                }
                                if (!$exists) {
                                    if ($group->TicketHandleUnknownEmails == 1) {
                                        if (DEBUG_MODE) {
                                            logit("SAVE EMAIL: " . $temail->Id . " - " . $temail->Subject);
                                        }
                                        $temail->Save();
                                    } else {
                                        if ($group->TicketHandleUnknownEmails == 0) {
                                            $Ticket = new Ticket(getObjectId("ticket_id", DATABASE_TICKETS), true);
                                            $Ticket->Group = $group->Id;
                                            $Ticket->CreationType = 1;
                                            $Ticket->Language = strtoupper($CONFIG["gl_default_language"]);
                                            $Ticket->Messages[0]->Id = $Ticket->Id;
                                            $Ticket->Messages[0]->Type = 3;
                                            $Ticket->Messages[0]->Text = $temail->Body;
                                            $Ticket->Messages[0]->Email = !empty($temail->ReplyTo) ? $temail->ReplyTo : $temail->Email;
                                            $Ticket->Messages[0]->ChannelId = $temail->Id;
                                            $Ticket->Messages[0]->Fullname = $temail->Name;
                                            $Ticket->Messages[0]->Created = $temail->Created;
                                            $Ticket->Messages[0]->Subject = $temail->Subject;
                                            $Ticket->Messages[0]->Attachments = $temail->Attachments;
                                            $Ticket->Messages[0]->SaveAttachments();
                                            $Ticket->Save();
                                            $Ticket->AutoAssignEditor();
                                            $Ticket->SetLastUpdate(time());
                                            languageSelect(strtolower($CONFIG["gl_default_language"]), true);
                                            Visitor::SendTicketAutoresponder($Ticket, strtoupper($CONFIG["gl_default_language"]), false);
                                            languageSelect("", true);
                                        }
                                    }
                                    if (!empty($CONFIG["gl_mpm"])) {
                                        foreach ($INTERNAL as $operator) {
                                            if ($operator->IsInPushMessageState()) {
                                                if ($operator->HasAccessToEmail($group->Id)) {
                                                    $operator->AddPushMessage($temail->Id, $this->SystemId, !empty($temail->Name) ? $temail->Name : $temail->Email, 3, $temail->Body);
                                                }
                                            }
                                        }
                                    }
                                }
                                foreach ($temail->Attachments as $attid => $attdata) {
                                    file_put_contents(PATH_UPLOADS . $attdata[0], $attdata[2]);
                                    processResource("SYSTEM", $attid, $attdata[0], 3, $attdata[1], 0, 100, 1);
                                    if (!$exists && $group->TicketHandleUnknownEmails == 1) {
                                        $temail->SaveAttachment($attid);
                                    }
                                    if (!empty($Ticket)) {
                                        $Ticket->Messages[0]->ApplyAttachment($attid);
                                    }
                                }
                            }
                        }
                        if (!$cronJob) {
                            return;
                        }
                    }
                }
            }
        }
    }
}
function sendTestMail($amount = 0)
{
    global $RESPONSE;
    $account = Mailbox::GetById($_POST["p_mailbox"]);
    try {
        if ($account->Type == "IMAP" || $account->Type == "POP") {
            $reload = false;
            $amount = downloadFromMailbox($reload, $account->Type, $account->Host, $account->Port, $account->Password, $account->Username, $account->SSL, false, true);
            $return = 1;
        } else {
            $return = sendMail($account, $account->Email, $account->Email, "LiveZilla Test Mail", "LiveZilla Test Mail", true);
        }
    } catch (Exception $e) {
        logit(serialize($e));
        $return = $e->getMessage();
    }
    if (is_array($amount)) {
        $amount = count($amount);
    }
    if ($return == 1) {
        $RESPONSE->SetStandardResponse(1, base64_encode($amount));
    } else {
        $RESPONSE->SetStandardResponse(2, base64_encode($return));
    }
}