/** * create instance with parameters * Supported paramters are * - user username * - host hostname or ip address of IMAP server [optional, default = 'localhost'] * - password password for user 'username' [optional, default = ''] * - port port for IMAP server [optional, default = 110] * - ssl 'SSL' or 'TLS' for secure sockets * - folder select this folder [optional, default = 'INBOX'] * * @param array $params mail reader specific parameters * @throws Zend_Mail_Storage_Exception * @throws Zend_Mail_Protocol_Exception */ public function __construct($params) { $this->_has['flags'] = true; if ($params instanceof Zend_Mail_Protocol_Imap) { $this->_protocol = $params; try { $this->selectFolder('INBOX'); } catch (Zend_Mail_Storage_Exception $e) { throw new Zend_Mail_Storage_Exception('cannot select INBOX, is this a valid transport?'); } return; } if (!isset($params['user'])) { throw new Zend_Mail_Storage_Exception('need at least user in params'); } $params['host'] = isset($params['host']) ? $params['host'] : 'localhost'; $params['password'] = isset($params['password']) ? $params['password'] : ''; $params['port'] = isset($params['port']) ? $params['port'] : null; $params['ssl'] = isset($params['ssl']) ? $params['ssl'] : false; $this->_protocol = new Zend_Mail_Protocol_Imap(); $this->_protocol->connect($params['host'], $params['port'], $params['ssl']); if (!$this->_protocol->login($params['user'], $params['password'])) { throw new Zend_Mail_Storage_Exception('cannot login, user or password wrong'); } $this->selectFolder(isset($params['folder']) ? $params['folder'] : 'INBOX'); }
/** * create instance with parameters * Supported paramters are * - user username * - host hostname or ip address of IMAP server [optional, default = 'localhost'] * - password password for user 'username' [optional, default = ''] * - port port for IMAP server [optional, default = 110] * - ssl 'SSL' or 'TLS' for secure sockets * - folder select this folder [optional, default = 'INBOX'] * * @param array $params mail reader specific parameters * @throws Zend_Mail_Storage_Exception * @throws Zend_Mail_Protocol_Exception */ public function __construct($params) { if (is_array($params)) { $params = (object) $params; } $this->_has['flags'] = true; if ($params instanceof Zend_Mail_Protocol_Imap) { $this->_protocol = $params; try { $this->selectFolder('INBOX'); } catch (Zend_Mail_Storage_Exception $e) { throw new Zend_Mail_Storage_Exception('cannot select INBOX, is this a valid transport?'); } return; } if (!isset($params->user)) { throw new Zend_Mail_Storage_Exception('need at least user in params'); } $host = isset($params->host) ? $params->host : 'localhost'; $password = isset($params->password) ? $params->password : ''; $port = isset($params->port) ? $params->port : null; $ssl = isset($params->ssl) ? $params->ssl : false; $this->_protocol = new Zend_Mail_Protocol_Imap(); $this->_protocol->connect($host, $port, $ssl); if (!$this->_protocol->login($params->user, $password)) { throw new Zend_Mail_Storage_Exception('cannot login, user or password wrong'); } $this->selectFolder(isset($params->folder) ? $params->folder : 'INBOX'); }
/** * get imap connection * * @return Zend_Mail_Protocol_Imap * @throws Tinebase_Exception_AccessDenied */ protected function _getImapConnection() { if (!$this->_imap instanceof Zend_Mail_Protocol_Imap) { $this->_imap = new Zend_Mail_Protocol_Imap($this->_config['host'], $this->_config['port'], $this->_config['ssl']); $loginResult = $this->_imap->login($this->_config['admin'], $this->_config['password']); if (!$loginResult) { throw new Tinebase_Exception_AccessDenied('Could not login to cyrus server ' . $this->_config['host'] . ' with user ' . $this->_config['admin']); } } return $this->_imap; }
public function index() { $this->id = "content"; $this->template = "import/list.tpl"; $this->layout = "common/layout"; require_once 'Zend/Mail/Protocol/Imap.php'; require_once 'Zend/Mail/Protocol/Pop3.php'; $request = Registry::get('request'); $db = Registry::get('db'); $lang = Registry::get('language'); if ($this->request->post['type'] == 'pop3') { try { $conn = new Zend_Mail_Protocol_Pop3($this->request->post['server'], '110', false); } catch (Zend_Mail_Protocol_Exception $e) { print "<span class=\"text-error\">" . $this->request->post['server'] . ": " . $lang->data['text_connection_failed'] . "</span> "; } if ($conn) { $s = $conn->connect($this->request->post['server']); if ($s) { try { $conn->login($this->request->post['username'], $this->request->post['password']); print "<span class=\"text-success\">" . $lang->data['text_connection_ok'] . "</span> "; } catch (Zend_Mail_Protocol_Exception $e) { print "<span class=\"text-error\">" . $this->request->post['username'] . ": " . $lang->data['text_login_failed'] . "</span> "; } } } } else { if ($this->request->post['type'] == 'imap') { try { $conn = new Zend_Mail_Protocol_Imap($this->request->post['server'], '143', false); $login = $conn->login($this->request->post['username'], $this->request->post['password']); if ($login) { print "<span class=\"text-success\">" . $lang->data['text_connection_ok'] . "</span> "; } else { print "<span class=\"text-error\">" . $this->request->post['username'] . ": " . $lang->data['text_login_failed'] . "</span> "; } } catch (Zend_Mail_Protocol_Exception $e) { print "<span class=\"text-error\">" . $this->request->post['server'] . ": " . $lang->data['text_connection_failed'] . "</span> "; } } else { print "<span class=\"text-error\">" . $lang->data['text_error'] . "</span> "; } } }
public function testStore() { $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']); $protocol->login($this->_params['user'], $this->_params['password']); $protocol->select('INBOX'); $this->assertTrue($protocol->store(array('\\Flagged'), 1)); $this->assertTrue($protocol->store(array('\\Flagged'), 1, null, '-')); $this->assertTrue($protocol->store(array('\\Flagged'), 1, null, '+')); $result = $protocol->store(array('\\Flagged'), 1, null, '', false); $this->assertTrue(in_array('\\Flagged', $result[1])); $result = $protocol->store(array('\\Flagged'), 1, null, '-', false); $this->assertFalse(in_array('\\Flagged', $result[1])); $result = $protocol->store(array('\\Flagged'), 1, null, '+', false); $this->assertTrue(in_array('\\Flagged', $result[1])); }
/** * 检查未读邮件 */ public function checkAction() { //$address = $this->_request->getQuery('address'); $emails = $this->_daoEmail->getEmails(array('orgid' => $this->_user->orgId, 'userid' => $this->_user->userId)); $result = array(); foreach ($emails as $email) { if (null != $email && $email->protocol == 'imap' && $email->host) { $port = $email->port ? $email->port : ($email->isSsl ? 993 : 143); $imap = new Zend_Mail_Protocol_Imap(); try { $connect = $imap->connect($email->host, $port, $email->isSsl); } catch (Zend_Mail_Protocol_Exception $e) { continue; } $login = $imap->login($email->address, $email->password); // 登录邮箱 if (!$login) { $result[$email->address] = array('msg' => sprintf($this->lang['mailbox_login_failure'], $email->address, $email->address)); continue; } // 打开INBOX $ret = $imap->examineOrSelect(); if (false !== $ret && isset($ret['recent'])) { $email->unreadNum = (int) $ret['recent']; // 查找新邮件 /*$records = $imap->search('RECENT'); if (false !== $records) { }*/ $result[$email->address] = array('recent' => $email->unreadNum); $this->_daoEmail->updateEmail($email->orgId, $email->userId, $email->address, array('lastcheckinfo' => implode("\n", array($email->unreadNum, $email->lastMailId, $email->lastMailSubject, $email->lastMailFrom)), 'lastchecktime' => time())); } $imap->logout(); } } $this->json(true, null, $result); }
public function testWithInstanceConstruction() { $protocol = new Zend_Mail_Protocol_Imap($this->_params['host']); $protocol->login($this->_params['user'], $this->_params['password']); // if $protocol is invalid the constructor fails while selecting INBOX $mail = new Zend_Mail_Storage_Imap($protocol); }
private function checkLoginAgainstIMAP($username = '', $password = '') { $session = Registry::get('session'); $emails = array($username); if (!strchr($username, '@')) { return 0; } $imap = new Zend_Mail_Protocol_Imap(IMAP_HOST, IMAP_PORT, IMAP_SSL); if ($imap->login($username, $password)) { $imap->logout(); $extra_emails = $this->model_user_user->get_email_addresses_from_groups($emails); $emails = array_merge($emails, $extra_emails); $this->add_session_vars($username, $username, $emails, 0); $session->set("password", $password); return 1; } return 0; }