Created
June 1, 2013 13:28
-
-
Save markcaudill/5690374 to your computer and use it in GitHub Desktop.
A simple CGI to receive arbitrary form submission data and send it via email (with optional GPG encryption) to you (or some other admin).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# Copyright (C) 2013 Mark Caudill | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining | |
# a copy of this software and associated documentation files | |
# (the "Software"), to deal in the Software without restriction, | |
# including without limitation the rights to use, copy, modify, merge, | |
# publish, distribute, sublicense, and/or sell copies of the Software, | |
# and to permit persons to whom the Software is furnished to do so, | |
# subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be | |
# included in all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
####################################################################### | |
# This CGI script does the following. | |
# 1. Compile all arbitrary fields submitted (no validation is done.) | |
# 2. If GnuPG encryption is enabled, encrypt the content. | |
# 3. Send the message to the specified recipient. | |
# | |
# Note: This is obviously not a super-robust solution. It is what it | |
# is: a super-simple way to start getting form data without a | |
# lot of backend programming. | |
####################################################################### | |
import cgi | |
import smtplib | |
from email.mime.text import MIMEText | |
import datetime | |
# Comment the next two lines before putting in production ... or don't. | |
import cgitb | |
cgitb.enable() | |
## | |
# Configuration | |
# | |
config = {} | |
# Encryption. | |
config['encryption'] = {} | |
# Set the following to False to disable encryption | |
config['encryption']['enabled'] = True | |
config['encryption']['gnupghome'] = '/path/to/gnupg' | |
config['email'] = {} | |
config['email']['from'] = '[email protected]' | |
config['email']['to'] = '[email protected]' | |
config['email']['subject'] = 'New Form Submission' | |
# Redirects | |
config['redirects'] = {} | |
config['redirects']['success'] = 'http://domain.tld/thanks.html' | |
config['redirects']['unspecified_error'] = 'http://domain.tld/error.html' | |
## | |
# Functions | |
## | |
def redirect(url=None): | |
''' | |
Redirect to to a URL. This doesn't work if any data has already | |
been returned to the client. | |
''' | |
print 'Location: %s' % url | |
print '' | |
## | |
# Main | |
## | |
# Read in all submitted fields. | |
data = {} | |
data['timestamp'] = datetime.datetime.now() | |
form = cgi.FieldStorage() | |
for field in form.keys(): | |
# If it's a list just stringify it now. | |
data[field] = ', '.join(form.getlist(field)) | |
# Compile the email. | |
text = '' | |
for field in data.keys(): | |
text += '%s: %s\n' % (field, data[field]) | |
# Encrypt if enabled | |
if config['encryption']['enabled'] == True: | |
import gnupg | |
gpg = gnupg.GPG(gnupghome=config['encryption']['gnupghome']) | |
text = str(gpg.encrypt(text, config['email']['to'])) | |
# Send email | |
message = MIMEText(text) | |
message['Subject'] = config['email']['subject'] | |
message['To'] = config['email']['to'] | |
message['From'] = config['email']['from'] | |
server = smtplib.SMTP('localhost') | |
server.sendmail(config['email']['from'], [config['email']['to']], message.as_string()) | |
server.quit() | |
# If we get here then assume everything went ok. | |
redirect(config['redirects']['success']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment