Advertisement
Guest User

Untitled

a guest
Feb 7th, 2019
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.46 KB | None | 0 0
  1. import email
  2. import smtplib
  3. import imaplib
  4. from email.MIMEText import MIMEText
  5. from email.MIMEMultipart import MIMEMultipart
  6.  
  7.  
  8. # Drop 'GMAIL_' prefix: mail provider may change.
  9. GMAIL_SMTP = "smtp.gmail.com"
  10. GMAIL_IMAP = "imap.gmail.com"
  11.  
  12. # These are all global vars. If they are really do need to be
  13. # global, they all should be uppercased.
  14. # Avoid globals at all costs. See for ex.: https://stackoverflow.com/questions/19158339/why-are-global-variables-evil
  15. # Length >= 3
  16. l = 'login@gmail.com'
  17. # Mixed case
  18. passwORD = 'qwerty'
  19. subject = 'Subject'
  20. recipients = ['vasya@email.com', 'petya@email.com']
  21. message = 'Message'
  22. header = None
  23.  
  24.  
  25. # def send(...):
  26. #send message
  27. msg = MIMEMultipart()
  28. # msg.update({
  29. #     'From': ...,
  30. #     ...: ...})
  31. msg['From'] = l
  32. msg['To'] = ', '.join(recipients)
  33. msg['Subject'] = subject
  34. msg.attach(MIMEText(message))
  35.  
  36. # More descriptive var name. I suggest `conn`. Define port as `SMTPS_PORT = 587`
  37. ms = smtplib.SMTP(GMAIL_SMTP, 587)
  38.  
  39. # identify ourselves to smtp gmail client
  40. ms.ehlo()
  41. # secure our email with tls encryption
  42. ms.starttls()
  43. # re-identify ourselves as an encrypted connection
  44. ms.ehlo()
  45.  
  46. ms.login(l, passwORD)
  47. # ms.sendmail(
  48. #     l,
  49. #     ms,
  50. #     msg.as_string())
  51. # -- or --
  52. # ms.sendmail(l, ms, msg.as_string)
  53. ms.sendmail(l,
  54. ms, msg.as_string())
  55.  
  56. ms.quit()
  57. #send end
  58.  
  59.  
  60. # def read(...):
  61. # Naming is broken. This is not an email "reception". We (or, rather MTA) receive using SMTP.
  62. # I believe "read" would sound closer to the point.
  63. #recieve
  64.  
  65. # rename mail -> conn. Why: scope for "mail" is way too broad. Conn is much more precise.
  66. mail = imaplib.IMAP4_SSL(GMAIL_IMAP)
  67. mail.login(l, passwORD)
  68. mail.list()
  69. # All hardcoded strings must be either defined as "constants" or be passed as an argument.
  70. mail.select("inbox")
  71. # This is definitely needs to be passed as an argument.
  72. criterion = '(HEADER Subject "%s")' % header if header else 'ALL'
  73. result, data = mail.uid('search', None, criterion)
  74. # Assertion errors are to dirty. Not ok for prod environment. `raise` is a preferrable way of signalling an error condition.
  75. assert data[0], 'There are no letters with current header'
  76. latest_email_uid = data[0].split()[-1]
  77. # The last arg should be in func params.
  78. result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
  79. raw_email = data[0][1]
  80. email_message = email.message_from_string(raw_email)
  81. mail.logout()
  82. #end recieve
  83.  
  84. # No print, no file was written, where does all the output goes to?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement