Advertisement
Guest User

Untitled

a guest
Mar 16th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.17 KB | None | 0 0
  1. #wno2l3szymon123@gmail.com szymon123
  2. import smtplib
  3. import imaplib
  4. from email.mime.text import MIMEText
  5. from email.mime.image import MIMEImage
  6. from email.mime.multipart import MIMEMultipart
  7. import os
  8. import email
  9.  
  10. class Email(object):
  11. mail = imaplib.IMAP4_SSL('imap.gmail.com')
  12. def __init__(self):
  13. self.mail.login('wno2l3szymon123@gmail.com', 'szymon123')
  14. self.mail.list()
  15. self.mail.select("inbox") # connect to inbox.
  16. # Out: list of "folders" aka labels in gmail.
  17.  
  18. def sendEmail(self):
  19. fromaddr = 'wno2l3szymon123@gmail.com'
  20. toaddrs = 'wno2l3szymon123@gmail.com'
  21. msg = "\r\n".join([
  22. "From: wno2l3szymon123@gmail.com",
  23. "To: wno2l3szymon123@gmail.com",
  24. "Subject: WNO czwartek",
  25. "",
  26. "Wiadomosc"
  27. ])
  28.  
  29. username = fromaddr
  30. password = 'szymon123'
  31. server = smtplib.SMTP('smtp.gmail.com:587')
  32. server.ehlo()
  33. server.starttls()
  34. server.login(username, password)
  35. server.sendmail(fromaddr, toaddrs, msg)
  36. server.quit()
  37.  
  38. def sendEmailWithAttachment(self):
  39. img_data = open('canny2.bmp', 'rb').read()
  40. msg = MIMEMultipart()
  41. msg['Subject'] = 'subject'
  42. msg['From'] = 'wno2l3szymon123@gmail.com'
  43. msg['To'] = 'wno2l3szymon123@gmail.com'
  44.  
  45. text = MIMEText("mail.txt")
  46. msg.attach(text)
  47. image = MIMEImage(img_data, name=os.path.basename('canny2.bmp'))
  48. msg.attach(image)
  49.  
  50. s = smtplib.SMTP('smtp.gmail.com:587')
  51. s.ehlo()
  52. s.starttls()
  53. s.ehlo()
  54. s.login('wno2l3szymon123@gmail.com', 'szymon123')
  55. s.sendmail('wno2l3szymon123@gmail.com', 'wno2l3szymon123@gmail.com', msg.as_string())
  56. s.quit()
  57.  
  58. def getLastMail(self):
  59. result, data = self.mail.search(None, "ALL")
  60. ids = data[0]
  61. id_list = ids.split()
  62. latest_email_id = id_list[-1]
  63.  
  64. result, data = self.mail.fetch(latest_email_id, "(RFC822)")
  65. return data[0][1]
  66.  
  67. def save_attachment(self, msg, download_folder="/tmp"):
  68.  
  69. att_path = "No attachment found."
  70. for part in msg.walk():
  71. if part.get_content_maintype() == 'multipart':
  72. continue
  73. if part.get('Content-Disposition') is None:
  74. continue
  75.  
  76. filename = part.get_filename()
  77. att_path = os.path.join(download_folder, filename)
  78.  
  79. if not os.path.isfile(att_path):
  80. fp = open(att_path, 'wb')
  81. fp.write(part.get_payload(decode=True))
  82. fp.close()
  83. return att_path
  84.  
  85. def get_attachments(self):
  86. self.imap.select()
  87. typ, data = self.imap.uid('SEARCH', 'ALL')
  88. msgs = data[0].split()
  89. print
  90. ("Found {0} msgs".format(len(msgs)))
  91.  
  92. for uid in msgs:
  93. typ, s = self.imap.uid('FETCH', uid, '(RFC822)')
  94. mail = email.message_from_string(s[0][1])
  95.  
  96. print
  97. ("From: {0}, Subject: {1}, Date: {2}\n".format(mail["From"], mail["Subject"], mail["Date"]))
  98.  
  99. if mail.is_multipart():
  100. print
  101. ('multipart')
  102. for part in mail.walk():
  103. ctype = part.get_content_type()
  104. if ctype in ['image/jpeg', 'image/png']:
  105. open(part.get_filename(), 'wb').write(part.get_payload(decode=True))
  106.  
  107. def getMultipleMails(self):
  108. result, data = self.mail.search(None, "ALL")
  109. ids = data[0]
  110. id_list = ids.split()
  111. latest_email_id = id_list[-1]
  112. for i in range(-1, 100):
  113. try:
  114. latest_email_id = id_list[i]
  115. result, data = self.mail.fetch(latest_email_id, "(RFC822)")
  116. print(data[0][1])
  117. except:
  118. return None
  119. def main():
  120. gmailObject = Email()
  121. #gmailObject.sendEmailWithAttachment()
  122. #gmailObject.sendEmail()
  123. #print(gmailObject.getLastMail())
  124. #gmailObject.get_attachments()
  125. (gmailObject.getMultipleMails())
  126. if __name__ == '__main__':
  127. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement