Mercurial > public > src > moin > extensions
changeset 629:23bf696cf294 default tip
add a sendmail action, which can be use to submit POST data by E-Mail
author | Thomas Waldmann <tw AT waldmann-edv DOT de> |
---|---|
date | Mon, 25 Mar 2013 23:40:03 +0100 |
parents | 819c4121f9a0 |
children | |
files | data/plugin/action/sendmail.py |
diffstat | 1 files changed, 55 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data/plugin/action/sendmail.py Mon Mar 25 23:40:03 2013 +0100 @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +""" +MoinMoin - sendmail action + +Send an E-Mail to configured recipient(s). + +Configuration: + sendmail_recipients = [u'admin@example.org', ] # only config + sendmail_subject = u'MoinMoin Form submission' # also from values + sendmail_template = u'%(values_json)s' # also from values + +Form: +sendmail_subject and sendmail_template can also be given via form elements. + +Placeholders in the sendmail_template will be replaced with the respective +form values. Be careful: if you need a verbatim % (percent char), you have +to put %% into the template. + +There are some special placeholders: + %(values_json)s json dump of the values + %(user_name)s user name of the current user + %(user_email)s user E-Mail address of the current user + +@copyright: 2013 MoinMoin:ThomasWaldmann +@license: GNU GPL, see COPYING for details. +""" + +try: + import json +except ImportError: + import simplejson as json + +from werkzeug import MultiDict + +from MoinMoin.Page import Page +from MoinMoin.mail.sendmail import sendmail + + +def execute(pagename, request): + if request.method == 'POST': + values = MultiDict(request.values) # make a mutable copy + # note: we do NOT get the recipients from the values to avoid spam abuse + to = request.cfg.sendmail_recipients + subject = values.pop('sendmail_subject', request.cfg.sendmail_subject) + template = values.pop('sendmail_template', request.cfg.sendmail_template) + values.add('values_json', json.dumps(values, sort_keys=True, ensure_ascii=False, indent=4)) + values.add('user_name', request.user.name) + values.add('user_email', request.user.email) + text = template % values.to_dict() + ok, msg = sendmail(request, to, subject, text) + else: + ok, msg = False, 'Please use a POST request.' + thispage = Page(request, pagename) + request.theme.add_msg(msg, "info" if ok else "error") + thispage.send_page()