diff options
author | Magnus Hagander | 2015-01-19 21:02:58 +0000 |
---|---|---|
committer | Magnus Hagander | 2015-01-19 21:02:58 +0000 |
commit | 9c10bad85bf90e76d6da0bf4703a73b03fe3b8b6 (patch) | |
tree | 0a15ccd6b9de1320167d280cc05a8d64b7f848ff | |
parent | 28159a0e953d5b29ac06d0847f24c922ed8ca901 (diff) |
Add the tool to send email
Forgot to git add that one, it seems..
-rwxr-xr-x | tools/mail/send_queued_mail.py | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/tools/mail/send_queued_mail.py b/tools/mail/send_queued_mail.py new file mode 100755 index 0000000..5a8005f --- /dev/null +++ b/tools/mail/send_queued_mail.py @@ -0,0 +1,45 @@ +#!/usr/bin/env python +# +# Script to send off all queued email. +# +# This script is intended to be run frequently from cron. We queue things +# up in the db so that they get automatically rolled back as necessary, +# but once we reach this point we're just going to send all of them one +# by one. +# + +import sys +import os +import smtplib + +# Set up to run in django environment +from django.core.management import setup_environ +sys.path.append(os.path.join(os.path.abspath(os.path.dirname(sys.argv[0])), '../../pgcommitfest')) +import settings +setup_environ(settings) + +from django.db import connection, transaction + +from pgcommitfest.mailqueue.models import QueuedMail + +if __name__ == "__main__": + # Grab advisory lock, if available. Lock id is just a random number + # since we only need to interlock against ourselves. The lock is + # automatically released when we're done. + curs = connection.cursor() + curs.execute("SELECT pg_try_advisory_lock(72181379)") + if not curs.fetchall()[0][0]: + print "Failed to get advisory lock, existing send_queued_mail process stuck?" + connection.close() + sys.exit(1) + + for m in QueuedMail.objects.all(): + # Yes, we do a new connection for each run. Just because we can. + # If it fails we'll throw an exception and just come back on the + # next cron job. And local delivery should never fail... + smtp = smtplib.SMTP("localhost") + smtp.sendmail(m.sender, m.receiver, m.fullmsg.encode('utf-8')) + smtp.close() + m.delete() + transaction.commit() + connection.close() |