How to send emails on Vault events

An Email needs to be sent to specific users on a specific Vault event using powerEvents

powerEvents is able to hook up to specific Vault Events. You can choose your preferred event and send an email when the event is triggered. In this example an email is sent when a file is released.

Crate a new event script

Open the powerEvents IDE and create a new event script with the name YourCompanyName.SendMailOnRelease.ps1 in "C:\ProgramData\coolOrange\powerEvents\Events" and open it in a PowerShell ISE.

Register to Vault Lifecycle State changes

With this code you can register to the UpdateFileStates_Post event and the function gets executed every time the event is raised. The EventName defines the event while -Action defines the function that is executed when the event is triggered.

Register-VaultEvent -EventName UpdateFileStates_Post -Action FileReleased
 

This is the function that gets executed when a Vault file changes state to Released. At the beginning information about the E-Mail gets collected and then the E-Mail is sent with Send-MailMessage

Function FileReleased ($files) {
foreach ($file in $files) {
if ($file._State -eq "Released") {
$sender = "sender@yourcompany.com"
$receivers = @("receiver@yourcompany.com", "receiver2@yourcompany.com")
$subject = "Subject"
$body = "Write your email text here and use variables to add additional information"
$smtp = "yoursmtp"
$passwd = ConvertTo-SecureString -AsPlainText "Password" -Force #Insert your password
$cred = new-object Management.Automation.PSCredential $sender, $passwd
$attachments = @() #Fill with absolute filepaths to add attachments to the mail. E.g. @("C:\TEMP\testfile.txt", "C:\TEMP\testfile2.txt")
$useSSL = $false

foreach($address in $receivers) {
if($attachments.Count -gt 0) {
Send-MailMessage -From $sender -To $receivers -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -Attachments $attachments -UseSsl:$useSSL
}
else {
Send-MailMessage -From $sender -To $receivers -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -UseSsl:$useSSL
}
}
}
}
}

Troubleshooting

If you get following error message:

Change $useSLL = $false to:

$useSSL = $true
See Also