forked from imperd1x/SumMail
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
153 lines (124 loc) · 5.07 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import email
from plistlib import UID # This import is unused and can be removed
import openai
import imaplib
import requests
import html2text
import smtplib, ssl
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from dotenv import load_dotenv
from email.header import decode_header
def fetch_emails_from_sender():
try:
# Load environment variables from .env file
load_dotenv()
# Email account credentials
IMAP_SERVER = os.getenv('IMAP_SERVER')
IMAP_USERNAME = os.getenv('IMAP_USERNAME')
IMAP_PASSWORD = os.getenv('IMAP_PASSWORD')
# Sender
SENDER_EMAIL = os.getenv('SENDER_EMAIL')
# Connect to the IMAP server
mail = imaplib.IMAP4_SSL(IMAP_SERVER, 993)
mail.login(IMAP_USERNAME, IMAP_PASSWORD)
mail.select("inbox")
status, response = mail.search(None, "UNSEEN FROM", SENDER_EMAIL)
email_ids = response[0].split()
emails = []
for email_id in email_ids:
# Fetch the email
status, response = mail.fetch(email_id, "(RFC822)")
raw_email = response[0][1]
# Parse the email content using the email module
email_message = email.message_from_bytes(raw_email)
# Extract the subject and decode it
subject = decode_header(email_message["Subject"])[0][0]
if isinstance(subject, bytes):
# If it's bytes, decode to string
subject = subject.decode()
# Extract the text content of the email
body = ""
for part in email_message.walk():
content_type = part.get_content_type()
if content_type == "text/plain":
charset = part.get_content_charset()
body += str(part.get_payload(decode=True), str(charset), "ignore")
# Convert only to Text and remove html
body = html2text.html2text(body)
# Split the email body into chunks of 2037 tokens (max 2048)
chunk_size = 2037
chunks = [body[i:i+chunk_size] for i in range(0, len(body), chunk_size)]
emails.append((subject, chunks))
mail.store(email_id, '+X-GM-LABELS', '\\Trash')
# mail.expunge()
return emails
except Exception as e:
print(f"An error occurred: {e}")
return []
def summarize_chunks(chunks):
try:
# Set up the OpenAI API client
openai.api_key = os.getenv('OPENAI_API_KEY')
# Set up the API endpoint
openai_url = "https://api.openai.com/v1/chat/completions"
# Set up the request headers
headers = {
"Authorization": f"Bearer {openai.api_key}",
"Content-Type": "application/json",
}
# Summarize each chunk using OpenAI's gpt-4 model
summaries = []
for chunk in chunks:
messages = [
{"role": "system", "content": "Your job is to summarize the content of email newsletters. Highlight the main points, important updates, and key takeaways."},
{"role": "user", "content": f"Summarize the content of this email into bullet points: {chunk}"}
]
data = {
"model": "gpt-4o",
"messages": messages,
"temperature": 0.7,
"max_tokens": 1000
}
# Make the HTTP request
response = requests.post(openai_url, headers=headers, json=data)
# Extract the summary from the response
summary = response.json()['choices'][0]['message']['content'].strip()
summaries.append(summary)
# Join the bullet points into a single string
bullet_list = "\n- ".join(summaries)
return bullet_list
except Exception as e:
print(f"An error occurred: {e}")
def main():
emails = fetch_emails_from_sender()
if not emails:
print("No emails found from the specified sender.")
return
for subject, chunks in emails:
bullet_list = summarize_chunks(chunks)
if not bullet_list:
print(f"Failed to summarize email with subject: {subject}")
continue
# Print the subject and bullet list
print("Subject: " + subject)
print("Summary: \n- " + bullet_list)
print()
SMTP_SERVER = os.getenv('SMTP_SERVER')
PORT = 465
EMAIL = os.getenv('IMAP_USERNAME')
EMAIL_PASSWORD = os.getenv('EMAIL_PASSWORD')
# Create email message
message = MIMEMultipart()
message["From"] = EMAIL
message["To"] = EMAIL
message["Subject"] = f"Summary of: {subject}"
message.attach(MIMEText(f"Summary of email:\n- {bullet_list}", "plain"))
# Send email using SMTP
context = ssl.create_default_context()
with smtplib.SMTP_SSL(SMTP_SERVER, PORT, context=context) as server:
server.login(EMAIL, EMAIL_PASSWORD)
server.sendmail(EMAIL, EMAIL, message.as_string())
if __name__ == "__main__":
main()