diff options
Diffstat (limited to 'email_gateway.py')
-rwxr-xr-x | email_gateway.py | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/email_gateway.py b/email_gateway.py new file mode 100755 index 0000000..e73e3e2 --- /dev/null +++ b/email_gateway.py | |||
@@ -0,0 +1,84 @@ | |||
1 | #!/usr/bin/env python2.6 | ||
2 | |||
3 | import os | ||
4 | import smtplib | ||
5 | import re | ||
6 | import urlparse | ||
7 | from cStringIO import StringIO | ||
8 | from email.mime.text import MIMEText | ||
9 | from ConfigParser import SafeConfigParser as ConfigParser, NoSectionError | ||
10 | |||
11 | |||
12 | config = ConfigParser() | ||
13 | with open("/etc/email_gateway.cfg") as fp: | ||
14 | config.readfp(fp) | ||
15 | |||
16 | |||
17 | def send_message(text, subject, to, from_email): | ||
18 | msg = MIMEText(text.getvalue(), "plain") | ||
19 | |||
20 | msg['Subject'] = subject | ||
21 | msg['From'] = from_email | ||
22 | msg['To'] = to | ||
23 | |||
24 | p = os.popen("/usr/sbin/sendmail -t", "w") | ||
25 | p.write(msg.as_string()) | ||
26 | p.close() | ||
27 | |||
28 | |||
29 | def email_app(environ, start_response): | ||
30 | ignored_fields = [] | ||
31 | useful_fields = [] | ||
32 | form_key = None | ||
33 | message_buffer = StringIO() | ||
34 | |||
35 | context = {} | ||
36 | |||
37 | fields = urlparse.parse_qsl(environ["wsgi.input"].read()) | ||
38 | for key, value in fields: | ||
39 | if key == "mailer.form-key": | ||
40 | form_key = value | ||
41 | elif key == "mailer.redirect": | ||
42 | context["redirect"] = value | ||
43 | elif key == "mailer.subject": | ||
44 | context["subject"] = value | ||
45 | elif key == "mailer.message": | ||
46 | context["message"] = value | ||
47 | elif key == "mailer.fields.ignore": | ||
48 | ignored_fields = value.split(",") | ||
49 | else: | ||
50 | useful_fields.append((key, value)) | ||
51 | |||
52 | try: | ||
53 | my_config = dict(config.items(form_key)) | ||
54 | site_matcher = re.compile(my_config["site"]) | ||
55 | except NoSectionError: | ||
56 | start_response('403 Forbidden', [('Content-Type', 'text/plain')]) | ||
57 | return "Invalid form key!" | ||
58 | |||
59 | if not site_matcher.match(environ["HTTP_REFERER"]): | ||
60 | start_response('403 Forbidden', [('Content-Type', 'text/plain')]) | ||
61 | return "Invalid send!" | ||
62 | |||
63 | useful_fields = ["{0}: {1}".format(*f) | ||
64 | for f in useful_fields | ||
65 | if f[0] not in ignored_fields] | ||
66 | |||
67 | message_buffer.write(context.get("message", my_config["message"])) | ||
68 | message_buffer.write("\n\n") | ||
69 | message_buffer.write("\n".join(useful_fields)) | ||
70 | |||
71 | send_message(message_buffer, | ||
72 | context.get("subject", my_config["subject"]), | ||
73 | my_config["to"], | ||
74 | my_config["from"]) | ||
75 | |||
76 | redirect_location = context.get("redirect", my_config["redirect"]) | ||
77 | start_response('302 Found', [('Location', redirect_location)]) | ||
78 | |||
79 | return "" | ||
80 | |||
81 | |||
82 | if __name__ == "__main__": | ||
83 | from flup.server.fcgi_fork import WSGIServer | ||
84 | WSGIServer(email_app, maxSpare=1).run() | ||