Mercurial > public > sg101
comparison gpp/mailer/__init__.py @ 316:767cedc7d12a
Fixing #144; integrate with new Django logging support. Also added unit tests for Donations app.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 30 Jan 2011 20:02:32 +0000 |
parents | aef00df91165 |
children |
comparison
equal
deleted
inserted
replaced
315:36373d995611 | 316:767cedc7d12a |
---|---|
1 from socket import error as socket_error | 1 from socket import error as socket_error |
2 import smtplib | 2 import smtplib |
3 import time | 3 import time |
4 import logging | |
4 | 5 |
5 import django.core.mail | 6 import django.core.mail |
6 | 7 |
7 import mailer.models | 8 import mailer.models |
8 | 9 |
25 """Reads the queued messages from the database, sends them, and removes them | 26 """Reads the queued messages from the database, sends them, and removes them |
26 from the queue.""" | 27 from the queue.""" |
27 | 28 |
28 sent, errors = 0, 0 | 29 sent, errors = 0, 0 |
29 | 30 |
30 import logging | |
31 logging.debug("Sending queued mail...") | 31 logging.debug("Sending queued mail...") |
32 start_time = time.time() | 32 start_time = time.time() |
33 | 33 |
34 msgs = mailer.models.Message.objects.all() | 34 msgs = mailer.models.Message.objects.all() |
35 for msg in msgs: | 35 for msg in msgs: |
36 try: | 36 try: |
37 django.core.mail.send_mail( | 37 django.core.mail.send_mail( |
38 msg.subject, | 38 msg.subject, |
39 msg.body, | 39 msg.body, |
40 msg.from_address, | 40 msg.from_address, |
41 [msg.to_address], | 41 [msg.to_address], |
42 fail_silently=False) | 42 fail_silently=False) |
43 except (socket_error, smtplib.SMTPException), e: | 43 except (socket_error, smtplib.SMTPException), e: |
44 errors += 1 | 44 errors += 1 |
45 logging.error("Error sending queued mail: %s" % e) | 45 logging.error("Error sending queued mail: %s", e) |
46 else: | 46 else: |
47 sent += 1 | 47 sent += 1 |
48 msg.delete() | 48 msg.delete() |
49 | 49 |
50 end_time = time.time() | 50 end_time = time.time() |
51 logging.debug("Sent queued mail: %d successful, %d error(s); elapsed time: %.2f" % ( | 51 logging.debug("Sent queued mail: %d successful, %d error(s); elapsed time: %.2f" % ( |
52 sent, errors, end_time - start_time)) | 52 sent, errors, end_time - start_time)) |