-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathemail.py
More file actions
72 lines (60 loc) · 2.43 KB
/
email.py
File metadata and controls
72 lines (60 loc) · 2.43 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from fastapi import APIRouter, BackgroundTasks, Depends, Form, HTTPException
from pydantic import BaseModel, EmailStr
from pydantic.errors import EmailError
from sqlalchemy.orm.session import Session
from starlette.responses import RedirectResponse
from app.dependencies import get_db
from app.internal.email import send as internal_send
from app.internal.email import send_email_invitation
router = APIRouter(
prefix="/email",
tags=["email"],
responses={404: {"description": "Not found"}},
)
@router.post("/send")
async def send(
db: Session = Depends(get_db),
send_to: str = "/",
title: str = Form(...),
event_used: str = Form(...),
user_to_send: str = Form(...),
background_tasks: BackgroundTasks = BackgroundTasks
) -> RedirectResponse:
if not internal_send(
title=title, event_used=event_used,
user_to_send=user_to_send,
background_tasks=background_tasks, session=db):
raise HTTPException(status_code=404, detail="Couldn't send the email!")
return RedirectResponse(send_to, status_code=303)
INVALID_EMAIL_ADDRESS_ERROR_MESSAGE = "Please enter valid email address"
SUCCESSFULLY_SENT_EMAIL_MESSAGE = "Your message was sent successfully"
class InvitationParams(BaseModel):
send_to: str = "/"
sender_name: str
recipient_name: str
recipient_mail: str
@router.post("/invitation/")
def send_invitation(invitation: InvitationParams,
background_task: BackgroundTasks):
"""
This function sends the recipient an invitation
to his email address in the format HTML.
:param invitation: InvitationParams, invitation parameters
:param background_task: BackgroundTasks
:return: json response message,
error message if the entered email address is incorrect,
confirmation message if the invitation was successfully sent
"""
try:
EmailStr.validate(invitation.recipient_mail)
except EmailError:
raise HTTPException(
status_code=422,
detail=INVALID_EMAIL_ADDRESS_ERROR_MESSAGE)
if not send_email_invitation(
sender_name=invitation.sender_name,
recipient_name=invitation.recipient_name,
recipient_mail=invitation.recipient_mail,
background_tasks=background_task):
raise HTTPException(status_code=422, detail="Couldn't send the email!")
return RedirectResponse(invitation.send_to, status_code=303)