0

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.

Schalton
  • 2,867
  • 2
  • 32
  • 44
  • problem statement is not clear. Add more sample code and clearly elaborate your problem and expected output. – Akshay Pratap Singh Sep 07 '19 at 10:27
  • I'm using Django rest auth, and want to override the method that handles sending emails, and I know how to write what I want to replace it with, but don't know what method(s) to override or how to reference them once I do. – Schalton Sep 07 '19 at 14:48
  • It sounds like your question is: how can you override where [django-rest-auth](https://github.com/Tivix/django-rest-auth) sends password/account emails so that you can add [django-anymail](https://github.com/anymail/django-anymail)-specific options like `template_id`? It looks like password reset emails come from [`PasswordResetSerializer`](https://github.com/Tivix/django-rest-auth/blob/master/rest_auth/serializers.py#L156-L162) via `django.contrib.auth`'s `PasswordResetForm`, so you'd need to override that. Something like this: https://stackoverflow.com/q/54153097/647002 – medmunds Sep 07 '19 at 17:09
  • 1
    (Also, FYI: including `**request.data` in your template data could be a security risk, as it will include *every* GET and POST parameter. Someone might use that to supply template merge data fields you didn't intend. Better to extract only the specific GET or POST fields your template needs.) – medmunds Sep 07 '19 at 17:13

1 Answers1

0

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 
Wade Williams
  • 3,943
  • 1
  • 26
  • 35
  • thank you for taking the time to respond. You're answer is correct in so much as it says what to do -- it is essentially quoted from the docs: "If this does not suit your needs, you can hook up your own custom mechanism by overriding the send_mail method of the account adapter (allauth.account.adapter.DefaultAccountAdapter)." I'm not sure how to do this. It also seems like I'd more need to override the "render_mail" method, but I'm not sure about that either. My goal is to treat all of the account emails as transactional emails and use Postmark templates. – Schalton Sep 08 '19 at 22:51
  • Added a basic example. You can subclass the DefaultAccountAdapter which will preserve all the DefaultAccountAdapter's functionality and you can override only what you need. This uses traditional object inheritance allowing you to augment functionality from the parent class. – Wade Williams Sep 08 '19 at 22:57
  • So the correct way to handle this is by using a switch case equivalent on the template prefix param to handle the various cases? registration, password reset, etc? – Schalton Sep 08 '19 at 23:01