after many diversions and forlorn pivot from copilot to claude, new infra/ structure contains group_vars and ansible vaults; requests added to dev and prod dependencies; apps.lyric.views and core
.settings both abandon django's send_mail(); instead incorporate requests to target Mailgun's HTTP API (DigitalOcean's SMTP blocker thwarted previous magic login link email attempts, but this issue has been resol ved with this commit)
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import requests
|
||||
from django.contrib import auth, messages
|
||||
from django.core.mail import send_mail
|
||||
from django.conf import settings
|
||||
# from django.core.mail import send_mail
|
||||
from django.shortcuts import redirect
|
||||
from django.urls import reverse
|
||||
from .models import Token
|
||||
@@ -11,12 +13,21 @@ def send_login_email(request):
|
||||
reverse("login") + "?token=" + str(token.uid),
|
||||
)
|
||||
message_body = f"Use this magic link to login to your Dashboard:\n\n{url}"
|
||||
send_mail(
|
||||
"A magic login link to your Dashboard",
|
||||
message_body,
|
||||
"adman@howdy.earthmanrpg.me",
|
||||
[email],
|
||||
# Send mail via Mailgun HTTP API
|
||||
response = requests.post(
|
||||
f"https://api.mailgun.net/v3/{settings.MAILGUN_DOMAIN}/messages",
|
||||
auth=("api", settings.MAILGUN_API_KEY),
|
||||
data={
|
||||
"from": "adman@howdy.earthmanrpg.me",
|
||||
"to": email,
|
||||
"subject": "A magic login link to your Dashboard",
|
||||
"text": message_body,
|
||||
},
|
||||
)
|
||||
# Log any errors
|
||||
if response.status_code != 200:
|
||||
print(f"Mailgun API error: {response.status_code}: {response.text}")
|
||||
|
||||
messages.success(
|
||||
request,
|
||||
"Check your email!—there you'll find a magic login link. But hurry… it's only temporary!",
|
||||
|
||||
@@ -156,4 +156,7 @@ EMAIL_HOST = "smtp.mailgun.org"
|
||||
EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER") # switch back to .environ[] when collectstatic moved outside docker build process
|
||||
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD") # switch back to .environ[]
|
||||
EMAIL_PORT = 587
|
||||
EMAIL_USER_TLS = True
|
||||
EMAIL_USE_TLS = True
|
||||
# Mailgun API settings (for HTTP API instead of SMTP)
|
||||
MAILGUN_API_KEY = os.environ.get("MAILGUN_API_KEY")
|
||||
MAILGUN_DOMAIN = "howdy.earthmanrpg.me" # Your Mailgun domain
|
||||
Reference in New Issue
Block a user