Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import ssl
- import sys
- from base64 import *
- from email.header import decode_header,make_header
- import quopri
- host,port = "pop.gmail.com",995 #Read mail
- host1,port1 = "smtp.gmail.com",465 #Send mail
- buffer = 4096
- username = b"komangclone1@gmail.com"
- password = b"thao081196"
- s = socket.socket()
- #Check connection
- try:
- s = ssl.wrap_socket(s)
- s.connect((host,port))
- except:
- print("Cant connect, check your connection!")
- sys.exit(0)
- print("Connect successfully!!")
- s.recv(buffer)
- #Check username
- s.send(b'user ' + username + b'\n')
- mess = s.recv(buffer)
- if b"OK" in mess:
- #Check password
- s.send(b'pass ' + password + b'\n')
- mess = s.recv(buffer)
- if b"OK" in mess:
- print("Login successfully")
- else:
- print("Wrong password")
- sys.exit(0)
- else:
- print("Wrong username or your account is not in database")
- while(1):
- print("MENU")
- print("1. Send message")
- print("2. List mail")
- print("3. Read mail")
- print("4. Exit")
- try:
- choice = int(input("Your choice: "))
- except:
- print("Watch your input")
- if choice == 1:
- ss = socket.socket()
- try:
- ss = ssl.wrap_socket(ss)
- ss.connect((host1,port1))
- #Send EHLO to identify who you are
- ss.send(b'HELO localhost\r\n' + b'\n')
- ss.recv(buffer)
- #Login
- ss.send(b'AUTH LOGIN' + b'\n')
- ss.recv(buffer)
- #send username
- ss.send(b64encode(username) + b'\n')
- ss.recv(buffer)
- #send password
- ss.send(b64encode(password) + b'\n')
- ss.recv(buffer)
- #Send mail
- ss.send(b'MAIL FROM: <' + username + b'>\n')
- ss.recv(buffer)
- #Send to
- send_to = input("Who you want to send to: ").strip().encode()
- ss.send(b'RCPT TO: <' + send_to + b'>\n')
- ss.recv(buffer)
- #Data
- ss.send(b'' + b'\n')
- ss.recv(buffer)
- ss.send(b'DATA' + b'\n')
- ss.recv(buffer)
- subject = input("Your subject: ").strip().encode() + b'\r\n'
- ss.send(b'Subject: ' + subject + b'\n')
- content = input("Content: ").strip().encode() + b'\r\n'
- ss.send(content + b'.\r\n' + b'\n')
- mess = ss.recv(buffer)
- if b'250' in mess:
- print("Send successfully!!")
- ss.close()
- else:
- print("Cant send mail, maybe something wrong!")
- ss.close()
- except:
- print("Cant connect to the server!")
- sys.exit(0)
- #List mail
- elif choice == 2:
- #Send LIST command
- s.send(b"LIST" + b"\n")
- mess = s.recv(buffer)
- print(b"You have " + mess.split(b"OK ")[1].split(b"\r\n")[0])
- num = int(mess.split(b"OK ")[1].split(b" messages")[0])
- for stt in range(1,num + 1):
- have_subject = 0
- content = b''
- s.send(b"RETR " + str(stt).encode() + b'\n')
- mess = s.recv(buffer)
- while b".\r\n" not in mess:
- content += mess
- mess = s.recv(buffer)
- content = content.split(b"\r\n")
- text = b''
- for i in range(len(content)):
- if b"Subject: " in content[i]:
- have_subject = 1
- text+= content[i]
- k = i + 1
- while b'\t' in content[k]:
- text+= content[k].strip()
- k = k + 1
- h = make_header(decode_header(str(text)))
- h = str(h)
- print( str(stt) + ". " + h)
- if ( have_subject == 0 ):
- print(str(stt) + ". " + "No Subject")
- #Read mail
- elif choice == 3:
- k = input("Choose email to read: ")
- s.send(b"RETR " + str(k).encode() + b"\n")
- content = b''
- mess = s.recv(buffer)
- while b".\r\n" not in mess:
- content += mess
- mess = s.recv(buffer)
- content = content.split(b'\r\n')
- for i in range(len(content)):
- #Date/Receive/Message ID
- if b"Date:" in content[i]:
- print(content[i])
- elif b"Receive:" in content[i]:
- print(content[i])
- elif b"MIME-Version" in content[i]:
- print(content[i])
- elif b"Message-ID:" in content[i]:
- print(content[i])
- elif b"Receive:" in content[i]:
- print(content[i])
- #Header
- elif b"Subject:" in content[i]:
- text = b''
- text+= content[i]
- k = i + 1
- while b'\t' in content[k]:
- text+= content[k].strip()
- k = k + 1
- h = make_header(decode_header(str(text)))
- print(str(h))
- elif b"From:" in content[i]:
- h = make_header(decode_header(str(content[i])))
- print(str(h))
- elif b"To:" in content[i]:
- h = make_header(decode_header(str(content[i])))
- print(str(h))
- #MIME type
- elif b"Content-Transfer-Encoding: base64" in content[i] and b"Content-Type: text/plain; charset=\"UTF-8\"" in content[i-1]:
- text = b''
- k = i + 2
- while b"--" not in content[k]:
- text+= content[k]
- k = k + 1
- print(content[i-1])
- #print(b64decode(text).decode('UTF-8'))
- # elif b"Content-Transfer-Encoding: quoted-printable" in content[i]:
- # text = b''
- # k = i + 2
- # while b"--" not in content[k] :
- # text+= content[k]
- # k = k + 1
- # if k == len(content):
- # break
- # print(content[i-1])
- # print(quopri.decodestring(text))
- #File handle
- elif b"Content-Disposition: attachment" in content[i]:
- name = content[i].split(b"filename=\"")[1].split(b"\"")[0]
- text = b''
- k = i + 3
- while b"--" not in content[k]:
- text+= content[k].strip()
- k = k + 1
- if ( k == len(content) ):
- break
- #print(text)
- #f = open(str(name),'wb').write(b64decode(text + b"=="))
- #print(text)
- print(content[i])
- #Exit
- elif choice == 4:
- s.close()
- print("Tks for using our service")
- break
- else:
- print("Wrong command")
Add Comment
Please, Sign In to add comment