发送电子邮件后登录 Django Allauth

huangapple go评论62阅读模式
英文:

Sending email after Login Django Allauth

问题

有没有办法在使用Django Allauth时实现登录后发送电子邮件?例如,每当有人登录到我的帐户时,我会自动收到一封电子邮件。

英文:

I’m new using Django Allauth, is there a way I can implement sending email after login? For example; every time someone logged into my account I will receive an email automatically.

答案1

得分: 0

你可以覆盖登录表单并在其中添加任何你想要的内容。查看这里。例如,你可以添加代码来使用Django的email功能发送电子邮件。

自定义登录表单可能位于accounts应用程序中。在该目录中,你可以添加一个名为forms.py的文件,其中包含以下内容,它将覆盖(实际上不是覆盖,而是添加到)allauth的登录表单。

# forms.py

from django import forms
from django.core.mail import send_mail
from allauth.account.forms import LoginForm

class MyCustomLoginForm(LoginForm):

    def login(self, *args, **kwargs):

        # 在此处,你可以添加其他处理,每当有人使用allauth登录时,都会触发这些处理,最终会发送你想要的消息:
        # ...
        send_mail(
            "主题",
            "这是消息。",
            "from@example.com",
            ["to@example.com"],
            fail_silently=False,
        )

        # 你必须返回原始结果。
        return super(MyCustomLoginForm, self).login(*args, **kwargs)

确保在settings.py中添加以下内容,以便Django知道使用这个自定义登录表单而不是allauth的表单(尽管你实际上将使用allauth的登录表单,只是添加了额外的内容):

# settings.py

ACCOUNT_FORMS = {'login': 'accounts.forms.MyCustomLoginForm'}

还要了解,根据你使用的电子邮件服务器(例如,使用Zoho时),你可能需要在settings.py中进行其他更改,需要遵循Zoho Django配置以使其正常工作。

你不应该需要修改views.py,因为所有处理都在覆盖的表单中完成。

**注意:**尽管这个方法应该能够工作,但我还没有时间测试它!

英文:

Answer:
You can override the login form and add whatever you want there. Checkout this. For example you can add code to send you email using django's email functionality.

The custom login form will probably be in the accounts app. Within this directory you can add a forms.py with the following, which will overrride (well, not really override, rather, add to) allauth's login form.

# forms.py

from django import forms
from django.core.mail import send_mail
from allauth.account.forms import LoginForm

class MyCustomLoginForm(LoginForm):

    def login(self, *args, **kwargs):

        # HERE is where you would add additional processing whenever someone logs in
        # using allauth, ending with the actual sending of the message you want:
        # ...
        send_mail(
            "Subject here",
            "Here is the message.",
            "from@example.com",
            ["to@example.com"],
            fail_silently=False,
        )

        # You must return the original result.
        return super(MyCustomLoginForm, self).login(*args, **kwargs)

Make sure to add the following to settings.py so that django knows to use this custom login form instead of the one from allauth (even though you will actually be using allauth's login form, just with extra stuff added):

# settings.py

ACCOUNT_FORMS = {'login': 'accounts.forms.MyCustomLoginForm'}

Also understand that you will probably be making other changes in settings.py depending on which email server you are using (for example, with Zoho you'd need to do follow Zoho Django Configuration in your settings to get it to work.

You shouldn't need to mess with views.py as all the processing is done in the overridden form.

NOTE: Although this should work, I have not had time to test it!

huangapple
  • 本文由 发表于 2023年7月10日 20:26:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76653733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定