core/auth/email.py

31 lines
860 B
Python
Raw Normal View History

import requests
2022-10-05 15:54:29 +00:00
from settings import MAILGUN_API_KEY, MAILGUN_DOMAIN
2022-10-05 15:54:29 +00:00
api_url = "https://api.mailgun.net/v3/%s/messages" % MAILGUN_DOMAIN
noreply = "discours.io <noreply@%s>" % MAILGUN_DOMAIN
2022-10-05 17:06:29 +00:00
subject = "Confirm email"
2022-10-05 15:54:29 +00:00
tmplt = """<html><body>
Follow the <a href='%s'>link</a> to authorize
</body></html>
"""
2022-10-05 15:54:29 +00:00
async def send_auth_email(user, token):
try:
to = "%s <%s>" % (user.username, user.email)
2022-10-05 17:06:29 +00:00
url_with_token = "https://newapi.discours.io/confirm/" + token
2022-10-05 15:54:29 +00:00
response = requests.post(
api_url,
auth=("api", MAILGUN_API_KEY),
data={
"from": noreply,
"to": to,
2022-10-05 17:06:29 +00:00
"subject": subject,
2022-10-05 15:54:29 +00:00
"html": tmplt % url_with_token,
},
)
response.raise_for_status()
except Exception as e:
print(e)