Mummia-99 commited on
Commit
3e847ba
·
verified ·
1 Parent(s): cbe7efb

Create server.py

Browse files
Files changed (1) hide show
  1. server.py +48 -0
server.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, jsonify
2
+ import imaplib
3
+ import email
4
+ import os
5
+
6
+ app = Flask(__name__)
7
+
8
+ EMAIL = "your_email@gmail.com"
9
+ PASSWORD = "your_app_password" # Use App Password if 2FA is enabled
10
+
11
+ def fetch_latest_email():
12
+ try:
13
+ mail = imaplib.IMAP4_SSL("imap.gmail.com")
14
+ mail.login(EMAIL, PASSWORD)
15
+ mail.select("inbox")
16
+
17
+ result, data = mail.search(None, "ALL")
18
+ email_ids = data[0].split()
19
+
20
+ if not email_ids:
21
+ return "No emails found."
22
+
23
+ latest_email_id = email_ids[-1]
24
+ result, data = mail.fetch(latest_email_id, "(RFC822)")
25
+
26
+ raw_email = data[0][1]
27
+ msg = email.message_from_bytes(raw_email)
28
+
29
+ email_content = ""
30
+ if msg.is_multipart():
31
+ for part in msg.walk():
32
+ if part.get_content_type() == "text/plain":
33
+ email_content = part.get_payload(decode=True).decode()
34
+ else:
35
+ email_content = msg.get_payload(decode=True).decode()
36
+
37
+ return email_content.strip()
38
+
39
+ except Exception as e:
40
+ return f"Error: {str(e)}"
41
+
42
+ @app.route("/get-latest-email", methods=["GET"])
43
+ def get_email():
44
+ email_content = fetch_latest_email()
45
+ return jsonify({"email": email_content})
46
+
47
+ if __name__ == "__main__":
48
+ app.run(port=5000, debug=True)