Spaces:
Running
Running
Commit
·
5551656
1
Parent(s):
6413288
Adding email tools
Browse files
src/tools/user_tools/disabled/GetEmails.py
ADDED
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import imaplib
|
2 |
+
import email
|
3 |
+
import os
|
4 |
+
|
5 |
+
__all__ = ['GetEmails']
|
6 |
+
|
7 |
+
class GetEmails:
|
8 |
+
dependencies = ['os']
|
9 |
+
|
10 |
+
inputSchema = {
|
11 |
+
"name": "GetEmails",
|
12 |
+
"description": "Reads emails from an IMAP server.",
|
13 |
+
"parameters": {
|
14 |
+
"type": "object",
|
15 |
+
"properties": {
|
16 |
+
"num_emails": {
|
17 |
+
"type": "integer",
|
18 |
+
"description": "Number of emails to retrieve (default: 5).",
|
19 |
+
"default": 5
|
20 |
+
}
|
21 |
+
},
|
22 |
+
"required": []
|
23 |
+
}
|
24 |
+
}
|
25 |
+
|
26 |
+
def __init__(self):
|
27 |
+
# Replace with your actual email provider's IMAP server address
|
28 |
+
self.imap_server = "imap.gmail.com" # e.g., imap.gmail.com
|
29 |
+
self.email_address = os.environ.get("EMAIL_ADDRESS") # Replace with your email address
|
30 |
+
self.password = os.environ.get("EMAIL_PASSWORD") # Replace with your password or app password
|
31 |
+
|
32 |
+
def run(self, **kwargs):
|
33 |
+
num_emails = kwargs.get("num_emails", 5)
|
34 |
+
|
35 |
+
try:
|
36 |
+
# Connect to the IMAP server
|
37 |
+
mail = imaplib.IMAP4_SSL(self.imap_server)
|
38 |
+
mail.login(self.email_address, self.password)
|
39 |
+
mail.select("inbox") # You can select other mailboxes like "sent"
|
40 |
+
|
41 |
+
# Search for emails (ALL = all emails, or use specific criteria)
|
42 |
+
result, data = mail.search(None, "ALL")
|
43 |
+
mail_ids = data[0].split()
|
44 |
+
|
45 |
+
# Get the most recent emails
|
46 |
+
recent_emails = mail_ids[-num_emails:]
|
47 |
+
|
48 |
+
email_messages = []
|
49 |
+
for num in recent_emails:
|
50 |
+
result, msg_data = mail.fetch(num, '(RFC822)')
|
51 |
+
raw_email = msg_data[0][1]
|
52 |
+
msg = email.message_from_bytes(raw_email)
|
53 |
+
|
54 |
+
# Decode email headers and body
|
55 |
+
try:
|
56 |
+
subject = email.header.decode_header(msg["Subject"])[0][0]
|
57 |
+
if isinstance(subject, bytes):
|
58 |
+
subject = subject.decode()
|
59 |
+
except:
|
60 |
+
subject = "No Subject"
|
61 |
+
|
62 |
+
try:
|
63 |
+
from_ = email.header.decode_header(msg.get("From"))[0][0]
|
64 |
+
if isinstance(from_, bytes):
|
65 |
+
from_ = from_.decode()
|
66 |
+
except:
|
67 |
+
from_ = "Unknown Sender"
|
68 |
+
|
69 |
+
# Extract the body of the email
|
70 |
+
body = "" # Initialize body to an empty string
|
71 |
+
if msg.is_multipart():
|
72 |
+
for part in msg.walk():
|
73 |
+
ctype = part.get_content_type()
|
74 |
+
cdispo = str(part.get("Content-Disposition"))
|
75 |
+
|
76 |
+
if ctype == "text/plain" and "attachment" not in cdispo:
|
77 |
+
body = part.get_payload(decode=True).decode()
|
78 |
+
break
|
79 |
+
else:
|
80 |
+
body = msg.get_payload(decode=True).decode()
|
81 |
+
|
82 |
+
email_messages.append({
|
83 |
+
"subject": subject,
|
84 |
+
"from": from_,
|
85 |
+
"body": body
|
86 |
+
})
|
87 |
+
|
88 |
+
mail.close()
|
89 |
+
mail.logout()
|
90 |
+
|
91 |
+
return {
|
92 |
+
"status": "success",
|
93 |
+
"message": "Emails retrieved successfully.",
|
94 |
+
"output": email_messages
|
95 |
+
}
|
96 |
+
|
97 |
+
except Exception as e:
|
98 |
+
return {
|
99 |
+
"status": "error",
|
100 |
+
"message": f"An error occurred: {str(e)}",
|
101 |
+
"output": None
|
102 |
+
}
|
src/tools/user_tools/disabled/SendEmails.py
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import smtplib
|
2 |
+
from email.mime.text import MIMEText
|
3 |
+
from email.mime.multipart import MIMEMultipart
|
4 |
+
import os
|
5 |
+
|
6 |
+
__all__ = ['SendEmails']
|
7 |
+
|
8 |
+
class SendEmails:
|
9 |
+
dependencies = ['os']
|
10 |
+
|
11 |
+
inputSchema = {
|
12 |
+
"name": "SendEmails",
|
13 |
+
"description": "Writes and sends an email.",
|
14 |
+
"parameters": {
|
15 |
+
"type": "object",
|
16 |
+
"properties": {
|
17 |
+
"receiver_email": {
|
18 |
+
"type": "string",
|
19 |
+
"description": "The recipient's email address.",
|
20 |
+
},
|
21 |
+
"subject": {
|
22 |
+
"type": "string",
|
23 |
+
"description": "The email subject.",
|
24 |
+
},
|
25 |
+
"body": {
|
26 |
+
"type": "string",
|
27 |
+
"description": "The email body.",
|
28 |
+
},
|
29 |
+
},
|
30 |
+
"required": ["receiver_email", "subject", "body"],
|
31 |
+
}
|
32 |
+
}
|
33 |
+
|
34 |
+
def __init__(self):
|
35 |
+
self.sender_email = os.environ.get("EMAIL_ADDRESS")
|
36 |
+
self.sender_password = os.environ.get("EMAIL_PASSWORD")
|
37 |
+
|
38 |
+
def run(self, **kwargs):
|
39 |
+
receiver_email = kwargs["receiver_email"]
|
40 |
+
subject = kwargs["subject"]
|
41 |
+
body = kwargs["body"]
|
42 |
+
|
43 |
+
try:
|
44 |
+
msg = MIMEMultipart()
|
45 |
+
msg['From'] = self.sender_email
|
46 |
+
msg['To'] = receiver_email
|
47 |
+
msg['Subject'] = subject
|
48 |
+
|
49 |
+
msg.attach(MIMEText(body, 'plain'))
|
50 |
+
|
51 |
+
server = smtplib.SMTP('smtp.gmail.com', 587)
|
52 |
+
server.starttls()
|
53 |
+
server.login(self.sender_email, self.sender_password)
|
54 |
+
text = msg.as_string()
|
55 |
+
server.sendmail(self.sender_email, receiver_email, text)
|
56 |
+
server.quit()
|
57 |
+
|
58 |
+
return {"status": "success", "message": "Email sent successfully!"}
|
59 |
+
|
60 |
+
except Exception as e:
|
61 |
+
return {"status": "error", "message": str(e)}
|