-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
48 lines (43 loc) · 1.27 KB
/
test.py
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
from fastapi import FastAPI, File, UploadFile
import pandas as pd
import json
import os
import time
from msgemail import render_template, send_email
from util import allowed_csv
app = FastAPI()
async def save_file(file):
df = pd.read_csv(file.file)
df.to_json(file.filename, orient='records')
@app.post("/emailmessage")
async def emailmessage(
sender: str,
password: str,
subject: str,
context: str,
port: str = 587,
host: str = "smtp.gmail.com",
htmlfile: str = 'base/default.j2',
file: UploadFile = File(...)
):
if await allowed_csv(file):
await save_file(file)
with open(file.filename, 'r') as file:
data = file.read()
obj = json.loads(data)
for key in obj:
name = key['firstname'] + ' ' + key['lastname']
email = key['email']
html = await render_template(htmlfile, context, name)
await send_email(
receiver=email,
sender=sender,
password=password,
subject=subject,
body=html,
host=host,
port=port
)
time.sleep(5)
os.remove(file.filename)
return {'message': 'sent message success !'}