phone = телефон (из темы письма)
imgcount = (количество вложений)
email = от кого
ip =
added = true
после чего письмо удаляется с сервера…
Но письмо не удаляется….))
Также, если будут советы по оптимизации, буду премного благодарен)))
код класса:
class MailClass():
def __init__(self):
self.box = poplib.POP3('10.0.0.88')
print (self.box.getwelcome())
print('connect to server... ', )
try:
self.box.user('user')
self.box.pass_('pass')
except:
print('authorisation with pop3-server failed')
print('Authorisation successful')
def __destroy__(self):
self.box.quit()
#uznaem kol-vo soobschenii
def getCount(self):
return len(self.box.list()[1])
def getMessage(self):
print('read message...')
response, lst, octets = self.box.list()
print ("DEBUG: Total messages: %s" % (len(lst)))
self.box.noop()
for msgnum,msgsize in [i.split() for i in lst]:
now = None
(resp, lines, octets) = self.box.retr(msgnum.decode('utf8'))
lines = [line.decode('utf8','ignore') for line in lines]
msgtext = "\n".join(lines) + "\n\n"
message = email.message_from_string(msgtext)
self.mailFrom = re.search(r"([\w_\-\.\d]{1,}@[\w_\-\.\d]{2,}\.[\w]{2,4})",message['From'],re.IGNORECASE|re.MULTILINE).group(1)
print('From: ', self.mailFrom)
try:
self.mailIp = re.search(r"\[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]",message['Received'],re.IGNORECASE|re.MULTILINE).group(1)
except:
self.mailIp = None
print('Ip = ', self.mailIp)
print('uidl = ', self.box.uidl(msgnum))
if email.message.Message.is_multipart(message): #proveryaem na vlozheniya
attachs = email.message.Message.get_payload(message)
i = 0
for part in attachs:
filename = part.get_filename()
if filename and re.match(".+\.(pdf|tif)$",filename,re.IGNORECASE): #esli rashirenie tif ili pdf, registr ignoriruem
i += 1
print (filename)
if not now:
now = getNowDir()
path = os.path.join(mainDir,now)
os.mkdir(path)
f = open(os.path.join(path, "faxmail" + now + "-"+ str(i) +"."+filename.split(".")[-1]), "wb")
f.write(part.get_payload(decode=1))
f.close
if now:
#sozdaem zadanie
config = configparser.RawConfigParser()
config.add_section('main')
config.set('main', 'phone', message['Subject'])
config.set('main', 'imgcount', i)
config.add_section('user')
config.set('user', 'email', self.mailFrom)
config.set('user', 'ip', self.mailIp)
with open(os.path.join(path, 'job.ini'), 'w') as configfile:
config.write(configfile)
run = Runner(now) #zapuskaem
self.box.dele(int(msgnum))