Commits

Thomas Waldmann  committed 23bf696 Draft

add a sendmail action, which can be use to submit POST data by E-Mail

  • Participants
  • Parent commits 819c412

Comments (0)

Files changed (1)

File data/plugin/action/sendmail.py

+# -*- 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()