Generating Outlook Signatures Based on Active Directory Information

Virtually all modern email clients support HTML-based emails, which means you can deliver a web page into people's mailboxes. While this brings up security issues, it also means we can make our email look a lot nicer than plain old text. Most people use this functionality to create an email signature. These normally provide contact information, web sites, company logos, etc. Many end users don’t have the necessary skills to create a nice looking email signature. This article will show you how to create one automatically.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 3
June 23, 2008
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

We will use a VB Script to create an HTML file and populate this with information from the client's user object in the active directory. The HTML file will be saved into the user's Outlook signature folder, but you can use the same basic technique with any modern email client.

Getting started ... Connecting to the User object in the Active Directory

So, the first thing we need to do is connect to the client's user object in the active directory. This is surprisingly easy to do. First of all, we will create an object that points to the ADSystemInfo object. This object will give you some basic information about the currently logged in user.

The information in here is useful, but in our case it does not contain everything we need. What we can do, however, is use the ADSystemInfo.UserName property to open a connection to the active directory LDAP database, which is where the information we are after is held. So the first part of the script will look like this:


'create the objects and set the initial vars

Set WshShell = WScript.CreateObject("WScript.Shell")

Set FileSysObj = CreateObject("Scripting.FileSystemObject")

Set objADSysInfo = CreateObject("ADSystemInfo")

Set UserObj = GetObject("LDAP://" & objADSysInfo.UserName)


You have probably seen the first two lines before; they are creating objects (the shell and file system objects) we will use later.

The third and fourth lines are what we are using to connect to the active directory. First we are using the ADSystemInfo, and then, using the UserName property of that object, we can create a new object that binds to the client's user in the active directory.

Now that we have done that, we have access to all the information for the client, and we can use this information in our email signature. Next, the script will use our shell object to build some paths to where we are going to save the new signature file:


strAppData = WshShell.ExpandEnvironmentStrings("%APPDATA%")

SigFolder = StrAppData & "MicrosoftSignatures"

strQuteChr = chr(34)

SigFile = SigFolder & UserObj.sAMAccountName & ".htm"


These lines are just using the system environment variable APPDATA so that we know where to build the signature file. We then create an easy to remember variable for the double quote character, and set the name/path of our signature file to be the user's account name in the default signature folder.

Checking for Existing Signature File

Before we start to create anything, it's always a good idea to do a couple of checks first. First of all, let's make sure the signature directory exists before trying to create anything in it -- if it's not there, then create it:


'check the existence of the sig dir, if not there create it

if not FileSysObj.FolderExists(SigFolder) then

FileSysObj.CreateFolder(SigFolder)

end if


These lines use the File system object we created at the start of the script to check for the existence of the signature folder. If that check returns false, then we create it using the file system object again.

The next thing we are going to do is work out if we actually need to go through the process of creating the signature.

We will do this by first checking for the existence of the signature file, then seeing if the last modified date of the file is before the last modified date of the user. If the user has changed since the file was created, we need to create the file again, otherwise we don't need to bother. The following section of code accomplishes this task:


if FileSysObj.FileExists(SigFile) then

'get amended date of file, then compare it to the changed date of the user

Set objFile = FileSysObj.GetFile(SigFile)

datSigAlt = objFile.DateLastModified

intTimeDiff = DateDiff("n", datSigAlt, UserObj.whenChanged)

'if the difference is less than 0 then we do not need to do anything as the sig file is uptodate

if intTimeDiff < 0 then wscript.Quit

end if


Once we know if the signature file exists, we get a handle on it and find out the last time it was modified. Once we know this, we compare in minutes to the last time the user object in the active directory was modified. If the difference is less than zero (0), then the script must be up-to-date, so we don't need to do anything and can just quit the script.

If we are still running at this point, it's time to create the signature file.

Getting the information for the signature file

Before going headlong into creating the file, it's a good idea to first know what you want in it. The example I am going to use will contain simple contact details (name, position, address, telephone, fax, email, and website), a company logo graphic, and a link to some terms and conditions. Your requirements, however, might be different. So how do you know what all the fields in the active directory user object are called?

Well, there is a little app available from Microsoft, called ADSIEdit, that comes with the Windows 2003 support tools. Using this app, you can browse your domain, and see all the objects in it.

If you right click on an object and view its properties, you can see a list of all the available attributes and their current values. You can pick up any of these attributes and insert them into your signature.

Creating the signature

 

Hopefully, you now have an idea of what you want to put into your signature file. Armed with this information, we can now start to create the file. The first thing you need to do is create the file itself; we can do this using a simple CreateTextFile command:


Set CreateSigFile = FileSysObj.CreateTextFile (SigFile, 8, False)


This will give us our file and a handle to it. Now we can start to write some information into it.


Before writing the code to build the file, it might be worth it to first look at the signature HTML document in a WYSIWYG editor. Once you are happy with the way it looks, you can copy and paste the HTML code into your script.


First of all, we will write the opening HTML code into the file:


CreateSigFile.WriteLine "<!DOCTYPE HTML PUBLIC " & """-//W3C//DTD HTML 4.0 Transitional//EN""" & ">"

CreateSigFile.WriteLine "<HTML><HEAD><TITLE>Outlook Signature</TITLE>"

CreateSigFile.WriteLine "<META http-equiv=Content-Type content=" & """text/html; charset=windows-1252""" & ">"

CreateSigFile.WriteLine "<META content=" & """MSHTML 6.00.2900.3059""" & " name=GENERATOR></HEAD>"

CreateSigFile.WriteLine "<BODY>"

CreateSigFile.WriteLine "<HR>"


Now that we have that, the next step is to start using information we have about the user, which is held in the active directory object we opened. To get at this information, we just use UserObj.AttributeName. So if we want to print the user's company, we call UserObj.Company.

As mentioned before, you get to see all the attributes using the ADSIEdit application. So, here is the object in action:


CreateSigFile.WriteLine "<DIV align=left><STRONG><FONT face=Arial color=#000000>" & UserObj.displayName & "</strong></FONT></DIV>"

If UserObj.description <> "" then CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial size=2 color=#000000><strong>" & UserObj.description & "</strong></FONT></DIV>"

CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial size=2></FONT><BR></DIV>"

CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial size=2 color=#000000><strong>" & UserObj.company & "</strong></FONT></DIV>"


As you can see, we are getting the user's Display name (not their login name), description, and company. Before writing the description out, we first check to see if there is one. If not, we try to write one out or we would just end up with a blank line.

Finishing the Signature File

Now that we have the basics down, let's create the rest of the file using the same method:


If UserObj.telephoneNumber <> "" then CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial color=#808080 size=1>" & "Tel: &nbsp;&nbsp;&nbsp;&nbsp;" & "</font><FONT face=Arial color=#808080 size=2>" & UserObj.telephoneNumber & "</FONT></DIV>"

If UserObj.mobile <> "" then CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial color=#808080 size=1>" & "Mob: &nbsp;&nbsp;" & "</font><FONT face=Arial color=#808080 size=2>" & UserObj.mobile & "</FONT></DIV>"

If UserObj.mail <> "" then CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial color=#808080 size=1>Email: <A href=" & strQuteChr & "mailto:" & UserObj.mail & strQuteChr & ">" & UserObj.mail & "</A></FONT></DIV>"

If UserObj.wWWHomePage <> "" then CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial color=#808080 size=1>" & "Web: &nbsp;&nbsp;" & "</font><FONT face=Arial color=#808080 size=1><A href=" & strQuteChr & UserObj.wWWHomePage & strQuteChr & ">" & UserObj.wWWHomePage & "</A></FONT></DIV>"

CreateSigFile.WriteLine "<DIV align=left><FONT face=Arial size=2></FONT><br></DIV>"


'append the image

strLogoPath = "http://www.myorg.com/logos/email_sig.jpg

CreateSigFile.WriteLine "<DIV align=left><IMG alt=" & strQuteChr & UserObj.company & strQuteChr & " hspace=0 src=" & strQuteChr & strLogoPath & strQuteChr & " border=0> </DIV>"


CreateSigFile.WriteLine "</BODY></HTML>"

CreateSigFile.Close


In addition to adding the user's contact details to the signature file, we have also added a company logo. Remember, because it is an HTML document, we can do quite a bit with it.

Now save the script and run it. If all goes according to plan, you should see a new HTML file in your signatures directory that you can use as your signature. If you are a Microsoft Outlook user, go to options and under the mail format tag, you should see your new file in the available signature drop down.

Improving the Script

Of course, there are a few things that could help improve the script. You could base the company logo on an active directory group or organizational unit, check for the existence of Outlook before creating the script, and automatically make your signature the default one in Outlook. There's quite a bit of room for improvement, but this script should give you a good base from which to start.

blog comments powered by Disqus
WINDOWS SCRIPTING ARTICLES

- More Windows Scripting Workarounds from Nilpo
- Overloading Methods and More in VBScript
- Improving MFC for Windows Vista
- Regular Expressions in VBScript
- Working with Dates in WMI
- Completing Calendars with VBScript Date Func...
- Building Calendars with VBScript Date Functi...
- Working With Dates and Times in VBScript
- Designing WCF DataContract Classes Using the...
- Understanding Dates and Times in VBScript
- Working With Arrays in VBScript
- Compressed Folders in WSH
- Using .NET Interops in VBScript
- Nilpo`s Scripting Secrets, Vol I
- Database operations using Silverlight 2.0 WC...

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 5 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials