Merge pull request #208 from ttt246/feature/email_attach_194
Browse files
Brain/src/rising_plugin/gmail/email_plugin.py
CHANGED
|
@@ -219,12 +219,12 @@ class EmailPlugin:
|
|
| 219 |
|
| 220 |
messages.append(
|
| 221 |
{
|
| 222 |
-
"
|
| 223 |
-
"
|
| 224 |
-
"
|
| 225 |
-
"
|
| 226 |
-
"
|
| 227 |
-
"
|
| 228 |
}
|
| 229 |
)
|
| 230 |
|
|
@@ -232,12 +232,12 @@ class EmailPlugin:
|
|
| 232 |
if not messages:
|
| 233 |
messages.append(
|
| 234 |
{
|
| 235 |
-
"
|
| 236 |
-
"
|
| 237 |
-
"
|
| 238 |
-
"
|
| 239 |
-
"
|
| 240 |
-
"
|
| 241 |
}
|
| 242 |
)
|
| 243 |
return json.dumps(messages)
|
|
@@ -359,9 +359,16 @@ class EmailPlugin:
|
|
| 359 |
|
| 360 |
return email_body
|
| 361 |
|
| 362 |
-
def write_attachment(self, filename: str, file_content: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
file_content = base64.b64decode(file_content).decode("utf-8")
|
| 364 |
-
file = open(
|
| 365 |
file.write(file_content)
|
| 366 |
file.close()
|
| 367 |
-
return
|
|
|
|
| 219 |
|
| 220 |
messages.append(
|
| 221 |
{
|
| 222 |
+
"from": from_address,
|
| 223 |
+
"to": to_address,
|
| 224 |
+
"date": date,
|
| 225 |
+
"cc": cc,
|
| 226 |
+
"subject": subject,
|
| 227 |
+
"body": body,
|
| 228 |
}
|
| 229 |
)
|
| 230 |
|
|
|
|
| 232 |
if not messages:
|
| 233 |
messages.append(
|
| 234 |
{
|
| 235 |
+
"from": "",
|
| 236 |
+
"to": "",
|
| 237 |
+
"date": "",
|
| 238 |
+
"cc": "",
|
| 239 |
+
"subject": "",
|
| 240 |
+
"body": "There are no Emails",
|
| 241 |
}
|
| 242 |
)
|
| 243 |
return json.dumps(messages)
|
|
|
|
| 359 |
|
| 360 |
return email_body
|
| 361 |
|
| 362 |
+
def write_attachment(self, filename: str, file_content: str) -> (str, str):
|
| 363 |
+
# create folder for temporarily saving attached file
|
| 364 |
+
milliseconds = int(time.time() * 1000)
|
| 365 |
+
file_path = f"Brain/assets/{milliseconds}/{filename}"
|
| 366 |
+
file_directory = f"Brain/assets/{milliseconds}"
|
| 367 |
+
os.mkdir(file_directory)
|
| 368 |
+
|
| 369 |
+
# file write
|
| 370 |
file_content = base64.b64decode(file_content).decode("utf-8")
|
| 371 |
+
file = open(file_path, "w")
|
| 372 |
file.write(file_content)
|
| 373 |
file.close()
|
| 374 |
+
return file_path, file_directory
|
Brain/src/router/email_router.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import json
|
| 2 |
-
|
| 3 |
from fastapi import APIRouter
|
| 4 |
|
| 5 |
from Brain.src.common.assembler import Assembler
|
|
@@ -30,12 +30,12 @@ def construct_blueprint_email_api() -> APIRouter:
|
|
| 30 |
|
| 31 |
@generator.response(
|
| 32 |
status_code=200, schema={"message": "message", "result": [{
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
"
|
| 36 |
-
"
|
| 37 |
-
"
|
| 38 |
-
"
|
| 39 |
}]}
|
| 40 |
)
|
| 41 |
|
|
@@ -119,7 +119,7 @@ def construct_blueprint_email_api() -> APIRouter:
|
|
| 119 |
to_send=data.data.to_send,
|
| 120 |
)
|
| 121 |
else:
|
| 122 |
-
file_name = email_manager.write_attachment(
|
| 123 |
filename=data.data.filename, file_content=data.data.file_content
|
| 124 |
)
|
| 125 |
|
|
@@ -133,6 +133,8 @@ def construct_blueprint_email_api() -> APIRouter:
|
|
| 133 |
filename=file_name,
|
| 134 |
)
|
| 135 |
|
|
|
|
|
|
|
| 136 |
except Exception as e:
|
| 137 |
if isinstance(e, BrainException):
|
| 138 |
return e.get_response_exp()
|
|
|
|
| 1 |
import json
|
| 2 |
+
import shutil
|
| 3 |
from fastapi import APIRouter
|
| 4 |
|
| 5 |
from Brain.src.common.assembler import Assembler
|
|
|
|
| 30 |
|
| 31 |
@generator.response(
|
| 32 |
status_code=200, schema={"message": "message", "result": [{
|
| 33 |
+
"from": "testfrom@test.com",
|
| 34 |
+
"to": "test@gmail.com",
|
| 35 |
+
"date": "Tue, 04 Jul 2023 12:55:19 +0000",
|
| 36 |
+
"cc": "",
|
| 37 |
+
"subject": "subject",
|
| 38 |
+
"body": "message"
|
| 39 |
}]}
|
| 40 |
)
|
| 41 |
|
|
|
|
| 119 |
to_send=data.data.to_send,
|
| 120 |
)
|
| 121 |
else:
|
| 122 |
+
file_name, file_directory = email_manager.write_attachment(
|
| 123 |
filename=data.data.filename, file_content=data.data.file_content
|
| 124 |
)
|
| 125 |
|
|
|
|
| 133 |
filename=file_name,
|
| 134 |
)
|
| 135 |
|
| 136 |
+
shutil.rmtree(file_directory)
|
| 137 |
+
|
| 138 |
except Exception as e:
|
| 139 |
if isinstance(e, BrainException):
|
| 140 |
return e.get_response_exp()
|