2023-10-05 19:18:05 +00:00
|
|
|
from httpx import AsyncClient
|
2022-09-17 18:12:14 +00:00
|
|
|
|
2022-10-05 15:54:29 +00:00
|
|
|
from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
|
2022-09-17 18:12:14 +00:00
|
|
|
|
2023-10-05 19:18:05 +00:00
|
|
|
api_url = "https://api.mailgun.net/v3/%s/messages" % (MAILGUN_DOMAIN or "discours.io")
|
|
|
|
noreply = "discours.io <noreply@%s>" % (MAILGUN_DOMAIN or "discours.io")
|
|
|
|
lang_subject = {"ru": "Подтверждение почты", "en": "Confirm email"}
|
2022-10-05 15:54:29 +00:00
|
|
|
|
2022-10-21 05:47:58 +00:00
|
|
|
|
2022-12-01 19:26:53 +00:00
|
|
|
async def send_auth_email(user, token, lang="ru", template="email_confirmation"):
|
2022-10-05 15:54:29 +00:00
|
|
|
try:
|
2022-10-20 22:05:37 +00:00
|
|
|
to = "%s <%s>" % (user.name, user.email)
|
2023-10-05 19:18:05 +00:00
|
|
|
if lang not in ["ru", "en"]:
|
|
|
|
lang = "ru"
|
2022-10-22 12:02:15 +00:00
|
|
|
subject = lang_subject.get(lang, lang_subject["en"])
|
2022-11-27 13:14:17 +00:00
|
|
|
template = template + "_" + lang
|
2022-10-22 12:02:15 +00:00
|
|
|
payload = {
|
|
|
|
"from": noreply,
|
|
|
|
"to": to,
|
|
|
|
"subject": subject,
|
|
|
|
"template": template,
|
2023-10-05 19:18:05 +00:00
|
|
|
"h:X-Mailgun-Variables": '{ "token": "%s" }' % token,
|
2022-10-22 12:02:15 +00:00
|
|
|
}
|
2023-10-05 19:18:05 +00:00
|
|
|
print("[auth.email] payload: %r" % payload)
|
2022-11-22 03:11:26 +00:00
|
|
|
# debug
|
|
|
|
# print('http://localhost:3000/?modal=auth&mode=confirm-email&token=%s' % token)
|
2023-10-05 19:18:05 +00:00
|
|
|
async with AsyncClient() as client:
|
|
|
|
response = await client.post(api_url, headers=headers, data=gql)
|
|
|
|
if response.status_code != 200:
|
|
|
|
return False, None
|
|
|
|
r = response.json()
|
|
|
|
api_url, auth=("api", MAILGUN_API_KEY), data=payload
|
|
|
|
)
|
|
|
|
response.raise_for_status()
|
2022-10-05 15:54:29 +00:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|