Send email notification

Discuss new features and functions
User avatar
Site Admin
Posts: 7212
Joined: 9 Dec 2007

Zenju

A user sent me a script which may be useful for admins who want to run FreeFileSync and send a status report via email:

>I needed email notification containing log warnings and errors, so I wrote the attached VBS. It is probably not bug-free as I could test it only in my environment, but you are welcome to it. If it's something you would like to distribute to add value to your product, fell free to do so.
Again, thank you.
Lane Beneke
Attachments
FreeFileSync.vbs
(10.99 KiB) Downloaded 7401 times
Posts: 10
Joined: 17 Sep 2013

nodough

Hi All,

I've updated this script several times and thought it might be a good idea to upload the updated version.

Enjoy,
NoDough

Edit: Attachment removed in favor of newer version below.
Posts: 16
Joined: 13 Apr 2014

eahm

Hi, thank you for this. Few questions:

1) Do I have to put this file on "On completion:"? Just exactly like a .bat file?

2) Do I have to create multiple copies based on how many profiles I have? With different names?

3) How can I use it with the Gmail SMTP (SSL, Auth etc.)?

Thank again.
Posts: 10
Joined: 17 Sep 2013

nodough

Hello All,

Posting yet another update to the script. The most obvious change is I've added duration to the report.

eahm, I'll attempt to answer your questions.

1) The script will execute FreeFileSync.exe and wait for it to terminate. How it terminates is up to you, but the script will not continue until it does.

2) I like to divide my ffs_batch files up according to their task. But there's no reason you couldn't dump all your tasks into a single ffs_batch file.

3) Good question. I have no idea. But this site appears to guide you through it.

Enjoy!

Edit: Removed file attachment in favor of newer version below.
Posts: 1
Joined: 10 Nov 2014

berettatim

If for someone could help....
You could update the script to send email with more paramenters of authentication, I found this:
Const SMTPServer = "yourserver.yourdomain.com"
Const SMTPAuth = 1
Const SMTPPort = 25
Const SMTPUsername = "your.smtp.username"
Const SMTPPassword = "your.smtp.password"


oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = SMTPAuth
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'SendUsingPort
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPUsername
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword

Dim ArgArray(10,1)


ArgArray(6,0) = "SMTPServer"
ArgArray(6,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/smtpserver"")"
ArgArray(7,0) = "SMTPAuth"
ArgArray(7,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"")"
ArgArray(8,0) = "SMTPPort"
ArgArray(8,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/smtpserverport"")"
ArgArray(9,0) = "SMTPUsername"
ArgArray(9,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/sendusername"")"
ArgArray(10,0) = "SMTPPassword"
ArgArray(10,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/sendpassword"")"
Posts: 2
Joined: 14 Nov 2015

teymur-bagirov

we use blat (command line smtp sender)
the next line in bat file after FFS is blat, which sends log.
that's it
Posts: 10
Joined: 17 Sep 2013

nodough

I've updated the script and attached it to this message. Updates include...

Added the fields for SMTP authentication.
Modified the Email send process to retry on failure.
Fixed bug finding log files.
Fixed bug calculating elapsed time.
Added server name and batch name to body of report.

NoDough

P.S.: Two users have inquired how to send the entire log file. This script is not designed to send the entire log file. The purpose of this script is to send only the errors and warnings when things go wrong.
Attachments
FreeFileSync.vbs
(20.3 KiB) Downloaded 7299 times
Posts: 11
Joined: 31 Mar 2016

FileSyncExpert

Thanks for sharing this. I worked on an similar Project with autohotkey. Its a beta Version.
When its done, i will share this to... I hope that it does not take longer than 2 - 3 Weeks...
Posts: 2
Joined: 13 Apr 2016

hughc

great script!

I modified it slightly to work with gmail as the SMTP provider, as SSL is required; adding an SSL param was not difficult, but I did not bother to expose it as a command line argument (for my end use) just a configuration constant:
Const SMTPSsl = 1
...
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SMTPSsl
Hope that might help anyone making use.

As an aside, if you set up a dummy gmail account just for the purposes of sending the mail, you will need to enable the 'Less Secure Apps setting in order that mail be sent.

Image
cheers,
Hugh
Posts: 1
Joined: 9 Jun 2016

JonBenavides

it is possible to attach the last log file in email?
Posts: 7
Joined: 29 Apr 2016

timberval

Is it possible to insert in the subject the tag [Warning] or [Error] if the exiterror code is it?
Pls anyone could help?
Thank you.
Posts: 23
Joined: 9 Aug 2016

serendipityguy

it is possible to attach the last log file in email?JonBenavides
I guess that some of the log files are too large for email depending on the size of your backup disk.
Posts: 23
Joined: 9 Aug 2016

serendipityguy

A user sent me a script which may be useful for admins who want to run FreeFileSync and send a status report via email:
Lane BenekeZenju
Could this be added into the next build as a feature request? Thanks for sharing Lane.
Posts: 68
Joined: 13 Aug 2018

TheExpert

Hi all,

because of a change in the FFS batch files regarding the tags for log folder you have to replace the search string in the VBS script to find the log folder. If you don't change it you get mail notifications with the following line at the end of the mail "No log file is configured for this batch.".

Instead of the code "If InStr( sLine, "LogfileFolder" ) Then 'FIND LINE CONTAINING LOG FILE FOLDER"
you have to set "If InStr( sLine, "LogFolder" ) Then 'FIND LINE CONTAINING LOG FILE FOLDER".

Update 23 Aug 2018: There were some errors with the log file handling and with the search strings for errors or warnings which I have fixed now. You can choose between German and English error / warning search strings in the section for configuring the script settings. If there are errors or warnings in the log file the lines with the errors or warnings are now added to the notification mail as it was originally designed by Lane Beneke.

Update 08 Sep 2018: I found a new bug in the VBS script. Sometimes the script checked the wrong log file. This is possible if there is more than one job running at the same time. So I've uploaded a new version of the script with the bugfix.

Update 22 Oct 2018: There's now an option to define the path to another GlobalSettings.xml or similar named file.

Update 19 Nov 2018: Because of an error when there's no GlobalSettings.xml or similar named file defined I added a check for 'ConstGlobalSettingsFile': If it isn‘t defined the call procedure of FreeFileSync will be changed. And thanks to @timberetta I added the option for a SSL secured SMTP connection. I don't have the possibility to test the SSL encryption for the SMTP connection. Maybe someone else can test it and give feedback if this is working?

Update 26 Feb 2019: There was an error with parsing the command line when setting a GlobalSettings.xml or similar named file as parameter. Added the description of the new command line parameters 'SMTPSsl' and 'GlobalSettingsFile' to the usage manual at the beginning of the script.

Update 05 Mai 2020: Corrected an error with retrieving the log file under some circumstances.

Update 14 Feb 2021: Corrected an error with the error handling when sending mails.

Update 31 Aug 2021: Added constants 'IncludeServerNameInSubject' and 'IncludeSyncResultInSubject' to allow users to specify what to include in email subject.

You'll find the modified script in the attachment.

Kind Regards

TheExpert
Attachments
FreeFileSync.vbs
(24.87 KiB) Downloaded 5982 times
User avatar
Site Admin
Posts: 7212
Joined: 9 Dec 2007

Zenju

What about using FreeFileSync's "On completion" to send the email and evaluating "%logfile_path%"?
Posts: 68
Joined: 13 Aug 2018

TheExpert

Hi Zenju,

I asked for the usage of this new variable, but I didn't get information about this, see viewtopic.php?t=5590.

Can FreeFileSync now send mail notifications, too? For about more than a year this feature wasn't available. So I have to use the VBS script to get mail notifications.

Kind Regards

TheExpert
Posts: 1
Joined: 25 Oct 2018

Rechotech

@The Expert

I have been unable to get the most current script tor run i get an error message

Cannot find file"".

followed by windows script host message

C:\backup_scripts\FreeFileSync.vbs
line 469
Char 4
Error Invalid procedure coll or argument 'Mid'
code 800A0005

Any ideas.
Posts: 68
Joined: 13 Aug 2018

TheExpert

@The Expert

I have been unable to get the most current script tor run i get an error message

Cannot find file"".

followed by windows script host message

C:\backup_scripts\FreeFileSync.vbs
line 469
Char 4
Error Invalid procedure coll or argument 'Mid'
code 800A0005

Any ideas. Rechotech, 25 Oct 2018, 15:36
The issue with the line and char information in the error message is that these don't correspond to the lines and chars in the script. So it's not very easy to find the error.

Have you defined the path to the log files of your sync / backup jobs at the beginning of the script? Have you saved the job files with the option to save the log file to the same location as defined in the script? The script doesn't search for the file LastSync.log. It searches for log files named like the job with the suffix of the timestamp. And if there are more than one files it looks for the timestamp of the file.

Maybe it will help you to set the option DoDebug = True at the beginning of the script. This will show you a lot of windows with the variables set by the script during the run.
Posts: 19
Joined: 8 Nov 2018

sagitario

Hi,
I have the same error.
After some tests I find out the problem.
"Cannot find file "".

This is regarding the GlobalSettings. I put the default full path and the error message disappear. For some reason leaving blank don't work.
Const GlobalSettingsFile = "%appdata%\FreeFileSync\GlobalSettings.xml"
The second error Is regarding the log file. For some reason it can't find/read the log file. In the "Const BatchFile = " I put the full path to file instead the batchfile name only. Changing this and the error disappear and the script work like a charm.

Regards,

PS: The "log file search strings" need to be change to the current language that is use.
Posts: 68
Joined: 13 Aug 2018

TheExpert

OK, I will insert a check for ConstGlobalSettingsFile: If it isn‘t defined the call procedure of FreeFileSync has to be changed.

You can define the batch file as command line parameter. So there‘s no need to set the ConstBatchFile in the script. With the command line parameter you are able to use different batch files for your synchronisation jobs - if needed.
Posts: 19
Joined: 8 Nov 2018

sagitario

Define the batch file as command line parameter so there‘s no need to set the ConstBatchFile in the script is a good idea. Can you put a example how to execute the command?

By the way, if you are changing the script, can you add the SSL/TLS authentication?

Thank you for good job with this script.
Posts: 68
Joined: 13 Aug 2018

TheExpert

Thank you.

Please have look at the first lines of the script, where Lars Beneke as originator of this script described the usage:

' Usage: cscript.exe FreeFileSync.vbs <hideconsole true|false>
' <exefile d:\path\to\freefilesync.exe>
' <batchfile d:\path\to\batch.ffs_batch>
' <msgsubject "Message Subject for BatchName">
' <msgto recipient@somedomain.tld>
' <msgfrom sender@somedomain.tld>
' <smtpserver smtp.somedomain.tld | n.n.n.n>
' <smtpport nn>
' <smtpauth 0|1|2>
' <smtpusername "user@mailserver">
' <smtppassword "user's password">
'
' Notes: 1) Parameter/Value pairs can be specified at the
' command line, or changed in code below. Command
' line values override coded values.
' 2) If the word "BatchName" (case sensitive) is in the
' subject line, it will be replaced by the name of
' the batch file less path and extension.

Sorry, I'm not able to implement the SSL/TLS authentication because I don't have enough experience with Visual Basic Script to do this. Do you need this feature for the authentication with the mail server? Feel free to add this on your own or maybe there's someone else in this community who can help?
Posts: 19
Joined: 8 Nov 2018

sagitario

Yeh... stupid question I know. I already read that but never put it working well... it return all the time a script error regarding the reading log file. But I understand now that the problem is regarding the batchfile path. Putting the command batch script in a different folder where the vbscript is, need to put de full path to BatchFile and doing this the reading log error came across.

I'm just curious, I do not know this type of programming very well.

For now I sending through a old mailserver that still don't ask for SSL encryption, but is a question of time so that all mailservers only accept secure connections.

Regards,
Posts: 7
Joined: 29 Apr 2016

timberval


Sorry, I'm not able to implement the SSL/TLS authentication because I don't have enough experience with Visual Basic Script to do this. Do you need this feature for the authentication with the mail server? Feel free to add this on your own or maybe there's someone else in this community who can help? TheExpert, 09 Nov 2018, 14:17

This is the line to add/modify to implement SSL authentication (TLS is not possible in cdo):
(modify in date 10/11/18 because I left one row for mistake the following code is corrected)
'  SMTPAuth 0=off, 1=clear text, 2=NTLM.  IF YOU SET THIS
'  TO 1, THEN YOU MUST ALSO SET THE SMTPUSERNAME AND
'  SMTPPASSWORD FIELDS.
'  Use SSL for the connection (False or True)
Const SMTPAuth = 0
Const SMTPPort = 25
Const SMTPUsername = "your.smtp.username"
Const SMTPPassword = "your.smtp.password"
Const SMTPssl = False
'  THE SCRIPT WILL ATTEMPT TO SEND THE MESSAGE MaxEmailSendAttempts
'  TIMES BEFORE GIVING UP.  IF THE MESSAGE FAILS TO SEND, THE
'  SCRIPT WILL SET THE DOS ERRORLEVEL TO 1 UPON EXIT.
Const MaxEmailSendAttempts = 10
Const SleepSecondsBeforeRetryingEmailSend = 301
'  DoDebug CAUSES DIAGNOSTIC OUTPUT TO BE WRITTEN TO THE
'  CONSOLE.  NOTE THAT WSCRIPT.EXE USES DIALOG BOXES FOR
'  OUTPUT. SO USE CSCRIPT.EXE WHEN DODEBUG IS TRUE.
Const DoDebug = False
'  IF DODEBUG IS TRUE, AND THE SHELL IS EXECUTED FROM A SCHEDULE
'  OR SOME OTHER PROCESS, THEN THE VALUE BELOW WILL DELAY CLOSING
'  OF THE WINDOW SO YOU CAN EXAMINE THE DEBUG OUTPUT.
Const SleepSecondsBeforeClosingShell = 15
Const IncludeErrorsInReport = True
Const IncludeWarningsInReport = True
Const IncludeRuntimeStringInReport = True
Const IncludeRuntimeNumbersInReport = False
Const IncludeServerNameInReport = True
'  LOG FILE SEARCH STRINGS, LANGUAGE BASED
'  PLEASE SELECT THE CORRESPONDING STRINGS
'  GERMAN
'Const sLogFileError = "]  Fehler:"
'Const sLogFileWarning = "]  Warnung:"
'Const sLogFileSeriousError = "]  Schwerer Fehler:"
'  ENGLISH
Const sLogFileError = "]  Error:"
Const sLogFileWarning = "]  Warning:"
Const sLogFileSeriousError = "]  Serious Error:"
'--------------------------------------------------------------

'--------------------------------------------------------------
' CHANGE NOTHING FROM THIS POINT FORWARD
Dim bHideConsole, sExeFile, sBatchFile, sGlobalSettingsFile, dtExecutionTime, dtCompletionTime
Dim oMsg, fso, sLogFile, Args, i, j, ArgTemp, ArgLen, ArgFound, ArgErrors
Set oMsg = CreateObject("CDO.Message")
Set fso = CreateObject("Scripting.FileSystemObject")

bHideConsole = HideConsole
sExeFile = ExeFile
sBatchFile = BatchFile
sGlobalSettingsFile = GlobalSettingsFile
oMsg.Subject = MsgSubject
oMsg.From = MsgFrom
oMsg.To = MsgTo
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = SMTPServer
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = SMTPAuth
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SMTPPort
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'SendUsingPort
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = SMTPUsername
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = SMTPPassword
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SMTPssl
Const sDashedLine = "--------------------------------------------------------------"
Const sErrLogNotFound = "Unable to locate Log file for processing."
Const sErrLogNotCfgrd = "No log file is configured for this batch."
' THESE VALUES MATCH THE EXIT CODES FOR THE FreeFileSync.exe PROGRAM
Dim ExitCodeArray(3)
ExitCodeArray(0) = "Synchronization completed successfully"
ExitCodeArray(1) = "Synchronization completed with warnings"
ExitCodeArray(2) = "Synchronization completed with errors"
ExitCodeArray(3) = "Synchronization was aborted"

' PARSE THE COMMAND LINE
Dim ArgArray(11,1)
ArgArray(0,0) = "HideConsole"
ArgArray(0,1) = "bHideConsole"
ArgArray(1,0) = "ExeFile"
ArgArray(1,1) = "sExeFile"
ArgArray(2,0) = "BatchFile"
ArgArray(2,1) = "sBatchFile"
ArgArray(3,0) = "MsgSubject"
ArgArray(3,1) = "oMsg.Subject"
ArgArray(4,0) = "MsgFrom"
ArgArray(4,1) = "oMsg.From"
ArgArray(5,0) = "MsgTo"
ArgArray(5,1) = "oMsg.To"
ArgArray(6,0) = "SMTPServer"
ArgArray(6,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/smtpserver"")"
ArgArray(7,0) = "SMTPAuth"
ArgArray(7,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"")"
ArgArray(8,0) = "SMTPPort"
ArgArray(8,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/smtpserverport"")"
ArgArray(9,0) = "SMTPUsername"
ArgArray(9,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/sendusername"")"
ArgArray(10,0) = "SMTPPassword"
ArgArray(10,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/sendpassword"")"
ArgArray(11,0) = "SMTPssl"
ArgArray(11,1) = "oMsg.Configuration.Fields.Item(""http://schemas.microsoft.com/cdo/configuration/smtpusessl"")"
Posts: 68
Joined: 13 Aug 2018

TheExpert

@sagitario: Did you have a chance to test the SSL connection to your mail server after adding the lines timbretta added in his post above? Is it working? I hope I will have the time to build a new script at this weekend and will then add the SSL feature to the new script version.
Posts: 10
Joined: 15 Nov 2018

Squalo

Hi,
I have the same error.
After some tests I find out the problem.
"Cannot find file "".

This is regarding the GlobalSettings. I put the default full path and the error message disappear. For some reason leaving blank don't work.
Const GlobalSettingsFile = "%appdata%\FreeFileSync\GlobalSettings.xml"
The second error Is regarding the log file. For some reason it can't find/read the log file. In the "Const BatchFile = " I put the full path to file instead the batchfile name only. Changing this and the error disappear and the script work like a charm.

Regards,

PS: The "log file search strings" need to be change to the current language that is use. sagitario, 08 Nov 2018, 19:11
I have the same errors!
I solved the first error as sagitario said: I put the default full path and the error message disappear
But i can't solve the second error (i tried to put the full path of batchfile in Const BatchFile in vain)...
The script return this error:
line 465
Char 4
Error Invalid procedure coll or argument 'Mid'
code 800A0005
Posts: 19
Joined: 8 Nov 2018

sagitario

@sagitario: Did you have a chance to test the SSL connection to your mail server after adding the lines timbretta added in his post above? Is it working? I hope I will have the time to build a new script at this weekend and will then add the SSL feature to the new script version. TheExpert, 14 Nov 2018, 09:05
Sorry, but not. I already put the code in my script but didn't have free time to test it.

I ended up losing a lot more time trying to get the script work. My problem is that, for the sake of organization, I have the batch file in a folder outside where I have the FFS settings and vbscript .... So adding a command in the batch script return a error... I ended up doing 2 batch files. one in an external folder where I control what I want or not to sync and anothers in the various folders where I have the various scripts. So far it has been working fine, with the exception of a single problem which is the fact that I have FFS in Portuguese language (perceive by my posts in bad English that it is not my native language) and the log files are in UDF-8 and the script can not read the special characters in that format....
Posts: 19
Joined: 8 Nov 2018

sagitario

Hi,
I have the same error.
After some tests I find out the problem.
"Cannot find file "".

This is regarding the GlobalSettings. I put the default full path and the error message disappear. For some reason leaving blank don't work.
Const GlobalSettingsFile = "%appdata%\FreeFileSync\GlobalSettings.xml"
The second error Is regarding the log file. For some reason it can't find/read the log file. In the "Const BatchFile = " I put the full path to file instead the batchfile name only. Changing this and the error disappear and the script work like a charm.

Regards,

PS: The "log file search strings" need to be change to the current language that is use. sagitario, 08 Nov 2018, 19:11
I have the same errors!
I solved the first error as sagitario said: I put the default full path and the error message disappear
But i can't solve the second error (i tried to put the full path of batchfile in Const BatchFile in vain)...
The script return this error:
line 465
Char 4
Error Invalid procedure coll or argument 'Mid'
code 800A0005
Squalo, 15 Nov 2018, 14:23
If you send me your script I can try to see what is the problem... but I notice that although I really like helping others, I do not realize much of vbscript and I do not have much free time ...
Posts: 68
Joined: 13 Aug 2018

TheExpert

@Squalo: Did you edit the script with the Windows editor notepad.exe? Then please download the script once again from the forum and edit it with Notepad++. I had the same issue and after editing the script with Notepad++ everything was fine.
Posts: 10
Joined: 15 Nov 2018

Squalo

If you send me your script I can try to see what is the problem... but I notice that although I really like helping others, I do not realize much of vbscript and I do not have much free time ... sagitario, 16 Nov 2018, 14:30
Thanks a lot, I attached the script!
PS. I set the language of freefilesync in US
@Squalo: Did you edit the script with the Windows editor notepad.exe? Then please download the script once again from the forum and edit it with Notepad++. I had the same issue and after editing the script with Notepad++ everything was fine. TheExpert, 16 Nov 2018, 18:08
No, I didn't use notepad.exe!
I only use Notepad++

Thank you all
Attachments
FreeFileSync.vbs
(22.95 KiB) Downloaded 6153 times