I cant send email with aws-ses with python

setting.py =

EMAIL_BACKEND = 'django_amazon_ses.EmailBackend' 
AWS_SES_REGION = env('AWS_SES_REGION') 
AWS_SES_SENDER_MAIL = env('AWS_SES_SENDER_MAIL') 
AWS_SES_SENDER = env('AWS_SES_SENDER') 
AWS_ACCESS_KEY = env('AWS_ACCESS_KEY') 
AWS_KEY_ID = env('AWS_KEY_ID')

views.py =

  try:
                subject = f"Registration Confirmation for {event.title}"
                message = f"""
                Hello {request.user.username},

                You have successfully registered for the event: {event.title}

                Event Details:
                Date: {event.date}
                Location: {event.location}
                Description: {event.description}

                Thank you for joining!
                """

                send_mail(
                    subject,
                    message,
                    settings.AWS_SES_SENDER_MAIL,
                    [request.user.email],
                    fail_silently=False
                )
                return Response({
                    'message': 'Event joined and email sent.',
                    'email_status': 'sent'
                }, status=status.HTTP_200_OK)
            except Exception as e:
                return Response({
                    'message': 'Event joined but email could not be sent.',
                    'email_error': str(e)
                }, status=status.HTTP_200_OK)
        else:
            return Response({'error': 'Event member limit reached. Cannot join.'}, 
                          status=status.HTTP_406_NOT_ACCEPTABLE)

I am trying to send an e-mail when a function runs in views.py. I get HTTP 200, although there is no problem in the code, my email is not delivered, but I do not receive any errors Can anyone help?

Receiving 200 response code while experiencing unexpected behavior (no email delivered) must be due to the HTTP_200_OK you are returning on exception.

               return Response({
                    'message': 'Event joined and email sent.',
                    'email_status': 'sent'
                }, status=status.HTTP_200_OK)  # <- success 
            except Exception as e:
                return Response({
                    'message': 'Event joined but email could not be sent.',
                    'email_error': str(e)
                }, status=status.HTTP_200_OK)  # <- fail

Try returning an error code for exceptions and start debugging from there.

... This seems to be one of those programmer things when little mistakes are hard to catch until someone finds them for you.

Вернуться на верх