Extending PloneFormMailer to capture form data in Plone before emailing it
Note: I've only tested this with the 0.3 version of PloneFormMailer, it probably works with other versions, please post a comment to let me know if it worked for you with a different version and if there were any variations to the implementation.
This is a quick example of how to intercept the data from a formmailer and provide a primitive "database-type-storage" mechanism for that data. Plone Form Mailer is lovely for quickly creating a form, but, out of the box, it does not store that information anywhere, this is how I worked around that.
Assumptions
To follow along with the example it is assumed that the following are already in place:
- There is a folder called 'registration' which contains an instance of PloneFormMailer called 'course0205'. The heirarchy would look something like this {siteroot}/registration/course0205
- The data to be captured will be:
- Email Address (email)
- Fullname (fullname)
- Phone Number (phone)
- Address (address)
- A folder for capturing results already exists called 'course0205formresults'. If this is not the case, create the folder now in the location: '{siteroot}/registration/results/course0205formresults'. (You probably want to make this folder private, to keep it from the nosey site visitors).
Create a custom controller script
We'll start by creating a custom controller script ( I call mine 'course0205storeandmail' ) to replace PloneFormMailer's default script.
- Create a custom version of PloneFormMailer's 'formmailer_send' script called course0205storeandmail, Note: this ensures that all the proper settings are maintained (
Click here to get the zexp of the script)- Navigate to '{siteroot}/portal_skins/PloneFormMailer/formmailer_send/manage'
- and 'customize' to the destination 'custom'.
- Then rename the newly customized controller script to course0205storeandmail.
- Under the 'Proxy' tab, set the proxy role to Manager.
- Edit the script so that it looks something like the one below:
- Navigate to '{siteroot}/portal_skins/PloneFormMailer/formmailer_send/manage'
## Controller Python Script "course0205storeandmail"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind state=state
##bind subpath=traverse_subpath
##parameters=
##title=
#some useful utilities
from Products.CMFCore.utils import getToolByName
urltool = getToolByName(context, "portal_url")
catalogtool = getToolByName(context, "portal_catalog")
portal = urltool.getPortalObject()
request=context.REQUEST
#define a uniquely named storage folder
#there can only be one item in the portal with this name
storagefolder = course0205formresults
#generate a unique document name
document_name=str(context.ZopeTime().strftime('%Y%m%d%H%M%s'))
#create a document description
fullname,email=request['field_fullname'],request['field_email']
description="""%s,%s""" % (fullname,email)
#create document body text
for key in ("field_email",
"field_fullname",
"field_phone",
"field_address"):
value = request[key]
print "%s: %s" % (key[6:],value)
#create and store the data in a document
myformstorage=getattr(context, storagefolder, None)
doc = myformstorage.invokeFactory("Document", document_name)
document = getattr(myformstorage, document_name)
document.setTitle(document_name)
document.setDescription(description)
document.edit(text_format="plain",
text=printed)
#send of the form as usual
context.send_form()
return state #this is critical!!! redirection and send response will not work
# without this
Set PloneFormMailer to use your custom controller script
You will need to tell your PloneFormMailer to use your new controller script.
- Go to '{siteroot}/registration/course0205' and 'edit' the formmailer
- Under the 'Controller' section set an alternate controller python script (cpy) as 'string:course0205storeandmail'

