From 3813d329d10557ef5fc520db8f58edb64763817b Mon Sep 17 00:00:00 2001 From: Hostmaster of the Day Date: Fri, 1 Apr 2011 02:23:43 +0200 Subject: added script for sending an email to all --- bin/send_emails_to_all.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 bin/send_emails_to_all.py diff --git a/bin/send_emails_to_all.py b/bin/send_emails_to_all.py new file mode 100644 index 0000000..736831d --- /dev/null +++ b/bin/send_emails_to_all.py @@ -0,0 +1,51 @@ +#!/usr/bin/python + +# setup django environment, so we can use their template engine +from django.template import Context +from django.template.loader import render_to_string +# import mysql bindings +import MySQLdb + +# render mail template and send it using local sendmail binary +def sendmail(c, template): + msg = render_to_string(template + "_mail.txt", c) + + # open sendmail process for writing + p = os.popen("/usr/sbin/sendmail -t", 'w') + p.write(msg) + + # close will return exit status + # TODO: log error to some file + if p.close(): + return False + return True + +def get_emails(): + db = MySQLdb.connect(host="localhost", user="apache", passwd="password", db="trac") + cursor = db.cursor() + cursor.execute("SELECT login, email FROM user") + users = cursor.fetchall() + + for user in users: + c = Context() + c['from_addr'] = "dev@spline.de" + c['to_addr'] = user[1] + c['name'] = user[0] + cursor.execute("select project_name from project_members where login = '%s'" % (user[0], )) + projects = cursor.fetchall() + if len(projects) < 1: + continue + pstring = "" + for project in projects: + pstring += "* %s\n" % (project[0], ) + c['projects'] = pstring + sendmail(c, "delete_reminder") + + cursor.close() + db.close() + +import os, sys +sys.path = ['/var/www/localhost/htdocs'] + sys.path +os.environ['DJANGO_SETTINGS_MODULE'] = 'account.settings' + +get_emails() -- cgit v1.2.3