Mercurial > public > sg101
annotate gpp/mailer/__init__.py @ 450:acc129bfd0d3
Adding google verification file.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 26 Jun 2011 00:15:36 +0000 |
parents | 767cedc7d12a |
children |
rev | line source |
---|---|
bgneal@180 | 1 from socket import error as socket_error |
bgneal@180 | 2 import smtplib |
bgneal@180 | 3 import time |
bgneal@316 | 4 import logging |
bgneal@180 | 5 |
bgneal@180 | 6 import django.core.mail |
bgneal@180 | 7 |
bgneal@180 | 8 import mailer.models |
bgneal@180 | 9 |
bgneal@180 | 10 |
bgneal@180 | 11 def enqueue_mail(subject, message, from_email, recipient_list): |
bgneal@180 | 12 """Creates mailer mail queue entries for the given email.""" |
bgneal@180 | 13 |
bgneal@180 | 14 if len(subject) > mailer.models.MAX_SUBJECT: |
bgneal@180 | 15 subject = u'%s...' % subject[:mailer.models.MAX_SUBJECT - 3] |
bgneal@180 | 16 |
bgneal@180 | 17 for recipient in recipient_list: |
bgneal@180 | 18 mailer.models.Message( |
bgneal@180 | 19 from_address=from_email, |
bgneal@180 | 20 to_address=recipient, |
bgneal@180 | 21 subject=subject, |
bgneal@180 | 22 body=message).save() |
bgneal@180 | 23 |
bgneal@180 | 24 |
bgneal@180 | 25 def send_queued_mail(): |
bgneal@180 | 26 """Reads the queued messages from the database, sends them, and removes them |
bgneal@180 | 27 from the queue.""" |
bgneal@180 | 28 |
bgneal@180 | 29 sent, errors = 0, 0 |
bgneal@180 | 30 |
bgneal@180 | 31 logging.debug("Sending queued mail...") |
bgneal@180 | 32 start_time = time.time() |
bgneal@180 | 33 |
bgneal@180 | 34 msgs = mailer.models.Message.objects.all() |
bgneal@180 | 35 for msg in msgs: |
bgneal@180 | 36 try: |
bgneal@180 | 37 django.core.mail.send_mail( |
bgneal@316 | 38 msg.subject, |
bgneal@316 | 39 msg.body, |
bgneal@316 | 40 msg.from_address, |
bgneal@180 | 41 [msg.to_address], |
bgneal@180 | 42 fail_silently=False) |
bgneal@180 | 43 except (socket_error, smtplib.SMTPException), e: |
bgneal@180 | 44 errors += 1 |
bgneal@316 | 45 logging.error("Error sending queued mail: %s", e) |
bgneal@180 | 46 else: |
bgneal@316 | 47 sent += 1 |
bgneal@316 | 48 msg.delete() |
bgneal@180 | 49 |
bgneal@180 | 50 end_time = time.time() |
bgneal@180 | 51 logging.debug("Sent queued mail: %d successful, %d error(s); elapsed time: %.2f" % ( |
bgneal@180 | 52 sent, errors, end_time - start_time)) |