Send Welcome Mail to Each Newly Created Mailbox in Microsoft Exchange 2010

Introduction

This script could be used to send welcome mail for each newly created mailbox in Microsoft Exchange 2010. This script allows you to use an .html file as a template. You can use placeholders in this template file. This script allows you to replace these placeholders when the script is being executed.

Scenarios

In a real world, IT administrators are always required by human resource department to send welcome mail to each new employee. A welcome mail to a new employee who has accepted your job offer confirms the employee's decision to accept the position. It will save the new employee some feelings of insecurity and avoids misunderstandings.

Script

This script contains one advanced function, Send-OSCEXWelcomeMail. It will create a log file (SentLog.csv) in the same folder where the script resides. If you want to run the script periodically, you should prepare a mail enabled user account which will be used to send welcome mails. And you should grant this account with proper permission, like Recipient Management, by using Role Base Access Control (RBAC) User Editor. Otherwise this account cannot run the cmdlets which are required by this script.

Also you should prepare an .html welcome mail template by using Word or any other web page editor.

You can use this script in following way:

  1. Download the script and copy it to a utility server with Microsoft Exchange Management Tools installed.
  2. Open the script file with Notepad or any other script editors.
  3. Scroll down to the end of the script file, and then add the example command which you want to run.
  4. There is a welcome mail template in the script package, please open it by using Microsoft Word or any other web page editors. After that, you can change it base on your business needs.
    Placeholder00 is reserved for the display name of newly created mailboxes. You can use Placeholder## or other text in the template. These texts can be replaced by using ReplacePlaceholders parameter.
  5. Create a task in Task Scheduler. Here are some screenshots for an example task.
    Screenshot 01: Task - General

    Note: If the script cannot send welcome mails, please change “Run whether user is logged on or not” to “Run only when user is logged on”. Thus, you can review the verbose message in the console.

    Screenshot 02: Task - Triggers

    Note: The value of “Repeat task every” should match the value of ScheduledTaskRepeatInterval parameter. If you use 2 hours here, you should use 120 as the value of ScheduledTaskRepeatInterval parameter.

    Screenshot 03: Task - Actions

    Note: You need to make sure that the arguments which will be used by PowerShell are correct. The easiest way to verify them is run the whole command in a command prompt. You can review Scripting with the Exchange Management Shell for more details.

 Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
Edit|Remove
if ($sentUsers.ContainsKey($newlyCreatedMailboxAlias)) { 
    $verboseMsg = $Messages.WelcomeMailHasBeenSentAlready 
    $verboseMsg = $verboseMsg -$newlyCreatedMailboxAlias 
    $pscmdlet.WriteVerbose($verboseMsg) 
} else { 
    Switch ($pscmdlet.ParameterSetName) { 
        "__AllParameterSets" { 
            #Send welcome mail to one employee 
            $verboseMsg = $Messages.SendingWelcomeMail 
            $verboseMsg = $verboseMsg -$newlyCreatedMailboxAlias 
            $pscmdlet.WriteVerbose($verboseMsg) 
            if ($Credential -ne $null) { 
                Send-MailMessage -From $From -To $($newlyCreatedMailbox.PrimarySmtpAddress) ` 
                -Subject $Subject -Body $mailBody -BodyAsHtml ` 
                -SmtpServer $SMTPServer -Credential $Credential 
            } else { 
                Send-MailMessage -From $From -To $($newlyCreatedMailbox.PrimarySmtpAddress) ` 
                -Subject $Subject -Body $mailBody -BodyAsHtml ` 
                -SmtpServer $SMTPServer 
            } 
            #Write user alias and mail sent time to the log file 
            Out-File -FilePath $scriptLogFile -InputObject "`"$newlyCreatedMailboxAlias`",`"$(Get-Date)`"" -Append -Encoding Unicode 
        } 
        "Pilot" { 
            #Redirect welcome mail to administrator 
            $verboseMsg = $Messages.RedirctingWelcomeMail 
            $verboseMsg = $verboseMsg -$newlyCreatedMailboxAlias,$AdminEmailAddress 
            $pscmdlet.WriteVerbose($verboseMsg) 
            if ($Credential -ne $null) { 
                Send-MailMessage -From $From -To $AdminEmailAddress ` 
                -Subject $Subject -Body $mailBody -BodyAsHtml ` 
                -SmtpServer $SMTPServer -Credential $Credential 
            } else { 
                Send-MailMessage -From $From -To $AdminEmailAddress ` 
                -Subject $Subject -Body $mailBody -BodyAsHtml ` 
                -SmtpServer $SMTPServer 
            } 
        } 
    } 
}

Examples

Example 01: Displays help about Send-OSCEXWelcomeMail
Command: Get-Help Send-OSCEXWelcomeMail -Full
Screenshot:

Example 02: Send welcome mails to these mailboxes which created four hours before the script is being executed. (In pilot mode)
If the script is executed in pilot mode, it means welcome mails will not be sent to the newly created mailboxes. These welcome mails will be redirected to the administrator's mailbox. It’s useful for verifying the functionality of the script.
Command:
Send-OSCEXWelcomeMail -ScheduledTaskRepeatInterval 240 -From "itadmin@corp.contoso.com" -Subject "Welcome to Contoso!" -SMTPServer "smtpserver.corp.contoso.com" -Template "C:\Scripts\029\MailTemplate.html" -AdminEmailAddress "itadmin@corp.contoso.com" -Verbose
Screenshot:

Note: Duplicate welcome mails may send to administrator’s mailbox if you run this command multiple times in a short time period(less than the value of ScheduledTaskRepeatInterval). Because of administrators may want to adjust the format of welcome mail, this behavior is by design. The root cause of this behavior is if the script is executed in pilot mode, it will not write a log entry in the log file.

Example 03: Send welcome mails to these mailboxes which created four hours before the script is being executed.
Command:
Send-OSCEXWelcomeMail -ScheduledTaskRepeatInterval 240 -From "itadmin@corp.contoso.com" -Subject "Welcome to Contoso!" -SMTPServer "smtpserver.corp.contoso.com" -Template "c:\Scripts\029\MailTemplate.html" -Verbose
Screenshot:

Example 04: Send welcome mails to these mailboxes which CustomAttribute1 is ITO. The mailboxes are created four hours before the script is being executed. Replace Placeholder01 which contains in the template file with the real department name.
Command:
Send-OSCEXWelcomeMail -MailboxFilter 'CustomAttribute1 -eq "ITO"' -ScheduledTaskRepeatInterval 240 -From "itadmin@corp.contoso.com" -Subject "Welcome to Contoso!" -SMTPServer "smtpserver.corp.contoso.com" -Template "c:\Scripts\029\MailTemplate.html" -ReplacePlaceholders @{"Placeholder01"="ITO department"} -Verbose
Screenshot:

 

Additional Resources

Technical Resources:

Windows PowerShell Advanced Function
Get-Mailbox
Filterable Properties for the -Filter Parameter in Exchange 2007 SP1 and SP2
Scripting with the Exchange Management Shell
Understanding Role Based Access Control