feat: Ajout de l'envoie de mail [#17]

This commit is contained in:
Luc SORIGNET
2025-05-04 14:52:47 +02:00
parent f38a4414c2
commit 99a882a64a
11 changed files with 181 additions and 8 deletions

View File

@ -1,6 +1,11 @@
from django.http.response import JsonResponse
from rest_framework.views import APIView
from rest_framework.parsers import JSONParser
from django.core.mail import send_mail
from django.utils.html import strip_tags
from django.conf import settings
from rest_framework.response import Response
from rest_framework import status
from .models import *
@ -32,3 +37,30 @@ class MessageSimpleView(APIView):
message_serializer=MessageSerializer(message)
return JsonResponse(message_serializer.data, safe=False)
class SendEmailView(APIView):
"""
API pour envoyer des emails aux parents et professeurs.
"""
def post(self, request):
data = request.data
recipients = data.get('recipients', [])
subject = data.get('subject', 'Notification')
message = data.get('message', '')
if not recipients or not message:
return Response({'error': 'Les destinataires et le message sont requis.'}, status=status.HTTP_400_BAD_REQUEST)
try:
plain_message = strip_tags(message)
send_mail(
subject,
plain_message,
settings.EMAIL_HOST_USER,
recipients,
html_message=message,
fail_silently=False,
)
return Response({'message': 'Email envoyé avec succès.'}, status=status.HTTP_200_OK)
except Exception as e:
return Response({'error': str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)