public function run() { $config = Config::get('xmailer.imap'); $mail = new StorageImap(array('host' => $config['host'], 'user' => $config['user'], 'password' => $config['password'])); //$mail = new StorageImap(Config::get('xmailer.imap')); if ($mail->countMessages() > 0) { $message = $mail->getMessage(1); if ($message->getHeaders()->get("from") != null) { $from = $message->getHeaders()->get("from")->toString(); } else { $from = ""; } if ($message->getHeaders()->get("to") != null) { $to = $message->getHeaders()->get("to")->toString(); } else { $to = ""; } preg_match("/[\\w._%+-]+@[\\w.-]+.[\\w]{2,}/", $from, $fromEmails); preg_match("/[\\w._%+-]+@[\\w.-]+.[\\w]{2,}/", $to, $toEmails); $sendTo = $this->getEmailAdresses($toEmails); if ($this->isValidSender($fromEmails[0])) { foreach ($sendTo as $mailadress) { $fwd = new Message(); $fwd->setBody($message->getContent()); $h = $message->getHeaders(); $h->removeHeader('From'); $h->removeHeader('To'); $h->removeHeader('Reply-To'); $fwd->setHeaders($h); $fwd->addFrom($mailadress['from']); $fwd->addTo($mailadress['to']); if (Config::get('xmailer.replyto')) { $fwd->addReplyTo($fromEmails[0]); } $transport = new SendmailTransport(); $transport->send($fwd); } $mail->moveMessage(1, "INBOX.Sent"); } else { $mail->moveMessage(1, "INBOX.Spam"); } return t("%d email sent to %d People.", 1, count($sendTo)); } return t("No emails sent."); }
/** * @test */ public function thatMessagesMarkedAsProcessedAndProcessedFolderAlreadyExists() { $this->zendImapStorage->expects($this->any())->method('getNumberByUniqueId')->with($this->isType(\PHPUnit_Framework_Constraint_IsType::TYPE_STRING))->will($this->returnValue(1)); $this->zendImapStorage->expects($this->once())->method('getFolders')->will($this->returnValue(new \Zend\Mail\Storage\Folder('/', '/', false, array(new \Zend\Mail\Storage\Folder('INBOX'), new \Zend\Mail\Storage\Folder('SENT'), new \Zend\Mail\Storage\Folder(ImapMessageProvider::NAME_OF_FOLDER_OF_PROCESSED_MESSAGES))))); $this->zendImapStorage->expects($this->never())->method('createFolder'); $this->zendImapStorage->expects($this->any())->method('move')->with($this->equalTo(1), $this->logicalAnd($this->isInstanceOf('\\Zend\\Mail\\Storage\\Folder'), $this->attributeEqualTo('localName', 'INBOX/' . ImapMessageProvider::NAME_OF_FOLDER_OF_PROCESSED_MESSAGES))); $messageProvider = new ImapMessageProvider($this->zendImapStorage); $messages = array(); foreach ($this->messages() as $message) { $messages[] = new \Diamante\EmailProcessingBundle\Model\Message($message['unique_id'], self::DUMMY_MESSAGE_ID, self::DUMMY_MESSAGE_SUBJECT, self::DUMMY_MESSAGE_CONTENT, $this->getDummyFrom(), self::DUMMY_MESSAGE_TO, self::DUMMY_MESSAGE_REFERENCE); } $messageProvider->markMessagesAsProcessed($messages); }
/** * Initialize mailbox folder with name 'Processing'. If folder is not exists - it will be created * @return void */ private function initialize() { if (is_null($this->folderOfProcessedMessages)) { $exists = false; $iterator = new \RecursiveIteratorIterator($this->zendImapStorage->getFolders()); foreach ($iterator as $folder) { if ($folder->getLocalName() == self::NAME_OF_FOLDER_OF_PROCESSED_MESSAGES) { $exists = true; break; } } if (false === $exists) { $this->zendImapStorage->createFolder(self::NAME_OF_FOLDER_OF_PROCESSED_MESSAGES, new \Zend\Mail\Storage\Folder('INBOX')); } $this->folderOfProcessedMessages = new \Zend\Mail\Storage\Folder('INBOX/' . self::NAME_OF_FOLDER_OF_PROCESSED_MESSAGES); } }
/** * Read Email * * @param $mailbox_email * @param $email * @param $accessToken * @param $email_type * @param $imap_server * @param $lastDate * @param $user_id * @param $module * @param string $signature * * @return bool * @internal param $module * @since rt-Helpdesk 0.1 */ public function reademail($mailbox_email, $email, $accessToken, $email_type, $imap_server, $lastDate, $user_id, $module, $signature = '') { set_time_limit(0); global $signature, $rt_mail_settings; if (!$this->try_imap_login($email, $accessToken, $email_type, $imap_server)) { $rt_mail_settings->update_sync_status($email, false); rt_log('login fail'); return false; } $storage = new ImapStorage($this->imap); $email_acc = $rt_mail_settings->get_email_acc($email, $module); if (empty($email_acc)) { $rt_mail_settings->update_sync_meta_time($email, current_time('mysql')); $rt_mail_settings->update_sync_status($email, false); rt_log('email fail'); return false; } $email_data = maybe_unserialize($email_acc->email_data); if (empty($email_data['mail_folders'])) { $rt_mail_settings->update_sync_meta_time($email, current_time('mysql')); $rt_mail_settings->update_sync_status($email, false); rt_log('inbox folder fail'); return false; } $mail_folders = explode(',', isset($email_data['mail_folders']) ? $email_data['mail_folders'] : ''); global $sync_inbox_type; global $rt_mail_uid; foreach ($mail_folders as $folder) { $storage->selectFolder($folder); rt_log(' Reading folder - ' . esc_attr($folder) . "\r\n"); $sync_inbox_type = $folder; if (!isset($rt_mail_uid[$sync_inbox_type])) { $rt_mail_uid[$sync_inbox_type] = 0; } global $rt_mail_uid; $arrayMailIds = array(); if ($rt_mail_uid[$sync_inbox_type] > 0) { $allMail = $storage->protocol->requestAndResponse("UID FETCH {$rt_mail_uid[$sync_inbox_type]}:* (UID)", array()); if (is_array($allMail)) { foreach ($allMail as $tempEmail) { $arrayMailIds[] = array('uid' => $tempEmail[2][1], 'msgid' => $tempEmail[0]); } } } else { $arrayMailIds = $storage->protocol->search(array('SINCE ' . $lastDate)); } $this->rt_parse_email($mailbox_email, $email, $storage, $arrayMailIds, $user_id, $module); rt_log(' Reading finish - ' . esc_attr($folder) . "\r\n"); } $rt_mail_settings->update_sync_meta_time($email, current_time('mysql')); $rt_mail_settings->update_sync_status($email, false); }
public function testCountFlags() { $mail = new Storage\Imap($this->_params); foreach ($mail as $id => $message) { $mail->setFlags($id, array()); } $this->assertEquals($mail->countMessages(Storage::FLAG_SEEN), 0); $this->assertEquals($mail->countMessages(Storage::FLAG_ANSWERED), 0); $this->assertEquals($mail->countMessages(Storage::FLAG_FLAGGED), 0); $mail->setFlags(1, array(Storage::FLAG_SEEN, Storage::FLAG_ANSWERED)); $mail->setFlags(2, array(Storage::FLAG_SEEN)); $this->assertEquals($mail->countMessages(Storage::FLAG_SEEN), 2); $this->assertEquals($mail->countMessages(Storage::FLAG_ANSWERED), 1); $this->assertEquals($mail->countMessages(array(Storage::FLAG_SEEN, Storage::FLAG_ANSWERED)), 1); $this->assertEquals($mail->countMessages(array(Storage::FLAG_SEEN, Storage::FLAG_FLAGGED)), 0); $this->assertEquals($mail->countMessages(Storage::FLAG_FLAGGED), 0); }
/** * {@inheritdoc} */ public function close() { if ($this->ignoreCloseCommand) { return; } parent::close(); }
/** * mailbox folder ui * @param $module * @param string $mailboxid */ function rtmailbox_mailbox_folder_ui($module, $mailboxid = '') { global $rt_mail_settings; $args = array('module' => $module); if (!empty($mailboxid)) { $args['id'] = $mailboxid; } $mailboxes = $rt_mail_settings->get_user_google_ac($args); ?> <h4>Select Mailbox Folders</h4> <?php if (isset($mailboxes) && !empty($mailboxes)) { foreach ($mailboxes as $mailbox) { $mailbox->email_data = unserialize($mailbox->email_data); $email = filter_var($mailbox->email_data['email'], FILTER_SANITIZE_EMAIL); $email_type = $mailbox->type; $imap_server = $mailbox->imap_server; $mail_folders = isset($mailbox->email_data['mail_folders']) ? $mailbox->email_data['mail_folders'] : ''; $mail_folders = array_filter(explode(',', $mail_folders)); $token = $mailbox->outh_token; $all_folders = null; $login_successful = true; try { $hdZendEmail = new Rt_Zend_Mail(); if ($hdZendEmail->try_imap_login($email, $token, $email_type, $imap_server)) { $storage = new ImapStorage($hdZendEmail->imap); $all_folders = $storage->getFolders(); } else { $login_successful = false; } } catch (Exception $e) { $login_successful = false; echo '<p class="description">' . esc_html($e->getMessage()) . '</p>'; } if ($login_successful) { ?> <form id="rtmailbox-imap-folder-form" method="post"> <input id="rtmailbox-module" name="rtmailbox[module]" value="<?php echo $module; ?> " type="hidden"> <input id="rtmailbox-email" name="rtmailbox[email]" value="<?php echo $email; ?> " type="hidden"> <?php wp_nonce_field('rtmailbox_folder_update'); do_action('rt_mailbox_folder_view_before'); ?> <div class="rtmailbox-row"> <label for=""><?php _e('Select Folder'); ?> </label> <div class="mailbox-folder-list"> <?php $hdZendEmail->render_folders_checkbox($all_folders, $element_name = 'mail_folders[' . esc_attr($email) . ']', $values = $mail_folders, $data_str = 'data-email-id=' . $mailbox->id); ?> </div> </div> <div class="clear"></div> <div class="rtmailbox-row"> <label></label> <input id="rtmailbox-action" name="rtmailbox[action]" value="rtmailbox_folder_update" type="hidden"> <input id="rtmailbox-save" data-mailboxid="<?php echo $mailboxid; ?> " name="rtmailbox[save]" class="button" value="Save" type="button"> </div> <?php do_action('rt_mailbox_folder_view_after'); ?> </form> <?php } } } else { echo '<p>Mailbox not found.</p>'; } }
public function countMessages($flags) { return $this->storage->countMessages($flags); }