kschaeffler kschaeffler - 11 months ago 69
Python Question

How can I send a user registration confirmation email using Tornado and MongoDB?

I'm working with Tornado and MongoDB and I would like to send a confirmation email to the user when he creates an account in my application.

For the moment, I use a simple XHTML page with a form and I send information to my MongoDB database using Tornado. I would like to have an intermediate step which sends an email to the user before inserting the data into the database.

I would like to know how could I send this email and insert the user account only after the user receives the email and confirms his registration.

Answer Source

What you need is an activation URL. The activation URL contains a unique ID, a UUID perhaps, which is verified when the user clicks the activation URL.

To avoid storing the user data in the database, you could store the user data in the activation URL sent to the user:

import urllib
data = urllib.urlencode({'name':'joe', 'password':'1234'})
activation_url = 'http://example.com/activate?%s' % data

But because user information is sent in plain text this method is very insecure.

The correct way to approach this is to store the user's information along with an activation flag in the database. When the user clicks the activation URL, the unique ID is verified and the activation flag is set to true enabling the new user account.

Sending email in Python is pretty straight forward when you have access to a SMTP server or Gmail account.

Examples sending email from Python: http://docs.python.org/library/email-examples.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download