-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement user email verification workflow (#177)
- Loading branch information
Showing
11 changed files
with
597 additions
and
374 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Any | ||
|
||
from fastapi import APIRouter, Depends | ||
from pydantic.networks import EmailStr | ||
|
||
from app import models, schemas | ||
from app.api import deps | ||
from app.app.utils import send_test_email | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post("/test-email/", response_model=schemas.Msg, status_code=201) | ||
def test_email( | ||
email_to: EmailStr, | ||
current_user: models.User = Depends(deps.get_current_active_superuser), | ||
) -> Any: | ||
"""Send out a test email.""" | ||
send_test_email(email_to=email_to) | ||
return {"msg": "Test email sent"} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<mjml> | ||
<mj-body background-color="#fff"> | ||
<mj-section> | ||
<mj-column> | ||
<mj-divider border-color="#555"></mj-divider> | ||
<mj-text font-size="20px" color="#555" font-family="helvetica">{{ project_name }}</mj-text> | ||
<mj-text font-size="16px" color="#555">Test email for: {{ email }}</mj-text> | ||
</mj-column> | ||
</mj-section> | ||
</mj-body> | ||
</mjml> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import logging | ||
from datetime import datetime, timedelta | ||
from pathlib import Path | ||
from typing import Any, Dict, Optional | ||
|
||
import emails | ||
from emails.template import JinjaTemplate | ||
|
||
from app.core.config import settings | ||
|
||
|
||
def send_email( | ||
email_to: str, | ||
subject_template: str = "", | ||
html_template: str = "", | ||
environment: Dict[str, Any] = {}, | ||
) -> None: | ||
assert settings.EMAILS_ENABLED, "no provided configuration for email variables" | ||
message = emails.Message( | ||
subject=JinjaTemplate(subject_template), | ||
html=JinjaTemplate(html_template), | ||
mail_from=(settings.EMAILS_FROM_NAME, settings.EMAILS_FROM_EMAIL), | ||
) | ||
smtp_options = {"host": settings.SMTP_HOST, "port": settings.SMTP_PORT} | ||
if settings.SMTP_TLS: | ||
smtp_options["tls"] = True | ||
if settings.SMTP_USER: | ||
smtp_options["user"] = settings.SMTP_USER | ||
if settings.SMTP_PASSWORD: | ||
smtp_options["password"] = settings.SMTP_PASSWORD | ||
response = message.send(to=email_to, render=environment, smtp=smtp_options) | ||
logging.info(f"send email result: {response}") | ||
|
||
|
||
def send_test_email(email_to: str) -> None: | ||
project_name = settings.PROJECT_NAME | ||
subject = f"{project_name} - Test email" | ||
with open(Path(settings.EMAIL_TEMPLATES_DIR) / "test_email.html") as f: | ||
template_str = f.read() | ||
send_email( | ||
email_to=email_to, | ||
subject_template=subject, | ||
html_template=template_str, | ||
environment={"project_name": settings.PROJECT_NAME, "email": email_to}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from pydantic import BaseModel | ||
|
||
|
||
class Msg(BaseModel): | ||
msg: str |