summaryrefslogtreecommitdiffstats
path: root/bin/send_emails_to_all.py
blob: 736831d05bcfcd75efb788a07b5e8abfcd152f05 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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()