quick search:
 

Send email near expiration date

Submitted by: ben
Last Edited: 2004-10-14

Category: CMF

Average rating is: 0.0 out of 5 (0 ratings)

Description:
This script sends an email out to the author when an item is nearing its expiration date.

This script borrows from several others, including "Use Workflow to announce new Content to all Members" by bobvin, "Use Workflow to send email" by tgraham with additions by others and "Number of days between two dates" by tgraham with changes by others. Any mistakes are all mine.


Source (Text):
from DateTime import DateTime

for r in context.portal_catalog():
    date1 = expires                 #the expiration date
    date2 = context.ZopeTime()      #the current date
    t = int(date1.earliestTime()-date2.earliestTime())  #the integer difference between dates
    x = r.getObjec()                #gets object via catalog

    if t==XX:                 #if there is XX days between dates...
      contentObject = r.getObject()
      objectHistory=contentObject.portal_workflow.getInfoFor(contentObject, 'review_history')
      thisRevision=objectHistory[-1]
      nowDate=thisRevision['time'].aCommon()
      authorComment=thisRevision['comments']

      mailList=[]
      try:
        mailhost=getattr(context, context.portal_url.superValues('Mail Host')[0].id)
      except:
        raise AttributeError, "Cannot find a Mail Host object"
      mMsg = 'An item you created on the website is nearing expiry.\n\nPlease update the item.\n\nThis email was generated and sent by the website.\n\n'
      mMsg += 'Title: ' + contentObject.TitleOrId() + '\n\n'
      mMsg += 'Description: ' + contentObject.Description() + '\n\n'
      mMsg += contentObject.absolute_url() + '\n'
      mMsg += 'Author comment:' + authorComment + '\n\n'
      mTo = context.portal_membership.getMemberById(contentObject.Creator()).email
      mFrom = 'Webmaster@acme.com'
      mSubj = 'Website item nearing expiration date'

      mailhost.send(mMsg, mTo, mFrom, mSubj)       #send email to author

    elif t==0:         #else if expiration date=today's date
      if x.review_state!="private":              #change review_state to private via expire transaction
        try:
          context.portal_workflow.doActionFor(contentObject,'expire')
        except:
           print 'didnt change state'
return printed

Explanation:
This script could be run every night by setting up a cron job to call it
from a URL using wget or something similar. It is supposed to go through
all items in the site, check if their expiry date is a certain number of
days away, and if so send an email to the author alerting them of this.

I am not certain that this works as I have yet to fully test it.

It could be altered easily to send an email to the author and managers of
the site, etc.


Comments:

Corrections by ben - 2004-10-14
The above does not work as expected if at all. Made changes and the below script works. It requires an "expire" workflow transation to be set up but if you don't want to do this then just change that part of the script (to "hide" or whatever you want).

from DateTime import DateTime

for r in context.portal_catalog():
        date1 = r.expires                                      #the expiration date
        date2 = context.ZopeTime()                             #the current date
        t = int(date1.earliestTime()-date2.earliestTime())     #the integer difference between dates                                  
        print t
        if t==14:                                              #if there is 14 days between dates...
          contentObject = r.getObject()                        #get object via catalog         
          objectHistory=contentObject.portal_workflow.getInfoFor(contentObject, 'review_history')
          thisRevision=objectHistory[-1]
          nowDate=thisRevision['time'].aCommon()
          authorComment=thisRevision['comments']
          
          mailList=[]          
          try:
             mailhost=getattr(context, context.portal_url.superValues('Mail Host')[0].id)
          except:
             raise AttributeError, "Cannot find a Mail Host object"
          mMsg = 'An item you created on the website is nearing expiry.\n\nPlease update the item.\n\nThis email was generated and sent by the COMESA website.\n\n'
          mMsg += 'Title: ' + contentObject.TitleOrId() + '\n\n'
          mMsg += 'Description: ' + contentObject.Description() + '\n\n'
          mMsg += contentObject.absolute_url() + '\n'
          #mMsg += 'Author: ' + contentObject.Creator()
          mMsg += 'Author comment:' + authorComment + '\n\n'
          mTo = context.portal_membership.getMemberById(contentObject.Creator()).email
          mFrom = 'Webmaster@comesa.int'
          mSubj = 'Website item nearing expiration date'

          mailhost.send(mMsg, mTo, mFrom, mSubj)       #send email to author

        elif t==0:                                         #else if expiration date=today's date
          contentObject = r.getObject()                   #get object via catalog                                       
          try:                                            #change review_state to expired
            context.portal_workflow.doActionFor(contentObject, 'expire')
          except:
            print 'didnt change state'                    #this is here just in case

return printed