public function run() { KalturaLog::info("Email ingestion batch is running"); if ($this->taskConfig->isInitOnly()) { return $this->init(); } // get parameters from ini file try { $this->TEMP_FILE_DIR = $this->taskConfig->params->localTempPath; } catch (Exception $e) { KalturaLog::crit("Cannot find all required parameters from config file"); } // create a temp file path if (!self::createDir($this->TEMP_FILE_DIR)) { KalturaLog::crit("Cannot continue email ingestion without a temp directory"); return false; // quit run() } // ---------------------------------- // loop through all defined mailboxes // ---------------------------------- $mailboxNumber = 0; while (isset($this->taskConfig->params->{'mailbox' . ($mailboxNumber + 1)})) { $mailboxNumber++; $mailesProcessed = 0; $keepCurMailbox = true; $params = $this->taskConfig->params->{'mailbox' . $mailboxNumber}; // get parameters try { $host = $params->hostname; $port = $params->port; $user = $params->user; $pass = $params->pass; $options = $params->options; $maxMails = $params->maxMailsPerRun; } catch (Exception $e) { KalturaLog::crit("Cannot find all required parameters from config file for mailbox number [{$mailboxNumber}]"); continue; // skip current mailbox } // connect to current mailbox $mailChecker = new KMailChecker($host, $port, $user, $pass, $options); if (!$mailChecker->connect()) { KalturaLog::crit("Error connecting to [{$host}:{$port}] as [{$user}] - " . imap_last_error()); continue; // skip current mailbox } KalturaLog::info("Sucessfuly connected to [{$host}:{$port}] as [{$user}]"); // check for unread mails $newMails = $mailChecker->getUnreadIds(); if (!$newMails || count($newMails) <= 0) { // no new mail availble in current mailbox KalturaLog::info("No new mails found on [{$user}@{$host}]"); continue; // skip current mailbox } KalturaLog::info('[' . count($newMails) . "] unread mails found on [{$user}@{$host}]"); // ----------------------------------------- // loop through all mails in current mailbox // ----------------------------------------- while ($keepCurMailbox && (list(, $curId) = each($newMails))) { if ($mailesProcessed >= $maxMails) { KalturaLog::info("Reached the max mails per job for current mailbox [{$mailboxNumber}] - skipping to next mailbox"); $keepCurMailbox = false; // skip current mailbox continue; // skip current mail --> skip current mailbox } $mailesProcessed++; // fetch current message $curMail = $mailChecker->fetchMsg($curId); if (!$curMail) { KalturaLog::err("Error fetching message with folder ID [{$curId}] from [{$user}@{$host}] - " . imap_last_error()); continue; // skip current mail - error fetching } // check if mail contains attachments if (!$curMail->attachments || count($curMail->attachments) == 0) { // no attachments found KalturaLog::info('No attachments found for mail [' . $curMail->header->msgid . "] on [{$user}@{$host}] from [" . $curMail->header->fromadd . '] with subject [' . $curMail->header->subject . ']'); if (!$mailChecker->moveMsg($curId, self::NO_ATTACHMENT)) { KalturaLog::err('Failed moving msg [' . $curMail->header->msgid . '] to the [' . self::NO_ATTACHMENT . '] folder - ' . imap_last_error()); } continue; // skip current mail - no attachments } // validate partner and get email profile $email_profiles = $this->validePartnerAndGetProfile($curMail->header->toadd, $user . '@' . $host); if (!$email_profiles) { // error validating partner KalturaLog::err('Partner validation failed for [' . $curMail->header->msgid . "] on [{$user}@{$host}] from [" . $curMail->header->fromadd . '] with subject [' . $curMail->header->subject . ']'); if (!$mailChecker->moveMsg($curId, self::PARTNER_INVALID)) { KalturaLog::err('Failed moving msg [' . $curMail->header->msgid . '] to the [' . self::PARTNER_INVALID . '] folder - ' . imap_last_error()); } continue; // skip current mail - partner invalid } // create a new media entry from data in mail body text $mediaEntry = $this->createMediaEntry($curMail->header, $curMail->body); // add the mail's attachment for each valid email profile $failures = new AddEntriesFailures(); foreach ($email_profiles as $profile) { KalturaLog::info("*** Currently processing attachments for email profile id [{$profile->id}] of partner id [{$profile->partnerId}]"); // add a new entry for each attachment //TODO: currently, the same attachment will be uploaded again and again for each different profile because the uploaded file is being transferred on the server - this should be changed. if (!$this->addEntries($curMail, $profile, $mediaEntry, $failures)) { KalturaLog::err("Some errors occured while adding entries for email profile id [{$profile->id}] of partner id [{$profile->partnerId}]"); } } // check if any problems happened if ($failures->problemsHappened()) { $new_folder = self::UNKNOWN; if ($failures->upload_failed || $failures->add_entry_failed || $failures->error_saving_temp_file) { // some attachments had problems $new_folder = self::ADD_ENTRY_FAIL; KalturaLog::crit('Failed adding some attachments for [' . $curMail->header->msgid . "] on [{$user}@{$host}] Moving msg to [" . $new_folder . '] folder'); } else { if ($failures->too_many_attachments) { // too many attachments $new_folder = self::INCOMPLETE; KalturaLog::err('Msg [' . $curMail->header->msgid . "] on [{$user}@{$host}] from [" . $curMail->header->fromadd . '] with subject [' . $curMail->header->subject . "] contains too many attachments. Moving msg to [{$new_folder}] folder"); } else { if ($failures->attachment_too_big || $failures->attachment_invalid) { // errors in specific attachments if (count($curMail->attachments) > 1) { $new_folder = self::INCOMPLETE; KalturaLog::err('Some invalid attachments found for msg [' . $curMail->header->msgid . "] on [{$user}@{$host}] from [" . $curMail->header->fromadd . '] with subject [' . $curMail->header->subject . "]. Moving msg to [{$new_folder}] folder"); } else { $new_folder = self::ATTACHMENT_INVALID; KalturaLog::err('Msg attachment is invalid for msg [' . $curMail->header->msgid . "] on [{$user}@{$host}] from [" . $curMail->header->fromadd . '] with subject [' . $curMail->header->subject . "]. Moving msg to [{$new_folder}] folder"); } } else { // shouldn't get here KalturaLog::err('*** Not all addEntriesFailures situations were handled.'); } } } // move msg to the right error folder if (!$mailChecker->moveMsg($curId, $new_folder)) { KalturaLog::err('Failed moving msg [' . $curMail->header->msgid . '] to the [' . $new_folder . '] folder - ' . imap_last_error()); } } else { // ------------------------------------------------------ // all attachments were added succesfuly for all profiles // ------------------------------------------------------ if (!$mailChecker->moveMsg($curId, self::PROCESS_OK)) { KalturaLog::err('Msg [' . $curMail->header->msgid . '] from [' . $curMail->header->fromadd . '] with subject [' . $curMail->header->subject . '] was processed OK but failed moving to the [' . self::PROCESS_OK . '] folder - ' . imap_last_error()); } } } // end loop through mails in current mailbox } // end loop through mailboxes }
* <user>@<host>: READ messages in INBOX: <number of read messages in INBOX> * * @package Scheduler * @subpackage Email-Ingestion */ require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'KMailChecker.class.php'; $arguments = $_SERVER['argv']; if (count($arguments) < 5) { die('Usages is \'php ' . basename(__FILE__) . ' <host> <port> <user> <pass>\'' . PHP_EOL); } $host = $arguments[1]; $port = $arguments[2]; $user = $arguments[3]; $pass = $arguments[4]; $mailChecker = new KMailChecker($host, $port, $user, $pass, '/novalidate-cert'); if (!@$mailChecker->connect()) { die("Can't connect to [{$host}:{$port}] as {$user} - " . imap_last_error() . PHP_EOL); } $folders = $mailChecker->getFolders(); $seenInInbox = false; foreach ($folders as $curFolder) { $overview = $mailChecker->getFolderOverview($curFolder); echo "{$user}@{$host}: {$curFolder}: {$overview->messages}" . PHP_EOL; // total messages for each folder if ($curFolder == 'INBOX' && $overview->unseen < $overview->messages) { $seenInInbox = $overview->messages - $overview->unseen; } } if ($seenInInbox) { echo "{$user}@{$host}: READ messages in INBOX: {$seenInInbox}"; }