Question

How to setup email confirmation in Pinax?, Do I need to setup SMTP server or something like that?, I tried to find documentation about that but failed. Can anyone redirect me to documentation or any related article explaning about this?, Pinax uses emailconfirmation app. I browsed the code of emailconfirmation, But It doesn't include any settings about host or server.

Was it helpful?

Solution

The integration of emailconfirmation is pretty straightforward. You don't have to set up any mailserver, if you have an existing mailserver that you can use to send your mails. You just have to fill in the data used for sending the mails in your standard Django settings, and emailconfirmation is going to use that:

# 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'

To summarize what to do next: You have to create a form for entering an email-adress (The reason why such a form doesn't come with emailconfiguration is somewhat obscure). This can look something like this:

# 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"])

This will allow a user to enter an email address, check if the email-adress allready exists and is in use by another account. After that it will add the emailadress to the unconfirmed emailadresses and send an email with a link to the user. The user then can confirm the emailadress by clicking on the link.

That's all there is to it. Let's hope the Pinax guys will do a quality offensive on their docs soon ;)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top