Guest User

Untitled

a guest
Jan 10th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.14 KB | None | 0 0
  1. import socket
  2. import ssl
  3. import sys
  4. from base64 import *
  5. from email.header import decode_header,make_header
  6. import quopri
  7.  
  8. host,port = "pop.gmail.com",995 #Read mail
  9. host1,port1 = "smtp.gmail.com",465 #Send mail
  10. buffer = 4096
  11.  
  12. username = b"komangclone1@gmail.com"
  13. password = b"thao081196"
  14.  
  15. s = socket.socket()
  16.  
  17. #Check connection
  18. try:
  19. s = ssl.wrap_socket(s)
  20. s.connect((host,port))
  21. except:
  22. print("Cant connect, check your connection!")
  23. sys.exit(0)
  24. print("Connect successfully!!")
  25. s.recv(buffer)
  26.  
  27. #Check username
  28. s.send(b'user ' + username + b'\n')
  29. mess = s.recv(buffer)
  30. if b"OK" in mess:
  31. #Check password
  32. s.send(b'pass ' + password + b'\n')
  33. mess = s.recv(buffer)
  34. if b"OK" in mess:
  35. print("Login successfully")
  36. else:
  37. print("Wrong password")
  38. sys.exit(0)
  39. else:
  40. print("Wrong username or your account is not in database")
  41. while(1):
  42. print("MENU")
  43. print("1. Send message")
  44. print("2. List mail")
  45. print("3. Read mail")
  46. print("4. Exit")
  47.  
  48. try:
  49. choice = int(input("Your choice: "))
  50. except:
  51. print("Watch your input")
  52. if choice == 1:
  53. ss = socket.socket()
  54. try:
  55. ss = ssl.wrap_socket(ss)
  56. ss.connect((host1,port1))
  57. #Send EHLO to identify who you are
  58. ss.send(b'HELO localhost\r\n' + b'\n')
  59. ss.recv(buffer)
  60. #Login
  61. ss.send(b'AUTH LOGIN' + b'\n')
  62. ss.recv(buffer)
  63. #send username
  64. ss.send(b64encode(username) + b'\n')
  65. ss.recv(buffer)
  66. #send password
  67. ss.send(b64encode(password) + b'\n')
  68. ss.recv(buffer)
  69. #Send mail
  70. ss.send(b'MAIL FROM: <' + username + b'>\n')
  71. ss.recv(buffer)
  72. #Send to
  73. send_to = input("Who you want to send to: ").strip().encode()
  74. ss.send(b'RCPT TO: <' + send_to + b'>\n')
  75. ss.recv(buffer)
  76. #Data
  77. ss.send(b'' + b'\n')
  78. ss.recv(buffer)
  79. ss.send(b'DATA' + b'\n')
  80. ss.recv(buffer)
  81. subject = input("Your subject: ").strip().encode() + b'\r\n'
  82. ss.send(b'Subject: ' + subject + b'\n')
  83. content = input("Content: ").strip().encode() + b'\r\n'
  84. ss.send(content + b'.\r\n' + b'\n')
  85. mess = ss.recv(buffer)
  86. if b'250' in mess:
  87. print("Send successfully!!")
  88. ss.close()
  89. else:
  90. print("Cant send mail, maybe something wrong!")
  91. ss.close()
  92. except:
  93. print("Cant connect to the server!")
  94. sys.exit(0)
  95.  
  96. #List mail
  97. elif choice == 2:
  98. #Send LIST command
  99. s.send(b"LIST" + b"\n")
  100. mess = s.recv(buffer)
  101. print(b"You have " + mess.split(b"OK ")[1].split(b"\r\n")[0])
  102. num = int(mess.split(b"OK ")[1].split(b" messages")[0])
  103. for stt in range(1,num + 1):
  104. have_subject = 0
  105. content = b''
  106. s.send(b"RETR " + str(stt).encode() + b'\n')
  107. mess = s.recv(buffer)
  108. while b".\r\n" not in mess:
  109. content += mess
  110. mess = s.recv(buffer)
  111. content = content.split(b"\r\n")
  112. text = b''
  113. for i in range(len(content)):
  114. if b"Subject: " in content[i]:
  115. have_subject = 1
  116. text+= content[i]
  117. k = i + 1
  118. while b'\t' in content[k]:
  119. text+= content[k].strip()
  120. k = k + 1
  121.  
  122. h = make_header(decode_header(str(text)))
  123. h = str(h)
  124. print( str(stt) + ". " + h)
  125. if ( have_subject == 0 ):
  126. print(str(stt) + ". " + "No Subject")
  127. #Read mail
  128. elif choice == 3:
  129. k = input("Choose email to read: ")
  130. s.send(b"RETR " + str(k).encode() + b"\n")
  131. content = b''
  132. mess = s.recv(buffer)
  133. while b".\r\n" not in mess:
  134. content += mess
  135. mess = s.recv(buffer)
  136. content = content.split(b'\r\n')
  137. for i in range(len(content)):
  138. #Date/Receive/Message ID
  139. if b"Date:" in content[i]:
  140. print(content[i])
  141. elif b"Receive:" in content[i]:
  142. print(content[i])
  143. elif b"MIME-Version" in content[i]:
  144. print(content[i])
  145. elif b"Message-ID:" in content[i]:
  146. print(content[i])
  147. elif b"Receive:" in content[i]:
  148. print(content[i])
  149. #Header
  150. elif b"Subject:" in content[i]:
  151. text = b''
  152. text+= content[i]
  153. k = i + 1
  154. while b'\t' in content[k]:
  155. text+= content[k].strip()
  156. k = k + 1
  157. h = make_header(decode_header(str(text)))
  158. print(str(h))
  159. elif b"From:" in content[i]:
  160. h = make_header(decode_header(str(content[i])))
  161. print(str(h))
  162. elif b"To:" in content[i]:
  163. h = make_header(decode_header(str(content[i])))
  164. print(str(h))
  165. #MIME type
  166. elif b"Content-Transfer-Encoding: base64" in content[i] and b"Content-Type: text/plain; charset=\"UTF-8\"" in content[i-1]:
  167. text = b''
  168. k = i + 2
  169. while b"--" not in content[k]:
  170. text+= content[k]
  171. k = k + 1
  172. print(content[i-1])
  173. #print(b64decode(text).decode('UTF-8'))
  174. # elif b"Content-Transfer-Encoding: quoted-printable" in content[i]:
  175. # text = b''
  176. # k = i + 2
  177. # while b"--" not in content[k] :
  178. # text+= content[k]
  179. # k = k + 1
  180. # if k == len(content):
  181. # break
  182. # print(content[i-1])
  183. # print(quopri.decodestring(text))
  184. #File handle
  185. elif b"Content-Disposition: attachment" in content[i]:
  186. name = content[i].split(b"filename=\"")[1].split(b"\"")[0]
  187. text = b''
  188. k = i + 3
  189.  
  190. while b"--" not in content[k]:
  191. text+= content[k].strip()
  192. k = k + 1
  193. if ( k == len(content) ):
  194.  
  195. break
  196. #print(text)
  197.  
  198. #f = open(str(name),'wb').write(b64decode(text + b"=="))
  199. #print(text)
  200. print(content[i])
  201.  
  202.  
  203. #Exit
  204. elif choice == 4:
  205. s.close()
  206. print("Tks for using our service")
  207. break
  208. else:
  209. print("Wrong command")
Add Comment
Please, Sign In to add comment