private function findMessage($token, $user) { $i = 0; do { $mailbox = new fMailbox('imap', EMAIL_SERVER, $user, EMAIL_PASSWORD); $messages = $mailbox->listMessages(); foreach ($messages as $number => $headers) { if (strpos($headers['subject'], $token) !== FALSE) { $message = $mailbox->fetchMessage($number, TRUE); $mailbox->deleteMessages($number); return $message; } } $mailbox->close(); usleep(500000); } while ($i < 20); throw new Exception('Email message ' . $token . ' never arrived'); }
/** * @dataProvider serverProvider */ public function testListGet($type, $host, $port, $secure, $username, $password) { $mailbox = new fMailbox($type, $host, $username, $password, $port, $secure, 5); fMailbox::addSMIMEPair('*****@*****.**', './email/tests@flourishlib.com.crt', './email/tests@flourishlib.com.key', EMAIL_PASSWORD); $messages = array(); foreach ($mailbox->listMessages() as $uid => $overview) { $info = $mailbox->fetchMessage($uid); if (!isset($info['headers'])) { fCore::expose($info); } $messages[$info['headers']['message-id']] = array('subject' => $info['headers']['subject'], 'from' => $info['headers']['from']['mailbox'] . '@' . $info['headers']['from']['host'], 'to' => $info['headers']['to'][0]['mailbox'] . '@' . $info['headers']['from']['host'], 'verified' => !empty($info['verified']) ? $info['verified'] : NULL, 'decrypted' => !empty($info['decrypted']) ? $info['decrypted'] : NULL); } $expected_output = array('<*****@*****.**>' => array('subject' => 'A Tést of Iñtërnâtiônàlizætiøn', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'UTF-8 …', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'More UTF-8: ⅞ ⅝ ⅜ ⅛', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Attachments Test', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Inline Images and Attachments', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Multiple To and Cc', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Re: Multiple To and Cc', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Re: Multiple To and Cc', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Re: Multiple To and Cc', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Re: Multiple To and Cc', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'Re: Multiple To and Cc', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'This message has been signed', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => TRUE, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'This message has also been signed', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => TRUE, 'decrypted' => NULL)); if ($username == '*****@*****.**') { $expected_output['<*****@*****.**>'] = array('subject' => 'This message is signed and encrypted', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => TRUE, 'decrypted' => TRUE); $expected_output['<*****@*****.**>'] = array('subject' => 'This message is encrypted', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => TRUE); } $expected_output = array_merge($expected_output, array('<*****@*****.**>' => array('subject' => 'This is a test of fEmail signing', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => TRUE, 'decrypted' => NULL), '<*****@*****.**>' => array('subject' => 'This is a test of fEmail encryption', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => NULL, 'decrypted' => TRUE), '<*****@*****.**>' => array('subject' => 'This is a test of fEmail encryption + signing', 'from' => '*****@*****.**', 'to' => '*****@*****.**', 'verified' => TRUE, 'decrypted' => TRUE))); $this->assertEquals($expected_output, $messages); }
function email__retrieve_incoming() { global $settings, $settings__email_server_type, $settings__email_server_name, $settings__email_server_port, $settings__email_username, $settings__email_password, $settings__email_ssl; $continue = true; $result = array(); $result['errors'] = array(); if (!isset($settings__email_server_type) || !in_array($settings__email_server_type, array('pop3', 'imap'))) { $result['errors'][] = 'No valid email server type given.'; $continue = false; } if (!isset($settings__email_server_name) || !$settings__email_server_name) { $result['errors'][] = 'No email server name given.'; $continue = false; } if (!isset($settings__email_username) || !$settings__email_username) { $result['errors'][] = 'No email username name given.'; $continue = false; } if (!isset($settings__email_password) || !$settings__email_password) { $result['errors'][] = 'No email username name given.'; $continue = false; } if (!isset($settings__email_server_port) || !$settings__email_server_port) { $settings__email_server_port = NULL; } if (!isset($settings__email_ssl) || !$settings__email_ssl) { $settings__email_ssl = FALSE; } else { $settings__email_ssl = TRUE; } if ($continue) { include_once '../tagsets/class.fmailbox.php'; $mailbox = new fMailbox($settings__email_server_type, $settings__email_server_name, $settings__email_username, $settings__email_password, $settings__email_server_port, $settings__email_ssl); $messages = $mailbox->listMessages(); $count = 0; foreach ($messages as $message) { $continue = true; if (isset($settings['email_module_delete_emails_from_server']) && $settings['email_module_delete_emails_from_server'] == 'n') { if (!isset($all_email_ids)) { $query = "SELECT message_id FROM " . table('emails'); $qresult = or_query($query); $all_email_ids = array(); while ($m = pdo_fetch_assoc($qresult)) { $all_email_ids[] = $m['message_id']; } } if (in_array($message['message_id'], $all_email_ids)) { $continue = false; } } if ($continue) { // download message $email = $mailbox->fetchMessage($message['uid'], TRUE); // prepare and save to db if (isset($email['text'])) { $body = email__strip_html($email['text']); } elseif (isset($email['html'])) { $body = email__strip_html($email['html']); } if (isset($email['attachment']) && count($email['attachment']) > 0) { $has_attachments = 1; $data_string = email__attachment_array_to_dbstring($email['attachment']); } else { $has_attachments = 0; $data_string = ''; } $to_adds = array(); $cc_adds = array(); foreach ($email['headers']['to'] as $to_add) { $to_adds[] = $to_add['mailbox'] . "@" . $to_add['host']; } if (isset($email['headers']['cc']) && is_array($email['headers']['cc'])) { foreach ($email['headers']['cc'] as $cc_add) { $cc_adds[] = $cc_add['mailbox'] . "@" . $cc_add['host']; } } $pars = array(); $pars[':message_id'] = $message['message_id']; $pars[':message_type'] = 'incoming'; $pars[':timestamp'] = strtotime($message['date']); $pars[':from_address'] = $email['headers']['from']['mailbox'] . "@" . $email['headers']['from']['host']; $pars[':from_name'] = isset($email['headers']['from']['personal']) ? $email['headers']['from']['personal'] : ''; $pars[':reply_to_address'] = ''; if (isset($email['headers']['reply-to'])) { $pars[':reply_to_address'] = $email['headers']['reply-to']['mailbox'] . "@" . $email['headers']['reply-to']['host']; } $pars[':to_address'] = implode(",", $to_adds); $pars[':cc_address'] = implode(",", $cc_adds); $pars[':subject'] = email__strip_html($message['subject']); if (!$pars[':subject']) { $pars[':subject'] = "no subject"; } $pars[':body'] = $body; $pars[':has_attachments'] = $has_attachments; $pars[':attachment_data'] = $data_string; $pars[':thread_id'] = $message['message_id']; $pars[':thread_time'] = $pars[':timestamp']; $pars[':mailbox'] = 'not_assigned'; $query = "INSERT IGNORE INTO " . table('emails') . "\n SET message_id= :message_id,\n message_type= :message_type,\n timestamp= :timestamp,\n from_address= :from_address,\n from_name= :from_name,\n reply_to_address= :reply_to_address,\n to_address= :to_address,\n cc_address= :cc_address,\n subject= :subject,\n body= :body,\n has_attachments= :has_attachments,\n attachment_data= :attachment_data,\n thread_id = :thread_id,\n thread_time = :thread_time,\n mailbox = :mailbox\n "; $done = or_query($query, $pars); if (pdo_num_rows($done) > 0) { $count++; } // delete from server if (!(isset($settings['email_module_delete_emails_from_server']) && $settings['email_module_delete_emails_from_server'] == 'n')) { $mailbox->deleteMessages($message['uid']); } } } $result['count'] = $count; } return $result; }