Send mail on a workflow transition
Shows how to send an email when a workflow transition is triggered, for example to notify content owners that their document has been rejected.
The example below shows how to create a script called reject_notification which is triggered on the reject workflow transition in order to send an email to the content submitter explaining that their document was rejected. It can of course be adapted to send different messages on different transitions easily.
Workflow Configuration:
- In the ZMI goto
portal_workflow - click the
scriptstab - add a new Script (Python)
- id: reject_notification
- parameter list: sti
- body:
obj=sti.object creator = obj.Creator() history = sti.getHistory() wf_tool = context.portal_workflow mMsg = """ Your submission was rejected. The url was %s. The reason was %s. """ member = context.portal_membership.getMemberById(creator) creator = {'member':member, 'id':member.getId(), 'fullname':member.getProperty('fullname', 'Fullname missing'), 'email':member.getProperty('email', None)} actorid = wf_tool.getInfoFor(obj, 'actor') actor = context.portal_membership.getMemberById(actorid) reviewer = {'member':actor, 'id':actor.getId(), 'fullname':actor.getProperty('fullname', 'Fullname missing'), 'email':actor.getProperty('email', None)} mTo = creator['email'] mFrom = reviewer['email'] mSubj = 'Your item has transitioned' obj_url = obj.absolute_url() #use portal_url + relative_url comments = wf_tool.getInfoFor(obj, 'comments') message = mMsg % (obj_url, comments) context.MailHost.send(message, mTo, mFrom, mSubj) - click save
Now we have added the script that does the transition. We want to wire this
script to the reject transition.
- click the
transitionstab in the ZMI - click the
rejecttransition - in the Script (after) drop down select the
reject_notification - click Save
Now whenever you reject a piece of content it will email the person who created the content. It will also say say it came from the person who rejected the document.
Cannot get it to work
Module None, line 14, in reject_notification
- <PythonScript at /test/portal_workflow/test_workflow2/scripts/reject_notification>
- Line 14
AttributeError: 'NoneType' object has no attribute 'getId'
I'm mystified. Ideas anyone?