summaryrefslogblamecommitdiffstats
path: root/bin/send_emails_to_all.py
blob: 736831d05bcfcd75efb788a07b5e8abfcd152f05 (plain) (tree)


















































                                                                                                   
#!/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()