« Sending mail to users whose password's about to expire | Main | Enabling and disabling MAPI, POP, IMAP, or HTTP »

October 03, 2005

Sending a welcome email to newly created users

Mike Ruman of Grant Thornton LLP sent me this cool script: it selects all the users created today and sends them a welcome message. You'll need to edit it to reflect your own domain information, as well as the contents of the message you want to send.

'Created by Mike Ruman 8/13/05
'Sends an email to accounts created today.

Dim StrDate, CurrentUTC

'Create the current date and time stamp for query for day before last
CurrentUTC = DatePart("yyyy", Date)
'now add Month in mm if only M add leading 0
if DatePart("m" , Now) < 10 then
CurrentUTC = CurrentUTC & 0 & DatePart("m" , Now)
else
CurrentUTC = CurrentUTC & DatePart("m" , Now)
end if
'now add Day in dd if only d add leading 0
if DatePart("d" , Now) < 10 then
'OPTIONAL - FOR MANY DAYS, replace line below with CurrentUTC = CurrentUTC & 0 & DatePart("d" , Now - X) where X = # of days

CurrentUTC = CurrentUTC & 0 & DatePart("d" , Now)
else
'OPTIONAL - FOR MANY DAYS, replace line below with CurrentUTC = CurrentUTC & DatePart("d" , Now - X) where X = # of days
CurrentUTC = CurrentUTC & DatePart("d" , Now)
end if
' Tag hour, minute, second on
strDate = CurrentUTC&"000001.0Z"

'Create AD Connection
Set oConnection1 = CreateObject("ADODB.Connection")
Set oCommand1 = CreateObject("ADODB.Command")
oConnection1.Provider = "ADsDSOObject" ' This is the ADSI OLE-DB provider name
oConnection1.Open "Active Directory Provider"
' Create a command object for this connection.
Set oCommand1.ActiveConnection = oConnection1
'Set Query definition

oCommand1.CommandText = "select mail from 'LDAP://DC=Fabrikam, DC=com' WHERE objectCategory='Person' AND objectClass='user'AND msExchHideFromAddressLists<>'True' AND whenCreated>='" & strDate & "'"

oCommand1.Properties("Page Size") = 30000
' Execute the query.
Set rs = oCommand1.Execute

rs.movefirst
'Create the loop of results
Do Until rs.EOF = True

'Create Email
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Admin@fabrikam.com"
objEmail.To = rs.Fields("mail")
'Optional BCC field
'objEmail.BCC = "Admin@fabrikam.com"
objEmail.Subject = "A welcome message from Exchange"
objEmail.HTMLbody = "Welcome to Fabrikam. Please click on the attached file for a message."

'Optional Add an attachment
objEmail.AddAttachment "C:\new_hire_audio_message.wav"

objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing ") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver ") = _
"ExchangeServer" 'Replace ExchangeServer with server IP or name
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport ") = 25
objEmail.Configuration.Fields.Update

'Optional - Read the message before it's sent
'MsgBox objEmail.GetStream.ReadText
objEmail.Send
rs.movenext
Loop

'Close AD Connection
oConnection1.close

Posted by Paul at October 3, 2005 06:00 AM

Comments

Looks like a good script but it really needs variables defining at the top section of the script so there is only a small place for users to edit.
Also I question the wisdom of attaching a file from the system administrator asking users to click on it - after all, most sysadmins spend most of their time asking users NOT to click on attachments that get sent to them (supposedly) by the system administrator.

Posted by: Andy [TypeKey Profile Page] at October 10, 2005 08:48 PM

Those are both good points, Andy. I generally post contributed scripts here without editing them; I actually had a to-do reminding me to write a script to do this, but it had lain undone for two months and I figured it would be better to post Mike's script than to keep ignoring the to-do item :)

Posted by: Paul at October 11, 2005 10:42 AM

Thanks, great script.

Posted by: Karl Tens at September 11, 2006 04:36 AM

Post a comment

sign out)'); } else { echo('If you have a TypeKey identity, you can sign in to use it here.'); } ?>




Remember Me?

(you may use HTML tags for style)