jupiter0913 commited on
Commit ·
3920357
1
Parent(s): 9993152
feature(#98): update gmail-managing moduel
Browse files
Brain/src/rising_plugin/gmail/manage_gmail.py
CHANGED
|
@@ -12,11 +12,23 @@ from email.message import EmailMessage
|
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
|
| 14 |
|
| 15 |
-
def send_email(
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
|
| 19 |
-
def send_email_with_attachment(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
attachment_path = filename
|
| 21 |
attachment = os.path.basename(filename)
|
| 22 |
return send_email_with_attachment_internal(
|
|
@@ -25,7 +37,14 @@ def send_email_with_attachment(sender: str, pwd: str, to: str, subject: str, bod
|
|
| 25 |
|
| 26 |
|
| 27 |
def send_email_with_attachment_internal(
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
) -> str:
|
| 30 |
"""Send an email
|
| 31 |
|
|
@@ -87,8 +106,13 @@ def send_email_with_attachment_internal(
|
|
| 87 |
|
| 88 |
|
| 89 |
def read_emails(
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
"""Read emails from an IMAP mailbox.
|
| 93 |
|
| 94 |
This function reads emails from a specified IMAP folder, using a given IMAP search command, limits, and page numbers.
|
|
@@ -197,7 +221,9 @@ def read_emails(
|
|
| 197 |
page_count = len(messages) // limit + (len(messages) % limit > 0)
|
| 198 |
|
| 199 |
if page < 1 or page > page_count:
|
| 200 |
-
raise ValueError(
|
|
|
|
|
|
|
| 201 |
|
| 202 |
# Calculate paginated indexes
|
| 203 |
start_index = len(messages) - (page * limit + 1)
|
|
@@ -221,7 +247,7 @@ def adjust_imap_folder_for_gmail(imap_folder: str, email_sender: str) -> str:
|
|
| 221 |
|
| 222 |
|
| 223 |
def imap_open(
|
| 224 |
-
|
| 225 |
) -> imaplib.IMAP4_SSL:
|
| 226 |
imap_server = os.getenv("EMAIL_IMAP_SERVER")
|
| 227 |
conn = imaplib.IMAP4_SSL(imap_server)
|
|
@@ -282,7 +308,8 @@ def clean_email_body(email_body):
|
|
| 282 |
"""
|
| 283 |
|
| 284 |
# If body is None, return an empty string
|
| 285 |
-
if email_body is None:
|
|
|
|
| 286 |
|
| 287 |
# Remove any HTML tags
|
| 288 |
email_body = BeautifulSoup(email_body, "html.parser")
|
|
|
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
|
| 14 |
|
| 15 |
+
def send_email(
|
| 16 |
+
sender: str, pwd: str, to: str, subject: str, body: str, to_send: bool
|
| 17 |
+
) -> str:
|
| 18 |
+
return send_email_with_attachment_internal(
|
| 19 |
+
sender, pwd, to, subject, body, None, None, to_send
|
| 20 |
+
)
|
| 21 |
|
| 22 |
|
| 23 |
+
def send_email_with_attachment(
|
| 24 |
+
sender: str,
|
| 25 |
+
pwd: str,
|
| 26 |
+
to: str,
|
| 27 |
+
subject: str,
|
| 28 |
+
body: str,
|
| 29 |
+
filename: str,
|
| 30 |
+
to_send: bool,
|
| 31 |
+
) -> str:
|
| 32 |
attachment_path = filename
|
| 33 |
attachment = os.path.basename(filename)
|
| 34 |
return send_email_with_attachment_internal(
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
def send_email_with_attachment_internal(
|
| 40 |
+
sender: str,
|
| 41 |
+
pwd: str,
|
| 42 |
+
to: str,
|
| 43 |
+
title: str,
|
| 44 |
+
message: str,
|
| 45 |
+
attachment_path: str,
|
| 46 |
+
attachment: str,
|
| 47 |
+
to_send: bool,
|
| 48 |
) -> str:
|
| 49 |
"""Send an email
|
| 50 |
|
|
|
|
| 106 |
|
| 107 |
|
| 108 |
def read_emails(
|
| 109 |
+
sender: str,
|
| 110 |
+
pwd: str,
|
| 111 |
+
imap_folder: str = "inbox",
|
| 112 |
+
imap_search_command: str = "UNSEEN",
|
| 113 |
+
limit: int = 5,
|
| 114 |
+
page: int = 1,
|
| 115 |
+
) -> str:
|
| 116 |
"""Read emails from an IMAP mailbox.
|
| 117 |
|
| 118 |
This function reads emails from a specified IMAP folder, using a given IMAP search command, limits, and page numbers.
|
|
|
|
| 221 |
page_count = len(messages) // limit + (len(messages) % limit > 0)
|
| 222 |
|
| 223 |
if page < 1 or page > page_count:
|
| 224 |
+
raise ValueError(
|
| 225 |
+
"Error: The page value references a page that is not part of the results"
|
| 226 |
+
)
|
| 227 |
|
| 228 |
# Calculate paginated indexes
|
| 229 |
start_index = len(messages) - (page * limit + 1)
|
|
|
|
| 247 |
|
| 248 |
|
| 249 |
def imap_open(
|
| 250 |
+
imap_folder: str, email_sender: str, email_password: str
|
| 251 |
) -> imaplib.IMAP4_SSL:
|
| 252 |
imap_server = os.getenv("EMAIL_IMAP_SERVER")
|
| 253 |
conn = imaplib.IMAP4_SSL(imap_server)
|
|
|
|
| 308 |
"""
|
| 309 |
|
| 310 |
# If body is None, return an empty string
|
| 311 |
+
if email_body is None:
|
| 312 |
+
email_body = ""
|
| 313 |
|
| 314 |
# Remove any HTML tags
|
| 315 |
email_body = BeautifulSoup(email_body, "html.parser")
|