Advertisement
Guest User

Untitled

a guest
Feb 7th, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 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. GMAIL_SMTP = "smtp.gmail.com"
  9. GMAIL_IMAP = "imap.gmail.com"
  10.  
  11. l = 'login@gmail.com'
  12. passwORD = 'qwerty'
  13. subject = 'Subject'
  14. recipients = ['vasya@email.com', 'petya@email.com']
  15. message = 'Message'
  16. header = None
  17.  
  18.  
  19. #send message
  20. msg = MIMEMultipart()
  21. msg['From'] = l
  22. msg['To'] = ', '.join(recipients)
  23. msg['Subject'] = subject
  24. msg.attach(MIMEText(message))
  25.  
  26. ms = smtplib.SMTP(GMAIL_SMTP, 587)
  27. # identify ourselves to smtp gmail client
  28. ms.ehlo()
  29. # secure our email with tls encryption
  30. ms.starttls()
  31. # re-identify ourselves as an encrypted connection
  32. ms.ehlo()
  33.  
  34. ms.login(l, passwORD)
  35. ms.sendmail(l,
  36. ms, msg.as_string())
  37.  
  38. ms.quit()
  39. #send end
  40.  
  41.  
  42. #recieve
  43. mail = imaplib.IMAP4_SSL(GMAIL_IMAP)
  44. mail.login(l, passwORD)
  45. mail.list()
  46. mail.select("inbox")
  47. criterion = '(HEADER Subject "%s")' % header if header else 'ALL'
  48. result, data = mail.uid('search', None, criterion)
  49. assert data[0], 'There are no letters with current header'
  50. latest_email_uid = data[0].split()[-1]
  51. result, data = mail.uid('fetch', latest_email_uid, '(RFC822)')
  52. raw_email = data[0][1]
  53. email_message = email.message_from_string(raw_email)
  54. mail.logout()
  55. #end recieve
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement