FreeFileSync Email Confirmation

Discuss new features and functions
DaBest
Posts: 3
Joined: 27 Feb 2019

Post by DaBest • 27 Feb 2019, 17:07

I would like to give my litle contribution to this great software as i couldn´t find a simple way to receive email confirmations of sync jobs.

We are going to use powershell for that, so it will only work with operating systems that have powershell support.

Here is the simple way i found to achieve that:

1. Create a powershell script file, for example smtp.ps1 with the following content:

Code: Select all

$lastfile = gci $env:USERPROFILE\Appdata\roaming\freefilesync\Logs | sort LastWriteTime | select -last 1
$emailattachment = $env:USERPROFILE + "\Appdata\roaming\freefilesync\Logs\" + $lastfile
$EmailFrom = “Source Email”
$EmailTo = “Destination Email”
$Subject = “Your Subjet Here”
$Body = “Your Message Here”
$mailmessage = New-Object system.net.mail.mailmessage
$mailmessage.from = ($EmailFrom) 
$mailmessage.To.add($EmailTo)
$mailmessage.Subject = $Subject
$mailmessage.Body = $Body
$attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'text/plain')
$mailmessage.Attachments.Add($attachment)
$SMTPServer = “smtp.gmail.com”
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential(“EMAIL USERNAME HERE”, “EMAIL PASSWORD HERE”);
$SMTPClient.Send($mailmessage)
You have to manipulate the script with your own data. This script sends the last log file it finds on the logs folder, you can remove the attachments lines if you don´t need that.

2. Put the above file in a folder, for example d:\smtp

3. Put the following script execution command in the job´s options command execution, choosing the alert on option that better suites you: powershell.exe -executionpolicy unrestricted -command d:\smtp\smtp.ps1

That´s all you need to achieve receiving email notifications of FreeFileSync Jobs.

If you need to hide your email credentials on the script you can encrypt the smtp.ps1 file (or whatever name you gave to it) by doing the following:

1. Create another script with the following content, give it a name, for example encrypt.ps1:

Code: Select all

function Encrypt-Script($path, $destination) {
  $script = Get-Content $path | Out-String
  $secure = ConvertTo-SecureString $script -asPlainText -force
  $export = $secure | ConvertFrom-SecureString
  Set-Content $destination $export
  "Script '$path' has been encrypted as '$destination'"
}

Encrypt-Script $PWD\smtp.ps1 $PWD\smtp.bin
2. run the following command in an administrator command line prompt (it should be run in the same folder of the script files): powershell.exe -executionpolicy unrestricted -command %cd%\encrypt.ps1

It will create a smtp.bin (encrypted script) file in the current folder.

3. Now you have to create a decrypt script with the following code to execute the encrypted bin file, give it a name , for example smtpexe.ps1:

Code: Select all

function Execute-EncryptedScript($path) {
  trap { "Decryption failed"; break }
  $raw = Get-Content $path
  $secure = ConvertTo-SecureString $raw
  $helper = New-Object system.Management.Automation.PSCredential("test", $secure)
  $plain = $helper.GetNetworkCredential().Password
  Invoke-Expression $plain
}

Execute-EncryptedScript "PATH OF THE BIN FILE"\smtp.bin
Note: you have to put the path of the bin file in the last line of the script (ex: d:\smtp).

4. The execution code you should put on FreeFileSync job´s options command execution is: powershell.exe -executionpolicy unrestricted -command d:\smtp\smtpexe.ps1

And thats it, simple, isn´t it!?

Hope it helps someone.

Best regards

User avatar
cc12
Posts: 2
Joined: 13 Mar 2019

Post by cc12 • 13 Mar 2019, 14:56

Hello,
your script is interesting but I didn't understand where it should be inserted for the right use.
That is, I have not understood where to go so that at the end of the execution of the freefilesync job logs can be sent by e-mail.
I also confirm that if I use a portable version of the software the first two lines must be adapted accordingly.
Thanks Carlo

DaBest
Posts: 3
Joined: 27 Feb 2019

Post by DaBest • 15 Mar 2019, 10:43

cc12 wrote:
13 Mar 2019, 14:56
Hello,
your script is interesting but I didn't understand where it should be inserted for the right use.
That is, I have not understood where to go so that at the end of the execution of the freefilesync job logs can be sent by e-mail.
I also confirm that if I use a portable version of the software the first two lines must be adapted accordingly.
Thanks Carlo
Hello cc12

Let me see if i can put an image here with the location where to put the last command, i tried it on the first post but the server thought i was a spammer :)

https://mega.nz/#!SDgRQQyK!HkTaTF__gF6O_PrJ7bbaSQlbR5V7Jvjq_G4g6F9tNPk

It´s on every job´s settings...

Best regards

User avatar
cc12
Posts: 2
Joined: 13 Mar 2019

Post by cc12 • 16 Mar 2019, 11:04

Thanks for the answer and the example.
I preferred to manage everything with a bat file.
It works very well.
Thanks again for the script you made really does for me.
Best regards
Carlo

merovingio
Posts: 1
Joined: 16 Mar 2019

Post by merovingio • 16 Mar 2019, 18:33

Is it possible to send an email only when there is a problem with the sync?

DaBest
Posts: 3
Joined: 27 Feb 2019

Post by DaBest • 16 Mar 2019, 18:38

merovingio wrote:
16 Mar 2019, 18:33
Is it possible to send an email only when there is a problem with the sync?
Yes of course, just look at the image i posted above, instead of "on complition" choose "on errors"

Best Regards