Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. application/vnd.ms-excel
  2. =?UTF-8?B?0JrQu9C10Y8sINC80LDRgdC70LAsINGB0LjQu9C40LrQvtC9IDIwMTcgKDEpLnhscw==?
  3.  
  4. import imaplib
  5. import email.message
  6. import os.path
  7. import datetime
  8. import xlrd
  9.  
  10. YA_HOST = "imap.yandex.ru"
  11. YA_PORT = 993
  12. YA_USER = "lapithome"
  13. YA_PASSWORD = "blabla"
  14.  
  15. pause_time = 5000
  16.  
  17.  
  18. class Getmail():
  19. """ClientTask"""
  20. def __init__(self):
  21. self.id = 1
  22.  
  23. def yandex(self,sender):
  24.  
  25. # подключились к почте и логинимся
  26. imap = imaplib.IMAP4_SSL(YA_HOST)
  27. imap.login(YA_USER, YA_PASSWORD)
  28. status, select_data = imap.select()
  29. # nmessages = select_data[0].decode('utf-8')
  30.  
  31. # от кого письмо
  32. status, search_data = imap.search(None, 'FROM', sender)
  33.  
  34. for msg_id in reversed(search_data[0].split()):
  35. status, msg_data = imap.fetch(msg_id, '(RFC822)')
  36. # включает в себя заголовки и альтернативные полезные нагрузки
  37. mail = email.message_from_bytes(msg_data[0][1])
  38.  
  39. if mail.is_multipart():
  40. filelist = []
  41. path = './xls/' + datetime.datetime.today().strftime("%d-%m-%Y") + '/'
  42. if not os.path.exists(path):
  43. os.makedirs(path)
  44. for part in mail.walk():
  45. content_type = part.get_content_type()
  46. filename = part.get_filename()
  47. if filename:
  48. print(content_type)
  49. print(filename)
  50. if '.xls' in filename:
  51. filelist.append(filename)
  52. print('Закачали файл: ', filename)
  53.  
  54. with open(path+part.get_filename(), 'wb') as new_file:
  55. new_file.write(part.get_payload(decode=True))
  56. break
  57. imap.expunge()
  58. imap.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement