annotate gpp/mailer/__init__.py @ 240:1246a4f1ab4f
For #93: fix url scheme for the news application.
author |
Brian Neal <bgneal@gmail.com> |
date |
Wed, 15 Sep 2010 00:14:54 +0000 |
parents |
aef00df91165 |
children |
767cedc7d12a |
rev |
line source |
bgneal@180
|
1 from socket import error as socket_error
|
bgneal@180
|
2 import smtplib
|
bgneal@180
|
3 import time
|
bgneal@180
|
4
|
bgneal@180
|
5 import django.core.mail
|
bgneal@180
|
6
|
bgneal@180
|
7 import mailer.models
|
bgneal@180
|
8
|
bgneal@180
|
9
|
bgneal@180
|
10 def enqueue_mail(subject, message, from_email, recipient_list):
|
bgneal@180
|
11 """Creates mailer mail queue entries for the given email."""
|
bgneal@180
|
12
|
bgneal@180
|
13 if len(subject) > mailer.models.MAX_SUBJECT:
|
bgneal@180
|
14 subject = u'%s...' % subject[:mailer.models.MAX_SUBJECT - 3]
|
bgneal@180
|
15
|
bgneal@180
|
16 for recipient in recipient_list:
|
bgneal@180
|
17 mailer.models.Message(
|
bgneal@180
|
18 from_address=from_email,
|
bgneal@180
|
19 to_address=recipient,
|
bgneal@180
|
20 subject=subject,
|
bgneal@180
|
21 body=message).save()
|
bgneal@180
|
22
|
bgneal@180
|
23
|
bgneal@180
|
24 def send_queued_mail():
|
bgneal@180
|
25 """Reads the queued messages from the database, sends them, and removes them
|
bgneal@180
|
26 from the queue."""
|
bgneal@180
|
27
|
bgneal@180
|
28 sent, errors = 0, 0
|
bgneal@180
|
29
|
bgneal@180
|
30 import logging
|
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@180
|
38 msg.subject,
|
bgneal@180
|
39 msg.body,
|
bgneal@180
|
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@180
|
45 logging.error("Error sending queued mail: %s" % e)
|
bgneal@180
|
46 else:
|
bgneal@180
|
47 sent += 1
|
bgneal@180
|
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))
|