cfabio

Email.py

Jan 19th, 2019
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.30 KB | None | 0 0
  1. ################
  2. # Send emails
  3. ################
  4.  
  5. import smtplib
  6. conn = smtplib.SMTP('smtp.gmail.com', 587)
  7. conn.ehlo() #start the connection
  8. conn.starttls() #start encryption before passing password
  9. conn.login('[email protected]', 'password')
  10. conn.sendmail('[email protected]', '[email protected]', 'Subject: so long...\n\n')
  11. conn.quit()
  12.  
  13.  
  14. ################
  15. # Read emails
  16. ################
  17. import imapclient
  18. conn = imapclient.IMAPClient('imap.gmail.com', ssl=True)
  19. conn.login('[email protected]', 'password')
  20. conn.select_folder('INBOX', readonly=True)
  21.  
  22. UIDs = conn.search(['SINCE 20-Aug-2015'])    #returns a list of unique ids of the emails we are filtering
  23. rawMessage = conn.fetch([46465], ['BODY[]'
  24.  
  25. #to parse the raw message and get the single fields ot of the email
  26. import pyzmail
  27. message = pyzmail.PyzMessage.factory(rawMessage[46465][b'BODY[]'])
  28. message.get_ subject()
  29.  
  30. message.get_addresses('from')
  31. message.get_addresses('to')
  32. message.get_addresses('bbc')
  33.  
  34. message.text_part #`the email can be text or html
  35. mesasge.html_part
  36. message.text_part.get_payload().decode('UTF-8')
  37.  
  38. conn.list_folders() #list all the directories of the email box
  39.  
  40.  
  41. conn.select_folder('INBOX', readonly=False)
  42. UIDs = conn.search(['ON 24-Aug-2015'])
  43. conn.delete_messages(UIDs)
Advertisement
Add Comment
Please, Sign In to add comment