Isaiah Oladapo
Isaiah Oladapo

Reputation: 47

Django Rest frame Work send email from contact form

I am not receiving emails sent from the contact form. I tried it with postman and it's not sending any emails. I just removed the EMAIL_HOST_USER and EMAIL_HOST_PASSWORD detail when I want to post the question. Here is my code:

Views.py:

from .serializers import *
from rest_framework.permissions import AllowAny
from rest_framework.views import APIView
from django.core.mail import send_mail
from rest_framework import status
from rest_framework.response import Response
from contactform.api.serializers import ContactSerailizer


class ContactView(APIView):
    
    def post(self, request, *args, **kwargs):
         serailizer_class = ContactSerailizer(data=request.data)
        if serailizer_class.is_valid():
             data = serailizer_class.validated_data
             w3lName = request.POST('w3lName')
             w3lSender = request.POST('w3lSender')
             w3lMessage = request.POST('w3lMessage')
             send_mail('Contact Form mail from ' + w3lName,
                       w3lMessage, w3lSender, ['[email protected]'],)
            return Response({"success": "Sent"}, status=status.HTTP_200_OK)
        else:
            return Response({'success': "Failed"}, status=status.HTTP_400_BAD_REQUEST)

Serializers.py:

from rest_framework import serializers

class ContactSerailizer(serializers.Serializer):
    w3lName = serializers.CharField()
    w3lSender = serializers.EmailField()
    w3lMessage = serializers.CharField()

Settings:

#Email config
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_PORT = '587'
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
EMAIL_USE_TLS = True

Contact.html:

<form action="{% url 'contact' %}" method="post" class="signin-form">
                  {% csrf_token %}  
                  <div class="form-input">
                        <input type="text" name="w3lName" id="w3lName" placeholder="Your name">
                    </div>
                    <div class="form-input">
                        <input type="email" name="w3lSender" id="w3lSender" placeholder="Your email address"
                            required="">
                    </div>
                    <div class="form-input">
                        <textarea name="w3lMessage" id="w3lMessage" placeholder="Your message" required=""></textarea>
                    </div>
                    <div class="text-right">
                        <button type="submit" class="btn btn-style btn-primary">Submit</button>
                    </div>
                </form>

Upvotes: 0

Views: 2018

Answers (2)

Elliot13
Elliot13

Reputation: 398

I know this is a late answer, but maybe this will help many others.

Here's how to do it right, with a class.

class ContactView(APIView):
    serializer_class = ContactSerailizer
    
    def post(self, request, *args, **kwargs):
        serailizer_class = ContactSerailizer(data=request.data)
        if serailizer_class.is_valid():
             data = serailizer_class.validated_data
             w3lName = data.get('w3lName')
             w3lSender = data.get('w3lSender')
             w3lMessage = data.get('w3lMessage')
             send_mail('Contact Form mail from ' + w3lName,
                       w3lMessage, w3lSender, ['[email protected]'],)
            return Response({"success": "Sent"}, status=status.HTTP_200_OK)
        else:
            return Response({"error": "Failed"}, status=status.HTTP_400_BAD_REQUEST)

Upvotes: 0

Isaiah Oladapo
Isaiah Oladapo

Reputation: 47

I have been able to fixed it, this is what I used:

Views.py

from rest_framework import status
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.core.mail import send_mail

from contactform.models import ContactModel
from contactform.api.serializers import ContactSerailizer

@api_view(['POST',])
def api_create_contact_view(request):
    if request.method == "POST":
        serializer = ContactSerailizer(data=request.data)
        if serializer.is_valid():
            name = request.POST['name']
            sender = request.POST['sender']
            message = request.POST['message']

            # send mail
            send_mail(
                'Contact Form mail from ' + name,
                message,
                sender,
                ['[email protected]'],
            )
            serializer.save()
            return Response(serializer.data, status=status.HTTP_201_CREATED)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Upvotes: 0

Related Questions