Esempio n. 1
0
    def update(self, _reuse_imap=None) -> List[int]:
        new_msgs = []
        if _reuse_imap is None:
            imap = IMAPClient(host='imap.ietf.org', ssl=False, use_uid=True)
            imap.login("anonymous", "anonymous")
        else:
            imap = _reuse_imap
        imap.select_folder("Shared Folders/" + self._list_name, readonly=True)

        msg_list  = imap.search()
        msg_fetch = []

        for msg_id, msg in imap.fetch(msg_list, "RFC822.SIZE").items():
            cache_file = Path(self._cache_folder, F"{msg_id:06d}.msg")
            if not cache_file.exists():
                msg_fetch.append(msg_id)
            else:
                file_size = cache_file.stat().st_size
                imap_size = msg[b"RFC822.SIZE"]
                if file_size != imap_size:
                    self.log.warn(F"message size mismatch: {self._list_name}/{msg_id:06d}.msg ({file_size} != {imap_size})")
                    cache_file.unlink()
                    msg_fetch.append(msg_id)

        if len(msg_fetch) > 0:
            aa_cache     = Path(self._cache_folder, "aa-cache.json")
            aa_cache_tmp = Path(self._cache_folder, "aa-cache.json.tmp")
            aa_cache.unlink()

            for msg_id, msg in imap.fetch(msg_fetch, "RFC822").items():
                cache_file = Path(self._cache_folder, F"{msg_id:06d}.msg")
                fetch_file = Path(self._cache_folder, F"{msg_id:06d}.msg.download")
                if not cache_file.exists():
                    with open(fetch_file, "wb") as outf:
                        outf.write(msg[b"RFC822"])
                    fetch_file.rename(cache_file)

                    e = email.message_from_bytes(msg[b"RFC822"])
                    if e["Archived-At"] is not None:
                        list_name, msg_hash = _parse_archive_url(e["Archived-At"])
                        self._archive_urls[msg_hash] = msg_id
                    self._num_messages += 1
                    new_msgs.append(msg_id)

                    self._msg_metadata[msg_id] = {}
                    for helper in self._helpers:
                        self.log.info(F"{helper.name}: scan message {self._list_name}/{msg_id:06} for metadata")
                        self._msg_metadata[msg_id][helper.name] = helper.scan_message(e)

            with open(aa_cache_tmp, "w") as aa_cache_file:
                json.dump(self._archive_urls, aa_cache_file)
            aa_cache_tmp.rename(aa_cache)

        self.serialise_metadata()

        imap.unselect_folder()
        if _reuse_imap is None:
            imap.logout()
        self._last_updated = datetime.now()
        return new_msgs
Esempio n. 2
0
def test_imap(user):
    credentials = decorator.credentials
    if credentials.access_token_expired:
        logging.debug('Refreshing...')
        credentials.refresh(httplib2.Http())
    conn = IMAPClient('imap.gmail.com', use_uid=True, ssl=True)
    conn.debug = 4

    conn.oauth2_login(user, credentials.access_token)

    # status, labels = conn.list()
    folders = conn.list_folders()
    try:
        all_box = next(box for (flags, _, box) in folders if '\All' in flags)
    except StopIteration:
        raise Error('all message box not found')

    logging.debug('All message box is {}'.format(all_box))

    conn.select_folder(all_box)
    # Once authenticated everything from the impalib.IMAP4_SSL class will
    # work as per usual without any modification to your code.
    # typ, msgnums = conn.search('X-GM-RAW vget')
    tid = int('14095f27c538b207', 16)
    # msgs = conn.search('X-GM-THRID {}'.format(tid))
    msgs = conn.search('X-GM-RAW uniquetokenXXX')

    logging.info(msgs)
    logging.info(conn.fetch(msgs, 'X-GM-MSGID'))
    msg = conn.fetch(msgs, 'RFC822')
    logging.info(msg)
    return msg
Esempio n. 3
0
class Inbox(Contextual):
    def __init__(self, account, host=None):
        if host is None or host=="":
            logging.debug("No hostname provided, trying detection from username %s",
                          account.username)
            try:
                host = account.username.split("@")[1]
                logging.debug("Using hostname %s", host)
            except IndexError as e:
                raise HostNotFoundException("No hostname provided, username {0} contains none as well".format(
                    account.username)) from e
        self.client = IMAPClient(host, use_uid=True, ssl=True)  # TODO: treat non-ssl
        msg = self.client.login(account.username, account.password)
        logging.debug(msg)

    def close(self):
        msg = self.client.logout()
        logging.debug(msg)

    def messages(self, folder="INBOX", searchstring="NOT DELETED", keep_listening=True):
        fetchstr = b'RFC822'
        selection = self.client.select_folder(folder)
        logging.debug("IMAP selection: %s", selection)
        uids = self.client.search(searchstring)
        #uids = self.client.search("SUBJECT test")
        logging.debug('Found %i messages matching "%s", %s', len(uids), searchstring, uids)
        for uid in uids:
            logging.debug("Fetching message %s", uid)
            msg_bytes = self.client.fetch(uid, [fetchstr])[uid][fetchstr]
            logging.debug("Parsing fetched message" + os.linesep + "%s", msg_bytes.decode('utf-8'))
            msg = email.parser.BytesParser().parsebytes(msg_bytes)
            yield uid, msg
        logging.debug("All messages fetched")
        if not keep_listening:
            return
        msg = self.client.subscribe_folder(folder)
        logging.debug(str(msg))
        while True:  # TODO: maybe a better abortion mechanism
            logging.debug("Idling...")
            self.client.idle()
            idle = self.client.idle_check()  # TODO: use timeout
            msg = self.client.idle_done()
            # FIXME: while the new messages are fetched, new incoming ones aren't discovered!
            logging.debug(str(msg))
            for msg in idle:
                logging.debug('Idle response: "%"', msg)
                uid, state = msg
                if state == "EXISTS":
                    logging.debug("Fetching message %s", uid)
                    msg_bytes = self.client.fetch(uid, [fetchstr])[uid][fetchstr]
                    logging.debug("Parsing fetched message" + os.linesep + "%s", msg_bytes.decode('utf-8'))
                    msg = email.parser.BytesParser().parsebytes(msg_bytes)
                    yield uid, msg
def email_parser_method(test_date, emailSub, emaill, text):
    try:
        # test_date which is passed by test cases
        emailDate = test_date
        changesDate = emailDate[:-7]
        data = ''
        email_body = ''
        email_subject = ''

        # Read credentials file
        with open('test_driver/credentials.json') as f:
            credentials = json.load(f)

        server = IMAPClient(credentials['mail_server'], use_uid=True)
        server.login(credentials['email'], credentials['password'])
        select_info = server.select_folder('INBOX')
        messages = server.search(['FROM', credentials['from_email']])

        for uid, message_data in server.fetch(messages, ['ENVELOPE']).items():
            envelope = message_data[b'ENVELOPE']
            print(envelope.subject.decode())
            print('fourdate:' + str(changesDate).replace('_', ''))
            print('fenvdate:' + str(
                str(
                    str(str(envelope.date).replace(':', '_')).replace(
                        '-', '_')).replace(' ', '_')).replace('_', ''))
            if int(str(changesDate).replace('_', '')) < int(
                    str(
                        str(
                            str(str(envelope.date).replace(':', '_')).replace(
                                '-', '_')).replace(' ', '_')).replace('_',
                                                                      '')):
                for second_uid, message in server.fetch(messages,
                                                        ['RFC822']).items():
                    if (uid == second_uid):
                        email_message = email.message_from_bytes(
                            message[b'RFC822'])
                        email_subject = envelope.subject.decode()
                        email_body = get_text(email_message)

        server.logout()

        searching_parsed_mail('Thank you for subscribing', email_subject,
                              emailSub, email_body, emaill, text)

        searching_parsed_mail('Reset your MyLawn password', email_subject,
                              emailSub, email_body, emaill, text)

    except Exception as e:
        print('Handling run-time error:', e)
        raise
Esempio n. 5
0
def mail_check():
    # login to mailserver
    server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
    server.login(USERNAME, PASSWORD)

    # select our MAILBOX and looked for unread messages
    unseen = server.folder_status(MAILBOX, ['UNSEEN'])
    newmail_count = (unseen[b'UNSEEN'])
    print('%d unseen messages' % newmail_count)

    select_info = server.select_folder('INBOX')
    print('%d messages in INBOX' % select_info[b'EXISTS'])

    messages_from_kerem = server.search(['FROM', FROM_KEREM])
    print("%d messages from Kerem" % len(messages_from_kerem))

    messages_from_thomas = server.search(['FROM', FROM_THOMAS])
    print("%d messages from Thomas" % len(messages_from_thomas))

    if len(messages_from_kerem) > 0:
        for mail_id, data in server.fetch(messages_from_kerem,
                                          ['ENVELOPE', 'BODY[TEXT]']).items():
            envelope = data[b'ENVELOPE']
            body = data[b'BODY[TEXT]']
            print("%d messages from Kerem" % len(messages_from_kerem))
            print('ID #%d: "%s" ' % (mail_id, envelope.subject.decode()))
            if envelope.subject.decode() == "Temp":
                cmd_beginning = 'echo Temp = '
                cmd_end = 'C | msmtp ' + FROM_KEREM
                cmd_temp = read_temp()
                cmd = cmd_beginning + cmd_temp + cmd_end
                os.system(cmd)
                server.delete_messages(mail_id, False)
                print("Mail deleted")

    if len(messages_from_thomas) > 0:
        for mail_id, data in server.fetch(messages_from_thomas,
                                          ['ENVELOPE', 'BODY[TEXT]']).items():
            envelope = data[b'ENVELOPE']
            body = data[b'BODY[TEXT]']
            print("%d messages from Thomas" % len(messages_from_thomas))
            print('ID #%d: "%s" ' % (mail_id, envelope.subject.decode()))
            if envelope.subject.decode() == "Temp":
                cmd_beginning = 'echo Temp = '
                cmd_end = 'C | msmtp ' + FROM_THOMAS
                cmd_temp = read_temp()
                cmd = cmd_beginning + cmd_temp + cmd_end
                os.system(cmd)
                server.delete_messages(mail_id, False)
                print("Mail deleted")
Esempio n. 6
0
def receive_imap(host, user, password, num):
    conn = IMAPClient(host)
    conn.login(user, password)
    info = conn.select_folder("INBOX")
    print(info)
    messages = conn.fetch("1:*", [b'FLAGS'])
    latest_msg = max(messages)
    requested = "FLAGS BODY.PEEK[]"
    for uid, data in conn.fetch([latest_msg],
                                [b"FLAGS", b"BODY.PEEK[]"]).items():
        body_bytes = data[b'BODY[]']
        email_message = email.message_from_bytes(body_bytes)
        assert email_message["subject"] == str(num)
        print("received message num={}".format(num))
        print(email_message)
Esempio n. 7
0
class GmailImapClient(object):
    """Imap client with some specific methods for working with gmail"""

    IMAP_SERVER = "imap.gmail.com"
    IMAP_SERVER_PORT = "993"

    def __init__(self, email_address, password):
        self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
        self.email_address = email_address
        self.password = password
        self.messages_for_this_session = []
        self._login()

    def search(self, from_address, to_address, subject,
               since=datetime.utcnow()-timedelta(minutes=1)):
        """Search for emails on an IMAP server"""

        return self.emails_from_messages(
            self.client.search(
                [
                    'FROM "%s"' % (from_address,),
                    'TO "%s"' % (to_address,),
                    'SUBJECT "%s"' % (subject,),
                    'SINCE %s' % (since.strftime('%d-%b-%Y'),),
                ],
            )
        )

    def _login(self):
        """Login to imap server"""
        self.client.login(self.email_address, self.password)
        self.client.select_folder("INBOX")

    def delete_seen_messages(self):
        """Delete messages that have been accessed with this client"""
        self.client.delete_messages(self.messages_for_this_session)
        self.client.expunge()

    def gmail_search(self, query):
        """Search the gmail imap server using gmail queries"""
        self.client.logout()
        self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
        self._login()
        # Can use full gmail queries like 'has:attachment in:unread'
        messages = self.client.gmail_search(query)
        self.messages_for_this_session.append(messages)
        # We recreate a whole connection after querying gmail because
        # otherwise it caches our search results forever
        self.client.logout()
        self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
        self._login()
        return self.emails_from_messages(messages)

    def emails_from_messages(self, messages):
        """Convert a list of IMAP messages into email objects"""
        response = self.client.fetch(messages, ["RFC822"])
        return [
            email.message_from_string(data["RFC822"])
            for _, data in response.items()
        ]
def check_photo_request(hostname, username, password, boss):
    from imapclient import IMAPClient
    import email
    from email.utils import parseaddr

    server = IMAPClient(hostname, use_uid=True, ssl=True)
    server.login(username, password)
    select_info = server.select_folder('Inbox')
    msg_count = select_info[b'EXISTS']
    if msg_count == 0:
        return False
    command_photo = False
    messages = server.search()
    for msgid, data in server.fetch(messages, [b'RFC822']).items():
        msg = email.message_from_bytes(data[b'RFC822'])
        subject = str(msg['Subject'])
        mfrom = str(parseaddr(msg['From'])[1])
        if 'photo' == subject.strip().lower() and boss == mfrom.strip().lower(
        ):
            command_photo = True
            break
    server.delete_messages(messages, silent=True)
    server.expunge(messages)
    server.logout()
    return command_photo
Esempio n. 9
0
    def get_email(self):
        context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2)
        mail = IMAPClient(self.email_server, ssl=True, ssl_context=context)

        try:
            mail.login(self.email_address, self.email_password)
        except BaseException as e:
            return "ERROR: >>> " + str(e)

        mail.select_folder('INBOX', readonly=True)
        res = mail.search()
        # 防止循环太多邮件,只查16个
        j = 0
        for i in res[::-1]:
            if j > 15:
                print "EMAIL SEARCH TOO MUCH"
                exit()
            msg_dict = mail.fetch(i, ['BODY.PEEK[]'])
            mail_body = msg_dict[i][b'BODY[]']
            e = email.message_from_string(mail_body)
            decode_subject = email.Header.decode_header(e['Subject'])
            utf_subject = decode_subject[0][0]

            # 获取安全码标题邮件
            if utf_subject.find("通用安全码更新通知") != -1:
                print "GET EMAIL CODING......"
                payload = e.get_payload()
                self.safe_code = re.findall(r"<font color=red>(.*?)</font>",
                                            payload)[0]
                mail.logout()
                return
            j = j + 1

        mail.logout()
        return
Esempio n. 10
0
def get_email(user):
    server = IMAPClient(HOST, use_uid=True, ssl=True)
    username = db.get("user:%s:login" % user)
    password = db.get("user:%s:password" % user)
    server.login(username, password)
    server.select_folder('INBOX', readonly=True)
    messages = server.search(['NOT DELETED','SINCE 15-May-2014'])
    response = server.fetch(messages, ['RFC822', 'FLAGS'])
    for msgid, data in response.iteritems():
        # check for duplicates
        duplicate = db.zrangebyscore("mail:%s:inbox" % user, msgid, msgid)
        if duplicate:
            continue
        emailUTF8 = data['RFC822'].encode('utf-8')
        msg = parser.parsestr(emailUTF8)
        body = extract_body(msg).encode('utf-8')
        msg['message'] = body
        msg['subject'] = ('NoSubj' if (msg['Subject'] == None or msg['Subject'].encode('utf-8') == "".encode('utf-8'))  else msg['Subject'])
        msg['to'] = ('NoTo' if (msg['To'] == None) else msg['To'])
        plain = {'plain_body': extract_body_text(msg).encode('utf-8'), 'subject': msg['subject']}
        # TODO set unread
        email = {'id': msgid, 'from': msg['From'], 'to': msg['To'], 'subject': msg['Subject'],
                'date': msg['Date'], 'cc': msg['CC'], 'read': False,
                'message': body, 'categorized': False, 'summary': shorten(plain), 'archived': False}

        trained = db.get("user:%s:trained" % user)
        if trained == "true":
            email['category'] = int(classify(msg, user))
        else:
            email['category'] = 1
        emailJSON = json.dumps(email, sort_keys=True, indent=4, separators=(',', ': '))
        db.zadd("mail:%s:inbox" % user, emailJSON, msgid)
        db.sadd("mail:%s:%s" % (user, email['category']), msgid)
    server.logout()
def get_unsubscribe_links(imap_domain, email_address, email_password):
    """
    Logs into the IMAP server with the specified credentials. After
    logging in, all messages in the inbox are searched. This function
    returns a list of links to unsubscribe from email newsletters

    :param str imap_domain: email provider's IMAP server domain name
    :param str email_address: user
    :param str email_password: password
    """
    imaplib._MAXLINE = 10_000_000
    imap = IMAPClient(imap_domain, ssl=True)
    imap.login(email_address, email_password)
    imap.select_folder('INBOX', readonly=True)

    unique_ids = imap.search(['ALL'])
    links = []

    for unique_id in unique_ids:
        raw_messages = imap.fetch([unique_id], ['BODY[]', 'FLAGS'])
        message = pyzmail.PyzMessage.factory(
            raw_messages[unique_id][b'BODY[]'])

        if message.html_part:
            links.extend(search_html_part(message=message))

    return links
Esempio n. 12
0
def check_email(user, password, timeout=0, server=None):
    time.sleep(timeout)
    code = None

    if not server:
        domain = user.split('@')[1]
        if domain in imap_database:
            server = IMAPClient(imap_database[domain][0],
                                port=imap_database[domain][1])
            server.login(user, password)
        else:
            raise ValueError('Unknown email domain!')

    select_info = server.select_folder('INBOX')
    print('%d messages in INBOX' % select_info[b'EXISTS'])
    messages = server.search(
        ['UNSEEN', 'FROM', '*****@*****.**'])
    messages_len = len(messages)
    i = 0
    for uid, message_data in server.fetch(messages, 'RFC822').items():
        i += 1

        if i == messages_len:
            email_message = email.message_from_bytes(message_data[b'RFC822'])
            code = get_code(email_message)

    if server:
        server.logout()

    return code
Esempio n. 13
0
def testserver(aserver, aport, auser, apassword):
    """Do work"""
    server = IMAPClient(aserver, aport, use_uid=True, ssl=False)
    print "Logging in"
    try:
        server.login(auser, apassword)
    except Exception: 
        print "\n *** Something has gone horribly wrong ***\n"
    else:
        print "\n Great Success !\n"
        pass

    select_info = server.select_folder('INBOX')
    print '%d messages in INBOX' % select_info['EXISTS']

    messages = server.search(['NOT DELETED'])
    print "%d messages that aren't deleted" % len(messages)

    print "\n Message Summary:"
    response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE'])
    for msgid, data in response.iteritems():
        print '   ID %d: %d bytes, flags=%s' % (msgid,
        data['RFC822.SIZE'],
        data['FLAGS'])

    sys.exit("\nWe're done here")
Esempio n. 14
0
def m():
    server = IMAPClient('imap.gmail.com', use_uid=True)
    server.login('*****@*****.**', 'Reddwarf')
    select_info = server.select_folder('[Gmail]/All Mail')
    messages = server.search()
    mail = []
    id = []
    spam = []
    mailbody = []
    mailbody2 = []
    for msgid, data in server.fetch(messages, ['ENVELOPE']).items():
        envelope = data[b'ENVELOPE']
        id.append(msgid)
        subject = envelope.subject.decode('utf-8')
        sub = decode_mime_words(subject)
        mail.append(sub)
    con = imaplib.IMAP4_SSL('imap.gmail.com')
    con.login('*****@*****.**', 'Reddwarf')
    con.list()
    con.select("INBOX")
    rv, data = con.search(None, "ALL")
    for num in data[0].split():
        result, data = con.fetch(num, '(RFC822)')
        raw = email.message_from_bytes(data[0][1])
        body = ""
        if raw.is_multipart():
            for payload in raw.walk():
                if payload.get_content_type() == 'text/plain':
                    mailbody.append(payload.get_payload(None, True))
        else:
            mailbody.append(raw.get_payload())
    return simplejson.dumps({"mail": mail, "body": mailbody})
Esempio n. 15
0
def fetch_test_submissions(previous_history, config):
    """ fetch emails from IMAP server using the given configuration
        each email is parsed to see whether it matches a submission
        if so, its token is extracted and checked against the given
        history.
        if found, the email is deleted from the server and the entry
        is removed from the history.
        any entries left in the history are returned
    """
    server = IMAPClient(config['imap_host'], use_uid=True, ssl=True)
    server.login(config['imap_user'], config['imap_passwd'])
    server.select_folder('INBOX')
    history = previous_history.copy()
    candidates = server.fetch(
        server.search(criteria=['NOT DELETED', 'SUBJECT "Drop ID"']),
        ['BODY[HEADER.FIELDS (SUBJECT)]'])
    for imap_id, message in candidates.items():
        subject = message.get('BODY[HEADER.FIELDS (SUBJECT)]', 'Subject: ')
        try:
            drop_id = find_drop_id.findall(subject)[0]
        except IndexError:
            # ignore emails that are not test submissions
            continue
        print "Found submission '%s'" % drop_id
        server.delete_messages([imap_id])
        try:
            del history[drop_id]
        except KeyError:
            pass  # TODO: log this?
    server.logout()
    return history
Esempio n. 16
0
class IMAPService(object):
    def __init__(self, host, port=None):

        self.service = IMAPClient(host, port=port, ssl=True)

    def auth(self, usr, pwd):

        self.service.login(usr, pwd)

    def list(self):

        self.service.select_folder('INBOX')

        messages = self.service.search(['UNSEEN'])

        response = self.service.fetch(messages, ['RFC822'])

        for msg_id, data in response.iteritems():

            yield (msg_id,
                   email.message_from_string(data['RFC822'].encode(
                       "utf-8", "ignore")))

    def quit(self):

        pass

    def mark_unread(self, msg_ids):

        self.service.remove_flags(msg_ids, [SEEN])
Esempio n. 17
0
    def parse(self):
        use_ssl = True if self.config["EMAIL"]["useSSL"] else False
        server = IMAPClient(self.config["EMAIL"]["servername"], ssl=use_ssl)
        server.login(self.config["EMAIL"]["username"], self.config["EMAIL"]["password"])
        logging.getLogger().debug("connected to IMAP server")

        select_info = server.select_folder("INBOX")
        # get list of fitting messages
        messages = server.search(["NOT", "DELETED", "SUBJECT", self.config["EMAIL"]["subject"]])
        logging.getLogger().info("%d email message(s) found" % len(messages))

        # loop through all messages
        for msgid in messages:
            # download it
            response = server.fetch(msgid, ["RFC822"])
            msg = email.message_from_bytes(response[msgid][b"RFC822"])
            self.__process_message(msg)

        # delete messages?
        if len(messages) > 0 and int(self.config["EMAIL"]["deleteAfterProcessing"]):
            if int(self.config["EMAIL"]["deleteAfterProcessing"]) > 1:
                messages = messages[:-1]
            server.delete_messages(messages)
            if self.config["EMAIL"]["expungeMailbox"]:
                server.expunge()
            logging.getLogger().info("Deleted email message(s) from server")

        server.logout()
Esempio n. 18
0
class ImapFunctions:
    def __init__(self, host, username, password):
        self.host = host
        self.username = username
        self.password = password
        self.client = IMAPClient(host)

    def login(self, username, password):
        "Handling various auth protocols"
        #client.starttls() --- gmail does not support starttls
        self.client.login(username, password)
        return

    def get_folders(self):
        "Grabbing the list of folders for the account"
        get_folders = self.client.list_folders()
        folder_list = []
        for folders in get_folders:
            folder_list.append(folders[2])
        return folder_list

    def get_messages(self, folder):

        self.client.select_folder(folder)
        messages = self.client.search([u'OLD'])
        message_list = []
        for uid, message_data in self.client.fetch(messages, "RFC822").items():
            email_message = email.message_from_bytes(message_data[b"RFC822"])
            message_list.append(email_message.get("Subject"))

        return message_list
Esempio n. 19
0
class GmailImapClient(object):
    """Imap client with some specific methods for working with gmail"""

    IMAP_SERVER = "imap.gmail.com"
    IMAP_SERVER_PORT = "993"

    def __init__(self, email_address, password):
        self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
        self.email_address = email_address
        self.password = password
        self.messages_for_this_session = []
        self._login()

    def search(self,
               from_address,
               to_address,
               subject,
               since=datetime.utcnow() - timedelta(minutes=1)):
        """Search for emails on an IMAP server"""

        return self.emails_from_messages(
            self.client.search([
                'FROM "%s"' % (from_address, ),
                'TO "%s"' % (to_address, ),
                'SUBJECT "%s"' % (subject, ),
                'SINCE %s' % (since.strftime('%d-%b-%Y'), ),
            ], ))

    def _login(self):
        """Login to imap server"""
        self.client.login(self.email_address, self.password)
        self.client.select_folder("INBOX")

    def delete_seen_messages(self):
        """Delete messages that have been accessed with this client"""
        self.client.delete_messages(self.messages_for_this_session)
        self.client.expunge()

    def gmail_search(self, query):
        """Search the gmail imap server using gmail queries"""
        self.client.logout()
        self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
        self._login()
        # Can use full gmail queries like 'has:attachment in:unread'
        messages = self.client.gmail_search(query)
        self.messages_for_this_session.append(messages)
        # We recreate a whole connection after querying gmail because
        # otherwise it caches our search results forever
        self.client.logout()
        self.client = IMAPClient(self.IMAP_SERVER, use_uid=True, ssl=True)
        self._login()
        return self.emails_from_messages(messages)

    def emails_from_messages(self, messages):
        """Convert a list of IMAP messages into email objects"""
        response = self.client.fetch(messages, ["RFC822"])
        return [
            email.message_from_string(data["RFC822"])
            for _, data in response.items()
        ]
Esempio n. 20
0
def do_worker_qq(smtp_account, smtp_password, smtp_list):
    # 通过一下方式连接smtp服务器,没有考虑异常情况,详细请参考官方文档
    server = IMAPClient(GLB_IMAP_HOSTNAME, ssl= True)
    try:
        server.login(smtp_account, smtp_password)
        server.select_folder('INBOX')

        year, mouth, day = glb_p_date.split('-')
        date = datetime.date(int(year), int(mouth), int(day))
        date2 = date - datetime.timedelta(days=1)
        result = server.search(['UNSEEN', 'SINCE', date2])
        msgdict = server.fetch(result, ['BODY.PEEK[]'] )
        msgtuple=sorted(msgdict.items(), key=lambda e:e[0], reverse=True)

        log_ids = []
        for message_id, message in msgtuple:
            msg_content = message['BODY[]']
            for log_id, mail_from, mail_to in smtp_list:
                if log_id in log_ids: continue
                error_type, return_said = do_email(msg_content, mail_to)
                if error_type is not None:
                    log_ids.append(log_id)
                    do_worker_imap_update(log_id, mail_to, return_said, error_type)
                    server.add_flags(message_id, '\\seen')
    finally:
        server.logout()
    return
Esempio n. 21
0
def testserver(aserver, aport, auser, apassword):
    """Do work"""
    server = IMAPClient(aserver, aport, use_uid=True, ssl=False)
    print "Logging in"
    try:
        server.login(auser, apassword)
    except Exception:
        print "\n *** Something has gone horribly wrong ***\n"
    else:
        print "\n Great Success !\n"
        pass

    select_info = server.select_folder('INBOX')
    print '%d messages in INBOX' % select_info['EXISTS']

    messages = server.search(['NOT DELETED'])
    print "%d messages that aren't deleted" % len(messages)

    print "\n Message Summary:"
    response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE'])
    for msgid, data in response.iteritems():
        print '   ID %d: %d bytes, flags=%s' % (msgid, data['RFC822.SIZE'],
                                                data['FLAGS'])

    sys.exit("\nWe're done here")
Esempio n. 22
0
def cleanup():
    """EmailListener test suite teardown, moves the test email to the Trash."""

    # Let the other fixtures and the test run
    yield

    # Create an email listener
    email = os.environ['EL_EMAIL']
    app_password = os.environ['EL_APW']
    server = IMAPClient('imap.gmail.com')
    server.login(email, app_password)
    server.select_folder("email_listener", readonly=False)
    messages = server.search("UNSEEN")
    for uid, message_data in server.fetch(messages, 'RFC822').items():
        print("uid: {}".format(uid))
        server.set_gmail_labels(uid, "\\Trash")
    server.logout()

    # Delete any downloaded attachments
    download_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)),
                                "attachments")
    for file in os.listdir(download_dir):
        file_ext = file.split('.')[-1]
        if (file_ext == "txt"):
            full_path = os.path.join(download_dir, file)
            os.remove(full_path)
Esempio n. 23
0
def getMail():

    # SECRET = getSecret()
    SECRET = "{0}".format(getSecret())


    # server = IMAPClient("imap.gmail.com", use_uid=True, ssl=True)
    server = IMAPClient("imap.comcast.net", use_uid=True, ssl=True)
    server.login(username, SECRET)

    select_info = server.select_folder('INBOX')
#     print('%d messages in INBOX' % select_info['EXISTS'])
# 
    messages = server.search(['NOT', 'DELETED'])
#     print("%d messages that aren't deleted" % len(messages))

    theMsgs = []
    response = server.fetch(messages, ['RFC822'])
    for messagegId,data in response.iteritems():
            messageString = data['RFC822']
            msgStringParsed = email.message_from_string(messageString)
            messageFrom = msgStringParsed['From']
            messageSubj = msgStringParsed['Subject']
            
            bodytext=''
            for part in msgStringParsed.walk():
                if part.get_content_type() == "text/plain":
                    bodytext=part.get_payload().strip()

            theMsgs.append({'from':messageFrom, 'subj':messageSubj.strip(), 'body':bodytext})

    server.delete_messages(response.keys())

    server.logout()
    return theMsgs
Esempio n. 24
0
class EmailBot:
    def __init__(self):
        # TODO: refactor for OAUTH2  https://developers.google.com/gmail/imap/xoauth2-protocol , https://stackoverflow.com/questions/5193707/use-imaplib-and-oauth-for-connection-with-gmail
        # IMAP lib docs https://imapclient.readthedocs.io/en/2.1.0/concepts.html#tls-ssl
        ssl_context = ssl.create_default_context()
        ssl_context.check_hostname = False
        ssl_context.verify_mode = ssl.CERT_NONE
        self.ssl_context = ssl_context

    def connect_for_user(self, user: str, passw: str):
        self.server = IMAPClient('imap.gmail.com',
                                 ssl_context=self.ssl_context)
        self.server.login(user, passw)
        inbox_selected = self.server.select_folder('INBOX')

    def get_emails_for_user(self,
                            from_address: str = '*****@*****.**',
                            last_n_days: int = 5):
        cutoff = datetime.today() - timedelta(days=last_n_days)
        messages = self.server.search([
            'FROM "%s' % from_address,
            'SINCE %s' % cutoff.strftime('%d-%b-%Y')
        ])
        response = self.server.fetch(messages, ['RFC822'])

        for id, data in response.iteritems():
            msg_string = data['RFC822']
            msg = email.message_from_string(msg_string)
            print('IFrom: %s Date: %s' % (msg['From'], msg['date']))
Esempio n. 25
0
    def _fetch(self, imapc: imapclient.IMAPClient) -> typing.List[MessageContainer]:
        """
        Get news message from mailbox
        :return: list of new mails
        """
        messages = []

        logger.debug(self, "Fetch unflagged messages")
        uids = imapc.search(["UNFLAGGED"])
        logger.debug(self, "Found {} unflagged mails".format(len(uids)))
        for msgid, data in imapc.fetch(uids, ["BODY.PEEK[]"]).items():
            # INFO - G.M - 2017-12-08 - Fetch BODY.PEEK[]
            # Retrieve all mail(body and header) but don't set mail
            # as seen because of PEEK
            # see rfc3501
            logger.debug(self, 'Fetch mail "{}"'.format(msgid))

            try:
                msg = message_from_bytes(data[b"BODY[]"])
            except KeyError as e:
                # INFO - G.M - 12-01-2018 - Fetch may return events response
                # In some specific case, fetch command may return events
                # response unrelated to fetch request.
                # This should happen only when someone-else use the mailbox
                # at the same time of the fetcher.
                # see https://github.com/mjs/imapclient/issues/334
                except_msg = "fetch response : {}".format(str(data))
                raise BadIMAPFetchResponse(except_msg) from e

            msg_container = MessageContainer(msg, msgid)
            messages.append(msg_container)

        return messages
Esempio n. 26
0
class IMAPService(object):

    def __init__(self, host, port=None):

        self.service = IMAPClient(host, port=port, ssl=True)

    def auth(self, usr, pwd):

        self.service.login(usr, pwd)

    def list(self):

        self.service.select_folder('INBOX')

        messages = self.service.search(['UNSEEN'])

        response = self.service.fetch(messages, ['RFC822'])

        for msg_id, data in response.iteritems():

            yield (msg_id, email.message_from_string(
                data['RFC822'].encode("utf-8", "ignore")))

    def quit(self):

        pass

    def mark_unread(self, msg_ids):

        self.service.remove_flags(msg_ids, [SEEN])
Esempio n. 27
0
def remove_attachments(host, username, password, folder_uids):

    server = IMAPClient(host)
    server.login(username, password)

    # trash_folder = server.find_special_folder(imapclient.TRASH)

    for folder_name, uids in folder_uids.items():
        server.select_folder(folder_name)

        to_fetch = ["RFC822", "FLAGS", "INTERNALDATE"]
        fetched = server.fetch(uids, to_fetch)
        for uid, msg in fetched.items():
            email_message = email.message_from_bytes(msg[b"RFC822"])
            final_msg = set_email_payload(email_message)

            # server.move(uid, trash_folder)
            # server.expunge(uid)
            server.delete_messages(uid)
            server.append(
                folder="INBOX",
                msg=final_msg.as_string().encode(),
                flags=msg[b"FLAGS"],
                msg_time=msg[b"INTERNALDATE"],
            )
        server.unselect_folder()

    server.logout()
Esempio n. 28
0
def fetch_test_submissions(previous_history, config):
    """ fetch emails from IMAP server using the given configuration
        each email is parsed to see whether it matches a submission
        if so, its token is extracted and checked against the given
        history.
        if found, the email is deleted from the server and the entry
        is removed from the history.
        any entries left in the history are returned
    """
    context = create_default_context()
    context.check_hostname = False
    context.verify_mode = False
    server = IMAPClient(config['imap_host'], use_uid=True, ssl=True, ssl_context=context)
    server.login(config['imap_user'], config['imap_passwd'])
    server.select_folder('INBOX')
    history = previous_history.copy()
    candidates = server.fetch(server.search(criteria='NOT DELETED SUBJECT "Drop ID"'), ['BODY[HEADER.FIELDS (SUBJECT)]'])
    for imap_id, message in candidates.items():
        subject = message.get('BODY[HEADER.FIELDS (SUBJECT)]', 'Subject: ')
        try:
            drop_id = find_drop_id.findall(subject)[0]
        except IndexError:
            # ignore emails that are not test submissions
            continue
        print "Found submission '%s'" % drop_id
        server.delete_messages([imap_id])
        try:
            del history[drop_id]
        except KeyError:
            pass  # TODO: log this?
    server.logout()
    return history
Esempio n. 29
0
class EmailClientIMAP():
    '''
    This class is used to find/check emails on server through imap protocol
    '''
    def __init__(self, host, port, user, passwd, ssl=True):
        self.imap_server = IMAPClient(host=host,
                                      port=port,
                                      use_uid=True,
                                      ssl=ssl,
                                      stream=False,
                                      ssl_context=None,
                                      timeout=10)
        self.imap_server.login(user, passwd)

    def select_folder(self, folder_name):
        self.imap_server.select_folder(folder_name)

    # return id sequence of messages
    def search(self, criteria='ALL', charset=None):
        return self.imap_server.search(criteria, charset)

    # get emails by id sequence
    def fetch(self, id_sequence):
        response = self.imap_server.fetch(id_sequence, ['RFC822'])
        print(type(response))
        email_list = []
        for key, values in response.items():
            raw_msg = values['RFC822']
            email_list.append(email.message_from_string(raw_msg))
        return email_list
Esempio n. 30
0
    def parse(self):
        server = IMAPClient(self.config['EMAIL']['servername'])
        server.login(self.config['EMAIL']['username'], self.config['EMAIL']['password'])
        logging.getLogger().debug("connected to IMAP server")

        select_info = server.select_folder('INBOX')
        # get list of fitting messages
        messages = server.search(['NOT DELETED',
                                  'SUBJECT "' + self.config['EMAIL']['subject'] + '"'])
        logging.getLogger().info("%d email message(s) found" % len(messages))

        # loop through all messages
        for msgid in messages:
            # download it
            response = server.fetch(msgid, ['RFC822'])
            msg = email.message_from_bytes(response[msgid][b'RFC822'])
            self.__process_message(msg)

        # delete messages?
        if len(messages) > 0 and int(self.config['EMAIL']['deleteAfterProcessing']):
            if int(self.config['EMAIL']['deleteAfterProcessing']) > 1:
                messages = messages[:-1]
            server.delete_messages(messages)
            if self.config['EMAIL']['expungeMailbox']:
                server.expunge()
            logging.getLogger().info("Deleted email message(s) from server")

        server.logout()
Esempio n. 31
0
def get_past_emails(folder="", from_date="", to_date="", from_person="", to_person=""):
    """Get past emails from/to given date/person"""
    global HOST
    global USERNAME
    global PASSWORD
    server = IMAPClient(HOST, use_uid=True, ssl=ssl)
    server.login(USERNAME, PASSWORD)
    # IF wp.pl -> use_uid = False
    # IF gmail -> use_uid = True
    imap_query_start = u"("
    imap_query_end = ")"
    imap_query=""


    #folders_list = server.list_folders()
    if folder is None:
        folder = b'INBOX'

    select_info = server.select_folder(folder)

    if from_person:
        imap_query += 'FROM {}'.format(from_person)
    if to_person:
        imap_query += ' TO {}'.format(to_person)
    if from_date:
        imap_query += ' SINCE {}'.format(from_date)
    if to_date:
        imap_query += ' BEFORE {}'.format(to_date)

    if len(imap_query) > 0:
        imap_query = "{}{}{}".format(imap_query_start, imap_query, imap_query_end)
        messages = server.search(imap_query)
    else:
        messages = server.search()

    # search_query = "{} {} {} {} {} {}".format(
    #     imap_query_start,
    #     imap_from,
    #     imap_to,
    #     imap_since,
    #     imap_before,
    #     imap_query_end
    # )


    #print('%d messages in INBOX' % select_info['EXISTS']) # or b'EXISTS'

    # messages = server.search(['NOT', 'DELETED'])
    # print("%d messages that aren't deleted" % len(messages))

    print()
    print("Messages:")
    response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE'])
    for msgid, data in response.items():
    #for msgid, data in response.iteritems():
        print('   ID %d: %d bytes, flags=%s' % (msgid,
                                                data[b'RFC822.SIZE'],
                                                data[b'FLAGS']))
Esempio n. 32
0
def localizar():
    conn = IMAPClient(config.end_imap, use_uid=True)
    conn.login(config.user, config.passwd)
    buscar = input('Digite o assunto do e-mail que deve ser Localizado? ')
    conn.select_folder('INBOX', readonly=True)
    pesquisa = conn.search('SUBJECT %s' % (buscar))
    resultado = conn.fetch(pesquisa, ['BODY[]'])
    pprint.pprint(resultado)
    conn.logout()
Esempio n. 33
0
File: main.py Progetto: IOEPAS/zippy
def process_mails(client: IMAPClient, uids: List[int],
                  logger: logging.Logger) -> Dict[int, ProcessedMessage]:
    """Retrieve all mails and process them, one by one."""
    client.select_folder(EmailFolders.INBOX, readonly=True)
    processed_msgs: Dict[int, ProcessedMessage] = {}
    for uid, message_data in client.fetch(uids, MESSAGE_FORMAT).items():
        processed_msgs[uid] = process_mail(client, uid, message_data, logger)

    return processed_msgs
Esempio n. 34
0
    def update(self):
        """Update data from Email API."""
        self._attr = {
            ATTR_EMAILS: [], 
            ATTR_TRACKING_NUMBERS: {}
        }
        emails = []
        server = IMAPClient(self.imap_server, use_uid=True)

        try:
            server.login(self.email_address, self.password)
            server.select_folder(self.email_folder, readonly=True)
        except Exception as err:
            _LOGGER.error('IMAPClient login error {}'.format(err))
            return False

        try: 
            messages = server.search(self.flag )
            for uid, message_data in server.fetch(messages, 'RFC822').items():
                try:
                    mail = parse_from_bytes(message_data[b'RFC822'])
                    emails.append({
                        EMAIL_ATTR_FROM: mail.from_,
                        EMAIL_ATTR_SUBJECT: mail.subject,
                        EMAIL_ATTR_BODY: mail.body
                    })
                    self._attr[ATTR_EMAILS].append({
                        EMAIL_ATTR_FROM: mail.from_,
                        EMAIL_ATTR_SUBJECT: mail.subject,
                    })
                except Exception as err:
                    _LOGGER.error('mailparser parse_from_bytes error: {}'.format(err))

        except Exception as err:
            _LOGGER.error('IMAPClient update error: {}'.format(err))

        self._attr[ATTR_COUNT] = len(emails)
        self._attr[ATTR_TRACKING_NUMBERS] = {}

        # empty out all parser arrays
        for ATTR, parser in parsers:
            self._attr[ATTR_TRACKING_NUMBERS][ATTR] = []

        # for each email run each parser and save in the corresponding ATTR
        for email in emails:
            for ATTR, parser in parsers:
                try:
                    self._attr[ATTR_TRACKING_NUMBERS][ATTR] = self._attr[ATTR_TRACKING_NUMBERS][ATTR] + parser(email)
                except Exception as err:
                    _LOGGER.error('{} error: {}'.format(ATTR, err))            

        # remove duplicates
        for ATTR, parser in parsers:
            self._attr[ATTR_TRACKING_NUMBERS][ATTR] = list(
                dict.fromkeys(self._attr[ATTR_TRACKING_NUMBERS][ATTR]))
           
        server.logout()
Esempio n. 35
0
    def get_mail(self):
        # import modules
        from imapclient import IMAPClient
        from sucsessreports.MailBot import MailBot

        # setup bot data
        bot = MailBot()
        bot.get_cred("imap")

        # connect to imap server
        # print(bot.server + bot.name + bot.port)
        server = IMAPClient(bot.server, port=bot.port, use_uid=True, ssl=True)
        print(server.login(bot.name, bot.pw))

        # select messages
        # print(server.list_folders())
        server.select_folder('INBOX', readonly=False)
        uids = server.search(['FROM', self.mail1, 'UNSEEN'])
        # print(uids)

        # save messages to file with email module
        if len(uids) > 0:
            import email
            from sucsessreports.Entry import Entry
            from datetime import datetime

            for uid in uids:
                r_msg = server.fetch([uid], ['RFC822'])
                msg = email.message_from_bytes(r_msg[uid][b'RFC822'])

                if not msg.get_payload() is None:
                    # get clean from-address
                    clean_from = msg.get("from").split()[-1][1:-1]

                    # get a clean date
                    messy_date = " ".join(msg.get("date").split()[0:4])
                    clean_date = datetime.strptime(messy_date,
                                                   "%a, %d %b %Y").date()

                    # get text from message
                    clean_text = ""
                    for part in msg.walk():
                        if part.get_content_type() == 'text/plain':
                            clean_text = part.get_payload().split(
                                '\r\n\r\n')[0]

                    ent = Entry(uid, clean_date, self.name, msg.get("subject"),
                                clean_text)
                    ent.save(
                        f'./SR_Data/userdata/db/{self.name}_db/{self.name}_db.csv'
                    )

        else:
            print("no messages found")

        print(server.logout())
Esempio n. 36
0
    def update(self):
        """Update data from Email API."""
        self._attr = {ATTR_EMAILS: [], ATTR_TRACKING_NUMBERS: {}}
        emails = []
        server = IMAPClient(self.smtp_server, use_uid=True)

        try:
            server.login(self.email_address, self.password)
            server.select_folder(self.email_folder, readonly=True)
        except Exception as err:
            _LOGGER.error('IMAPClient login error {}'.format(err))
            return False

        try:
            messages = server.search(self.flag)
            for uid, message_data in server.fetch(messages, 'RFC822').items():
                try:
                    mail = parse_from_bytes(message_data[b'RFC822'])
                    emails.append({
                        EMAIL_ATTR_FROM: mail.from_,
                        EMAIL_ATTR_SUBJECT: mail.subject,
                        EMAIL_ATTR_BODY: mail.body
                    })
                    self._attr[ATTR_EMAILS].append({
                        EMAIL_ATTR_FROM:
                        mail.from_,
                        EMAIL_ATTR_SUBJECT:
                        mail.subject,
                    })
                except Exception as err:
                    _LOGGER.error(
                        'mailparser parse_from_bytes error: {}'.format(err))

        except Exception as err:
            _LOGGER.error('IMAPClient update error: {}'.format(err))

        self._attr[ATTR_COUNT] = len(emails)

        try:
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_UPS] = parse_ups(emails)
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_FEDEX] = parse_fedex(emails)
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_USPS] = parse_usps(emails)
            self._attr[ATTR_TRACKING_NUMBERS][
                ATTR_ALI_EXPRESS] = parse_ali_express(emails)
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_NEWEGG] = parse_newegg(
                emails)
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_ROCKAUTO] = parse_rockauto(
                emails)
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_BH_PHOTO] = parse_bh_photo(
                emails)
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_EBAY] = parse_ebay(emails)
            self._attr[ATTR_TRACKING_NUMBERS][ATTR_DHL] = parse_dhl(emails)
        except Exception as err:
            _LOGGER.error('Parsers error: {}'.format(err))

        server.logout()
Esempio n. 37
0
class Mails:

    def __init__(self, host=None, port=None, ssl=False, config_file_name=None):
        """ Initialize and connect to the imap server

        An IMAPClient instance is created and connected to using the given
        arguments

        Keyword arguments:
        host -- (mandatory) The URI of the IMAP server
        port -- (mandatory) The port of the IMAP server
        ssl -- (optional) If SSL should be used
        config_file_name -- (optional) Can be used instead of the other
        arguments and should be a JSON file of the following
        type: {"imap": {SSL, host, username, password}} with the inner pairs
        representing key-value pairs of the keyword arguments not stated."""
        if config_file_name:
            # Read from config file
            with open(config_file_name, 'r') as configFile:

                config = json.load(configFile)
                ssl = config['imap'].get('SSL', False)
                host = config['imap']['host']
                port = config['imap'].get('port', None)
                password = config['imap']['password'] \
                    if 'password' in config['imap'] \
                    else None

        # Retrieve password from command line
        if password is None:
            password = getpass()

        # Generate imap server class
        print('Connecting to server')
        self.imap = IMAPClient(host, ssl=ssl, port=port, use_uid=True)

        # Try login
        print('Authenticating')

        self.imap.login(config['imap']['username'], password)

    def get_mails(self, mailbox='INBOX', criterion='ALL'):
        """ Return a list of mail messages matching the arguments

        Keyword arguments:
        mailbox -- name of the mailbox
        criterion -- IMAP search criterion
        """
        self.imap.select_folder(mailbox)
        uids = self.imap.search(criterion)
        mails = self.imap.fetch(uids, ['RFC822'])
        emails = [message_from_string(m['RFC822']) for m in mails.values()]
        return zip(uids, emails)
Esempio n. 38
0
class Mails:
    def __init__(self, host=None, port=None, ssl=False, config_file_name=None):
        """ Initialize and connect to the imap server

        An IMAPClient instance is created and connected to using the given
        arguments

        Keyword arguments:
        host -- (mandatory) The URI of the IMAP server
        port -- (mandatory) The port of the IMAP server
        ssl -- (optional) If SSL should be used
        config_file_name -- (optional) Can be used instead of the other
        arguments and should be a JSON file of the following
        type: {"imap": {SSL, host, username, password}} with the inner pairs
        representing key-value pairs of the keyword arguments not stated."""
        if config_file_name:
            # Read from config file
            with open(config_file_name, 'r') as configFile:

                config = json.load(configFile)
                ssl = config['imap'].get('SSL', False)
                host = config['imap']['host']
                port = config['imap'].get('port', None)
                password = config['imap']['password'] \
                    if 'password' in config['imap'] \
                    else None

        # Retrieve password from command line
        if password is None:
            password = getpass()

        # Generate imap server class
        print('Connecting to server')
        self.imap = IMAPClient(host, ssl=ssl, port=port, use_uid=True)

        # Try login
        print('Authenticating')

        self.imap.login(config['imap']['username'], password)

    def get_mails(self, mailbox='INBOX', criterion='ALL'):
        """ Return a list of mail messages matching the arguments

        Keyword arguments:
        mailbox -- name of the mailbox
        criterion -- IMAP search criterion
        """
        self.imap.select_folder(mailbox)
        uids = self.imap.search(criterion)
        mails = self.imap.fetch(uids, ['RFC822'])
        emails = [message_from_string(m['RFC822']) for m in mails.values()]
        return zip(uids, emails)
Esempio n. 39
0
    def getl(self):
        hostname = 'imap.gmail.com'
        username = '******'
        passwd = 'Pwcwelcome2'
        List = []
        c = IMAPClient(hostname, ssl=True)
        try:
            c.login(username, passwd)
        except c.Error:
            print('Could not log in')
            sys.exit(1)
        else:
            c.select_folder('INBOX', readonly=False)
            result = c.search('UNSEEN')
            msgdict = c.fetch(result, ['BODY.PEEK[]'])
            c.set_flags(msgdict, '\Seen')

            for message_id, message in msgdict.items():
                e = email.message_from_string(message['BODY[]'])
                subject = email.header.make_header(
                    email.header.decode_header(e['SUBJECT']))
                mail_from = email.header.make_header(
                    email.header.decode_header(e['From']))
                maintype = e.get_content_maintype()
                if maintype == 'multipart':
                    for part in e.get_payload():
                        if part.get_content_maintype() == 'text':
                            mail_content = part.get_payload(
                                decode=True).strip()
                elif maintype == 'text':
                    mail_content = e.get_payload(decode=True).strip()

                try:
                    mail_content = mail_content.decode('gbk')
                except UnicodeDecodeError:
                    print('decode error')
                    sys.exit(1)
                else:
                    dic = {}
                    dic['From'] = str(mail_from)
                    dic['Subject'] = str(subject)

                    mailcontent = mail_content.replace('<br>', '\n')
                    p = re.compile('<[^>]+>')
                    content = p.sub('', mailcontent)

                    dic['Content'] = str(content)
                    List.append(dic)
        finally:
            c.logout()
        return List
Esempio n. 40
0
    def get_email_from_web(self, host, account, password,
            ssl=True): # pragma: no cover
        conn = IMAPClient(host, use_uid=True, ssl=ssl)
        conn.login(account, password)
        self.assertTrue(conn.folder_exists('Inbox'))
        select_info = conn.select_folder('Inbox')

        messages = self.wait_for(lambda: self.has_unseen_emails(conn),
            timeout=15)
        response = conn.fetch(messages, ['RFC822'])
        for msgid, data in response.items():
            m = email.message_from_bytes(data[b'RFC822'])
            email_body = m.get_payload()
        return email_body
def check_for_resolution(code, description):
    from datetime import datetime, timedelta
    import email
    from imapclient import IMAPClient

    ssl = True

    today = datetime.today()
    cutoff = today - timedelta(days=1)

    ## Connect, login and select the INBOX
    server = IMAPClient(HOST, use_uid=True, ssl=ssl)
    server.login(USERNAME, PASSWORD)
    select_info = server.select_folder('INBOX')

    ## Search for relevant messages
    ## see http://tools.ietf.org/html/rfc3501#section-6.4.5
    messages = server.search(['SINCE %s' % cutoff.strftime('%d-%b-%Y')])
    response = server.fetch(messages, ['RFC822'])

    target_subject = "Re: OBA Alert!: " + str(code) + '.'
    resolved = False
    for msgid, data in response.iteritems():
        msg_string = data['RFC822']
        msg = email.message_from_string(msg_string)
        subject = msg['Subject']

        #Pulls the body of the solution email.
        body = ''
        for part in msg.walk():
            if part.get_content_type() == 'text/plain':
                body += str(part.get_payload()) + '\n'

        if subject == target_subject:
            resolved = True
            resolver = msg['From']
            time_resolved = msg['date']
            break
    if resolved:
        clear_alert(code, description)
        description = "OBA Alert: " + str(
            code
        ) + ", " + description + "\nwas resolved by " + resolver + ' at ' + time_resolved + '.'
        description += "\n\nSOLUTION:  " + body

        sendGmail(alert_list, description,
                  "OBA Alert Resolved!: " + str(code) + '.')
        return True
    else:
        return False
Esempio n. 42
0
def receive(username, password, sender):
    HOST = 'imap.gmail.com'
    ssl = True

    server = IMAPClient(HOST, use_uid=True, ssl=ssl)
    server.login(username, password)
    select_info = server.select_folder('INBOX')
    messages = server.search(['FROM "%s"' % sender])

    # print '%d messages in INBOX' % select_info['EXISTS']
    # print "%d messages that aren't deleted" % len(messages)

    response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE', 'BODY[TEXT]', 'INTERNALDATE'])
    mostrecent = max(response.keys(), key=int)
    return response[mostrecent]['BODY[TEXT]']
Esempio n. 43
0
def get_messages(hostname, username, password, folder, ssl=True):
    ''' Retrieve the headers of the email messages '''
    server = IMAPClient(hostname, use_uid=True, ssl=ssl)
    server.login(username, password)
    select_info = server.select_folder(folder)
    messages = server.search(['NOT DELETED'])
    what = 'RFC822.SIZE ENVELOPE'.split()
    response = server.fetch(messages, what)
    summaries = []
    for msgid, data in response.iteritems():
        env = envelope_fields(data)
        env['Size'] = data['RFC822.SIZE']
        summaries.append(env)
    summaries.sort(key=lambda d: d['Date'])
    return summaries
def check_for_resolution(code, description):
	from datetime import datetime, timedelta
	import email
	from imapclient import IMAPClient

	ssl = True

	today = datetime.today()
	cutoff = today - timedelta(days=1)

        ## Connect, login and select the INBOX
	server = IMAPClient(HOST, use_uid=True, ssl=ssl)
	server.login(USERNAME, PASSWORD)
	select_info = server.select_folder('INBOX')

       ## Search for relevant messages
       ## see http://tools.ietf.org/html/rfc3501#section-6.4.5
	messages = server.search(
		['SINCE %s' % cutoff.strftime('%d-%b-%Y')])
	response = server.fetch(messages, ['RFC822'])

	target_subject = "Re: OBA Alert!: " + str(code) + '.'
	resolved = False
	for msgid, data in response.iteritems():
		msg_string = data['RFC822']
		msg = email.message_from_string(msg_string)
		subject =  msg['Subject']

		#Pulls the body of the solution email.
		body = ''
		for part in msg.walk():
			if part.get_content_type() == 'text/plain':
				body += str(part.get_payload()) + '\n'

		if subject == target_subject:
			resolved = True
			resolver = msg['From']
			time_resolved = msg['date']
			break
	if resolved:
		clear_alert(code, description)
		description = "OBA Alert: " + str(code) + ", " + description + "\nwas resolved by " + resolver + ' at ' + time_resolved + '.' 
		description += "\n\nSOLUTION:  " + body

		sendGmail(alert_list, description ,"OBA Alert Resolved!: " + str(code) + '.' )
		return True
	else:
		return False
Esempio n. 45
0
def main():
    mail = IMAPClient(config['mail']['host'], use_uid=True)
    mail.login(config['mail']['user'], config['mail']['password'])
    mail.select_folder('INBOX')
    messages = mail.search(['ALL'])
    if len(messages) > 0:
        response = mail.fetch(messages, ['BODY.PEEK[HEADER.FIELDS (TO)]', 'RFC822'])
        mandrill_client = mandrill.Mandrill(config['mandrill']['password'])
        for msgnum, data in response.items():
            do_forward(mandrill_client, data[b'BODY[HEADER.FIELDS (TO)]'], data[b'RFC822'])
        mail.delete_messages(messages)
        mail.expunge()
        return True
    else:
        print('No messages')
        return False
Esempio n. 46
0
class Notifier:
    def __init__(self, server, username, password):
        self.server = server
        self.username = username
        self.password = password
        self.ssl = True
        self.frequency = 3

        self.imap_connection = IMAPClient(self.server, use_uid=True, ssl=self.ssl)
        self.seen_unread = []

        self.imap_connection.login(self.username, self.password)
        self.notifier = aamnotifs.Notifs(
            "amqps://*****:*****@domain.tld:5673/%2F")

        while True:
            self.check_unread()
            sleep(self.frequency)

    def check_unread(self):
        try:
            self.imap_connection.select_folder("INBOX")
            unread_list = self.imap_connection.search("UNSEEN")
        except:
            self.__init__(self.server, self.username, self.password)
            self.imap_connection.select_folder("INBOX")
            unread_list = self.imap_connection.search("UNSEEN")

        for unread in unread_list:
            if unread not in self.seen_unread:
                self.seen_unread.append(unread)
                envelope = self.imap_connection.fetch(
                    [unread], ["ENVELOPE"])[unread]["ENVELOPE"]
                subject = envelope[1]
                from_name = envelope[2][0][0]

                if not from_name:
                    if envelope[2][0][2] and envelope[2][0][3]:
                        from_name = envelope[2][0][2] + "@" + envelope[2][0][3]
                    else:
                        from_name = "(unknown)"

                if not subject:
                    subject = "(no subject)"

                self.notifier.send("mails", from_name, subject)
Esempio n. 47
0
def fetchmail(pid):
	server = IMAPClient(HOST, use_uid=True, ssl=ssl)
#        server.debug = 5;
	server.login(USERNAME, 'XXXX')
	select_info = server.select_folder('INBOX', readonly=True)
	print('%d messages in INBOX' % select_info['EXISTS'])
	messages = server.search(["NOT DELETED‏"])
	print("%d messages that aren't deleted" % len(messages))
	print()
	print("Messages:")
        response = server.fetch(messages, ['RFC822'])
	for msgid, data in response.iteritems():
	    msg_string = data['RFC822']
            msg = email.message_from_string(msg_string)
            printmail(msg);
            break;
        print "Done"
Esempio n. 48
0
def main():
    server = IMAPClient(HOST, use_uid=True, ssl=ssl)
    server.login(USERNAME, PASSWORD)

    select_info = server.select_folder('INBOX')
    print('%d messages in INBOX' % select_info[b'EXISTS'])

    messages = server.search(['NOT', 'DELETED'])
    print("%d messages that aren't deleted" % len(messages))

    print()
    print("Messages:")
    response = server.fetch(messages, ['FLAGS', 'RFC822', 'RFC822.SIZE', 'INTERNALDATE'])
    for msgid, data in response.items():
        print('   ID %d: %d bytes, flags=%s' % (msgid,
                                                data[b'RFC822.SIZE'],
                                                data[b'FLAGS']))
Esempio n. 49
0
def get_emails():
    FETCH_KEY = b'BODY[HEADER.FIELDS (SUBJECT FROM DATE)]'
    server = IMAPClient(HOST, use_uid=True, ssl=ssl)
    server.login(USERNAME, PASSWORD)
    server.select_folder('INBOX', readonly=True)
    messages = server.search(['UNSEEN'])
    response = server.fetch(messages, ['FLAGS', FETCH_KEY])
    emails = []
    for _, data in response.items():
        msg = email.message_from_bytes(data[FETCH_KEY])
        date = msg['date']
        subject = getmailheader(msg["Subject"])
        from_ = ', '.join((x[1] for x in getaddresses([msg['From']])))
        row = [subject, from_, date]
        emails.append(row)

    return emails
Esempio n. 50
0
def main(username, password, host, folders):
    client = MongoClient()
    email = client.email.email
    ssl = False

    server = IMAPClient(host, use_uid=True, ssl=ssl)
    server.login(username, password)

    # for i, folder in enumerate(server.list_folders(), start=1):
    #    print("{0}: {1}".format(i, folder))

    for folder in folders:
        select_info = server.select_folder(folder, readonly=True)
        num_msgs = select_info['EXISTS']
        print("{0}: {1} messages".format(folder, num_msgs))

        messages = server.search(['NOT DELETED'])
        messages = messages[:10]
        print("{0} messages that aren't deleted".format(len(messages)))

        fields = [
            'RFC822',
            'BODY[TEXT]',
            'INTERNALDATE',
            'FLAGS',
            'RFC822.SIZE',
            'BODY[HEADER.FIELDS (SUBJECT FROM)]',
        ]
        response = server.fetch(messages, fields)
        for msgid, data in response.iteritems():
            try:
                parsed = message_from_string(data['RFC822'])
                print(dir(parsed))
                obj = {
                    "folder": folder,
                    "msgid": msgid,
                    "msg": parsed.get_payload(),
                    "date": parsed["date"],
                    "from": parsed["From"],
                    "subject": parsed["Subject"],
                }
                email.insert(obj)
            except Exception as err:
                print(msgid, err)
    server.logout()
Esempio n. 51
0
def mailcheck_func():
    # Login credentials
    server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
    server.login(USERNAME, PASSWORD)
    b'[CAPABILITY IMAP4rev1 LITERAL+ SAL-IR [...] LIST-STATUS QUOTA] Logged in'

    # Select folder and search for the unread messages
    select_info = server.select_folder(MAILBOX, readonly=True)
    unread = server.folder_status(MAILBOX, ['UNSEEN'])
    messages = server.search('UNSEEN')

    for uid, message_data in server.fetch(messages, 'RFC822').items():
        #print('enter here')
        email_message = email.message_from_bytes(message_data[b'RFC822']) 
        #print('this is email message %s' % (email_message))
        ##print('this is email message %s' % (email_message))
        if (not email_message):
          print('No new message in the Inbox')
          continue
        else:
            mail_sub = email_message.get('Subject') 
#            print('New email')  

####  adding new lines
#           Print total count of unread messages
            newmail_count = (unread[b'UNSEEN'])
            print('%d unread messages in the Inbox' % newmail_count)
####
            if (mail_sub):
              if (SUBJECT_KEYWORD in mail_sub):
                print('There is a Severity-1 case in Queue.')  
####
                speech=gTTS("Hello Heera..., Please check there is a Severity-1 case in the queue.")
                speech.save("Hello.mp3")
                os.system("mpg321 -q Hello.mp3")
####
              else:
                #print('There is no Sev-1')
                print(' ')
            else:
              print('There is no mail')

            print('')
Esempio n. 52
0
 def update_email(self):
     '''
     Fetch email messages from the IMAP server and store them
     @return: None
     '''
     server = IMAPClient(settings.IMAP_HOST, use_uid=False, ssl=True)
     server.login(settings.IMAP_HOST_USER, settings.IMAP_HOST_PASSWORD)
     
     select_info = server.select_folder('INBOX')
     
     # get a list of messages 
     potentials = server.search(['NOT DELETED'])
     response = server.fetch(potentials, ['BODY[HEADER]', 'BODY[TEXT]'])
     
     for k, v in response.iteritems():
         message = self.parse_rfc822(v['BODY[HEADER]'],
                                     v['BODY[TEXT]'])
         # get a list of all recipients
         recip = message.recipients() + message.cc
         # split off gus users from this list for special handling
         gus_recip = []
         for r in recip:
             if r.endswith(settings.EMAIL_SUFFIX):
                 gus_recip.append(r[:-len(settings.EMAIL_SUFFIX)])
         recip = [r for r in recip if r not in gus_recip]
         
         try:
             date = parse(re.search('date:([^\n]*)\n', 
                              v['BODY[HEADER]'], re.I).group(1).strip())
             t = date.utcoffset()
             date = date.replace(tzinfo=None) - t
         except Exception, e:
             logging.debug(e)
             date = None
         
         # store the email in the DB
         em = DBEmail()
         em.fill(v['BODY[HEADER]'], bleach.clean(v['BODY[TEXT]'], tags=settings.BLEACH_ALLOWED_TAGS), date,
                 message.from_email, recip, gus_recip)
         
         # now delete from server
         server.add_flags(k, ['\Deleted'])
Esempio n. 53
0
def fetchmail(pid):
	server = IMAPClient(HOST, use_uid=True, ssl=ssl)
#        server.debug = 5;
	server.login(USERNAME, 'xxxx')
	select_info = server.select_folder('INBOX', readonly=True)
	print('%d messages in INBOX' % select_info['EXISTS'])
	messages = server.search(['SUBJECT aaaa'])
	print("%d messages that aren't deleted" % len(messages))
	print()
	print("Messages:")
        print messages
        response = server.fetch(messages, ['BODY.PEEK[1]', 'RFC822.HEADER'])
	for msgid, data in response.iteritems():
#            print data;
	    print data['BODY[1]']
            print data['RFC822.HEADER']
            #msg = email.message_from_string(msg_string)
            #printmail(msg);
            break;
        print "Done"
def readTheEmails():
	import mainEmail
	from mainEmail import masterPassword

	HOST = 'imap.gmail.com'
	USERNAME = '******'
	PASSWORD = mainEmail.masterPassword
	ssl = 993
	server = IMAPClient(HOST, use_uid=True, ssl=ssl)
	server.login(USERNAME, PASSWORD)

	select_info = server.select_folder('INBOX')

	messages = server.search(['NOT DELETED'])


	seenS="\\\\Seen"

	response = server.fetch(messages, ['FLAGS', 'RFC822.SIZE', 'BODY[TEXT]', 'BODY[HEADER.FIELDS (FROM)]'])
	for msgid, data in response.iteritems():

		senderAdrs=parseaddr(data['BODY[HEADER.FIELDS (FROM)]'])
		#print "from is: ",az
		#print('ID',msgid,"d:", data['RFC822.SIZE'], ' bytes, flags=', data['FLAGS'], " from= ", senderAdrs)
		flagsStuff =str(data['FLAGS'])
		boyStuff= str(data['BODY[TEXT]'])
		firstP = "tr\">"
		lastP = "</div>"
		bodyTrue= find_between(boyStuff, firstP, lastP)
		#print "body is ", bodyTrue

		#print flagsStuff
		if(seenS in flagsStuff):
			print "message  seen"
			print('ID',msgid,"d:", bodyTrue, ' body, flags=', data['FLAGS'], " from= ", senderAdrs)
			mainEmail.gotEmails(senderAdrs, bodyTrue)

		else: 
			print "NOT SEEN"
			print('ID',msgid,"d:", bodyTrue, ' body,  flags=', data['FLAGS'], " from= ", senderAdrs)
			mainEmail.gotEmails(senderAdrs, bodyTrue)
Esempio n. 55
0
def get_subject_content_by_uid(uid):
    server = IMAPClient(config['email']['host'], use_uid=True, ssl=False)
    server.login(config['email']['user'], config['email']['password'])

    server.select_folder('INBOX')
    messages = server.search(['NOT', 'DELETED'])

    response = server.fetch(messages, ['RFC822'])
    for msgid, data in response.items():
        msg = email.message_from_string(data[b'RFC822'].decode("utf-8"))
        if msg['Message-ID'] == uid:

            fp = StringIO()
            g = Generator(fp, mangle_from_=True, maxheaderlen=60)
            g.flatten(msg)
            text = fp.getvalue()

            start_index = text.find('Content-Type: text/plain;')
            stop_index = text.find("Content-Transfer", start_index)
            html = text[start_index:stop_index]
            return msg['subject'], html
Esempio n. 56
0
def getunseen(cfg):
    CHECKED = cfg['CHECKED']

    imap = cfg.get("__client")
    if not imap:
        imap = IMAPClient(cfg["HOST"], cfg["PORT"], use_uid=True, ssl= True)
        imap.login(cfg["USER"], cfg["PWD"])
        cfg["__client"] = imap

    checks = []

    for attr, fa, folder in imap.list_folders():
        if '\\Noselect' in attr or folder.startswith('[Gmail]/'):
            continue

        imap.select_folder(folder, readonly = True)

        ids = []
        _ids = imap.search('UNSEEN')
        for i in _ids:
            if i in CHECKED:
                continue
            ids.append(i)

        checks += ids
        if len(ids) > 5 or len(checks) > 20:
            notify('Check', 'Too More Email', cfg["GMAIL_URL"])
            break

        for msg in imap.fetch(ids, ['RFC822.HEADER', 'X-GM-THRID']).values():
            thrid = hex(msg['X-GM-THRID'])[2:]#just for google
            MSG = Emsg(msg['RFC822.HEADER'])


            notify(MSG.From[0][0], MSG.Subject, cfg["GMAIL_MSG_URL"] % thrid)


    return checks
Esempio n. 57
0
def checkForMail():
    try:
        server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
        server.login(EMAIL, PASSWORD)
        server.select_folder(MAILBOX)
        messages = server.search(['UNSEEN'])
        retString = ""
        fromEmail = ""
        if len(messages) > 0:
            # you've got mail!
            data = server.fetch(messages[0], ['RFC822'])
            # get email content
            msg = email.message_from_string(data[messages[0]]['RFC822'])
            # who's it from? maybe later we will add some verification of who it came from
            fromEmail = msg['From']
            print "received email from" + fromEmail

            #figure out email format - get content appropriately - then parse for commands
            bodyPlain = ""
            bodyHTML = ""
            if msg.is_multipart():
                for part in msg.walk():
                    if part.get_content_type() == "text/plain":
                        bodyPlain = part.get_payload(decode=True)
                        bodyPlain = bodyPlain.decode('utf8', 'ignore');
                    elif part.get_content_type() == "text/html":
                        bodyHTML = part.get_payload(decode=True)
                        bodyHTML = bodyHTML.decode('utf8', 'ignore');
                        continue
            if bodyPlain != "":
                retString = str(bodyPlain).strip()
            else:
                doc = l.document_fromstring(bodyHTML)
                content = doc.find('.//td')
                retString =  str(content.text).strip()
    except IMAPClient.Error, err:
        print "oh no we caught an exception" + err
Esempio n. 58
0
class MailFetcher(object):

    def __init__(self, host, username, password, use_ssl, image_folder,
                 force=False):
        self.host = host
        self.username = username
        self.password = password
        self.use_ssl = use_ssl
        self.image_folder = image_folder
        self.connection = None
        self.context = create_default_context()
        self.context.verify_mode = ssl.CERT_NONE
        self.force = force

    def connect(self):
        LOG.debug('Connecting to mail host...')
        self.connection = IMAPClient(self.host, use_uid=True, ssl=self.use_ssl,
                                     ssl_context=self.context)
        LOG.debug('Logging in...')
        self.connection.login(self.username, self.password)

    def fetch(self):
        LOG.debug('Fetching mail...')
        if not exists(self.image_folder):
            makedirs(self.image_folder)
            LOG.info('Created image folder at %r' % self.image_folder)

        self.connection.select_folder('INBOX')
        messages = self.connection.search(['NOT', 'DELETED'])
        response = self.connection.fetch(messages, ['FLAGS', 'BODY'])
        for msgid, data in response.items():
            is_read = SEEN in data[b'FLAGS']
            if is_read and not self.force:
                LOG.debug('Skipping already processed message #%r', msgid)
                continue
            else:
                # Add a "forced" note only if the message would not have been
                # processed otherwise.
                LOG.debug('Processing message #%r%s', msgid,
                          ' (forced override)' if is_read else '')
            body = data[b'BODY']
            el = []
            extract_elements(body, el)
            fetch_meta = [(i, data) for i, data in enumerate(el)
                          if (data[0], data[1]) in IMAGE_TYPES]
            if fetch_meta:
                self.download(msgid, fetch_meta)

    def in_index(self, md5sum):
        indexfile = join(self.image_folder, 'index')
        if not exists(indexfile):
            LOG.debug('%r does not exist. Assuming first run.', indexfile)
            return False
        hashes = [line.strip() for line in open(indexfile)]
        return md5sum in hashes

    def add_to_index(self, md5sum):
        if not md5sum.strip():
            return
        indexfile = join(self.image_folder, 'index')
        with open(indexfile, 'a+') as fptr:
            fptr.write(md5sum + '\n')

    def download(self, msgid, metadata):
        LOG.debug('Downloading images for mail #%r', msgid)
        has_error = False

        parts = flatten_parts(self.connection.fetch(
            [msgid], [b'BODY'])[msgid][b'BODY'][0])
        images = [part for part in parts if part[1][0] == b'image']
        for image_id, header in images:
            try:
                (major, minor, params, _, _, encoding, size) = header
                # Convert "params" into a more conveniend dictionary
                params = dict(zip(params[::2], params[1::2]))
                filename = params[b'name'].decode('ascii', errors='ignore')
                unique_name = 'image_{}_{}_{}'.format(msgid, image_id, filename)
                encoding = encoding.decode('ascii')
                LOG.debug('Processing part #%r in mail #%r', image_id, msgid)
                element_id = ('BODY[%d]' % image_id).encode('ascii')
                response = self.connection.fetch([msgid], [element_id])
                content = response[msgid][element_id]
                if not content:
                    LOG.error('Attachment data was empty for '
                              'message #%r', msgid)
                    has_error = True
                    continue

                if encoding == 'base64':
                    bindata = b64decode(content)
                else:
                    bindata = content.decode(encoding)
                md5sum = md5(bindata).hexdigest()
                if self.in_index(md5sum) and not self.force:
                    LOG.debug('Ignored duplicate file (md5=%s).', md5sum)
                    continue
                elif self.in_index(md5sum) and self.force:
                    LOG.debug('Bypassing index check (force=True)')

                fullname = join(self.image_folder, unique_name)
                if not exists(fullname) or self.force:
                    suffix = ' (forced overwrite)' if exists(fullname) else ''
                    with open(fullname, 'wb') as fptr:
                        fptr.write(bindata)
                    LOG.info('File written to %r%s', fullname, suffix)
                    self.add_to_index(md5sum)
                else:
                    has_error = True
                    LOG.warn('%r already exists. Not downloaded!' % fullname)
            except:
                LOG.error('Unable to process mail #%r', msgid, exc_info=True)
                has_error = True

        if has_error:
            self.connection.add_flags([msgid], FLAGGED)
        else:
            self.connection.add_flags([msgid], SEEN)
Esempio n. 59
0
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
server.login(username, password)

#folderlist = server.xlist_folders()
#print(folderlist)

select_info = server.select_folder(folder)

## Search for relevant messages
## see http://tools.ietf.org/html/rfc3501#section-6.4.5
messages = server.search(
['SUBJECT "E-post fra CULTURA SPAREBANK"', 
 'FROM "*****@*****.**"', 
 'UNKEYWORD "done"',
 'SINCE %s' % cutoff.strftime('%d-%b-%Y')])
response = server.fetch(messages, ['RFC822'])

count = 0
for msgid, data in response.iteritems():
   msg_string = data['RFC822']
   msg = email.message_from_string(msg_string)
   #print 'ID %d: From: %s Date: %s' % (msgid, msg['From'], msg['date'])
   #print msg['Subject']
	#print(msg.get_content_type())
   labels = server.get_gmail_labels(msgid)
   if done_label in labels[msgid]:
      #print server.get_flags(msgid) # should have 'done' flag
      #server.remove_flags(msgid, ('\\Flagged'))
      #server.add_flags(msgid,(done_flag))
      #print('#skipped %s'% msgid)
      continue
Esempio n. 60
-1
def test_imap(user):
    credentials = decorator.credentials
    if credentials.access_token_expired:
        logging.debug('Refreshing...')
        credentials.refresh(httplib2.Http())
    conn = IMAPClient('imap.gmail.com', use_uid=True, ssl=True)
    conn.debug = 4

    conn.oauth2_login(user, credentials.access_token)

    # status, labels = conn.list()
    folders = conn.list_folders()
    try:
        all_box = next(box for (flags, _, box) in folders if '\All' in flags)
    except StopIteration:
        raise Error('all message box not found')

    logging.debug('All message box is {}'.format(all_box))

    conn.select_folder(all_box)
    # Once authenticated everything from the impalib.IMAP4_SSL class will
    # work as per usual without any modification to your code.
    # typ, msgnums = conn.search('X-GM-RAW vget')
    tid = int('14095f27c538b207', 16)
    # msgs = conn.search('X-GM-THRID {}'.format(tid))
    msgs = conn.search('X-GM-RAW uniquetokenXXX')

    logging.info(msgs)
    logging.info(conn.fetch(msgs, 'X-GM-MSGID'))
    msg = conn.fetch(msgs, 'RFC822')
    logging.info(msg)
    return msg