Script – Send a Message to Employees Celebrating their Work Anniversary

I have posted this on Spiceworks but figured I should post it here as well.

#Thanks to the following sources
#1 https://www.cgoosen.com/2015/04/user-powershell-to-bulk-email-your-users/
#2 https://community.spiceworks.com/topic/1332314-automatic-birthday-reminder-script-issues-powershell

# Define local Exchange server info for message relay. Ensure that any servers running this script have permission to relay.
$ExchangeServer = "mail.yourdomain.com"
$SMTPPort = "25"
$FromAddress = "AnniversaryTeam <AnniversaryTeam@yourdomain.com>"

# Function to create report email
function SendNotification{
 $Msg = New-Object Net.Mail.MailMessage
 $Smtp = New-Object Net.Mail.SmtpClient($ExchangeServer, $SMTPPort);
 $Msg.From = $FromAddress
 $Msg.To.Add($ToAddress)
 $Msg.Subject = "Happy Anniversary!"
 $Msg.Body = $EmailBody
 $Msg.IsBodyHTML = $true
 $Smtp.Send($Msg)
}

# Import user list and information from .CSV file and get today's date
$Date = Get-Date
$Users = Import-Csv C:\Scripts\Anniversary.csv
 
# Send notification to each user in the list whose anniversary is today
Foreach ($User in $Users) {
if ($Date.Day -eq $User.Anniversary_day -and $Date.Month -eq $User.Anniversary_month) {
 $ToAddress = $User.Email
 $Name = $User.FirstName
 $EmailBody = @"
 <html>
 <head>
 </head>
 <body>
 <p>Dear $Name,</p>
 
 <p>Today is your work anniversary! We appreciate you! Now, get back to work!</p>
 
 <p>Regards,</p>
 
 <p>Anniversary Team</p>
 </body>
 </html>
"@
 Write-Host "Sending notification to $Name ($ToAddress)" -ForegroundColor Yellow
 SendNotification
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *