How to send emails with powerJobs Processor

A mail needs to be sent to a couple of users within a powerJobs Processor script.

Powershell already has a cmdlet to send mails. This code demonstrates how to use it to send a mail to a list of users. The script is pretty straight forward and most of the work is collecting the information you want to send. You have to make sure your SMTP server is configured properly. If it isn't you won't get an error in most cases so you'll have to check the server logs if the script is not working.

#region settings
$sender = "sender@yourcompany.com"
$receivers = @("receiver1@yourcompany.com", "receiver2@yourcompany.com")
$subject = "YourSubject"
$body = "Write your email text here and use variables to add additional information"
$smtp = "YourSMTP"
$passwd = ConvertTo-SecureString -AsPlainText "" -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 #Depending on the SMTP this might need to be $true
#endregion

#region send mail
foreach($address in $receivers) {
    if($attachments.Count -gt 0) {
        Send-MailMessage -From $sender -To $address -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -Attachments $attachments -UseSsl:$useSSL
    }
    else {
        Send-MailMessage -From $sender -To $address -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -UseSsl:$useSSL
    }
}
#endregion
Write-Host "Job completed"

Send E-mail on lifecycle transition

This sample code demonstrates how to send E-Mails to a list of users on a lifecycle transition. The script has to be added to the powerJobs Processor Jobs folder on "C:\ProgramData\coolOrange\powerJobs\Jobs". In order to execute the job on Lifecycle transition refer to this article. The code also collects some information about the Vault and then, on the specified lifecycle changes a E-Mail will be sent that communicates the changes that were made.

#region vault information
$vaultname = $vaultConnection.Vault
$vaultserver = $vaultConnection.Server
$folderpath = $file.path.replace("$","%24").replace("/","%2f")
$fullPath = $folderpath + "%2f" + $file.Name.Replace(" ","+")
$link = "http://$($vaultserver)/AutodeskDM/Services/EntityDataCommandRequest.aspx?Vault=$($vaultname)&ObjectId="+$fullPath+"&ObjectType=File&Command=Select"
#endregion

#region settings
$sender = "sender@yourcompany.com"
$receivers = @("receiver1@yourcompany.com", "receiver2@yourcompany.com")
$subject = "The file $($file.Name) has changed from $oldState to $newState"
$body = "If you like to view the related document, just follow this link: $link"
$smtp = "Yoursmtp"
$passwd = ConvertTo-SecureString -AsPlainText "YourPassword" -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 #Depending on the SMTP this might need to be $true
#endregion

#region lifecycle transition information
$lfcTransId = $job.LifeCycleTransitionId
$lfcTrans = $vault.LifeCycleService.GetLifeCycleStateTransitionsByIds(@($lfcTransId))
$lfcs = $vault.LifeCycleService.GetLifeCycleStatesByIds(@($lfcTrans[0].FromId, $lfcTrans[0].ToId))
$oldstate = $lfcs[0].DispName
$newstate = $lfcs[1].DispName
#endregion

#region send mail
foreach($address in $receivers) {
    if($attachments.Count -gt 0) {
        Send-MailMessage -From $sender -To $address -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -Attachments $attachments -UseSsl:$useSSL
    }
    else {
        Send-MailMessage -From $sender -To $address -Subject $subject -Body $body -SmtpServer $smtp -Credential $cred -UseSsl:$useSSL
    }
}
#endregion
Write-Host "Job completed"

Remarks