电子邮件确认的集成非常简单。如果你有一个可以用来发送邮件的现有邮件服务器,你不必设置任何邮件服务器。您只需在标准Django设置中填写用于发送邮件的数据,电子邮件确认将使用该数据:
# e-mail settings
# XXXXXXXXXXXXXXXXXXXXXXX THESE ARE NOT YET PRODUCTIONREADY!
EMAIL_HOST= mail.your_mailserver.com
EMAIL_PORT=1025
EMAIL_HOST_USER= your_username
EMAIL_HOST_PASSWORD= your_password
总结下一步要做的事情:你必须创建一个表单来输入电子邮件地址(为什么这样的表单没有附带电子邮件配置的原因有点模糊)。这看起来像这样:
# email form using emailconfirmation
class AddEmailForm(forms.Form):
def __init__(self, *args, **kwargs):
try:
self.user = kwargs.pop( user )
except KeyError:
pass
super(AddEmailForm, self).__init__(*args, **kwargs)
email = forms.EmailField(label="Email",required=True, widget=forms.TextInput())
def clean_email(self):
try:
EmailAddress.objects.get(user=self.user, email=self.cleaned_data["email"])
except EmailAddress.DoesNotExist:
try:
User.objects.get(email = self.cleaned_data[ email ])
except User.DoesNotExist:
return self.cleaned_data["email"]
raise forms.ValidationError(u"email address associated with another account.")
def save(self):
try:
self.user.message_set.create(message="Confirmation email sent to %s" % self.cleaned_data["email"])
except AttributeError:
pass
return EmailAddress.objects.add_email(self.user, self.cleaned_data["email"])
这将允许用户输入电子邮件地址,检查电子邮件地址是否存在并且是否被其他帐户使用。之后,它会将电子邮件添加到未确认的电子邮件中,并向用户发送一封带有链接的电子邮件。然后,用户可以通过单击链接确认电子邮件。
这就是全部。让我们希望Pinax的球员很快就能对他们的球队进行高质量的进攻;)