async-mail

This commit is contained in:
Tony Rewin 2023-10-05 22:47:02 +03:00
parent 9537814718
commit f6e3320e18
2 changed files with 13 additions and 13 deletions

4
CHECKS
View File

@ -1,5 +1,5 @@
WAIT=10 WAIT=10
TIMEOUT=10 TIMEOUT=5
ATTEMPTS=10 ATTEMPTS=3
/ /

View File

@ -1,15 +1,14 @@
import requests import httpx
from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
api_url = "https://api.mailgun.net/v3/%s/messages" % (MAILGUN_DOMAIN or "discours.io") api_url = f"https://api.mailgun.net/v3/{MAILGUN_DOMAIN or 'discours.io'}/messages"
noreply = "discours.io <noreply@%s>" % (MAILGUN_DOMAIN or "discours.io") noreply = f"discours.io <noreply@{MAILGUN_DOMAIN or 'discours.io'}>"
lang_subject = {"ru": "Подтверждение почты", "en": "Confirm email"} lang_subject = {"ru": "Подтверждение почты", "en": "Confirm email"}
async def send_auth_email(user, token, lang="ru", template="email_confirmation"): async def send_auth_email(user, token, lang="ru", template="email_confirmation"):
try: try:
to = "%s <%s>" % (user.name, user.email) to = f"{user.name} <{user.email}>"
if lang not in ["ru", "en"]: if lang not in ["ru", "en"]:
lang = "ru" lang = "ru"
subject = lang_subject.get(lang, lang_subject["en"]) subject = lang_subject.get(lang, lang_subject["en"])
@ -19,12 +18,13 @@ async def send_auth_email(user, token, lang="ru", template="email_confirmation")
"to": to, "to": to,
"subject": subject, "subject": subject,
"template": template, "template": template,
"h:X-Mailgun-Variables": '{ "token": "%s" }' % token, "h:X-Mailgun-Variables": f'{{ "token": "{token}" }}',
} }
print("[auth.email] payload: %r" % payload) print(f"[auth.email] payload: {payload}")
# debug async with httpx.AsyncClient() as client:
# print('http://localhost:3000/?modal=auth&mode=confirm-email&token=%s' % token) response = await client.post(
response = requests.post(api_url, auth=("api", MAILGUN_API_KEY), data=payload) api_url, auth=("api", MAILGUN_API_KEY), data=payload
response.raise_for_status() )
response.raise_for_status()
except Exception as e: except Exception as e:
print(e) print(e)