-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.py
55 lines (44 loc) ยท 1.52 KB
/
main.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
49
50
51
52
53
54
from fastapi import Depends, HTTPException
from fastapi import FastAPI
from fastapi_mail import FastMail, MessageSchema, ConnectionConfig
from sqlalchemy.orm import Session
from starlette.responses import JSONResponse
import crud
import models
import schemas
from database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine)
app = FastAPI()
conf = ConnectionConfig(
MAIL_USERNAME="email address",
MAIL_PASSWORD="email password",
MAIL_PORT=587,
MAIL_SERVER="smtp.gmail.com",
MAIL_FROM="dayeon0914@ewhain.net",
MAIL_TLS=True,
MAIL_SSL=False
)
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.post("/users/", response_model=schemas.EmailBase)
def create_user(user: schemas.EmailCreate, db: Session = Depends(get_db)):
db_user = crud.get_user_by_email(db, email=user.email)
if db_user:
raise HTTPException(status_code=400, detail="Email already registered")
return crud.create_user(db=db, user=user)
@app.post("/email")
async def simple_send(email: schemas.EmailBase) -> JSONResponse:
message = MessageSchema(
subject="Fastapi-Mail module",
recipients=email.email, # List of recipients, as many as you can pass
body="Amazon์ ์ฃผ๊ฐ๊ฐ " + str(email.predicted_price) + "$๋ฅผ ๋์ ๊ฒ์ผ๋ก ์์ธก๋ฉ๋๋ค.",
subtype="text"
)
fm = FastMail(conf)
await fm.send_message(message)
return JSONResponse(status_code=200, content={"message": "email has been sent"})