kujiulabs/contactmailer.py

73 lines
2 KiB
Python
Raw Normal View History

2016-03-03 01:09:08 +01:00
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import ssl
from smtplib import SMTP
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from urllib.parse import parse_qs
from html import escape
CONFIG = {
'kujiu': {
'smtp_server': '',
'smtp_port': 587,
'smtp_user': '',
'smtp_pass': '',
'sender_email': '',
'receiver_email': '',
'redirect_to': '/pages/submit_success.html',
},
'nerv-project': {
'smtp_server': '',
'smtp_port': 587,
'smtp_user': '',
'smtp_pass': '',
'sender_email': '',
'receiver_email': '',
'redirect_to': '/pages/submit_success.html',
}
}
def application(environ, start_fn):
website = environ.get('SERVER_NAME', '')
for ws in CONFIG:
config = CONFIG[ws]
if ws in website:
break
args = parse_qs(environ['QUERY_STRING'])
name = escape(args.get('name', [''])[0])
name = name.replace('<', '').replace('>', '')
email = escape(args.get('email', [''])[0])
email = email.replace('<', '').replace('>', '')
subject = escape(args.get('subject', [''])[0])
body = escape(args.get('message', [''])[0])
message = MIMEMultipart()
message["From"] = '%s/%s <%s>' % (
name, email, config['sender_email'])
message["Reply-To"] = '%s <%s>' % (name, email)
message["To"] = config['receiver_email']
message["Subject"] = "[WS " + website + "] " + subject
message.attach(MIMEText(body, "plain"))
context = ssl.create_default_context()
server = SMTP(config['smtp_server'], config['smtp_port'])
server.starttls(context=context)
server.ehlo()
server.login(config['smtp_user'], config['smtp_pass'])
server.sendmail(
config['sender_email'],
config['receiver_email'],
message.as_string()
)
server.quit()
start_fn(
'303 See Other',
[('Location', config['redirect_to'])]
)
return ['1']