Showing posts with label email. Show all posts
Showing posts with label email. Show all posts

Tuesday, 19 February 2008

Send Welcome Email

Wouldn't it be nice to send your new users an email welcoming them to the company and providing them with some info about how things work. Anyway, I had a request to do this and since I don't think you can do it natively within Exchange 2003 I turned straight away to Powershell.

So, all you need to do is run a daily sceduled task which calls the below Powershell script. It finds the current and next days's dates, searches AD for users created between those dates (i.e. on the current day) and sends an email to the users it finds with the relevant info attached.

(Note: You need the Quest AD cmdlets installed unless you want to use ADSI for the AD search part.)

Doing the search by specifiying the exact date didn't seem to work very successfully. Digging around the Internet revealed the correct format to get the dates into and also searching between the two adjacent dates.


#Find today's and tomorrow's dates and store them in the 'yyyyMMdd}000000.0Z' format
$dayStart = "{0:yyyyMMdd}000000.0Z" -f (Get-Date)
$dayEnd = "{0:yyyyMMdd}000000.0Z" -f ((Get-Date).AddDays(1))

#Find AD users created between the two dates
$newuser = Get-QADUser -searchroot 'springfield.local/userou' -LdapFilter "(&(whenCreated>=$dayStart)(whenCreated<=$dayEnd))" -size 0

#Find the email address for each of the users and send a mail to them with the WelcomeTo.doc attachment
foreach ($address in $newuser)
{
$recipient = $address.mail
$sender = "[email protected]"
$server = "smtpservername"
$file = "path to attachment"
$subject = "subject"
$body = "body text"
$msg = new-object System.Net.Mail.MailMessage $sender, $recipient, $subject, $body
$attachment = new-object System.Net.Mail.Attachment $file
$msg.Attachments.Add($attachment)
$client = new-object System.Net.Mail.SmtpClient $server
$client.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$client.Send($msg)
}