Files
core/auth/email.py

31 lines
1.1 KiB
Python
Raw Normal View History

2023-10-26 22:38:31 +02:00
import requests
2023-10-27 00:07:35 +03:00
from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
2023-10-30 22:00:55 +01: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 18:54:29 +03:00
2022-10-21 08:47:58 +03:00
2022-12-01 20:26:53 +01:00
async def send_auth_email(user, token, lang="ru", template="email_confirmation"):
2022-10-05 18:54:29 +03:00
try:
2022-10-21 00:05:37 +02:00
to = "%s <%s>" % (user.name, user.email)
2023-10-30 22:00:55 +01:00
if lang not in ["ru", "en"]:
lang = "ru"
2022-10-22 15:02:15 +03:00
subject = lang_subject.get(lang, lang_subject["en"])
2022-11-27 16:14:17 +03:00
template = template + "_" + lang
2022-10-22 15:02:15 +03:00
payload = {
"from": noreply,
"to": to,
"subject": subject,
"template": template,
2023-10-30 22:00:55 +01:00
"h:X-Mailgun-Variables": '{ "token": "%s" }' % token,
2022-10-22 15:02:15 +03:00
}
2023-10-30 22:00:55 +01:00
print("[auth.email] payload: %r" % payload)
2022-11-22 04:11:26 +01:00
# debug
# print('http://localhost:3000/?modal=auth&mode=confirm-email&token=%s' % token)
2023-10-30 22:00:55 +01:00
response = requests.post(api_url, auth=("api", MAILGUN_API_KEY), data=payload)
2022-10-05 18:54:29 +03:00
response.raise_for_status()
except Exception as e:
print(e)