The Django site I'm working on at the moment sends out emails when content is updated.
A feature I would like to add is where users can reply to these mails, ie comment on a topic. This reply then appears updated on the page.
What would be a good approach to tackle this? Are there any modules out there that can be dropped in?
First things first, you'll need to be able to receive mail and call a python function upon doing so. There are a number of ways to do this:
Once you're receiving mail and able to call a python script in this event, it could be simple as adding the comment to the correct thread in your Django models.
You should, however, when considering building this feature, also consider ways to make it more secure (emails are notoriously easy to forge) - you don't want random people to be able to post comments as others. This isn't all that easy to do, but unique email reply-to IDs is one reasonably straight-forward way (but in no way guaranteed secure).
A few basic issues that I have encountered while working on a similar project in case it is of any help:
Sending emails: I started using Django's send_mail
and send_mass_mail
in subprocesses, but I am now switching to django-mailer and cron entries so we are able to properly manage mail queues and have a record of what is being sent etc. You may find other apps from the Pinax project useful: http://pinaxproject.com/ecosystem/, like the one for notifications or another for email confirmation.
Fetching emails: I am using poplib as @herman-schaaf suggests, plus a cron entry to check mail every few minutes. I keep a record of all emails downloaded using their UIDL so I can check whether they have been already processed or not.
Watch out for loops: You will probably want to use a Return-Path different from the From address. To prevent flooding, I also put in place checks to block any user that submits more than x mails in a single iteration, or more than n mails per a number of iterations.
Dates and times: You will also probably want to parse the Sent date from the email to track the insertions, instead of using datetime.now()
or timezone.now()
since you can receive emails sent many minutes ago. And I guess you will have to deal with timezones as we had to. We switched to Django version 1.4 when it became available, it makes it easier to deal with timezone aware datetimes than previous versions (see release notes about this here), and combine it with PostgreSQL that stores aware datetimes. Pinax also have an app to ease the implementation of timezone localization for users.
Forged emails: In our case we were not concerned about forged emails due the closed nature of the app, but we notify all the participants in a ticket of any updates, including the creator being notified of the creation itself, so everyone could at least notice if he/she is were to be impersonated (you can include an admin's email address in your notification emails where users can report any suspicious activity).
check out django.core.mail module here...
from django.core.mail import send_mail
send_mail('Subject here', 'Here is the message.', '[email protected]',
['[email protected]'], fail_silently=False)
or checkout this post here...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With