Thursday, January 20, 2005

Draft DL templates Script

I've been thinking a lot about DL's over the last week or so and have been running a few idea's around about how to use them in different ways. One idea was to create a bunch of DL's by scanning though the sent items folder of a mailbox and looking at any mail that had more then a certain number of recipients. Although this sounded like a good idea when I looked into it further it seemed that these type of emails where never usually just sent to a large number of recipients but rather there was a bunch of people in the To line another bunch being CC and maybe a few BCC'd. So what I though about instead of creating DL's why not just create some template emails that where addressed with all the TO,CC and BCC address's of people you sent these type of emails to and then store all these templates in a folder within the drafts folder.

Out of this idea the following script was born, It uses CDO 1.2 basically because access to the recipients collection of a message is a lot easier when using CDO 1.2. It first checks to see if there is a folder within the drafts folder called DL drafts, if not it creates this folder. After this it creates a collection of the messages in the sent items folder and goes though each one looking for messages with 4 recipients or more. If it finds a messages with more then 4 recipients it then goes though and gets a ordered list of these recipients using a disconnected recordset (this is so I could sort the recipientlist alphabetically and by type). It compares this list with the list of any email already in the DL Drafts folder to ensure no duplicate templates are created. If no duplicates exist it creates the template using the recipientlist from the previous email and sets the subject to a single string list of all the recipients of the email.

So what you end up with is a folder under drafts with a bunch of templates based on your email sending trends. So when you want to send a mail to list of recipients that you know you have sent to before you should already have a template in the drafts folder with the address's setup the way you like . You then just need to change the subject, put your content in and click send. The one problem is this is a one shot template once you have sent it that template is gone. You can rerun the script which is fortunately smart enough not to create duplicates. Another way would be to put a event sink on the folder that would rerun the population script every time a template was used. The other problem is that in the time you sent the last message to the present, one of the address's may have become invalid because a person has left the company or changed their email address.

I not sure how useful this will be but a its little quirky and bit of fun.

I've posted a downloadable copy here the code look like


Public Const CdoDefaultFolderSentItems = 3
Public Const CdoPR_DRAFTS_FOLDER = &H36D70102
Public const adVarChar = 200
set objSession = CreateObject("MAPI.Session")
strProfile = "servername" & vbLf & "mailbox"
objSession.Logon "",,, False,, True, strProfile
Set objInbox = objSession.Inbox
Set objInfoStore = objSession.GetInfoStore(objSession.Inbox.StoreID)
strDraftsEntryID = objInbox.Fields.Item(CdoPR_DRAFTS_FOLDER)
Set objDraftsFolder = objSession.GetFolder(strDraftsEntryID, Null)
set objSentItemsFolder = objSession.GetDefaultFolder(CdoDefaultFolderSentItems)
set ColDraftfolders = objDraftsFolder.folders
bFound = False
Set CdoDraftsFolder = ColDraftfolders.GetFirst
Do While (Not bFound) And Not (CdoDraftsFolder Is Nothing)
If CdoDraftsFolder.Name = "DL Drafts" Then
bFound = True
set colDLDraftmsg = CdoDraftsFolder.messages
Else
Set CdoDraftsFolder = ColDraftfolders.GetNext
End If
Loop
If bFound = False then
Set CdoDraftsFolder = ColDraftfolders.Add("DL Drafts")
Wscript.echo "Create DL Drafts Folder"
set colDLDraftmsg = CdoDraftsFolder.messages
End If
Set objMessages = objSentItemsFolder.Messages
for each objmessage in objMessages
Set colRecips = objmessage.Recipients
if colRecips.Count => 4 then
wscript.echo objmessage.subject
wscript.echo colRecips.Count
strSubject = ""
ifound = false
set rs = createobject("ador.recordset")
rs.fields.append "name", adVarChar, 255
rs.fields.append "AddressType", adVarChar, 255
rs.open
for each objrecip in colRecips
rs.addnew
rs("name") = objrecip.name
rs("AddressType") = objrecip.Type
rs.Update
next
rs.sort = "AddressType,Name"
rs.moveFirst
do until rs.eof
select case rs.fields("AddressType").Value
case 1 strSubject = strSubject & " TO:" & replace(rs.fields("name").Value,"'","")
case 2 strSubject = strSubject & " CC:" & replace(rs.fields("name").Value,"'","")
case 3 strSubject = strSubject & " BCC:" & replace(rs.fields("name").Value,"'","")
end select
rs.moveNext
loop
set rs = nothing
for each objDLmessage in colDLDraftmsg
if objmessage.Recipients.Count = objDLmessage.Recipients.Count then
if strSubject = objDLmessage.Subject then
ifound = True
wscript.echo "Found Skipping"
exit for
end if
end if
next
if ifound = false then
set objnewdlmsg = colDLDraftmsg.add
set colnewdlmsgrecp = objnewdlmsg.Recipients
for each objrecip in colRecips
Set objOneRecip = colnewdlmsgrecp.Add(objrecip.name,objrecip.Address,objrecip.Type,objrecip.AddressEntry.ID)
objnewdlmsg.Subject = strSubject
set objOneRecip = nothing
next
objnewdlmsg.update
set objnewdlmsg = nothing
end if

end if
next


No comments: