Email Example

From ProVide DocWiki
Revision as of 13:03, 20 June 2018 by Lukas Wiberg (talk | contribs)
Jump to: navigation, search

For this example we'll use a powershell script that sends an email to the admin when a new file has been uploaded on the ProVide server.

# Setup script parameters
param ([Parameter(Mandatory=$true)][string]$local, [Parameter(Mandatory=$true)][string]$ftp)

# Email parameters

$SMTPServer = ""

$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)

$SMTPClient.EnableSsl = $true

# The email address that will appear to be the sender of the email.
$EmailFrom = ""

# The email address that will receive the email.
$EmailTo = ""

# This will be the subject of the email.
$Subject = "New file"

# The email text that will be sent will be what is contained within the '@"' in the start and the '"@' in the end.
$Body = @"
A new file has arrived through $ftp, ending up in $local.
Best regards,

# If credentials are required for sending through this SMTP server

$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("domain\user", "password");

# If we should skip certification validation check for this SMTP server
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = { return $true }

# Now we are ready to send the message

$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

We want this script to trigger when a user is done uploading a file so go to the "Messages and events" in the provide administrator interface and select "OnUploadEnd" from the drop-down menu. We want to add the follow line to the message box:

%EXECUTE(Powershell.exe -File "c:\PATH\TO\SCRIPT\SCRIPT.ps1" -local "%LOCAL_FILENAME%" -ftp "%FTP_FILENAME%")%

This line is telling ProVide that when the upload has been finished it will execute the powershell script located at the path and it will send with it the 'local_filename' and 'ftp_filename'.