protected function getallAction() { $translate = $this->application->getBootstrap()->getResource('translate'); $view = $this->application->getBootstrap()->getResource('view'); $view->setScriptPath(APPLICATION_PATH . '/views/scripts'); $acl = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('formacl'); $queueticketObj = new Queueticket(); $queueticketRows = $queueticketObj->fetchAll("ghost = false"); foreach ($queueticketRows as $queueticketRow) { try { $mail = new Zend_Mail_Storage_Imap(array('host' => $queueticketRow['input_email_host'], 'user' => $queueticketRow['input_email_user'], 'password' => $queueticketRow['input_email_password'])); for ($i = 1; $i <= $mail->countMessages(); $i++) { $message = $mail->getMessage($i); if (!$message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) { $values = array('subject' => $message->subject, 'content' => $message->getContent(), 'priority' => substr($queueticketRow['available_priority'], 0, 1), 'state' => substr($queueticketRow['available_states'], 0, 1), 'start' => date("c"), 'id_queue' => $queueticketRow['id']); $model = new Ticket(); $row = $model->createRow($values); $row->save(); $mail->setFlags($i, array(Zend_Mail_Storage::FLAG_SEEN)); } } } catch (Exception $e) { print_r($e->getMessage()); } } }
function connect($prefix = null) { // connecting with Imap $params = array('host' => 'imap.gmail.com', 'user' => $this->username, 'password' => $this->password, 'ssl' => 'SSL'); //$this->mail = new Zend_Mail_Storage_Imap ( $params ); parent::__construct($params); $this->mail = $this; }
/** * Given an open and authenticated IMAP connection, displays some basic info * about the INBOX folder. */ function showInbox($imap) { /** * Print the INBOX message count and the subject of all messages * in the INBOX */ $storage = new Zend_Mail_Storage_Imap($imap); include 'header.php'; echo '<h1>Total messages: ' . $storage->countMessages() . "</h1>\n"; /** * Retrieve first 5 messages. If retrieving more, you'll want * to directly use Zend_Mail_Protocol_Imap and do a batch retrieval, * plus retrieve only the headers */ echo 'First five messages: <ul>'; for ($i = 1; $i <= $storage->countMessages() && $i <= 5; $i++) { echo '<li>' . htmlentities($storage->getMessage($i)->subject) . "</li>\n"; } echo '</ul>'; }
public function testCountFlags() { $mail = new Zend_Mail_Storage_Imap($this->_params); foreach ($mail as $id => $message) { $mail->setFlags($id, array()); } $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_SEEN), 0); $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_ANSWERED), 0); $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_FLAGGED), 0); $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_ANSWERED)); $mail->setFlags(2, array(Zend_Mail_Storage::FLAG_SEEN)); $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_SEEN), 2); $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_ANSWERED), 1); $this->assertEquals($mail->countMessages(array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_ANSWERED)), 1); $this->assertEquals($mail->countMessages(array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_FLAGGED)), 0); $this->assertEquals($mail->countMessages(Zend_Mail_Storage::FLAG_FLAGGED), 0); }
public function testSetFlags() { $mail = new Zend_Mail_Storage_Imap($this->_params); $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN)); $message = $mail->getMessage(1); $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)); $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED)); $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_SEEN, Zend_Mail_Storage::FLAG_FLAGGED)); $message = $mail->getMessage(1); $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)); $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED)); $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_FLAGGED)); $message = $mail->getMessage(1); $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)); $this->assertTrue($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED)); $mail->setFlags(1, array('myflag')); $message = $mail->getMessage(1); $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)); $this->assertFalse($message->hasFlag(Zend_Mail_Storage::FLAG_FLAGGED)); $this->assertTrue($message->hasFlag('myflag')); try { $mail->setFlags(1, array(Zend_Mail_Storage::FLAG_RECENT)); } catch (Exception $e) { return; // ok } $this->fail('should not be able to set recent flag'); }
public function receive_email_crond() { $now = time(); $lock_time = AWS_APP::cache()->get('receive_email_locker'); if ($lock_time and $now - $lock_time <= 600) { return false; } $receiving_email_accounts = $this->get_receiving_email_config(); if (!$receiving_email_accounts) { return false; } AWS_APP::cache()->set('receive_email_locker', $now, 600); foreach ($receiving_email_accounts as $receiving_email_config) { if (!$receiving_email_config['server'] or !$receiving_email_config['protocol'] or !$receiving_email_config['username'] or !$receiving_email_config['password'] or !$receiving_email_config['uid']) { continue; } $mail_config = array('host' => $receiving_email_config['server'], 'user' => $receiving_email_config['username'], 'password' => $receiving_email_config['password']); if ($receiving_email_config['ssl'] == 1) { $mail_config['ssl'] = 'SSL'; } if ($receiving_email_config['port']) { $mail_config['port'] = $receiving_email_config['port']; } try { switch ($receiving_email_config['protocol']) { case 'pop3': $mail = new Zend_Mail_Storage_Pop3($mail_config); break; case 'imap': $mail = new Zend_Mail_Storage_Imap($mail_config); break; default: continue 2; } } catch (Exception $e) { $this->notification_of_receive_email_error($receiving_email_config['id'], $e->getMessage()); continue; } $this->notification_of_receive_email_error($receiving_email_config['id'], null); $received_email['config_id'] = $receiving_email_config['id']; $received_email['uid'] = $receiving_email_config['uid']; foreach ($mail as $num => $message) { try { if ($receiving_email_config['protocol'] == 'imap' and $message->hasFlag(Zend_Mail_Storage::FLAG_SEEN)) { continue; } $received_email['message_id'] = substr($message->messageID, 1, -1); $received_email['date'] = intval(strtotime($message->Date)); if ($now - $received_email['date'] > 604800 or $this->fetch_row('received_email', 'message_id = "' . $this->quote($received_email['message_id']) . '" AND date = ' . $received_email['date'])) { continue; } if ($message->isMultipart()) { for ($i = 1; $i <= $message->countParts(); $i++) { $part = $message->getPart($i); if (substr($part->contentType, 0, 5) == 'text/') { $encoding = $part->contentTransferEncoding; $type = $part->contentType; $received_email['content'] = $part->getContent(); break; } else { continue; } } } else { $encoding = $message->contentTransferEncoding; $type = $message->contentType; $received_email['content'] = $message->getContent(); } if (!$encoding or !$type) { continue; } preg_match('/charset\\s?=\\s?"?([a-zA-Z0-9-]+)"?$/i', $type, $matches); $charset = strtolower($matches[1]); $received_email['subject'] = decode_eml($message->Subject); preg_match('/<?([^<]+@.+(\\.[^>]+)+)>?$/i', $message->From, $matches); $received_email['from'] = strtolower($matches[1]); switch ($encoding) { case 'base64': $received_email['content'] = base64_decode($received_email['content']); break; case 'quoted-printable': $received_email['content'] = quoted_printable_decode($received_email['content']); break; } if ($charset and $charset != 'utf-8') { $received_email['subject'] = mb_convert_encoding($received_email['subject'], 'utf-8', $charset); $received_email['content'] = mb_convert_encoding($received_email['content'], 'utf-8', $charset); } $received_email['subject'] = strip_tags($received_email['subject']); $received_email['content'] = strip_tags(preg_replace(array('/<p(\\s+[^>]*)?>/i', '/<\\/p>/i', '/<br\\s*\\/?>/i'), "\n", $received_email['content'])); $now++; $received_email['access_key'] = md5($received_email['uid'] . $now); $this->insert('received_email', $received_email); if ($receiving_email_config['protocol'] == 'pop3') { $mail->removeMessage($num); } if ($receiving_email_config['protocol'] == 'imap') { $mail->setFlags($num, array(Zend_Mail_Storage::FLAG_SEEN)); } } catch (Exception $e) { continue; } } } AWS_APP::cache()->delete('receive_email_locker'); return true; }
} /** * Generate SASL client request, using base64 encoded * OAuth params */ $initClientRequest = 'GET ' . $urlWithXoauth . ' ' . $oauthParams; $initClientRequestEncoded = base64_encode($initClientRequest); /** * Make the IMAP connection and send the auth request */ $imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true); $authenticateParams = array('XOAUTH', $initClientRequestEncoded); $imap->requestAndResponse('AUTHENTICATE', $authenticateParams); /** * Print the INBOX message count and the subject of all messages * in the INBOX */ $storage = new Zend_Mail_Storage_Imap($imap); include 'header.php'; echo '<h1>Total messages: ' . $storage->countMessages() . ' for ' . $TWO_LEGGED_EMAIL_ADDRESS . "</h1>\n"; /** * Retrieve first 5 messages. If retrieving more, you'll want * to directly use Zend_Mail_Protocol_Imap and do a batch retrieval, * plus retrieve only the headers */ echo 'First five messages: <ul>'; for ($i = 1; $i <= $storage->countMessages() && $i <= 5; $i++) { echo '<li>' . htmlentities($storage->getMessage($i)->subject) . "</li>\n"; } echo '</ul>'; include 'footer.php';
public function testFetchHeaderFolder() { $mail = new Zend_Mail_Storage_Imap($this->_params); $mail->selectFolder('subfolder/test'); $subject = $mail->getMessage(1)->subject; $this->assertEquals('Message in subfolder', $subject); }
/** * Gathers all needed information to tranform an imap folder to a * Conjoon_Modules_Groupware_Email_Folder_Dto obejct. * * @param Zend_Mail_Storage_Folder $folder * @param Conjoon_Modules_Groupware_Email_Account_Dto $account * @param Zend_Mail_Protocol_Imap $protocol * @param boolean $isRootLevel Whether the folder is on the first level of * the mailbox hierarchy * * @return Conjoon_Modules_Groupware_Email_Folder_Dto */ private function _transformImapFolder(Zend_Mail_Storage_Folder $folder, Conjoon_Modules_Groupware_Email_Account_Dto $account, Zend_Mail_Protocol_Imap $protocol, $isRootLevel = false, $type = 'folder') { $delim = Conjoon_Modules_Groupware_Email_ImapHelper::getFolderDelimiterForImapAccount($account); $globalName = $folder->getGlobalName(); $path = explode($delim, $globalName); $path = $path[count($path) - 1]; $pendingCount = 0; if (!$this->itemListRequestFacade) { $this->itemListRequestFacade = Conjoon_Modules_Groupware_Email_Item_ItemListRequestFacade::getInstance(); } if ($folder->isSelectable()) { try { $pendingCount = $this->itemListRequestFacade->getPendingCountForGlobalName($account, $globalName); } catch (Exception $e) { // ignore } } $childCount = 0; if (!$folder->isLeaf()) { $protocol = Conjoon_Modules_Groupware_Email_ImapHelper::reuseImapProtocolForAccount($account); /** * @see Zend_Mail_Storage_Imap */ require_once 'Zend/Mail/Storage/Imap.php'; $imap = new Zend_Mail_Storage_Imap($protocol); /** * @ticket CN-595 */ if ($globalName === "INBOX") { $iFolders = $imap->getFolders(null)->getChildren(); } else { $iFolders = $imap->getFolders($globalName)->getChildren(); } foreach ($iFolders as $tmpFolder) { $childCount++; } } return Conjoon_Modules_Groupware_Email_ImapHelper::transformToFolderDto($folder, $isRootLevel, array('id' => $account->id . '_' . $globalName, 'idForPath' => $path, 'pendingCount' => $pendingCount, 'type' => $type, 'childCount' => $childCount)); }
<?php set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/../library'); require 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $autoloader->registerNamespace('ZendX'); $mailbox = new Zend_Mail_Storage_Imap(array('host' => 'imap.example.org', 'user' => '*****@*****.**', 'password' => '...')); if ($mailbox->countMessages()) { $request = new ZendX_MailControl_Controller_Request_Mail($mailbox->getMessage(3)); $front = Zend_Controller_Front::getInstance(); $front->setControllerDirectory(dirname(__FILE__) . '/../application/controllers')->setRouter(new ZendX_MailControl_Controller_Router_Null())->throwExceptions(true)->dispatch($request); }
/** * Perform periodic tasks * * @return boolean true because otherwise it disables subsequent plugins */ public function hookPeriodicTask(&$pa_params) { global $AUTH_CURRENT_USER_ID; $t_log = new Eventlog(); $o_db = new Db(); //$t_log->log(array('CODE' => 'ERR', 'MESSAGE' => _t('Could not authenticate to remote system %1', $vs_base_url), 'SOURCE' => 'traveloguePlugin->hookPeriodicTask')); // Get new email $pn_locale_id = 1; // US $vs_server = $this->opo_config->get('imap_server'); $vs_username = $this->opo_config->get('username'); $vs_password = $this->opo_config->get('password'); $vs_ssl = $this->opo_config->get('ssl'); if (!$vs_server) { return true; } if (!$vs_username) { return true; } try { $o_mail = new Zend_Mail_Storage_Imap(array('host' => $vs_server, 'user' => $vs_username, 'password' => $vs_password, 'ssl' => $vs_ssl)); } catch (Exception $e) { return true; } $va_mimetypes = $this->opo_config->getList('mimetypes'); $va_mail_to_delete = array(); foreach ($o_mail as $vn_message_num => $o_message) { $va_mail_to_delete[$vn_message_num] = true; // Extract title from subject line of email $vs_subject = $o_message->subject; $vs_from = $o_message->headerExists('from') ? $o_message->from : ""; print "PROCESSING {$vs_subject} FROM {$vs_from}\n"; // Extract media from email attachments // Extract caption from email body $va_images = array(); $va_texts = array(); foreach (new RecursiveIteratorIterator($o_message) as $o_part) { try { if (in_array(strtok($o_part->contentType, ';'), $va_mimetypes)) { $va_images[] = $o_part; } else { if (in_array(strtok($o_part->contentType, ';'), array("text/plain", "text/html"))) { $va_texts[] = (string) $o_part; } } } catch (Zend_Mail_Exception $e) { // ignore } } if (!sizeof($va_images)) { continue; } // Get user by email address if (preg_match('!<([^>]+)>!', $vs_from, $va_matches)) { // extract raw address from "from" header $vs_from = $va_matches[1]; } $t_user = new ca_users(); if ($t_user->load(array('email' => $vs_from))) { $AUTH_CURRENT_USER_ID = $vn_user_id = $t_user->getPrimaryKey(); // force libs to consider matched user as logged in; change log will reflect this name } else { $vn_user_id = null; } // Create object $t_object = new ca_objects(); $t_object->setMode(ACCESS_WRITE); $t_object->set('type_id', $this->opo_config->get('object_type')); // TODO: set idno to autogenerated # and/or allow for configurable policy $t_object->set('idno', ''); $t_object->set('access', $this->opo_config->get('default_access')); $t_object->set('status', $this->opo_config->get('default_status')); // TODO: make this a configurable mapping ala how media metadata is done $t_object->addAttribute(array('locale_id' => $pn_locale_id, 'generalNotes' => join("\n\n", $va_texts)), 'generalNotes'); $t_object->insert(); DataMigrationUtils::postError($t_object, "While adding object", "traveloguePlugin"); // TODO: log this $t_object->addLabel(array('name' => $vs_subject), $pn_locale_id, null, true); DataMigrationUtils::postError($t_object, "While adding label", "traveloguePlugin"); // TODO: log this // Add representation $vs_tmp_file_path = tempnam(caGetTempDirPath(), 'travelogue_'); foreach ($va_images as $vn_i => $vs_file_content) { if (file_put_contents($vs_tmp_file_path, base64_decode((string) $vs_file_content))) { $t_object->addRepresentation($vs_tmp_file_path, $this->opo_config->get('representation_type'), 1, $this->opo_config->get('default_status'), $this->opo_config->get('default_access'), true); DataMigrationUtils::postError($t_object, "While adding media", "traveloguePlugin"); // TODO: log this } } // TODO: add option to link user-as-entity to image (probably as creator) } foreach (array_reverse(array_keys($va_mail_to_delete)) as $vn_message_num) { $o_mail->removeMessage($vn_message_num); } return true; }
/** * Generate SASL client request, using base64 encoded * OAuth params */ $initClientRequest = 'GET ' . $url . ' ' . $oauthParams; $initClientRequestEncoded = base64_encode($initClientRequest); /** * Make the IMAP connection and send the auth request */ $imap = new Zend_Mail_Protocol_Imap('imap.gmail.com', '993', true); $authenticateParams = array('XOAUTH', $initClientRequestEncoded); $imap->requestAndResponse('AUTHENTICATE', $authenticateParams); /** * Print the INBOX message count and the subject of all messages * in the INBOX */ $storage = new Zend_Mail_Storage_Imap($imap); include 'header.php'; echo '<h1>Total messages: ' . $storage->countMessages() . "</h1>\n"; /** * Retrieve first 5 messages. If retrieving more, you'll want * to directly use Zend_Mail_Protocol_Imap and do a batch retrieval, * plus retrieve only the headers */ echo 'First five messages: <ul>'; for ($i = 1; $i <= $storage->countMessages() && $i <= 5; $i++) { echo '<li>' . htmlentities($storage->getMessage($i)->subject) . "</li>\n"; } echo '</ul>'; include 'footer.php'; }