how can i receive email content, save it in a file and delete it, using the POP3 protocol, using sockets

96 Views Asked by At

I want to receive email content using the POP3 protocol, but using sockets only. everything works fine, I can register into my Gmail account using the USER, PASS commands, when I use the RETR command, the server gives me the response: (+OK message follows). but afterward the terminal goes blink.

My code:

import socket
import ssl
import email
from email.header import decode_header
from cryptography.fernet import Fernet

# Decryption of the password
with open("mySecretKey.Key", "rb") as file:
    key = file.read()
f = Fernet(key)

# encryptedPassword = ''  
with open('password.txt', "rb") as file:
    encryptedPassword = file.read()
    
decryptedPassword = f.decrypt(encryptedPassword).decode() 

# Specify server, port, and email address
server = "pop.gmail.com"
port = 995
user = "[email protected]"
password = decryptedPassword

# Create a socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = ssl.wrap_socket(sock, ssl_version=ssl.PROTOCOL_TLS)

# Connect to the server
sock.connect((server, port))

# Get server's response
response = sock.recv(1024).decode()
print(response)

# Send USER command
sock.send(("USER " + user + "\r\n").encode())
print("Sent: USER " + user)

# Get server's response
response = sock.recv(1024).decode()
print(response)

# Send PASS command
sock.send(("PASS " + password + "\r\n").encode())
print("Sent: PASS *****")

# Get server's response
response = sock.recv(1024).decode()
print(response)

# Send STAT command
sock.send("STAT\r\n".encode())
print("Sent: STAT")

# Get server's response
response = sock.recv(1024).decode()
print(response)

# Send LIST command
sock.send("LIST\r\n".encode())
print("Sent: LIST")

# Get server's response
response = sock.recv(1024).decode()
print(response)

# Send RETR command
sock.send("RETR 1\r\n".encode())
print("Sent: RETR 1")

# Get server's response
response = sock.recv(1024).decode()
print(response)

# Receive the email content
message = b""
while True:
    part = sock.recv(1024)
    if not part:
        break
    message += part

# Parse the email content
msg = email.message_from_bytes(message)
print("Subject: " + str(decode_header(msg["Subject"])))
print("From: " + str(decode_header(msg["From"])))
print("Date: " + str(decode_header(msg["Date"])))

# Save email content to a file
with open("email_content.txt", "wb") as file:
    file.write(msg.get_payload(decode=True))

sock.send("DELE 1\r\n".encode())
print("Sent: DELE 1")

sock.send("NOOP\r\n".encode())
print("Sent: NOOP ")

sock.send("RSET\r\n".encode())
print("Sent: RSET ")

sock.send("TOP 1 2\r\n".encode())
print("Sent: NOOP 1 2")

# Send QUIT command
sock.send("QUIT\r\n".encode())
print("Sent: QUIT")

# Get server's response
response = sock.recv(1024).decode()
print(response)

# Close the socket
sock.close()

I checked the email settings an enabled the pop3 protocol settings, but still it does not work.

0

There are 0 best solutions below