-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmail.py
More file actions
426 lines (349 loc) · 17.5 KB
/
mail.py
File metadata and controls
426 lines (349 loc) · 17.5 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
import re
import smtplib
import ssl
from email.message import EmailMessage
import imaplib
import email
from email import policy
import os
from dotenv import load_dotenv
from colorama import Fore, init
# Initialize colorama
init()
# Native Imports
from kun import known_user_names
# Load environment variables from .env file
load_dotenv()
# Email credentials
EMAIL = os.getenv("SENDER_EMAIL")
PASSWORD = os.getenv("SENDER_PASSWORD")
# Establish an SSL context for secure communication
ssl_context = ssl.create_default_context()
def send_mail(prompt):
"""
Parses the prompt to extract email addresses, subject, and message body, then sends the email(s).
Returns a tuple (success, message), where success is True if emails were sent, False otherwise.
"""
emails = []
subject = None
body = None
# 1. Extract Email Addresses and ARPA IDs
emails = re.findall(r'[\w\.-]+@[\w\.-]+', prompt)
arpa_ids = re.findall(r'\b([A-Z]\d{3})\b', prompt) # Matches patterns like R001, A001, etc.
# 2. Map Known Contact Names and ARPA IDs to Emails
known_contacts = {}
for user, details in known_user_names.items():
if details['mail']: # Check if mail is not empty
# Add full name
known_contacts[details['full_name'].lower()] = details['mail']
# Add call name
known_contacts[details['call_name'].lower()] = details['mail']
# Add ARPA ID
known_contacts[details['arpa_id']] = details['mail']
# Replace names and ARPA IDs with emails
for identifier, email in known_contacts.items():
if re.search(r'\b' + re.escape(identifier) + r'\b', prompt, re.IGNORECASE):
emails.append(email)
# Add emails from ARPA IDs
for arpa_id in arpa_ids:
for user, details in known_user_names.items():
if details['arpa_id'] == arpa_id and details['mail']:
emails.append(details['mail'])
# Remove Duplicate Emails
emails = list(set(emails))
# 3. Validate Number of Recipients
if len(emails) == 0:
return False, "Error: No valid email addresses or known contacts found in the prompt."
if len(emails) > 5:
return False, "Error: Cannot send to more than 5 email addresses at once."
# 4. Define Keywords for Subject and Body
subject_pattern = r'(subject)\s*([\'"])(.*?)\2'
body_pattern = r'(body|content|message)\s*([\'"])(.*?)\2'
# 5. Search for Subject and Body
subject_match = re.search(subject_pattern, prompt, re.IGNORECASE)
if subject_match:
subject = subject_match.group(3)
body_match = re.search(body_pattern, prompt, re.IGNORECASE)
if body_match:
body = body_match.group(3)
# Fallback for Subject or Body if not explicitly found
if not subject or not body:
quotes = re.findall(r'(["\'])(.*?)\1', prompt)
if not subject and len(quotes) > 1:
subject = quotes[1][1]
if not body and quotes:
body = quotes[0][1]
if not subject:
return False, "Error: No subject found in the prompt."
if not body:
return False, "Error: No message body found in the prompt."
# HTML Signature
signature_html = """
<div style="font-family: 'Courier New', monospace; color: #0000FF;">
<!-- Divider symbol -->
<p style="font-size: 12px; color: #0000FF; margin: 8px 0; text-align: left;">~</p>
<!-- ASCII Logo with reduced font size for compact display -->
<pre style="font-size: 10px; line-height: 1; margin: 0; text-align: center;">
██████ ██████ ███████ ██ ██ ███████
██ ██ ██ ██ ██ ██ ██ ██
██ ██ ██████ ███████ ██ ██ █████
██ ██ ██ ██ ██ ██ ██
██████ ██ ███████ ██ ██ ███████
</pre>
<!-- Header text in 12px font size, no max-width restriction -->
<p style="font-size: 12px; color: #0000FF; margin: 8px 0; text-align: center;">
A Self-Centered Intelligence (SCI) Prototype<br>
By ARPA HELLENIC LOGICAL SYSTEMS<br>
Version: 0.3.79 XP | 01 JUL 2025
</p>
<!-- Divider symbol -->
<p style="font-size: 12px; color: #0000FF; margin: 8px 0; text-align: center;">~</p>
<!-- Disclaimer section with left alignment and smaller font size, no width restriction -->
<p style="font-size: 10px; color: grey; font-style: italic; text-align: left; margin: 0 auto 10px;">
This message was generated and disseminated by Non-Humanoid Intelligence Units (NHUIs) operating under the auspices of ARPA Corporation.
All email accounts managed by ARPA's SCI, AA, NHUI, and AI models are part of experimental frameworks and are intended solely for demonstration and authorized operational purposes.
</p>
<p style="font-size: 11px; color: grey; font-style: italic; text-align: left; margin: 0 auto;">
Unauthorized receipt or distribution of this email is strictly prohibited. If you have received this communication in error or are not the intended recipient,
you are hereby instructed to permanently delete this email from all systems and notify ARPA Hellenic Logical Systems immediately.
Sharing, copying, or distributing the contents of this email without explicit written consent from ARPA Corporation is forbidden
and may result in disciplinary action.
</p>
</div>
"""
# Combine message body with the signature
html_body = f"<div>{body}</div><br><br>{signature_html}"
try:
# Email configuration
sender_email = EMAIL
sender_password = PASSWORD
if not sender_email or not sender_password:
return False, "Error: Email credentials not set in environment variables."
# Create a secure SSL context
context = ssl.create_default_context()
# Create the Email Message
msg = EmailMessage()
msg['From'] = f'"Opsie by ARPA" <{sender_email}>'
msg['To'] = ', '.join(emails)
msg['Subject'] = subject
msg.set_content(body) # Plain text fallback
msg.add_alternative(html_body, subtype='html') # HTML version with signature
# Send the email
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=context) as server:
server.login(sender_email, sender_password)
server.send_message(msg)
return True, f"Email sent successfully to {', '.join(emails)}."
except Exception as e:
return False, f"Error sending email: {str(e)}"
def fetch_unread_primary_emails():
"""
Fetches the latest 10 unread emails from the primary inbox only.
Each email dictionary includes sender, subject, date, and read/unread status.
"""
inbox = []
with imaplib.IMAP4_SSL('imap.gmail.com') as mail:
mail.login(EMAIL, PASSWORD)
mail.select("inbox") # Open the primary inbox
# Fetch only unread emails in the inbox
result, data = mail.search(None, 'UNSEEN')
email_ids = data[0].split()[-10:] # Get up to the last 10 unread emails
for i, email_id in enumerate(reversed(email_ids), 1):
result, msg_data = mail.fetch(email_id, "(RFC822)")
raw_msg = msg_data[0][1]
msg = email.message_from_bytes(raw_msg, policy=policy.default)
# Parse email details for display
sender = email.utils.parseaddr(msg['From'])[1]
subject = msg.get("Subject", "No Subject")
date = msg.get("Date")
inbox.append({
'index': i,
'sender': sender,
'subject': subject,
'date': date,
'id': email_id,
'unread': True # We are fetching only unread emails
})
return inbox
def extract_body(msg):
"""
Extracts the body from a message, checking for both plain text and HTML parts.
"""
# Check if the email is multipart
if msg.is_multipart():
# Loop through each part and find plain text or HTML
for part in msg.iter_parts():
if part.get_content_type() == "text/plain" and part.get_content_disposition() != "attachment":
return part.get_payload(decode=True).decode()
elif part.get_content_type() == "text/html" and not part.get_content_disposition() == "attachment":
# If we don't find plain text, return HTML (with a warning)
html_body = part.get_payload(decode=True).decode()
return strip_html_tags(html_body)
else:
# If it's not multipart, just return the raw text (we assume it's plain text)
return msg.get_payload(decode=True).decode()
return "No readable content found"
def strip_html_tags(html):
"""
Strips HTML tags from a string and returns plain text.
"""
clean = re.compile("<.*?>")
return re.sub(clean, "", html)
def display_unread_inbox(inbox):
"""
Displays a list of unread emails in the inbox with DNA report-style formatting.
"""
unread_count = len(inbox)
print(Fore.LIGHTCYAN_EX + "\n" + "═" * 80)
print(Fore.LIGHTCYAN_EX + """
██ █▄ █ ▀█▀ █▀▀ █▀█ █▀█ █ ▄▀█ █▄ █ █▀▀ ▀█▀ ▄▀█ █▀█ █▄█
█▄ █ ▀█ █ ██▄ █▀▄ █▀▀ █▄▄ █▀█ █ ▀█ ██▄ █ █▀█ █▀▄ █
█▀▄▀█ ▄▀█ █ █ █▀ █▀▀ █▀█ █ █ █ █▀▀ █▀▀
█ ▀ █ █▀█ █ █▄▄ ▄█ ██▄ █▀▄ ▀▄▀ █ █▄▄ ██▄
v0.3.79 XP | ARPA CORP, 2025
""")
print(Fore.LIGHTCYAN_EX + "═" * 80)
print(Fore.LIGHTGREEN_EX + "\n[IMS] Primary Inbox Status:")
print(Fore.LIGHTGREEN_EX + f"[IMS] Unread Messages: {unread_count}")
print(Fore.LIGHTGREEN_EX + "[IMS] Temporal Window: Last 10 Messages\n")
print(Fore.LIGHTCYAN_EX + "═" * 80)
print(Fore.WHITE + " # | STATUS | SENDER | SUBJECT | DATE")
print(Fore.LIGHTCYAN_EX + "═" * 80)
for email_info in inbox:
status = Fore.LIGHTYELLOW_EX + "[UNREAD]" if email_info['unread'] else Fore.GREEN + "[READ]"
# Truncate long fields for better formatting
sender = email_info['sender'][:20] + '...' if len(email_info['sender']) > 20 else email_info['sender']
subject = email_info['subject'][:20] + '...' if len(email_info['subject']) > 20 else email_info['subject']
print(f"{Fore.WHITE}{email_info['index']:3d} | {status:8} | {Fore.WHITE}{sender:20} | {subject:20} | {email_info['date']}")
print(Fore.LIGHTCYAN_EX + "─" * 80)
print(Fore.LIGHTYELLOW_EX + "\n[IMS] Available Commands:")
print(Fore.WHITE + "• Enter message number to read.")
print(Fore.WHITE + "• Type 'exit' to return to main interface")
print(Fore.LIGHTCYAN_EX + "═" * 80 + "\n")
def read_email(inbox, email_index):
"""
Displays the selected email with DNA report-style formatting.
"""
selected_email = inbox[email_index - 1]
with imaplib.IMAP4_SSL('imap.gmail.com') as mail:
mail.login(EMAIL, PASSWORD)
mail.select("inbox")
result, msg_data = mail.fetch(selected_email['id'], "(RFC822)")
raw_msg = msg_data[0][1]
msg = email.message_from_bytes(raw_msg, policy=policy.default)
sender_name, sender_email = email.utils.parseaddr(msg['From'])
subject = msg.get("Subject", "No Subject")
date = msg.get("Date")
body = extract_body(msg)
print(Fore.LIGHTCYAN_EX + "\n" + "═" * 80)
print(Fore.LIGHTGREEN_EX + "[MESSAGE DETAILS]")
print(Fore.LIGHTCYAN_EX + "═" * 80)
print(Fore.LIGHTYELLOW_EX + "FROM:")
print(Fore.WHITE + f"{sender_name} <{sender_email}>")
print(Fore.LIGHTYELLOW_EX + "\nSUBJECT:")
print(Fore.WHITE + subject)
print(Fore.LIGHTYELLOW_EX + "\nDATE:")
print(Fore.WHITE + date)
print(Fore.LIGHTCYAN_EX + "\n" + "─" * 80)
print(Fore.LIGHTYELLOW_EX + "CONTENT:")
print(Fore.WHITE + body)
print(Fore.LIGHTCYAN_EX + "\n" + "═" * 80)
print(Fore.LIGHTYELLOW_EX + "[IMS] Available Commands:")
print(Fore.WHITE + "• Type 'reply' to compose response")
print(Fore.WHITE + "• Type 'inbox' to return to message list")
print(Fore.WHITE + "• Type 'exit' to return to main interface")
print(Fore.LIGHTCYAN_EX + "═" * 80 + "\n")
return selected_email
def reply_to_email(selected_email):
"""
Handles email reply with DNA report-style formatting.
Returns a tuple (success, message) where success is True if email was sent successfully.
"""
print(Fore.LIGHTCYAN_EX + "\n" + "═" * 80)
print(Fore.LIGHTGREEN_EX + "[COMPOSE REPLY]")
print(Fore.LIGHTCYAN_EX + "═" * 80)
print(Fore.LIGHTYELLOW_EX + "TO:")
print(Fore.WHITE + selected_email['sender'])
print(Fore.LIGHTYELLOW_EX + "\nSUBJECT:")
print(Fore.WHITE + f"Re: {selected_email['subject']}")
print(Fore.LIGHTYELLOW_EX + "\nMESSAGE:")
reply_body = input(Fore.WHITE)
print(Fore.LIGHTCYAN_EX + "\n" + "─" * 80)
print(Fore.LIGHTYELLOW_EX + "[SYSTEM] Sending reply...")
reply_msg = EmailMessage()
reply_msg['From'] = f'"Opsie by ARPA" <{EMAIL}>'
reply_msg['To'] = selected_email['sender']
reply_msg['Subject'] = f"Re: {selected_email['subject']}"
reply_msg.set_content(reply_body)
try:
with smtplib.SMTP_SSL('smtp.gmail.com', 465, context=ssl_context) as server:
server.login(EMAIL, PASSWORD)
server.send_message(reply_msg)
print(Fore.LIGHTGREEN_EX + "[SYSTEM] Reply sent successfully.")
print(Fore.LIGHTCYAN_EX + "═" * 80 + "\n")
return True, f"Reply sent successfully to {selected_email['sender']} regarding '{selected_email['subject']}'"
except Exception as e:
error_msg = f"[ERROR] Failed to send reply: {str(e)}"
print(Fore.RED + error_msg)
print(Fore.LIGHTCYAN_EX + "═" * 80 + "\n")
return False, error_msg
def inbox_interaction():
"""
Manages interaction with the inbox, allowing reading, replying, and navigating back to conversation mode.
"""
inbox = fetch_unread_primary_emails()
display_unread_inbox(inbox)
while True:
choice = input(Fore.LIGHTYELLOW_EX + "\n[INPUT] Select email number or 'exit': " + Fore.WHITE)
if choice.lower() == 'exit':
print(Fore.LIGHTGREEN_EX + "[SYSTEM] Returning to main interface...")
break
elif choice.isdigit() and 1 <= int(choice) <= len(inbox):
email_index = int(choice)
selected_email = read_email(inbox, email_index)
while True:
action = input(Fore.LIGHTYELLOW_EX + "\n[INPUT] Enter command: " + Fore.WHITE)
if action == 'reply':
reply_to_email(selected_email)
print(Fore.LIGHTGREEN_EX + "[SYSTEM] Returning to inbox...")
display_unread_inbox(inbox)
break
elif action == 'inbox':
display_unread_inbox(inbox)
break
elif action == 'exit':
print(Fore.LIGHTGREEN_EX + "[SYSTEM] Returning to main interface...")
return
else:
print(Fore.RED + "[ERROR] Invalid command. Please try again.")
# comment out ''' ''' to test
'''
def main():
"""
Main test loop for the email functionality.
"""
print(Fore.LIGHTCYAN_EX + "\n" + "═" * 80)
print(Fore.LIGHTGREEN_EX + """
╔═══════════════════════════════════════════╗
║ IMS Agent Test Loop ║
╚═══════════════════════════════════════════╝
""")
while True:
print(Fore.LIGHTYELLOW_EX + "\nAvailable Commands:")
print(Fore.WHITE + "1. Check Inbox")
print(Fore.WHITE + "2. Send New Email")
print(Fore.WHITE + "3. Exit")
choice = input(Fore.LIGHTYELLOW_EX + "\n[INPUT] Enter command number: " + Fore.WHITE)
if choice == "1":
inbox_interaction()
elif choice == "2":
prompt = input(Fore.LIGHTYELLOW_EX + "\n[INPUT] Enter email details (format: email@example.com subject 'Subject' body 'Message'): " + Fore.WHITE)
success, message = send_mail(prompt)
print(Fore.LIGHTGREEN_EX if success else Fore.RED + f"\n[SYSTEM] {message}")
elif choice == "3":
print(Fore.LIGHTGREEN_EX + "\n[SYSTEM] Exiting email system...")
break
else:
print(Fore.RED + "\n[ERROR] Invalid command. Please try again.")
if __name__ == "__main__":
main()
'''