Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os,email,imaplib,socket,requests,logging,http.client
- mail_user = os.environ.get('MAIL_USER')
- mail_password = os.environ.get('MAIL_PASS')
- mail_server = os.environ.get('MAIL_SERVER')
- detach_dir = '.'
- if mail_user is None or mail_password is None or mail_server is None:
- print ('VARIABILI DI AMBIENTE NON DEFINITE')
- exit(1)
- try:
- with imaplib.IMAP4_SSL(mail_server) as m:
- try:
- m.login(mail_user,mail_password)
- m.select("INBOX")
- resp, items = m.search(None, "UNSEEN")
- items = items[0].split()
- for emailid in items:
- resp, data = m.fetch(emailid, "(RFC822)")
- email_body = data[0][1] # getting the mail content
- mail = email.message_from_bytes(email_body) # parsing the mail content to get a mail object
- if mail.get_content_maintype() != 'multipart':
- continue
- for part in mail.walk():
- if part.get_content_maintype() == 'multipart':
- continue
- if part.get('Content-Disposition') is None:
- continue
- filename = part.get_filename().lower()
- if filename.endswith('.xlsx'):
- att_path = os.path.join(detach_dir, filename)
- fp = open(att_path, 'wb')
- fp.write(part.get_payload(decode=True))
- fp.close()
- except imaplib.IMAP4_SSL.error as e:
- print (e)
- print ("ERRORE")
- exit(1)
- except imaplib.IMAP4.error:
- print ("Errore di connessione al server")
- exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement