Noel-Teens commited on
Commit
6b6a264
·
1 Parent(s): fd9b2eb

resend API implemented

Browse files
Files changed (3) hide show
  1. admin_app/views.py +35 -29
  2. pms_server/settings.py +14 -1
  3. requirements.txt +3 -2
admin_app/views.py CHANGED
@@ -27,8 +27,6 @@ import os
27
  import requests
28
  import zipfile
29
  import base64
30
- from sendgrid import SendGridAPIClient
31
- from sendgrid.helpers.mail import Mail
32
  from django.http import FileResponse, Http404, JsonResponse, HttpResponseNotFound
33
 
34
  def _auth_from_query_token(request):
@@ -64,7 +62,7 @@ import logging
64
  logger = logging.getLogger(__name__)
65
 
66
  def _send_invite_email(email: str, invite_link: str, inviter: str = None):
67
- """Send an invitation email using SendGrid (works on Hugging Face)."""
68
  subject = "You're invited to join Project Management System"
69
  html_content = render_to_string(
70
  "emails/invite.html", {"invite_link": invite_link, "inviter": inviter}
@@ -74,39 +72,47 @@ def _send_invite_email(email: str, invite_link: str, inviter: str = None):
74
  f"Click the link to accept: {invite_link}"
75
  )
76
 
 
77
  try:
78
- api_key = os.getenv("SENDGRID_API_KEY")
79
  if not api_key:
80
- raise Exception("SENDGRID_API_KEY not set in environment.")
81
-
82
- from_email = os.getenv("FROM_EMAIL") or os.getenv("FROM_MAIL")
83
- if not from_email:
84
- raise Exception("FROM_EMAIL (or FROM_MAIL) not set in environment.")
85
-
86
- message = Mail(
87
- from_email=from_email,
88
- to_emails=email,
89
- subject=subject,
90
- plain_text_content=text_content,
91
- html_content=html_content
92
- )
93
-
94
- sg = SendGridAPIClient(api_key)
95
- response = sg.send(message)
96
 
97
- if response.status_code not in (200, 202):
98
- logger.error(f"SendGrid API error {response.status_code}: {response.body}")
99
- raise Exception(f"SendGrid API error {response.status_code}: {response.body}")
100
-
101
- logger.info(f"Invitation email sent successfully to {email}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  return {
103
  "status_code": response.status_code,
104
- "body": response.body.decode() if response.body else "",
105
- "headers": dict(response.headers)
106
  }
107
-
108
  except Exception as e:
109
- logger.error(f"Failed to send email to {email}: {str(e)}")
110
  raise
111
 
112
  @csrf_exempt
 
27
  import requests
28
  import zipfile
29
  import base64
 
 
30
  from django.http import FileResponse, Http404, JsonResponse, HttpResponseNotFound
31
 
32
  def _auth_from_query_token(request):
 
62
  logger = logging.getLogger(__name__)
63
 
64
  def _send_invite_email(email: str, invite_link: str, inviter: str = None):
65
+ """Send an invitation email using Resend API."""
66
  subject = "You're invited to join Project Management System"
67
  html_content = render_to_string(
68
  "emails/invite.html", {"invite_link": invite_link, "inviter": inviter}
 
72
  f"Click the link to accept: {invite_link}"
73
  )
74
 
75
+ # Try Resend API first
76
  try:
77
+ api_key = getattr(settings, 'RESEND_API_KEY', None)
78
  if not api_key:
79
+ raise Exception("RESEND_API_KEY not set in settings.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
 
81
+ from_email = getattr(settings, 'FROM_EMAIL', 'PMS <noreply@neurostack.com>')
82
+
83
+ # Resend API payload
84
+ payload = {
85
+ "from": from_email,
86
+ "to": [email],
87
+ "subject": subject,
88
+ "html": html_content,
89
+ "text": text_content
90
+ }
91
+
92
+ headers = {
93
+ "Authorization": f"Bearer {api_key}",
94
+ "Content-Type": "application/json"
95
+ }
96
+
97
+ response = requests.post(
98
+ "https://api.resend.com/emails",
99
+ json=payload,
100
+ headers=headers
101
+ )
102
+
103
+ if response.status_code not in (200, 201):
104
+ logger.error(f"Resend API error {response.status_code}: {response.text}")
105
+ raise Exception(f"Resend API error {response.status_code}: {response.text}")
106
+
107
+ logger.info(f"Invitation email sent successfully to {email} via Resend API")
108
  return {
109
  "status_code": response.status_code,
110
+ "body": response.text,
111
+ "method": "resend_api"
112
  }
113
+
114
  except Exception as e:
115
+ logger.error(f"Failed to send invitation email to {email}: {str(e)}")
116
  raise
117
 
118
  @csrf_exempt
pms_server/settings.py CHANGED
@@ -382,4 +382,17 @@ CORS_ALLOW_PRIVATE_NETWORK = True
382
  APPEND_SLASH=False
383
 
384
  # Frontend URL for invitation links
385
- FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:3000")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
382
  APPEND_SLASH=False
383
 
384
  # Frontend URL for invitation links
385
+ FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:3000")
386
+
387
+ # Email configuration for Resend API
388
+ RESEND_API_KEY = os.getenv("RESEND_API_KEY")
389
+ FROM_EMAIL = os.getenv("FROM_EMAIL", "PMS <onboarding@resend.dev>")
390
+
391
+ # Validate Resend API configuration
392
+ if not RESEND_API_KEY:
393
+ import warnings
394
+ warnings.warn(
395
+ "RESEND_API_KEY not set in environment. Email sending will fail. "
396
+ "Please set RESEND_API_KEY environment variable.",
397
+ RuntimeWarning
398
+ )
requirements.txt CHANGED
@@ -6,9 +6,10 @@ djangorestframework-simplejwt
6
  uvicorn==0.34.0
7
  asgiref==3.8.1
8
  dotenv
9
- sendgrid
10
  django-storages==1.14.3
11
  boto3==1.35.40
12
  botocore==1.35.40
13
  PyMySQL==1.1.0
14
- python-dotenv==1.0.1
 
 
 
6
  uvicorn==0.34.0
7
  asgiref==3.8.1
8
  dotenv
 
9
  django-storages==1.14.3
10
  boto3==1.35.40
11
  botocore==1.35.40
12
  PyMySQL==1.1.0
13
+ python-dotenv==1.0.1
14
+ resend==1.0.0
15
+ requests