function cleanup_lock() { global $account_id; Lock::release('download_emails_' . $account_id); } register_shutdown_function('cleanup_lock'); $account = Email_Account::getDetails($account_id); $mbox = Support::connectEmailServer($account); if ($mbox == false) { $uri = Support::getServerURI($account); $login = $account['ema_username']; $error = imap_last_error(); fatal("{$error}\n", "Could not connect to the email server '{$uri}' with login: '******'.", 'Please verify your email account settings and try again.'); } // if we only want new emails if ($account['ema_get_only_new']) { $new_emails = Support::getNewEmails($mbox); foreach ($new_emails as $new_email) { Support::getEmailInfo($mbox, $account, $new_email); } } else { $total_emails = Support::getTotalEmails($mbox); if ($total_emails > 0) { for ($i = 1; $i <= $total_emails; $i++) { Support::getEmailInfo($mbox, $account, $i); } } } Support::closeEmailServer($mbox); Support::clearErrors();