Esempio n. 1
0
    if request['type'] == 'login':
        try:
            _imap_host = request['imap-host']
            _imap_port = request['imap-port']
            _imap_user = request['imap-user']
            _imap_pass = request['imap-pass']

            _smtp_host = request['smtp-host']
            _smtp_port = request['smtp-port']
            _smtp_user = request['smtp-user']
            _smtp_pass = request['smtp-pass']

            _imap = IMAPClient(host=_imap_host, port=_imap_port)
            _imap.login(_imap_user, _imap_pass)

            folders = list(map(lambda folder: folder[2], _imap.list_folders()))

            response = dict(success=True, type='login', folders=folders)
        except Exception as error:
            response = dict(success=False, type='login', error=str(error))

    elif request['type'] == 'fetch-emails':
        try:
            emails = get_emails(request['seq'])
            response = dict(success=True, type='fetch-emails', emails=emails)
        except Exception as error:
            response = dict(success=False,
                            type='fetch-emails',
                            error=str(error))

    elif request['type'] == 'fetch-email':
Esempio n. 2
0
            imap_host = request["imap-host"]
            imap_port = request["imap-port"]
            imap_login = request["imap-login"]
            imap_passwd = request["imap-passwd"]

            smtp_host = request["smtp-host"]
            smtp_port = request["smtp-port"]
            smtp_login = request["smtp-login"]
            smtp_passwd = request["smtp-passwd"]

            imap_client = IMAPClient(host=imap_host, port=imap_port)
            imap_client.login(imap_login, imap_passwd)
            PreventLogout()

            folders = list(
                map(lambda folder: folder[2], imap_client.list_folders()))
            response = dict(success=True, type="login", folders=folders)
        except Exception as error:
            response = dict(success=False, type="login", error=str(error))

    elif request["type"] == "fetch-emails":
        try:
            emails = get_emails(request["seq"], request["chunk-size"])
            response = dict(success=True, type="fetch-emails", emails=emails)
        except Exception as error:
            response = dict(success=False,
                            type="fetch-emails",
                            error=str(error))

    elif request["type"] == "fetch-email":
        try:
def imapPage():

    #folder = request.args.get('folder','')
    #if (folder == ''):
    #    folder="All Mail"
    
    email = request.cookies.get('email')
    accesskey = request.cookies.get('ak')
    
    if email != "" and accesskey != "":
        try:
            server = IMAPClient('imap.gmail.com', use_uid=True, ssl=True)
            server.oauth2_login(email, accesskey)
        except Exception, e:
            print "oauth2_login ERROR??"
            print e.__doc__
            print e.message
            return "oauth2_login Error. Remove cookies and try again"
            
        #GET FOLDERS
        folders = server.list_folders()
        stri="";
        for folderx in folders:
            print folderx
            if 'SPAM' not in folderx[2].upper() and 'JUNK' not in folderx[2].upper() and folderx[2].upper()!='[GMAIL]':
                print "entered"
                folder=folderx[2]
                stri = stri + "\n"+str(folderx[0])+'|'+str(folderx[1])+'|'+str(folderx[2]);
        
                select_info = 0
                try:
                    select_info = server.select_folder(folder, readonly=True)
                except Exception, e:
                    print "MAILBOX ERROR??"
                    print e.__doc__
                    print e.message
                    continue
                                
                #print select_info
                
                #messages = server.search(['SINCE 05-Feb-2014 NOT FROM \"facebook\"'])
                messages = server.search(['SINCE 17-Feb-2014'])
        
                #print "-"
                #print messages
                #print "-"
                            
                response = server.fetch(messages, ['X-GM-MSGID', 'X-GM-THRID', 'FLAGS', 'INTERNALDATE', 'ENVELOPE'])
                for msgid, data in response.iteritems():
                    
                    #print "==========="
                    #print data['FLAGS']
                    #print data['INTERNALDATE']
                    #print data['ENVELOPE']
                    #print data['BODY[HEADER]']
            
                    '''
                    The fields of the envelope structure are in the following order: date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, and message-id.  
                    The date, subject, in-reply-to, and message-id fields are strings.  
                    The from, sender, reply-to, to, cc, and bcc fields are parenthesized lists of address structures.
                    '''
                    ##
                    #Making proper data structure for Envelope
                    ##
                    env = {}
                    env['date']= data['ENVELOPE'][0]
                    env['subject']= data['ENVELOPE'][1]
                    env['in-reply-to']= data['ENVELOPE'][8]
                    env['message-id']= data['ENVELOPE'][9]
                    
                    env['from']= ParseAddressStructure(data['ENVELOPE'][2])
                    env['sender']= ParseAddressStructure(data['ENVELOPE'][3])
                    env['reply-to']= ParseAddressStructure(data['ENVELOPE'][4])
                    env['to']= ParseAddressStructure(data['ENVELOPE'][5])
                    env['cc']= ParseAddressStructure(data['ENVELOPE'][6])
                    env['bcc']= ParseAddressStructure(data['ENVELOPE'][7])
                    
                    em = Email(email, data['X-GM-MSGID'],data['X-GM-THRID'],env['subject'], data['INTERNALDATE'], env['in-reply-to'])
                    
                    try:
                        db.session.add(em)
                        db.session.flush()
                    except IntegrityError:
                        db.session.rollback()
                        #print "Duplicate Ignored\n"
                    
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'from', env['from'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'sender', env['sender'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'reply-to', env['reply-to'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'to', env['to'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'cc', env['cc'] )
                    addEmailAddrToDd(db, email, data['X-GM-MSGID'], 'bcc', env['bcc'] )
                    
                    
                    #  Or for parsing headers in a string, use:
                    #headers = Parser().parsestr(data['BODY[HEADER]'])                
                    #  Now the header items can be accessed as a dictionary:
                    #print 'To: %s' % headers['to']
                    #print 'From: %s' % headers['from']
                    #print 'Subject: %s' % headers['Subject'] 
                    #print 'Date: %s' % headers['date']
                    #print "+++++++"
            
                db.session.commit()