NAME : SAGAR PRALHAD DHAKE
ROLL NO: 24
TITLE : SPAM EMAIL DETECTOR
BATCH : B2
CLASS : FYMCA
import imaplib, email
from [Link] import decode_header
import tkinter as tk
from tkinter import simpledialog, messagebox
keywords = ["free money", "winner", "congratulations", "urgent", "credit card", "click
here", "lottery", "act now", "limited offer", "100% free", "guaranteed", "investment", "no
cost", "extra cash", "apply now", "cash bonus", "fast cash", "cheap meds", "meet
singles", "risk-free", "earn per week", "work from home", "get paid", "miracle", "exclusive
deal", "urgent response", "pre-approved", "access now", "trial offer", "join my class",
"let's have fun", "interview"]
def is_spam(text): return any(k in [Link]() for k in keywords)
def fetch_emails(user, pwd, limit):
try:
imap = imaplib.IMAP4_SSL("[Link]"); [Link](user, pwd);
[Link]("inbox")
_, msgs = [Link](None, "ALL"); ids = msgs[0].split()[-limit:]
for i in ids:
_, data = [Link](i, "(RFC822)")
msg = email.message_from_bytes(data[0][1])
subj = "".join([[Link](enc or "utf-8") if isinstance(p, bytes) else p for p, enc in
decode_header(msg["subject"] or "")])
body = ""
for part in [Link]():
if part.get_content_type() == "text/plain" and not [Link]('Content-
Disposition'):
body = part.get_payload(decode=True).decode(errors="ignore"); break
result = "SPAM" if is_spam(subj + " " + body) else "HAM"
print(f"[{result}] {[Link]('date')} | {subj}\n{body[:60].strip()}...\n" + "-"*60)
[Link]()
except Exception as e: [Link]("Error", str(e))
def gui():
r = [Link](); [Link]()
u = [Link]("Email", "Gmail:"); p = [Link]("Password",
"App Password:", show="*")
l = [Link]("Limit", "How many emails?", minvalue=1)
if u and p and l: fetch_emails(u, p, l)
gui()
OUTPUT:-
Enter email:-
Enter password:-
Enter limit of email:-