Noel-Teens commited on
Commit
4927596
·
1 Parent(s): c34f45e

resend api implemented

Browse files
Files changed (2) hide show
  1. admin_app/views.py +54 -8
  2. pms_server/settings.py +9 -5
admin_app/views.py CHANGED
@@ -56,15 +56,51 @@ def _build_invite_link(token: str) -> str:
56
  return f"{settings.FRONTEND_URL}/accept-invite/{token}"
57
 
58
 
 
 
 
 
 
59
  def _send_invite_email(email: str, invite_link: str, inviter: str = None):
60
- """Send an HTML + text email."""
61
  subject = "You're invited to join Project Management System"
62
  text_content = f"You have been invited to join the Project Management System. Click the link to accept: {invite_link}"
63
  html_content = render_to_string("emails/invite.html", {"invite_link": invite_link, "inviter": inviter})
64
 
65
- msg = EmailMultiAlternatives(subject, text_content, settings.DEFAULT_FROM_EMAIL, [email])
66
- msg.attach_alternative(html_content, "text/html")
67
- msg.send()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
 
69
  @csrf_exempt
70
  @api_view(["POST"])
@@ -96,10 +132,20 @@ def invite_user(request):
96
 
97
  invite_link = _build_invite_link(invitation.token)
98
 
99
- # Send email
100
- _send_invite_email(email, invite_link, inviter=getattr(request.user, "username", None))
101
-
102
- return Response({"message": "Invitation sent."}, status=status.HTTP_201_CREATED)
 
 
 
 
 
 
 
 
 
 
103
 
104
 
105
  @csrf_exempt
 
56
  return f"{settings.FRONTEND_URL}/accept-invite/{token}"
57
 
58
 
59
+ import logging
60
+
61
+ logger = logging.getLogger(__name__)
62
+
63
+
64
  def _send_invite_email(email: str, invite_link: str, inviter: str = None):
65
+ """Send an HTML + text email using Resend SMTP with fallback."""
66
  subject = "You're invited to join Project Management System"
67
  text_content = f"You have been invited to join the Project Management System. Click the link to accept: {invite_link}"
68
  html_content = render_to_string("emails/invite.html", {"invite_link": invite_link, "inviter": inviter})
69
 
70
+ try:
71
+ # Use Resend SMTP with professional from address
72
+ msg = EmailMultiAlternatives(
73
+ subject=subject,
74
+ body=text_content,
75
+ from_email=settings.DEFAULT_FROM_EMAIL, # "PMS <noreply@neurostack.com>"
76
+ to=[email]
77
+ )
78
+ msg.attach_alternative(html_content, "text/html")
79
+ msg.send()
80
+ logger.info(f"Invitation email sent successfully to {email} via Resend SMTP")
81
+
82
+ except Exception as e:
83
+ logger.error(f"Failed to send email via Resend SMTP to {email}: {str(e)}")
84
+
85
+ # Fallback to console backend for development/testing
86
+ try:
87
+ from django.core.mail.backends.console import EmailBackend
88
+ console_backend = EmailBackend()
89
+
90
+ msg = EmailMultiAlternatives(
91
+ subject=subject,
92
+ body=text_content,
93
+ from_email=settings.DEFAULT_FROM_EMAIL,
94
+ to=[email],
95
+ connection=console_backend
96
+ )
97
+ msg.attach_alternative(html_content, "text/html")
98
+ msg.send()
99
+ logger.warning(f"Email sent to {email} via console fallback due to SMTP failure")
100
+
101
+ except Exception as fallback_error:
102
+ logger.error(f"Both SMTP and console fallback failed for {email}: {str(fallback_error)}")
103
+ raise Exception(f"Email sending failed: {str(e)}")
104
 
105
  @csrf_exempt
106
  @api_view(["POST"])
 
132
 
133
  invite_link = _build_invite_link(invitation.token)
134
 
135
+ # Send email with error handling
136
+ try:
137
+ _send_invite_email(email, invite_link, inviter=getattr(request.user, "username", None))
138
+ return Response({"message": "Invitation sent successfully."}, status=status.HTTP_201_CREATED)
139
+ except Exception as e:
140
+ # If email fails, we should clean up the invitation or mark it as failed
141
+ logger.error(f"Failed to send invitation email to {email}: {str(e)}")
142
+ # Optionally delete the invitation if email failed
143
+ if created:
144
+ invitation.delete()
145
+ return Response(
146
+ {"error": "Failed to send invitation email. Please check the email address and try again."},
147
+ status=status.HTTP_500_INTERNAL_SERVER_ERROR
148
+ )
149
 
150
 
151
  @csrf_exempt
pms_server/settings.py CHANGED
@@ -251,14 +251,18 @@ CORS_ALLOW_PRIVATE_NETWORK = True
251
 
252
  APPEND_SLASH=False
253
 
254
- # Email configuration
255
- # For production - use SMTP (uncomment and configure these settings)
256
  EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
257
- EMAIL_HOST = "smtp.gmail.com"
258
  EMAIL_PORT = 587
259
  EMAIL_USE_TLS = True
260
- EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER")
261
- EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD")
262
 
 
 
263
  FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:5173")
264
 
 
 
 
 
251
 
252
  APPEND_SLASH=False
253
 
254
+ # Email configuration using Resend SMTP
 
255
  EMAIL_BACKEND = "django.core.mail.backends.smtp.EmailBackend"
256
+ EMAIL_HOST = "smtp.resend.com"
257
  EMAIL_PORT = 587
258
  EMAIL_USE_TLS = True
259
+ EMAIL_HOST_USER = "resend"
260
+ EMAIL_HOST_PASSWORD = os.getenv("RESEND_API_KEY")
261
 
262
+ # Professional from address format
263
+ DEFAULT_FROM_EMAIL = "PMS <noreply@neurostack.com>"
264
  FRONTEND_URL = os.getenv("FRONTEND_URL", "http://localhost:5173")
265
 
266
+ # For development/testing - uncomment to use console backend
267
+ # EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
268
+