Skip to main content

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


Popular posts from this blog

Testing and Sending email via SMTP using Opportunistic TLS and oAuth in Office365 with PowerShell

As well as EWS and Remote PowerShell (RPS) other mail protocols POP3, IMAP and SMTP have had OAuth authentication enabled in Exchange Online (Official announcement here ). A while ago I created  this script that used Opportunistic TLS to perform a Telnet style test against a SMTP server using SMTP AUTH. Now that oAuth authentication has been enabled in office365 I've updated this script to be able to use oAuth instead of SMTP Auth to test against Office365. I've also included a function to actually send a Message. Token Acquisition  To Send a Mail using oAuth you first need to get an Access token from Azure AD there are plenty of ways of doing this in PowerShell. You could use a library like MSAL or ADAL (just google your favoured method) or use a library less approach which I've included with this script . Whatever way you do this you need to make sure that your application registration  https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-register-

How to test SMTP using Opportunistic TLS with Powershell and grab the public certificate a SMTP server is using

Most email services these day employ Opportunistic TLS when trying to send Messages which means that wherever possible the Messages will be encrypted rather then the plain text legacy of SMTP.  This method was defined in RFC 3207 "SMTP Service Extension for Secure SMTP over Transport Layer Security" and  there's a quite a good explanation of Opportunistic TLS on Wikipedia  https://en.wikipedia.org/wiki/Opportunistic_TLS .  This is used for both Server to Server (eg MTA to MTA) and Client to server (Eg a Message client like Outlook which acts as a MSA) the later being generally Authenticated. Basically it allows you to have a normal plain text SMTP conversation that is then upgraded to TLS using the STARTTLS verb. Not all servers will support this verb so if its not supported then a message is just sent as Plain text. TLS relies on PKI certificates and the administrative issue s that come around certificate management like expired certificates which is why I wrote th

The MailboxConcurrency limit and using Batching in the Microsoft Graph API

If your getting an error such as Application is over its MailboxConcurrency limit while using the Microsoft Graph API this post may help you understand why. Background   The Mailbox  concurrency limit when your using the Graph API is 4 as per https://docs.microsoft.com/en-us/graph/throttling#outlook-service-limits . This is evaluated for each app ID and mailbox combination so this means you can have different apps running under the same credentials and the poor behavior of one won't cause the other to be throttled. If you compared that to EWS you could have up to 27 concurrent connections but they are shared across all apps on a first come first served basis. Batching Batching in the Graph API is a way of combining multiple requests into a single HTTP request. Batching in the Exchange Mail API's EWS and MAPI has been around for a long time and its common, for email Apps to process large numbers of smaller items for a variety of reasons.  Batching in the Graph is limited to a m
All sample scripts and source code is provided by for illustrative purposes only. All examples are untested in different environments and therefore, I cannot guarantee or imply reliability, serviceability, or function of these programs.

All code contained herein is provided to you "AS IS" without any warranties of any kind. The implied warranties of non-infringement, merchantability and fitness for a particular purpose are expressly disclaimed.