public function testMailAccountConstruct() { $expected = ['accountId' => 12345, 'accountName' => 'Peter Parker', 'emailAddress' => '*****@*****.**', 'imapHost' => 'mail.marvel.com', 'imapPort' => 159, 'imapUser' => 'spiderman', 'imapSslMode' => 'tls', 'smtpHost' => 'smtp.marvel.com', 'smtpPort' => 458, 'smtpUser' => 'spiderman', 'smtpSslMode' => 'ssl']; $a = new MailAccount($expected); // TODO: fix inconsistency $expected['name'] = $expected['accountName']; unset($expected['accountName']); $this->assertEquals($expected, $a->toJson()); }
function deleteMailsFromServer(MailAccount $account) { $count = 0; if ($account->getDelFromServer() > 0) { $max_date = DateTimeValueLib::now(); $max_date->add('d', -1 * $account->getDelFromServer()); if ($account->getIsImap()) { if ($account->getIncomingSsl()) { $imap = new Net_IMAP($ret, "ssl://" . $account->getServer(), $account->getIncomingSslPort()); } else { $imap = new Net_IMAP($ret, "tcp://" . $account->getServer()); } if (PEAR::isError($ret)) { Logger::log($ret->getMessage()); throw new Exception($ret->getMessage()); } $ret = $imap->login($account->getEmail(), self::ENCRYPT_DECRYPT($account->getPassword())); $result = array(); if ($ret === true) { $mailboxes = MailAccountImapFolders::getMailAccountImapFolders($account->getId()); if (is_array($mailboxes)) { foreach ($mailboxes as $box) { if ($box->getCheckFolder()) { $numMessages = $imap->getNumberOfMessages(utf8_decode($box->getFolderName())); for ($i = 1; $i <= $numMessages; $i++) { $summary = $imap->getSummary($i); if (is_array($summary)) { $m_date = DateTimeValueLib::makeFromString($summary[0]['INTERNALDATE']); if ($m_date instanceof DateTimeValue && $max_date->getTimestamp() > $m_date->getTimestamp()) { if (MailContents::mailRecordExists($account->getId(), $summary[0]['UID'], $box->getFolderName(), null)) { $imap->deleteMessages($i); $count++; } } else { break; } } } $imap->expunge(); } } } } } else { //require_once "Net/POP3.php"; $pop3 = new Net_POP3(); // Connect to mail server if ($account->getIncomingSsl()) { $pop3->connect("ssl://" . $account->getServer(), $account->getIncomingSslPort()); } else { $pop3->connect($account->getServer()); } if (PEAR::isError($ret = $pop3->login($account->getEmail(), self::ENCRYPT_DECRYPT($account->getPassword()), 'USER'))) { throw new Exception($ret->getMessage()); } $emails = $pop3->getListing(); foreach ($emails as $email) { if (MailContents::mailRecordExists($account->getId(), $email['uidl'], null, null)) { $headers = $pop3->getParsedHeaders($email['msg_id']); $date = DateTimeValueLib::makeFromString(array_var($headers, 'Date')); if ($date instanceof DateTimeValue && $max_date->getTimestamp() > $date->getTimestamp()) { $pop3->deleteMsg($email['msg_id']); $count++; } } } $pop3->disconnect(); } } return $count; }
function fetch_imap_folders() { $server = array_var($_GET, 'server'); $ssl = array_var($_GET, 'ssl') == "checked"; $port = array_var($_GET, 'port'); $email = array_var($_GET, 'email'); $pass = array_var($_GET, 'pass'); $genid = array_var($_GET, 'genid'); tpl_assign('genid', $genid); $account = new MailAccount(); $account->setIncomingSsl($ssl); $account->setIncomingSslPort($port); $account->setEmail($email); $account->setPassword(MailUtilities::ENCRYPT_DECRYPT($pass)); $account->setServer($server); try { $real_folders = MailUtilities::getImapFolders($account); $imap_folders = array(); foreach ($real_folders as $folder_name) { $acc_folder = new MailAccountImapFolder(); $acc_folder->setAccountId(0); $acc_folder->setFolderName($folder_name); $acc_folder->setCheckFolder($folder_name == 'INBOX'); // By default only INBOX is checked $imap_folders[] = $acc_folder; } tpl_assign('imap_folders', $imap_folders); } catch (Exception $e) { //Logger::log($e->getTraceAsString()); } }