jupiter0913 commited on
Commit
ddf1138
·
1 Parent(s): 984d010

feature(#98): updated send email api

Browse files
Brain/src/rising_plugin/gmail/email_plugin.py CHANGED
@@ -6,6 +6,7 @@ import os
6
  import re
7
  import smtplib
8
  import time
 
9
  from email.header import decode_header
10
  from email.message import EmailMessage
11
 
@@ -15,7 +16,7 @@ from bs4 import BeautifulSoup
15
  EMAIL_SMTP_HOST = "smtp.gmail.com"
16
  EMAIL_SMTP_PORT = 587
17
  EMAIL_IMAP_SERVER = "imap.gmail.com"
18
- EMAIL_SIGNATURE = "This was sent by Brain Service"
19
 
20
 
21
  class EmailPlugin:
@@ -357,3 +358,10 @@ class EmailPlugin:
357
  email_body = re.sub(r"http\S+", "", email_body)
358
 
359
  return email_body
 
 
 
 
 
 
 
 
6
  import re
7
  import smtplib
8
  import time
9
+ import base64
10
  from email.header import decode_header
11
  from email.message import EmailMessage
12
 
 
16
  EMAIL_SMTP_HOST = "smtp.gmail.com"
17
  EMAIL_SMTP_PORT = 587
18
  EMAIL_IMAP_SERVER = "imap.gmail.com"
19
+ EMAIL_SIGNATURE = "This was sent by Rising Brain"
20
 
21
 
22
  class EmailPlugin:
 
358
  email_body = re.sub(r"http\S+", "", email_body)
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(filename, "w")
365
+ file.write(file_content)
366
+ file.close()
367
+ return filename
Brain/src/router/email_router.py CHANGED
@@ -119,6 +119,10 @@ def construct_blueprint_email_api() -> APIRouter:
119
  to_send=data.data.to_send,
120
  )
121
  else:
 
 
 
 
122
  result += email_manager.send_email_with_attachment(
123
  sender=data.data.sender,
124
  pwd=data.data.pwd,
@@ -126,8 +130,9 @@ def construct_blueprint_email_api() -> APIRouter:
126
  subject=data.data.subject,
127
  body=data.data.body,
128
  to_send=data.data.to_send,
129
- filename=data.data.filename,
130
  )
 
131
  except Exception as e:
132
  if isinstance(e, BrainException):
133
  return e.get_response_exp()
 
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
+
126
  result += email_manager.send_email_with_attachment(
127
  sender=data.data.sender,
128
  pwd=data.data.pwd,
 
130
  subject=data.data.subject,
131
  body=data.data.body,
132
  to_send=data.data.to_send,
133
+ filename=file_name,
134
  )
135
+
136
  except Exception as e:
137
  if isinstance(e, BrainException):
138
  return e.get_response_exp()
Brain/tests/functional/test_api.py CHANGED
@@ -235,3 +235,64 @@ def test_train_contacts(body):
235
  def test_delete_data(body):
236
  response = client.post("/auto_task/delete", json=body)
237
  assert response.status_code == 200
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  def test_delete_data(body):
236
  response = client.post("/auto_task/delete", json=body)
237
  assert response.status_code == 200
238
+
239
+
240
+ @pytest.mark.parametrize(
241
+ "body",
242
+ [
243
+ (
244
+ {
245
+ "data": {
246
+ "sender": "test@gmail.com",
247
+ "pwd": "use app password of your google account",
248
+ "imap_folder": "inbox or drafts",
249
+ },
250
+ "confs": {
251
+ "token": "test_token",
252
+ "uuid": "test-uuid",
253
+ "openai_key": "",
254
+ "pinecone_key": "",
255
+ "pinecone_env": "",
256
+ "firebase_key": "",
257
+ "settings": {"temperature": 0.6},
258
+ },
259
+ }
260
+ )
261
+ ],
262
+ )
263
+ def test_read_emails(body):
264
+ response = client.post("/email/read_emails", json=body)
265
+ assert response.status_code == 200
266
+
267
+
268
+ @pytest.mark.parametrize(
269
+ "body",
270
+ [
271
+ (
272
+ {
273
+ "data": {
274
+ "sender": "testsender@gmail.com",
275
+ "pwd": "use app password of your google account",
276
+ "to": "testto@gmail.com",
277
+ "subject": "title of email",
278
+ "body": "email content",
279
+ "to_send": "true or false whether send to inbox or drafts",
280
+ "filename": "test.txt",
281
+ "file_content": "string encoded with base64",
282
+ },
283
+ "confs": {
284
+ "token": "test_token",
285
+ "uuid": "test-uuid",
286
+ "openai_key": "",
287
+ "pinecone_key": "",
288
+ "pinecone_env": "",
289
+ "firebase_key": "",
290
+ "settings": {"temperature": 0.6},
291
+ },
292
+ }
293
+ )
294
+ ],
295
+ )
296
+ def test_send_email(body):
297
+ response = client.post("/email/send_email", json=body)
298
+ assert response.status_code == 200