-
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.
Browse files
Browse the repository at this point in the history
Co-authored-by: gromdimon <[email protected]> Since there's a separate issue for testing (#174 ), I'm closing this PR.
- Loading branch information
Showing
27 changed files
with
1,035 additions
and
471 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
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
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,23 @@ | ||
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 | ||
from app.core import auth | ||
|
||
router = APIRouter() | ||
|
||
current_active_superuser = auth.fastapi_users.current_user(active=True, superuser=True) | ||
|
||
|
||
@router.post("/test-email/", response_model=schemas.Msg, status_code=201) | ||
def test_email( | ||
email_to: EmailStr, | ||
current_user: models.User = Depends(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 @@ | ||
!build |
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,25 @@ | ||
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head><title></title><!--[if !mso]><!-- --><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]--><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style type="text/css">#outlook a { padding:0; } | ||
.ReadMsgBody { width:100%; } | ||
.ExternalClass { width:100%; } | ||
.ExternalClass * { line-height:100%; } | ||
body { margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; } | ||
table, td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; } | ||
img { border:0;height:auto;line-height:100%; outline:none;text-decoration:none;-ms-interpolation-mode:bicubic; } | ||
p { display:block;margin:13px 0; }</style><!--[if !mso]><!--><style type="text/css">@media only screen and (max-width:480px) { | ||
@-ms-viewport { width:320px; } | ||
@viewport { width:320px; } | ||
}</style><!--<![endif]--><!--[if mso]> | ||
<xml> | ||
<o:OfficeDocumentSettings> | ||
<o:AllowPNG/> | ||
<o:PixelsPerInch>96</o:PixelsPerInch> | ||
</o:OfficeDocumentSettings> | ||
</xml> | ||
<![endif]--><!--[if lte mso 11]> | ||
<style type="text/css"> | ||
.outlook-group-fix { width:100% !important; } | ||
</style> | ||
<![endif]--><!--[if !mso]><!--><link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css"><style type="text/css">@import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);</style><!--<![endif]--><style type="text/css">@media only screen and (min-width:480px) { | ||
.mj-column-per-100 { width:100% !important; max-width: 100%; } | ||
}</style><style type="text/css"></style></head><body style="background-color:#ffffff;"><div style="background-color:#ffffff;"><!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--><div style="Margin:0px auto;max-width:600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;"><tbody><tr><td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;vertical-align:top;"><!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]--><div class="mj-column-per-100 outlook-group-fix" style="font-size:13px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"><table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"><tr><td style="font-size:0px;padding:10px 25px;word-break:break-word;"><p style="border-top:solid 4px #555555;font-size:1;margin:0px auto;width:100%;"></p><!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" style="border-top:solid 4px #555555;font-size:1;margin:0px auto;width:550px;" role="presentation" width="550px" ><tr><td style="height:0;line-height:0;"> | ||
</td></tr></table><![endif]--></td></tr><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:helvetica;font-size:20px;line-height:1;text-align:left;color:#555555;">{{ project_name }}</div></td></tr><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:16px;line-height:1;text-align:left;color:#555555;">Test email for: {{ email }}</div></td></tr></table></div><!--[if mso | IE]></td></tr></table><![endif]--></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></div></body></html> |
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,21 @@ | ||
<!doctype html><html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"><head><title></title><!--[if !mso]><!--><meta http-equiv="X-UA-Compatible" content="IE=edge"><!--<![endif]--><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><meta name="viewport" content="width=device-width,initial-scale=1"><style type="text/css">#outlook a { padding:0; } | ||
body { margin:0;padding:0;-webkit-text-size-adjust:100%;-ms-text-size-adjust:100%; } | ||
table, td { border-collapse:collapse;mso-table-lspace:0pt;mso-table-rspace:0pt; } | ||
img { border:0;height:auto;line-height:100%; outline:none;text-decoration:none;-ms-interpolation-mode:bicubic; } | ||
p { display:block;margin:13px 0; }</style><!--[if mso]> | ||
<noscript> | ||
<xml> | ||
<o:OfficeDocumentSettings> | ||
<o:AllowPNG/> | ||
<o:PixelsPerInch>96</o:PixelsPerInch> | ||
</o:OfficeDocumentSettings> | ||
</xml> | ||
</noscript> | ||
<![endif]--><!--[if lte mso 11]> | ||
<style type="text/css"> | ||
.mj-outlook-group-fix { width:100% !important; } | ||
</style> | ||
<![endif]--><!--[if !mso]><!--><link href="https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700" rel="stylesheet" type="text/css"><style type="text/css">@import url(https://fonts.googleapis.com/css?family=Ubuntu:300,400,500,700);</style><!--<![endif]--><style type="text/css">@media only screen and (min-width:480px) { | ||
.mj-column-per-100 { width:100% !important; max-width: 100%; } | ||
}</style><style media="screen and (min-width:480px)">.moz-text-html .mj-column-per-100 { width:100% !important; max-width: 100%; }</style><style type="text/css"></style></head><body style="word-spacing:normal;background-color:#ffffff;"><div style="background-color:#ffffff;"><!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" class="" style="width:600px;" width="600" ><tr><td style="line-height:0px;font-size:0px;mso-line-height-rule:exactly;"><![endif]--><div style="margin:0px auto;max-width:600px;"><table align="center" border="0" cellpadding="0" cellspacing="0" role="presentation" style="width:100%;"><tbody><tr><td style="direction:ltr;font-size:0px;padding:20px 0;text-align:center;"><!--[if mso | IE]><table role="presentation" border="0" cellpadding="0" cellspacing="0"><tr><td class="" style="vertical-align:top;width:600px;" ><![endif]--><div class="mj-column-per-100 mj-outlook-group-fix" style="font-size:0px;text-align:left;direction:ltr;display:inline-block;vertical-align:top;width:100%;"><table border="0" cellpadding="0" cellspacing="0" role="presentation" style="vertical-align:top;" width="100%"><tbody><tr><td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"><p style="border-top:solid 4px #555555;font-size:1px;margin:0px auto;width:100%;"></p><!--[if mso | IE]><table align="center" border="0" cellpadding="0" cellspacing="0" style="border-top:solid 4px #555555;font-size:1px;margin:0px auto;width:550px;" role="presentation" width="550px" ><tr><td style="height:0;line-height:0;"> | ||
</td></tr></table><![endif]--></td></tr><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:helvetica;font-size:20px;line-height:1;text-align:left;color:#555555;">{{ project_name }}</div></td></tr><tr><td align="left" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:16px;line-height:1;text-align:left;color:#555555;">Your email: {{ email }}</div></td></tr><tr><td align="center" style="font-size:0px;padding:10px 25px;word-break:break-word;"><div style="font-family:Ubuntu, Helvetica, Arial, sans-serif;font-size:16px;line-height:1;text-align:center;color:#555555;"><a href="{{ base_url }}/verify-email?token={{token}}">Click here to verify your email</a></div></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></td></tr></tbody></table></div><!--[if mso | IE]></td></tr></table><![endif]--></div></body></html> |
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,16 @@ | ||
<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">Your email: {{ email }}</mj-text> | ||
<mj-text font-size="16px" color="#555" align="center"> | ||
<a href="{{ base_url }}/verify-email?token={{token}}"> | ||
Click here to verify your email | ||
</a> | ||
</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,70 @@ | ||
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 starlette.requests import Request | ||
|
||
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: | ||
"""Send out test email to ``email_to``""" | ||
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}, | ||
) | ||
|
||
|
||
def send_user_verify_email(email_to: str, token: str, request: Request | None = None) -> None: | ||
"""Send out user verification email""" | ||
if request: | ||
base_url = f"{request.url.components.scheme}://{request.url.components.netloc}" | ||
else: | ||
base_url = str(settings.SERVER_HOST) | ||
project_name = settings.PROJECT_NAME | ||
subject = f"{project_name} - Verify Your Email" | ||
with open(Path(settings.EMAIL_TEMPLATES_DIR) / "verify_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, | ||
"token": token, | ||
"base_url": base_url, | ||
}, | ||
) |
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
Oops, something went wrong.