Guest User

Untitled

a guest
Oct 29th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import os, sys
  4. import imaplib
  5. import email
  6. import smtplib
  7. from email.mime.multipart import MIMEMultipart
  8.  
  9. servername = 'SERVERNAME'
  10. username='USERNAME'
  11. password='PASSWORD'
  12.  
  13.  
  14. mail = imaplib.IMAP4_SSL(servername)
  15. (retcode, capabilities) = mail.login(username,password)
  16. mail.list()
  17. mail.select('inbox')
  18.  
  19. server_smtp = smtplib.SMTP_SSL('SMTP')
  20.  
  21. n=0
  22. (retcode, messages) = mail.search(None, '(UNSEEN)')
  23. if retcode == 'OK':
  24.  
  25. for num in messages[0].split() :
  26. n=n+1
  27. typ, data = mail.fetch(num,'(RFC822)')
  28. for response_part in data:
  29. if isinstance(response_part, tuple):
  30. original = email.message_from_string(response_part[1])
  31.  
  32. print original['From']
  33. typ, data = mail.store(num,'+FLAGS','\Seen')
  34.  
  35. body = ""
  36. if original.is_multipart():
  37. for part in original.walk():
  38. ctype = part.get_content_type()
  39. cdispo = str(part.get('Content-Disposition'))
  40.  
  41. if ctype == 'text/plain' and 'attachment' not in cdispo:
  42. body = part.get_payload(decode=True) # decode
  43. break
  44. else:
  45. body = original.get_payload(decode=True)
  46.  
  47. body = body.encode('UTF-8')
  48. body = str(body)
  49.  
  50. print(body)
  51. body = "Betreff: " + str(original['Subject']) + "nnn" + body.encode('UTF-8')
  52. SUBJECT = original['From']
  53.  
  54. server_smtp.login(username, password)
  55. msg = 'Subject: {}nn{}'.format(SUBJECT, body.decode('UTF-8'))
  56. server_smtp.sendmail(username, 'TARGET', msg)
Add Comment
Please, Sign In to add comment