djangodjango-rest-frameworkdjango-rest-authpostmarkdjango-anymail

django rest-auth email confirmation with postmark


I've seen a fair bit about how to override the email templates, but they seem to mostly involve creating HTML templates and overriding the file location.

I'm using postmark's templating which involves sending a post request with the email variables. I'm handling that with anymail, as shown below with a form that sends my customer service address an email:

class PartnerContact(APIView):
    """Sends email to Partners@***.com"""

    @authentication_classes([])
    @permission_classes([])
    def post(self, request):
        """Sends Form Data"""

        print("PartnerContact data", request.data)

        status_code = status.HTTP_400_BAD_REQUEST

        msg = EmailMessage(
            from_email='Partners@***.com',
            to=['Partners@***.com'],
            reply_to=[request.data['email']]
        )

        msg.template_id = ***

        logo = attach_inline_image_file(msg, finders.find("***.png"))

        msg.merge_global_data = { **{**request.data, **{"logo":logo} } }

        # <img alt="Logo" src="cid:{logo_cid}">
        msg.send()
        status_code = status.HTTP_200_OK

        return Response(status=status_code)

My goal is to use postmark templates for the account confirmation & password reset emails also, but I'm having a hard time figuring out how to override the send methods.


Solution

  • rest_auth only provides the rest framework interface to allauth.

    You want to override allauth's emails, not rest_auth's.

    allauth doc on sending email allauth.account.adapter.DefaultAccountAdapter.send_email()

    You can do this by setting the ACCOUNT_ADAPTER in your settings.py configuration and subclassing the DefaultAccountAdapter

    from allauth.account.adapter import DefaultAccountAdapter
    
    CustomAccountAdapter(DefaultAccountAdapter):
        def send_mail(self, template_prefix, email, context):
            # handle send mail the way you want to
            pass