Search code examples
python-3.xdjangoemaildjango-rest-frameworkdjango-allauth

When to use API vs SMTP in Django


Can API be used to replace SMTP for mail sending in Django especially for things like reset password and mail confirmation.

I will really love if I can get clarification on a topic in django. I am a newbie to django and when it comes to sending mail I register for Mailgun and used the API since I have used requests before but picking up django to work with I am trying to do user registration using DJ-Rest-auth and django_allauth and there is thing about configuring email backend using SMTP.

my question is

  • Can i do without using SMTP for django_allauth if Yes a workflow how to connect my password reset to use the api for mail.
  • I can easily pass in the mail function to serve as an alert in the views when user registers

I know I can send mails using django but things like reset password that has a uuid attached to it how can I go about it using API's

def send_simple_message(name, email, phone, course, mode):
    """ send mail after successful registration. """

    
    return requests.post(
        MAILGUN_BASE_URL,
        auth=("api", MAILGUN_KEY),
        data={"from": "mail <[email protected]>",
            "to": ["[email protected]"],
            "subject": "🚨📢New Student Alert🚨📣",
            "text": f"name: {name}\nemail: {email}\nphone: {phone} Just registered for {course.title()} class and wants the class {mode}!"})
    



class RegistrationAPIView(generics.CreateAPIView):
    """ The Registration API endpoint for cerebro code camp """
    queryset = Registration.objects.all()
    serializer_class = RegistrationSerializer

    def post(self, request, *args, **kwargs):
        serializer = self.serializer_class(data=request.data)
        if serializer.is_valid():
            name = serializer.data['name']
            email = serializer.data['email']
            phone = serializer.data['phone']
            course = serializer.data['course']
            mode = serializer.data['mode']
            send_simple_message(name, email, phone, course, mode)

            return Response(status=status.HTTP_201_CREATED)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
    


Solution

  • You should not write your plain own mail sending function, you should always use Django's builtin send_mail (or related) function(s), and configure a custom email backend in your settings.

    If you need to change how emails are sent you can write your own email backend. The EMAIL_BACKEND setting in your settings file is then the Python import path for your backend class.

    https://docs.djangoproject.com/en/3.2/topics/email/#defining-a-custom-email-backend

    Django can automatically send various emails in various circumstances, so centralising that configuration so all email sending uses your configured mail sending settings is important, unless you have specific reasons against that.

    Given that this is such a pluggable architecture, and both Django and Mailgun are popular, there are existing modules that allow you to send email via Mailgun with such a simple configuration change: https://djangopackages.org/grids/g/email/