Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #login: wnownowno2@wp.pl
  2. #haslo: laborkawno2
  3.  
  4. import imaplib
  5. import email
  6. import smtplib
  7. import os
  8. from email.mime.image import MIMEImage
  9. from email.mime.multipart import MIMEMultipart
  10. from PIL import Image
  11.  
  12.  
  13. class MailMan:
  14. def __init__(self,server,login,password):
  15. self.login = login#"wno2lab4@gmail.com"
  16. self.password = password#"LABLABLAB4"
  17. self.server = server#"imap.gmail.com"
  18.  
  19. def sendMail(self,subject, contents):
  20. msg = "From: {}\nTo: {}\nSubject: {}\n{}"\
  21. .format(self.login, self.login, subject, contents)
  22. s = self.smtpConnect()
  23. s.sendmail(self.login,self.login,msg)
  24. print("Mail sent")
  25. s.quit()
  26.  
  27. def smtpConnect(self):
  28. print("Connecting to ", self.server)
  29. s = smtplib.SMTP(self.server, 587)
  30. s.ehlo()
  31. s.starttls()
  32. print("TLS started")
  33. s.login(self.login, self.password)
  34. print("Logged in")
  35. return s
  36.  
  37. def sendImage(self,subject, contents, iname):
  38. s = self.smtpConnect()
  39. img_data = open(iname,'rb').read()
  40. msg = MIMEMultipart()
  41. msg['Subject'] = subject
  42. msg['From'] = self.login
  43. msg['To'] = self.login
  44. #text = MIMEText(contents)
  45. #msg.attach(text)
  46. image = MIMEImage(img_data, name=os.path.basename(iname))
  47. msg.attach(image)
  48. s.sendmail(self.login,self.login,msg.as_string())
  49. s.quit()
  50. pass
  51.  
  52. def processPart(self,part):
  53. if part.get_content_maintype() == 'multipart':
  54. print(part.as_string())
  55. pass
  56. if part.get('Content-Disposition') is None:
  57. return
  58. fileName = part.get_filename()
  59. dlName = "dl_" + fileName
  60. print("Opening image")
  61. filePath = os.path.join('.', 'attachments', fileName)
  62. # if not os.path.isfile(filePath):
  63. print("Downloading file: ", fileName)
  64. fp = open(dlName, 'wb')
  65. fp.write(part.get_payload(decode=True))
  66. fp.close()
  67.  
  68. im = Image.open(dlName)
  69. im.show()
  70. def processEmail(self,msg):
  71. print("Do: ", msg['To'])
  72. print("Od: ", email.utils.parseaddr(msg['From']))
  73. print("Temat: ", msg['Subject'])
  74. print(msg.items())
  75. for part in msg.walk():
  76. self.processPart(part)
  77.  
  78. def queryMail(self):
  79. M = imaplib.IMAP4_SSL(self.server)
  80. response = M.login(self.login, self.password)
  81. print(response)
  82. print("Logged in")
  83. typ, list = M.list()
  84. print(typ)
  85. print(list)
  86. print(M.select("INBOX"))
  87. result, data = M.search(None, 'ALL')
  88. for id in data[0].split():
  89. mailres, maildata = M.fetch(id, "(RFC822)")
  90. msg = email.message_from_string(maildata[0][1].decode())
  91. self.processEmail(msg)
  92.  
  93. M.logout()
  94.  
  95. login = "wnownowno2@gmail.com"
  96. password = "laborkawno2"
  97. server = "imap.gmail.com"
  98.  
  99. mailman = MailMan(server=server,login=login,password=password)
  100. mailman.sendMail("Email", "Hello!!!")
  101. mailman.sendImage("Email - sending image","Hello Image!","duck.bmp")
  102. mailman.queryMail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement