Skip to content

Instantly share code, notes, and snippets.

@tiramiseb
Created May 30, 2016 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiramiseb/a8f2ed56f58062e0597f54d8c2c6496e to your computer and use it in GitHub Desktop.
Save tiramiseb/a8f2ed56f58062e0597f54d8c2c6496e to your computer and use it in GitHub Desktop.
uWSGI app for sending Pelican comments
# -*- coding: utf-8 -*-
#
# Sorry, the text in the script is in french, I am French...
import datetime
import smtplib
from email.mime.text import MIMEText
from urlparse import parse_qs
SOURCEPATH="Where the sources are on my computer"
EMAIL_ADDRESS="My email address"
SMTP_SERVER="localhost"
def send_comment_app(environ, start_response):
"""Send an email from a Pelican comment"""
request = parse_qs(environ['wsgi.input'].read())
messagedate = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
text = request.get('text', [None])[0]
author = request.get('name', [None])[0]
email = request.get('email', [None])[0]
article = request.get('article', [None])[0]
if text and author and email and article:
message = [
'Un nouveau commentaire a été posté sur le blog.',
'Le billet concerné est :',
'',
article,
'',
'Pour approuver ce message, copie/colle le contenu ci-dessous',
'dans le fichier suivant :',
'',
'{}/content/comments/{}/XXX.rst'.format(SOURCEPATH, article),
'(remplace XXX par un nombre libre)',
'',
'=====================================================',
':author: {}'.format(author),
':email: {}'.format(email),
':date: {}'.format(messagedate)
]
if 'url' in request:
message.append(':authorurl: {}'.format(request['url'][0]))
if 'replyto' in request:
message.append(':replyto: {}'.format(request['replyto'][0]))
message.append('')
message.append(text)
message.append('=====================================================')
message = '\n'.join(message)
msg = MIMEText(message, _charset='utf-8')
msg['Subject'] = 'Nouveau commentaire dans le blog'
msg['From'] = EMAIL_ADDRESS
msg['To'] = EMAIL_ADDRESS
s = smtplib.SMTP(SMTP_SERVER)
s.sendmail(EMAIL_ADDRESS, [EMAIL_ADDRESS], msg.as_string())
redirect_to = '/comment-sent'
else:
redirect_to = '/comment-missing-field'
status = '303 See Other'
response_headers = [('Location', redirect_to)]
start_response(status, response_headers)
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment