function pfemail_check_accounts()
{
    global $conf, $user;
    conf_update_param('pfemail_last_check', date('Y-m-d H:i:s'));
    require_once PFEMAIL_PATH . 'include/ImapMailbox.php';
    $image_ids = array();
    $query = '
SELECT
    *
  FROM ' . PFEMAIL_MAILBOXES_TABLE . '
;';
    $accounts = query2array($query);
    foreach ($accounts as $account) {
        $mailbox = new ImapMailbox($account['path'], $account['login'], $account['password'], $conf['upload_dir'] . '/buffer', 'utf-8');
        $mails = array();
        // Get some mail
        $mailsIds = $mailbox->searchMailBox('UNSEEN');
        if (!$mailsIds) {
            continue;
            // check next email account
        }
        $mailId = reset($mailsIds);
        $mail = $mailbox->getMail($mailId);
        $attachments = $mail->getAttachments();
        include_once PHPWG_ROOT_PATH . 'admin/include/functions_upload.inc.php';
        foreach ($attachments as $attachment) {
            $extension = strtolower(get_extension($attachment->{'name'}));
            if (!in_array($extension, $conf['picture_ext'])) {
                // the file has been downloaded, we have to remove it now
                unlink($attachment->{'filePath'});
                continue;
            }
            $moderate = get_boolean($account['moderated']);
            $image_id = add_uploaded_file($attachment->{'filePath'}, stripslashes($attachment->{'name'}), array($account['category_id']), $moderate ? 16 : 0, null);
            // the photo is added by nobody (using the current user may make the
            // photo editable by her with Admin Tools...)
            single_update(IMAGES_TABLE, array('added_by' => null, 'name' => pfemail_clean_email_subject($mail->subject)), array('id' => $image_id));
            $state = 'auto_validated';
            if ($moderate) {
                $state = 'moderation_pending';
            }
            list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
            single_insert(PFEMAIL_PENDINGS_TABLE, array('image_id' => $image_id, 'state' => $state, 'added_on' => $dbnow, 'from_name' => $mail->fromName, 'from_address' => $mail->fromAddress, 'subject' => $mail->subject));
            $image_ids[] = $image_id;
        }
    }
    if (count($image_ids) > 0) {
        include_once PHPWG_ROOT_PATH . 'admin/include/functions.php';
        invalidate_user_cache();
        // let's notify administrators
        $query = '
SELECT id
  FROM ' . GROUPS_TABLE . '
;';
        $group_ids = query2array($query, null, 'id');
        if (count($group_ids) > 0) {
            include_once PHPWG_ROOT_PATH . 'include/functions_mail.inc.php';
            $thumb_urls = array();
            // force $conf['derivative_url_style'] to 2 (script) to make sure we
            // will use i.php?/upload and not _data/i/upload because you don't
            // know when the cache will be flushed
            $previous_derivative_url_style = $conf['derivative_url_style'];
            $conf['derivative_url_style'] = 2;
            $query = '
SELECT
    id,
    path
  FROM ' . IMAGES_TABLE . '
  WHERE id IN (' . implode(',', $image_ids) . ')
;';
            $result = pwg_query($query);
            while ($row = pwg_db_fetch_assoc($result)) {
                $thumb = DerivativeImage::thumb_url(array('id' => $row['id'], 'path' => $row['path']));
                $thumb_urls[] = $thumb;
            }
            // restore configuration setting
            $conf['derivative_url_style'] = $previous_derivative_url_style;
            $thumbs_html_string = '';
            foreach ($thumb_urls as $thumb_url) {
                if (!empty($thumbs_html_string)) {
                    $thumbs_html_string .= ' ';
                }
                $thumbs_html_string .= '<img src="' . $thumb_url . '">';
            }
            $content = $thumbs_html_string;
            // how many photos pending?
            $pendings = pfemail_get_pending_ids();
            if (count($pendings) > 0) {
                $content .= '<br><br>';
                $content .= '<a href="' . get_absolute_root_url() . 'admin.php?page=plugin-photo_from_email-pendings' . '">';
                $content .= l10n('%d photos pending for validation', count($pendings));
                $content .= '</a>';
            }
            $real_user_id = $user['id'];
            $user['id'] = $conf['guest_id'];
            $subject = l10n('%d photos added by email', count($thumb_urls));
            foreach ($group_ids as $group_id) {
                pwg_mail_group($group_id, array('subject' => '[' . $conf['gallery_title'] . '] ' . $subject, 'mail_title' => $conf['gallery_title'], 'mail_subtitle' => $subject, 'content' => $content, 'content_format' => 'text/html'));
            }
        }
        // restore current user
        $user['id'] = $real_user_id;
    }
}
예제 #2
0
require_once 'classes/ImapMailbox.php';
require_once 'classes/pushbullet.class.php';
// E-mail settings
define('MAIL_PATH', '{localhost:143}INBOX');
// See classes/ImapMailbox.php for some examples
define('MAIL_EMAIL', '*****@*****.**');
// Your e-mail username
define('MAIL_PASSWORD', 'password');
// Your e-mail password
// PushBullet settings
$PushBullet_API_key = 'pushbullet_api_key_goes_here';
$deviceIds = array('device_id_1', 'device_id_2', 'device_id_3');
// Start readin the mailbox
$mailbox = new ImapMailbox(MAIL_PATH, MAIL_EMAIL, MAIL_PASSWORD, false, 'utf-8');
// Get all e-mails from the mailbox
$mailsIds = $mailbox->searchMailBox('ALL');
if ($mailsIds) {
    // Read each mail individually
    foreach ($mailsIds as $mailId) {
        $mail = $mailbox->getMail($mailId);
        // Send the PushBullet notifications
        try {
            $p = new PushBullet($PushBullet_API_key);
            // Send a notification to each device
            foreach ($deviceIds as $deviceId) {
                $p->pushNote($deviceId, $mail->subject, $mail->textPlain);
            }
            // Delete the mail from the folder
            $mailbox->deleteMail($mailId);
        } catch (PushBulletException $e) {
            // Die() on error
 private function PHPParseEmails(&$_reload, $_delete, $_test)
 {
     $list = array();
     $starttime = time();
     $executiontime = Server::SetTimeLimit(CALLER_TIMEOUT - 10);
     try {
         $ssl = $this->Mailbox->SSL == 0 ? "" : ($this->Mailbox->SSL == 1 ? '/ssl' : '/tls');
         $service = $this->Mailbox->Type == "IMAP" ? "imap" : "pop3";
         $mailbox = new ImapMailbox('{' . $this->Mailbox->Host . ':' . $this->Mailbox->Port . '/' . $service . $ssl . '}INBOX', $this->Mailbox->Username, $this->Mailbox->Password, "./uploads/", 'utf-8');
         $mails = $mailbox->searchMailBox('ALL');
         if ($_test) {
             return count($mails);
         }
         if (empty($mails)) {
             return array();
         }
         $counter = 0;
         foreach ($mails as $mailId) {
             $subject = "";
             try {
                 $temail = new TicketEmail();
                 $message = $mailbox->getMail($mailId);
                 $subject = $temail->Subject = $message->subject;
                 //$temail->Id = $message->id;
                 $temail->Id = $message->message_id;
                 $temail->Name = $message->fromName;
                 if (empty($temail->Id)) {
                     $temail->Id = getId(32);
                 }
                 if ($_delete) {
                     $delete[$mailId] = $temail->Id;
                 }
                 $temail->Email = trim($message->fromAddress);
                 reset($message->to);
                 $temail->ReceiverEmail = key($message->to);
                 if (!empty($message->replyTo)) {
                     reset($message->replyTo);
                     $temail->ReplyTo = key($message->replyTo);
                 }
                 $temail->BodyHTML = $message->textHtml;
                 $temail->Body = $message->textPlain;
                 if (empty($temail->Body) && !empty($temail->BodyHTML)) {
                     $temail->Body = MailSystem::DownConvertHTML($temail->BodyHTML);
                 }
                 $atts = $message->getAttachments();
                 foreach ($atts as $att) {
                     $filename = $att->name;
                     $fileid = getId(32);
                     $filesid = Server::$Configuration->File["gl_lzid"] . "_" . $fileid;
                     $content = file_get_contents($att->filePath);
                     $temail->Attachments[$fileid] = array($filesid, $filename, $content);
                     @unlink($att->filePath);
                 }
                 $temail->Created = strtotime($message->date);
                 if (!is_numeric($temail->Created) || empty($temail->Created)) {
                     $temail->Created = time();
                 }
                 $list[] = $temail;
                 if (time() - $starttime >= $executiontime / 2 || $counter++ > DATA_ITEM_LOADS) {
                     $_reload = true;
                     break;
                 }
             } catch (Exception $e) {
                 if ($_test) {
                     throw $e;
                 } else {
                     handleError("115", "Email Error: " . $e->getMessage() . ", email: " . $subject, "functions.global.inc.php", 0);
                 }
             }
         }
         try {
             krsort($delete);
             foreach ($delete as $num => $id) {
                 $mailbox->deleteMail($num);
             }
         } catch (Exception $e) {
             if ($_test) {
                 throw $e;
             } else {
                 handleError("114", "Email delete error: " . $e->getMessage() . ", email: " . $subject, "functions.global.inc.php", 0);
             }
         }
     } catch (Exception $e) {
         if ($_test) {
             throw $e;
         } else {
             handleError("113", "Email Error: " . $e->getMessage() . ", email: " . $subject, "functions.global.inc.php", 0);
         }
     }
     return $list;
 }
예제 #4
0
 function fetch($mailbox_name, $conditions = null)
 {
     $imap_email_host = $this->email_setting['imap_email_host'];
     $imap_email_port = $this->email_setting['imap_email_port'];
     $imap_email_username = $this->email_setting['imap_email_username'];
     $imap_email_password = $this->email_setting['imap_email_password'];
     $imap_flags = $this->email_setting['imap_flags'];
     try {
         $mailbox = new ImapMailbox('{' . $imap_email_host . ':' . $imap_email_port . $imap_flags . '}' . $mailbox_name, $imap_email_username, $imap_email_password, "websites/" . $this->app->epan['name'] . "/upload", 'utf-8');
         if ($this->debug) {
             echo "Connected<br/>";
         }
         $return = [];
         $conditions = $conditions ?: 'UNSEEN';
         $mailsIds = $mailbox->searchMailBox($conditions);
         if (!$mailsIds) {
             $mailbox->disconnect();
             if ($this->debug) {
                 echo "<br/>NO {$conditions} found returning <br/>";
             }
             return $return;
         }
         if ($this->debug) {
             echo "has " . count($mailsIds) . " Emails<br/>";
         }
         $i = 1;
         $fetch_email_array = array();
         foreach ($mailsIds as $mailId) {
             if ($this->debug) {
                 echo "Getting email <br/>";
             }
             $fetched_mail = $mailbox->getMail($mailId);
             if ($this->debug) {
                 echo "got email <br/>";
             }
             $attach_email_files = [];
             //MAIL ATTACHME  NT
             $attachments = $fetched_mail->getAttachments();
             foreach ($attachments as $attach) {
                 $file = $this->add('xepan/filestore/Model_File', array('policy_add_new_type' => true, 'import_mode' => 'move', 'import_source' => $attach->filePath));
                 $file['filestore_volume_id'] = $file->getAvailableVolumeID();
                 $file['original_filename'] = $attach->name;
                 $file->save();
                 $attach_email_files[$attach->id] = ['file_id' => $file->id, 'path' => $file['url'], 'type' => 'attach'];
             }
             $mail_m = $this->add('xepan\\communication\\Model_Communication_Email_Received');
             $mail_m->addCondition('uid', $fetched_mail->id);
             $mail_m->addCondition('mailbox', $this->email_setting['imap_email_username'] . '#' . $mailbox_name);
             $mail_m->tryLoadAny();
             if ($mail_m->loaded()) {
                 if ($this->debug) {
                     echo "<br/> UID " . $fetched_mail->id . " found existed in " . $this->email_setting['imap_email_username'] . '#' . $mailbox_name . " continuing <br/>";
                 }
                 continue;
             }
             $mail_m->setFrom($fetched_mail->fromAddress, $fetched_mail->fromName);
             /*Fetch TO Email Array & Convert To array name or email format*/
             $to_email_arry = $fetched_mail->to;
             foreach ($to_email_arry as $email => $name) {
                 $mail_m->addTo($email, $name);
             }
             /*Fetch CC Email Array & Convert To array name or email format*/
             $cc_email_array = $fetched_mail->cc;
             foreach ($cc_email_array as $email => $name) {
                 $mail_m->addCc($email, $name);
             }
             if (isset($fetched_mail->bcc)) {
                 $bcc_email_array = $fetched_mail->bcc;
                 foreach ($bcc_email_array as $email => $name) {
                     $mail_m->addBcc($email, $name);
                 }
             }
             $email_content = $fetched_mail->textHtml ?: $fetched_mail->textPlain;
             foreach ($attach_email_files as $e_id => $detail) {
                 // var_dump($email_content);
                 if (strpos($email_content, 'cid:' . $e_id) != false) {
                     // echo 'cid:'.$e_id ." <br/> Path :   " . $detail['path'] ."<br/> <br/>";
                     $email_content = str_replace('cid:' . $e_id, $detail['path'], $email_content);
                     $attach_email_files[$e_id]['type'] = 'inline';
                 }
             }
             // echo "string" . $email_content;
             $mail_m['created_at'] = $fetched_mail->date;
             $mail_m['title'] = $fetched_mail->subject;
             $mail_m['description'] = $email_content;
             $mail_m['flags'] = $conditions;
             $mail_m->findContact('from');
             if ($this->debug) {
                 echo "Saving email <br/>";
             }
             $mail_m->save();
             if ($this->email_setting['auto_reply']) {
                 if ($this->debug) {
                     echo "Doing auto reply <br/>";
                 }
                 $mail_m->reply($this->email_setting);
             }
             $fetch_email_array[] = $mail_m->id;
             if (!isset($return['fetched_emails_from'])) {
                 $return['fetched_emails_from'] = $mail_m->id;
             }
             foreach ($attach_email_files as $eid => $detail) {
                 $mail_m->addAttachment($detail['file_id'], $detail['type']);
             }
             $mail_m->unload();
             $i++;
         }
     } catch (\Exception $e) {
         $mailbox->disconnect();
         echo $e->getMessage() . '<br/>';
     }
     $mailbox->disconnect();
     return $return;
 }
예제 #5
0
$cfiltro = gaz_dbi_get_row($gTables['company_config'],'var','cfiltro');
$cpopimap = gaz_dbi_get_row($gTables['company_config'],'var','cpopimap');
$last_fae_email = gaz_dbi_get_row($gTables['company_config'],'var','last_fae_email');
define('CATTACHMENTS_DIR',  '../../data/files/ricevutesdi');

$mailbox = new ImapMailbox($cpopimap['val'], $cemail['val'], $cpassword['val'], CATTACHMENTS_DIR, 'utf-8');
$mails = array();

//se passato checkall verranno riscaricate tutte le email senza tener conto dell'eventule filtro: UNSEEN (solo non lette) 
if (isset($_GET['checkall'])) {
   $cfiltro['val'] = str_replace("UNSEEN","", $cfiltro['val']);
}


// Get some mail
$mailsIds = $mailbox->searchMailBox($cfiltro['val'] );
if(!$mailsIds) {
	echo('Nessuna nuova email con questo filtro: ' . $cfiltro['val']);
  die("<p align=\"center\"><a href=\"./report_fae_sdi.php\">Ritorna a report Fatture elettroniche</a></p>");
}

//$mailId = reset($mailsIds);
//$mail = $mailbox->getMail($mailId);

echo "Attendere: Verifico la posta elettronica sulla casella " . $cemail['val'] ."<br />";
$n_email = count($mailbox->getMailsInfo($mailsIds));



// if ($n_email == $last_fae_email['val']) { 
//     echo "Nessuna variazione sul numero di email ($n_email) <br/>";