FreeFileSync Email Confirmation

Discuss new features and functions
Posts: 8
Joined: 27 Feb 2019

DaBest

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:
$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:
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:
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
Posts: 3
Joined: 13 Mar 2019

cc12

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
Posts: 8
Joined: 27 Feb 2019

DaBest

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 cc12, 13 Mar 2019, 14:56
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
Posts: 3
Joined: 13 Mar 2019

cc12

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
Posts: 3
Joined: 16 Mar 2019

merovingio

Is it possible to send an email only when there is a problem with the sync?
Posts: 8
Joined: 27 Feb 2019

DaBest

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

Best Regards
Posts: 3
Joined: 16 Mar 2019

merovingio

is it possibile to received a mail with in the subject the sentence:

Complete.... or Error....?
Posts: 8
Joined: 27 Feb 2019

DaBest

is it possibile to received a mail with in the subject the sentence:

Complete.... or Error....? merovingio, 04 Sep 2019, 16:28
You can use the "on completion" option to send the email with the attached file and you can check the attached file for errors or success...

Anyway, you should only worry if you have errors, if it´s complete you don´t have to worry about it, if you choose the option "on errors" you will always know that if you don´t get an email than it completed with success...

Best Regards
Posts: 1
Joined: 24 Oct 2019

ssorgem

DaBest - very nice and simple script, you really hit a home run with this !

Working perfectly.

Thanks for your contribution.
Posts: 4
Joined: 2 May 2019

hullabaluh

Dear DaBest,

What a great guide on how to achieve this. You're really da best ;-) Malfortunately I have some difficulties achieving this :-(
I can execute the command in a powershell and I receive my email.
The task scheduler executes freefilesync as the domain backup user (being domain admin but being the only one having access to the NAS)
With executing PowerShell as the domain backup user ("Run as different user" accoring to https://stackoverflow.com/a/44797801; start powershell -credential "" gives me a "frozen" powershell) I can also send/recieve the email

But when having freefilesync running as the domain backup user through the task scheduler, the email is not sent and thus I do receive no notification of my failed backup job. Freefilesync runs fine though. I do not see a trace of this failure in the freefilesync logfile or in the event viewer (but I do not know how to search in there precisely).

Do you have any idea, why the email is not sent through task scheduler? Has it something to do with the "frozen" powershell when executing start powershell -credential "" and providing the backups user credentials?

Any help is much appreciated!
Kindest regards,
Aurel

Edit: I have no environment variables like $PATH in my ps1 file:
$lastfile = gci C:\Backup_FreeFileSync | sort LastWriteTime | select -last 1
$emailattachment = "C:\Backup_FreeFileSync\" + $lastfile

Edit II:
Using the following command in freefilesync helped:
powershell.exe -NoLogo -NonInteractive -ExecutionPolicy Bypass -noexit -noprofile -command C:\Backup_FreeFileSync\sendFFSMail.ps1
Posts: 8
Joined: 27 Feb 2019

DaBest

DaBest - very nice and simple script, you really hit a home run with this !

Working perfectly.

Thanks for your contribution. ssorgem, 24 Oct 2019, 14:23
Your welcome...
Posts: 8
Joined: 27 Feb 2019

DaBest

Dear DaBest,

What a great guide on how to achieve this. You're really da best ;-) Malfortunately I have some difficulties achieving this :-(
I can execute the command in a powershell and I receive my email.
The task scheduler executes freefilesync as the domain backup user (being domain admin but being the only one having access to the NAS)
With executing PowerShell as the domain backup user ("Run as different user" accoring to https://stackoverflow.com/a/44797801; start powershell -credential "" gives me a "frozen" powershell) I can also send/recieve the email

But when having freefilesync running as the domain backup user through the task scheduler, the email is not sent and thus I do receive no notification of my failed backup job. Freefilesync runs fine though. I do not see a trace of this failure in the freefilesync logfile or in the event viewer (but I do not know how to search in there precisely).

Do you have any idea, why the email is not sent through task scheduler? Has it something to do with the "frozen" powershell when executing start powershell -credential "" and providing the backups user credentials?

Any help is much appreciated!
Kindest regards,
Aurel

Edit: I have no environment variables like $PATH in my ps1 file:
$lastfile = gci C:\Backup_FreeFileSync | sort LastWriteTime | select -last 1
$emailattachment = "C:\Backup_FreeFileSync\" + $lastfile

Edit II:
Using the following command in freefilesync helped:
powershell.exe -NoLogo -NonInteractive -ExecutionPolicy Bypass -noexit -noprofile -command C:\Backup_FreeFileSync\sendFFSMail.ps1 hullabaluh, 25 Oct 2019, 06:05
As i see it you don´t need to run the powershell as a different user you just have to create the freefilesync task on the task Scheduler and run it as admin (see attached file) if you done everything right on the script there should be nothing more to it...
Attachments
taskrunasadmin.JPG
taskrunasadmin.JPG (60.49 KiB) Viewed 16097 times
User avatar
Site Admin
Posts: 7211
Joined: 9 Dec 2007

Zenju

Email notifications are now directly supported with FreeFileSync 10.20!
Posts: 8
Joined: 27 Feb 2019

DaBest

Email notifications are now directly supported with FreeFileSync 10.20! Zenju, 15 Feb 2020, 09:51
Nice, have to test it out :)

Thanks
Best Regards
Posts: 2
Joined: 8 Aug 2021

sbourdon

Hello,

I've tried your script under Windows 10 with FreeFileSync's latest release but nothing happens...
Are you still using this? Is it still working for you with FreeFileSync 11.12?

Thanks!
Posts: 8
Joined: 27 Feb 2019

DaBest

Hello,

I've tried your script under Windows 10 with FreeFileSync's latest release but nothing happens...
Are you still using this? Is it still working for you with FreeFileSync 11.12?

Thanks! sbourdon, 09 Aug 2021, 11:09
@sbourdon

FreeFileSync now has a feature to send email notifications, just go in the Job´s configuration and you will see it there...

As for your question, the script is independent from freefilesync, so it should work no matter the version, you might have to change it if the log´s folder changes location. Just follow the instructions, and it should work...

Best Regards