可以使用 API 代替 SMTP 以在 Django 中發送郵件,尤其是重置密碼和郵件確認等。
如果我能在 django 中得到關于某個主題的澄清,我會非常高興。我是 django 的新手,在發送郵件時,我注冊了 Mailgun 并使用了 API,因為我之前使用過請求,但選擇 django 來使用我正在嘗試使用 DJ-Rest-auth 和 django_allauth 進行用戶注冊和有關于使用 SMTP 配置電子郵件后端的事情。
我的問題是
- 如果是作業流如何連接我的密碼重置以將 api 用于郵件,我可以不使用 SMTP 進行 django_allauth。
- 當用戶注冊時,我可以輕松地傳入郵件功能作為視圖中的警報
我知道我可以使用 django 發送郵件,但是諸如重置密碼之類的東西附加了 uuid 我該如何使用 API 來處理它
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)
uj5u.com熱心網友回復:
您不應該撰寫自己的普通郵件發送功能,您應該始終使用 Django 的內置send_mail(或相關)功能,并在您的設定中配置自定義電子郵件后端。
如果您需要更改電子郵件的發送方式,您可以撰寫自己的電子郵件后端。在
EMAIL_BACKEND您的設定檔案設定然后你的后臺類Python的匯入路徑。https://docs.djangoproject.com/en/3.2/topics/email/#defining-a-custom-email-backend
Django 可以在各種情況下自動發送各種電子郵件,因此集中該配置以便所有電子郵件發送都使用您配置的郵件發送設定很重要,除非您有特定的理由反對。
鑒于這是一個可插拔的架構,并且 Django 和 Mailgun 都很流行,有一些現有的模塊允許您通過如此簡單的配置更改通過 Mailgun 發送電子郵件:https ://djangopackages.org/grids/g/email/
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/383253.html
標籤:蟒蛇-3.x 姜戈 电子邮件 Django 休息框架 django-allauth
