ich möchte mit Emails bestimmte Aktionen ausführen, und die Emails dann löschen. Leider geht es nicht so recht.
Code: Alles auswählen
#!/usr/bin/env python3
import imaplib
import os
import configparser as ConfigParser
import email
import datetime
from time import sleep
imaplib._MAXLINE = 40000
class emailreceiver():
def open_connection(self, verbose=False):
# Read the config file
config = ConfigParser.ConfigParser()
config.read([os.path.expanduser('~/.pymotw')])
# Connect to the server
hostname = '<server>'
if verbose: print('Connecting to', hostname)
connection = imaplib.IMAP4_SSL(hostname)
# Login to our account
username = '<email>'
password = '<pw>'
if verbose: print('Logging in as', username)
connection.login(username, password)
return connection
if __name__ == '__main__':
c = emailreceiver().open_connection(True)
try:
typ, data = c.select('INBOX')
date = (datetime.date.today() - datetime.timedelta(1)).strftime("%d-%b-%Y")
num_msgs = int(data[0])
print('There are %d messages in INBOX' % num_msgs)
# result, data = c.uid('search', None, "ALL")
result, data = c.uid('search', None, '(SENTSINCE {date})'.format(date=date))
ids = data[0] # data is a list.
id_list = ids.split() # ids is a space separated string
latest_email_uid = id_list[-1] # get the latest
result, data = c.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]
email_message = email.message_from_bytes(raw_email)
if email.utils.parseaddr(email_message['From'])[1] == 'peterboelke@gmail.com':
subject_message = email_message['Subject'].lower()
if subject_message == 'test':
print("test")
c.store(latest_email_uid, '+FLAGS', r'(\\Deleted)')
typ, response = c.expunge()
print('Expunged:', response)
elif subject_message == 'loadpodcasts':
print('start loading podcasts')
c.store(latest_email_uid, '+FLAGS', r'(\\Deleted)')
typ, response = c.expunge()
print('Expunged:', response)
except IndexError:
print("No Messages")
c.logout()
Code: Alles auswählen
c.store(latest_email_uid, '+FLAGS', r'(\\Deleted)')
Code: Alles auswählen
Traceback (most recent call last):
File "/home/klofisch/PycharmProjects/pytools/Email/Reciever/receiver.py", line 60, in <module>
c.store(latest_email_uid, '+FLAGS', r'(\\Deleted)')
File "/usr/lib/python3.4/imaplib.py", line 778, in store
typ, dat = self._simple_command('STORE', message_set, command, flags)
File "/usr/lib/python3.4/imaplib.py", line 1134, in _simple_command
return self._command_complete(name, self._command(name, *args))
File "/usr/lib/python3.4/imaplib.py", line 965, in _command_complete
raise self.error('%s command error: %s %s' % (name, typ, data))
imaplib.error: STORE command error: BAD [b'Error in IMAP command STORE: Invalid messageset']
Habt Ihr noch einen Tip.