Simple POP3 Class


/ Published in: Python
Save to your folder(s)

The following code snippet shows a basic POP3 class, which can get the total message count and message details.


Copy this code and paste it in your HTML
  1. import socket;
  2.  
  3. class POP3():
  4.  
  5. def __init__(self, host, port):
  6. self.log = ""
  7. ip = socket.gethostbyname(host)
  8. self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  9. result = self.sock.connect_ex((ip, port))
  10.  
  11. if result == 0:
  12. self.log += "GREETING: " +self.read_line()
  13.  
  14. def credentials(self, username, password):
  15. self.send_cmd("USER " + username + "\n")
  16. self.log += "USER: " + self.read_line()
  17.  
  18. self.send_cmd("PASS " + password + "\n")
  19. self.log += "PASS: " + self.read_line()
  20.  
  21. def msg_count(self):
  22. self.send_cmd("STAT\n")
  23. response = self.read_line()
  24. parts = response.split(" ")
  25. return parts[1]
  26.  
  27. def get_message(self, msg_index):
  28. message = POP3Message()
  29. self.send_cmd("RETR " + str(msg_index) + "\n")
  30. parts = self.read_line().split(" ")
  31.  
  32. if parts[0] == "+OK":
  33. size = int(parts[1])
  34. data = ""
  35.  
  36. while len(data) < size:
  37. data += self.sock.recv(1024).decode("utf-8")
  38.  
  39. lines = data.split("\n")
  40. headers = {}
  41. is_header = True
  42. body = ""
  43.  
  44. for line in lines:
  45. if line.strip() != "" and is_header == True:
  46. key_value_header = line.strip().split(":", 1)
  47. if len(key_value_header) == 2:
  48. headers[key_value_header[0].strip()] = key_value_header[1].strip()
  49. else:
  50. is_header = False
  51.  
  52. if is_header == False:
  53. body += line
  54.  
  55. message.set_from(headers["From"])
  56. message.set_to(headers["To"])
  57. message.set_subject(headers["Subject"])
  58. message.set_date(headers["Date"])
  59. message.set_body(body)
  60. return message
  61.  
  62. def get_log(self):
  63. return self.log
  64.  
  65. def send_cmd(self, cmd):
  66. self.sock.sendall(bytes(cmd, "UTF-8"))
  67.  
  68. def read_line(self):
  69. return self.sock.recv(1024).decode("utf-8")
  70.  
  71. class POP3Message():
  72. def set_from(self, fromAddr):
  73. self.fromAddr = fromAddr
  74.  
  75. def get_from(self):
  76. return self.fromAddr
  77.  
  78. def set_to(self, to):
  79. self.to = to
  80.  
  81. def get_to(self):
  82. return self.to
  83.  
  84. def set_subject(self, subject):
  85. self.subject = subject
  86.  
  87. def get_subject(self):
  88. return self.subject
  89.  
  90. def set_date(self, date):
  91. self.date = date
  92.  
  93. def get_date(self):
  94. return self.date
  95.  
  96. def set_body(self, body):
  97. self.body = body
  98.  
  99. def get_body(self):
  100. return self.body
  101.  
  102.  
  103. pop_client = POP3("pop.host.com", 110)
  104. pop_client.credentials("username", "password")
  105.  
  106. print("Message count:" + pop_client.msg_count())
  107.  
  108. message = pop_client.get_message(1)
  109. print("From: " + message.get_from())
  110. print("To: " + message.get_to())
  111. print("Subject: " + message.get_subject())
  112. print("Date: " + message.get_date())
  113. print("Body: " + message.get_body())
  114. print(pop_client.get_log())

URL: http://www.hostprojects.net/snippets/python/147/simple-pop3-class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.