Implement scheduled mail sending via django infrastructure,

for testing the I-D Submission tool's announcements.

Note that this implementation does not use the queue fields
in the database, so before deploying in production the purpose
of those fields will have to be understood.
 - Legacy-Id: 1147
This commit is contained in:
Bill Fenner 2008-04-03 15:23:48 +00:00
parent dd7b2b2803
commit 027232d642
2 changed files with 42 additions and 4 deletions

33
ietf/bin/send-scheduled-mail Executable file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env python
#
from django.core.management import setup_environ
from ietf import settings
setup_environ(settings)
from django.db.models import Q
from django.db import connection
from ietf.announcements.models import ScheduledAnnouncement
from ietf.utils.mail import send_mail_text
import datetime
now = datetime.datetime.now()
candidates = ScheduledAnnouncement.objects.filter(to_be_sent_date__isnull=True) | ScheduledAnnouncement.objects.exclude(to_be_sent_date__gt=now.date())
candidates = candidates.filter(mail_sent=False) #.exclude(to_be_sent_date__gt=now.date())
for c in candidates:
if c.to_be_sent_time:
tbst = datetime.time( *[int(t) for t in c.to_be_sent_time.split(":")] )
if tbst > now.time():
# To be sent today, but not yet.
continue
extra={}
if c.content_type:
extra['Content-Type'] = c.content_type
if c.replyto:
extra['Reply-To'] = c.replyto
send_mail_text(None, to=c.to_val, frm=c.from_val, subject=c.subject,
txt=c.body, cc=c.cc_val, extra=extra, bcc=c.bcc_val)
c.actual_sent_date=now.date()
c.actual_sent_time=str(now.time()) # sigh
c.mail_sent=True
c.save()

View file

@ -22,7 +22,7 @@ def add_headers(msg):
msg['From'] = settings.DEFAULT_FROM_EMAIL
return msg
def send_smtp(msg):
def send_smtp(msg, bcc=None):
'''
Send a Message via SMTP, based on the django email server settings.
The destination list will be taken from the To:/Cc: headers in the
@ -31,7 +31,10 @@ def send_smtp(msg):
'''
add_headers(msg)
(fname, frm) = parseaddr(msg.get('From'))
to = [addr for name, addr in getaddresses(msg.get_all('To') + msg.get_all('Cc', []))]
addrlist = msg.get_all('To') + msg.get_all('Cc', [])
if bcc:
addrlist += [bcc]
to = [addr for name, addr in getaddresses(addrlist)]
server = None
try:
server = smtplib.SMTP()
@ -107,7 +110,7 @@ def send_mail(request, to, frm, subject, template, context, *args, **kwargs):
txt = render_to_string(template, context, context_instance=RequestContext(request))
return send_mail_text(request, to, frm, subject, txt, *args, **kwargs)
def send_mail_text(request, to, frm, subject, txt, cc=None, extra=None, toUser=None):
def send_mail_text(request, to, frm, subject, txt, cc=None, extra=None, toUser=None, bcc=None):
msg = MIMEText(txt)
if isinstance(frm, tuple):
frm = formataddr(frm)
@ -126,7 +129,7 @@ def send_mail_text(request, to, frm, subject, txt, cc=None, extra=None, toUser=N
for k, v in extra.iteritems():
msg[k] = v
if settings.SERVER_MODE == 'production':
send_smtp(msg)
send_smtp(msg, bcc)
elif settings.SERVER_MODE == 'test':
if toUser:
copy_email(msg, to, toUser=True)
@ -136,4 +139,6 @@ def send_mail_text(request, to, frm, subject, txt, cc=None, extra=None, toUser=N
copy_to = settings.EMAIL_COPY_TO
except AttributeError:
copy_to = "ietf.tracker.archive+%s@gmail.com" % settings.SERVER_MODE
if bcc:
msg['X-Tracker-Bcc']=bcc
copy_email(msg, copy_to)