django rest send email code example

Example: django rest framework send email

Method-1 : override the create() method of ConsultViewSet

def send_email():
    email = EmailMessage(
        'Title',
        (ConsultSerializer.name, ConsultSerializer.email, ConsultSerializer.phone),
        'my-email',
        ['my-receive-email']
    )
    email.attach_file(ConsultSerializer.file)
    email.send()


class ConsultViewSet(viewsets.ModelViewSet):
    queryset = Consult.objects.all()
    serializer_class = ConsultSerializer

    def create(self, request, *args, **kwargs):
        response = super(ConsultViewSet, self).create(request, *args, **kwargs)
        send_email()  # sending mail
        return response