Spaces:
Running
Running
axeronis commited on
Commit ·
29e7a84
1
Parent(s): 20ee727
feat: Configure Resend for email with smart fallback (onboarding@resend.dev) and fix email tasks
Browse files- backend/app/services/email.py +21 -14
- backend/app/tasks/email.py +2 -2
backend/app/services/email.py
CHANGED
|
@@ -108,21 +108,17 @@ class EmailService:
|
|
| 108 |
"""Send email via Resend API (HTTP)."""
|
| 109 |
import httpx
|
| 110 |
|
| 111 |
-
|
| 112 |
async with httpx.AsyncClient() as client:
|
| 113 |
payload = {
|
| 114 |
-
"from":
|
| 115 |
"to": to,
|
| 116 |
"subject": subject,
|
| 117 |
"html": html_body,
|
| 118 |
"text": text_body
|
| 119 |
}
|
| 120 |
|
| 121 |
-
|
| 122 |
-
# We will handle this gracefully: if user provided key but hasn't configured domain,
|
| 123 |
-
# they should probably set EMAIL_FROM to their verified domain or onboarding@resend.dev
|
| 124 |
-
|
| 125 |
-
response = await client.post(
|
| 126 |
"https://api.resend.com/emails",
|
| 127 |
headers={
|
| 128 |
"Authorization": f"Bearer {settings.RESEND_API_KEY}",
|
|
@@ -130,14 +126,23 @@ class EmailService:
|
|
| 130 |
},
|
| 131 |
json=payload
|
| 132 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
# Fallback log mainly for dev
|
| 137 |
-
return self._fallback_log(to, subject, text_body)
|
| 138 |
-
|
| 139 |
-
logger.info("Email sent successfully (Resend API)", to=to, id=response.json().get("id"))
|
| 140 |
-
return True
|
| 141 |
|
| 142 |
except Exception as e:
|
| 143 |
logger.error(f"Resend API unexpected error: {e}")
|
|
@@ -364,3 +369,5 @@ def get_email_service() -> EmailService:
|
|
| 364 |
if _email_service is None:
|
| 365 |
_email_service = EmailService()
|
| 366 |
return _email_service
|
|
|
|
|
|
|
|
|
| 108 |
"""Send email via Resend API (HTTP)."""
|
| 109 |
import httpx
|
| 110 |
|
| 111 |
+
async def try_send(from_addr: str):
|
| 112 |
async with httpx.AsyncClient() as client:
|
| 113 |
payload = {
|
| 114 |
+
"from": from_addr,
|
| 115 |
"to": to,
|
| 116 |
"subject": subject,
|
| 117 |
"html": html_body,
|
| 118 |
"text": text_body
|
| 119 |
}
|
| 120 |
|
| 121 |
+
return await client.post(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
"https://api.resend.com/emails",
|
| 123 |
headers={
|
| 124 |
"Authorization": f"Bearer {settings.RESEND_API_KEY}",
|
|
|
|
| 126 |
},
|
| 127 |
json=payload
|
| 128 |
)
|
| 129 |
+
|
| 130 |
+
try:
|
| 131 |
+
# 1. Try with configured email
|
| 132 |
+
from_addr = f"MicroFunnel Studio <{self.from_email}>"
|
| 133 |
+
response = await try_send(from_addr)
|
| 134 |
+
|
| 135 |
+
# 2. If rejected (likely domain not verified), try onboarding@resend.dev
|
| 136 |
+
if response.status_code in [403, 422]:
|
| 137 |
+
logger.warning(f"Resend rejected sender '{from_addr}'. Retrying with 'onboarding@resend.dev'...")
|
| 138 |
+
response = await try_send("onboarding@resend.dev")
|
| 139 |
+
|
| 140 |
+
if response.status_code >= 400:
|
| 141 |
+
logger.error(f"Resend API Error: {response.text}")
|
| 142 |
+
return self._fallback_log(to, subject, text_body)
|
| 143 |
|
| 144 |
+
logger.info("Email sent successfully (Resend API)", to=to, id=response.json().get("id"))
|
| 145 |
+
return True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
except Exception as e:
|
| 148 |
logger.error(f"Resend API unexpected error: {e}")
|
|
|
|
| 369 |
if _email_service is None:
|
| 370 |
_email_service = EmailService()
|
| 371 |
return _email_service
|
| 372 |
+
|
| 373 |
+
email_service = get_email_service()
|
backend/app/tasks/email.py
CHANGED
|
@@ -16,9 +16,9 @@ def send_email_task(to_email: str, subject: str, html_content: str):
|
|
| 16 |
|
| 17 |
# Run the async service method
|
| 18 |
loop.run_until_complete(email_service.send_email(
|
| 19 |
-
|
| 20 |
subject=subject,
|
| 21 |
-
|
| 22 |
))
|
| 23 |
loop.close()
|
| 24 |
return f"Email sent to {to_email}"
|
|
|
|
| 16 |
|
| 17 |
# Run the async service method
|
| 18 |
loop.run_until_complete(email_service.send_email(
|
| 19 |
+
to=[to_email],
|
| 20 |
subject=subject,
|
| 21 |
+
html_body=html_content
|
| 22 |
))
|
| 23 |
loop.close()
|
| 24 |
return f"Email sent to {to_email}"
|