Skip to content

Instantly share code, notes, and snippets.

@b4oshany
Created September 17, 2015 17:19
Show Gist options
  • Save b4oshany/5d534907c7834fee3612 to your computer and use it in GitHub Desktop.
Save b4oshany/5d534907c7834fee3612 to your computer and use it in GitHub Desktop.
ploneformgen custom mailer
from Products.CMFCore.utils import getToolByName
mailhost = getToolByName(ploneformgen, 'MailHost')
subject = "Email subject"
# Use this logger to output debug info from this script if needed
import logging
logger = logging.getLogger("mailer-logger")
tokenTool = context.onetimetoken_storage
token = tokenTool.setToken('anonymous')
portal = context.portal_url.getPortalObject()
token_url = "%s/full-sponsorship?logincode=%s" % (portal.absolute_url(), token)
# Create a message body by appending all the fields after each another
# This includes non-functional fields like labels too
message=""
for field in ploneformgen.fgFields():
label = field.widget.label.encode("utf-8")
value = str(fields[field.getName()])
# For non-functional fields draw a custom separator line
if not field.widget.blurrable:
value = "-------------------------------"
# Format lists on the same row
try:
if (value[0] == "["):
value = value.replace("'", "")[1:-1]
except IndexError:
# Skip formatting on error
pass
#remove last ':' from label
if (label[-1] == ":"):
label = label[0:-1]
message += label + ": " + value + "\n\n"
message += "<a href='%s'>Complete this form</a>" % token_url
source = "[email protected]"
receipt = "[email protected]"
mailhost.secureSend(message, receipt, source, subject=subject, subtype='plain', charset="utf-8", debug=False, )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment