I have created contact form with react and connected it with mysql database hosted using RDS. i used django for building the database. before some days i was able to get email when i am submitting the form and the data was also been stored in the database but now when i am submitting the form i am not getting an email notification of submission but i can see the data in my database. i also tried to send test email from the console and it worked. when i changed the API from urls.py and then i submitted the form, i got the mail but when i did the submission again i did not get any mail.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': config('DB_NAME', default='myportfolio'),
'USER': config('DB_USER', default='root'),
'PASSWORD': config('DB_PASSWORD', default=''),
'HOST': config('DB_HOST', default=''),
'PORT': config('DB_PORT', default='3306'),
}
}
# Email backend configuration
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
@csrf_exempt
def form_submission_view(request):
if request.method == 'POST':
try:
data = json.loads(request.body)
name = data.get('name')
email = data.get('email')
subject = data.get('subject')
message = data.get('message')
if not name:
return JsonResponse({'error': 'Name is required'}, status=400)
if not email:
return JsonResponse({'error': 'Email is required'}, status=400)
if not subject:
return JsonResponse({'error': 'Subject is required'}, status=400)
if not message:
return JsonResponse({'error': 'Message is required'}, status=400)
Form.objects.create(name=name, email=email, subject=subject, message=message)
send_mail(
'New Form Submission',
f'You have a new form submission:\n\nName: {name}\nEmail: {email}\nSubject: {subject}\nMessage: {message}',
settings.EMAIL_HOST_USER,
[settings.EMAIL_HOST_USER],
fail_silently=False,
)
return JsonResponse({'message': 'Form submitted successfully!'})
except Exception as e:
print(f"Error: {e}")
return JsonResponse({'error': 'Invalid data'}, status=400)
else:
return JsonResponse({'error': 'Invalid request method'}, status=405)