django - 带有邮戳的 django rest-auth 电子邮件确认
问题描述
我已经看到了很多关于如何覆盖电子邮件模板的内容,但它们似乎主要涉及创建 HTML 模板和覆盖文件位置。
我正在使用邮戳的模板,其中涉及发送带有电子邮件变量的发布请求。我正在使用 anymail 来处理这个问题,如下所示,其中有一个向我的客户服务地址发送电子邮件的表单:
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)
我的目标是也将邮戳模板用于帐户确认和密码重置电子邮件,但我很难弄清楚如何覆盖发送方法。
解决方案
rest_auth
仅提供其余框架接口到allauth
.
您想覆盖 allauth 的电子邮件,而不是 rest_auth 的。
关于发送电子邮件的 allauth 文档allauth.account.adapter.DefaultAccountAdapter.send_email()
您可以通过ACCOUNT_ADAPTER
在 settings.py配置中设置并继承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