send_email.ftp

This script synchronizes a remote folder from a local directory and sends an email if an error is found. The log file is also sent attached. This script uses the free command-line tool Blat for sending emails.
A different error email is sent depending on the error found

# FTP server settings
$ftp_server="ftp.myserver.com"
$ftp_user="myuser"
$ftp_password="mypass"
 
# Set here the directories that this script
# will synchronize.
$remote_dir_to_synchronize="/remotedir/"
$local_dir_to_synchronize="C:\MyDir"
 
 
# Blat parameters. This is the command line program
# used in this example to send emails from ScriptFTP
 
$blat_path="C:\blat\blat.exe"
 
$smtp_server="smtp.myserver.com"
$smtp_user="myuser_at_myserver.com"
$smtp_password="mypassword"
 
$email_from="scriptftp@myserver.com"
$email_to="me@myserver"
$email_subject="ScriptFTP error message"
$email_body1="Could not connect to FTP server"
$email_body2="Could not syncronize files"
 
 
# Build the log file path getting the windows temp path from
# the system environment variable TEMP and the file name "logfile-"
# appended with the current date. For example:
# C:\windows\temp\logfile-20070424.txt
$log_file_path=GETENV("TEMP")."\logfile-".GETDATE(FORMAT3).".txt"
 
# Start logging the script output
LOGTO($log_file_path)
 
 
# The blat command line to send an email should be
# like this (without the line breaks):
#
# C:\blat\blat.exe
# -server smtp.myserver.com
# -u myuser_at_myserver.com
# -pw mypassword
# -f scriptftp@myserver.com
# -to me@myserver
# -subject "ScriptFTP error message"
# -body "Error messsage here"
# -ps "C:\Users\Carlos\AppData\Local\Temp\logfile-20070427.txt"
#
# As there are two different kind of emails depending
# on the two errors that may happen we need to build
# two different blat command lines.
#
# Both have a common part, that is the variables
# $common_part_1, $common_part_2 and $common_part_3.
# The fourth part contains the different body and
# they are called $cmd_line_part_4_1 and $cmd_line_part_4_2
#
$common_part_1=$blat_path." -server ".$smtp_server." -u ".$smtp_user." -pw "
$common_part_2=$smtp_password." -f ".$email_from." -to ".$email_to." -subject ".'"'
$common_part_3=$email_subject.'"'
 
$cmd_line_part_4_1=" -body ".'"'.$email_body1.'"'." -ps ".'"'.$log_file_path.'"'
$cmd_line_part_4_2=" -body ".'"'.$email_body2.'"'." -ps ".'"'.$log_file_path.'"'
 
# Concatenate the text string to build the complete command lines
$blat_cmd_line_1=$common_part_1.$common_part_2.$common_part_3.$cmd_line_part_4_1
$blat_cmd_line_2=$common_part_1.$common_part_2.$common_part_3.$cmd_line_part_4_2
 
 
 
 
# Connect to the FTP server
$result=OPENHOST($ftp_server,$ftp_user,$ftp_password)
IF($result!="OK")
     PRINT("Cannot connect to FTP server. Sending an email and aborting in 5 seconds.")
     EXEC($blat_cmd_line_1)
     SLEEP(5)
     EXIT(1)
END IF
 
# Synchronize the files
$result=SYNC($local_dir_to_synchronize,$remote_dir_to_synchronize,UPLOAD)
IF($result!="OK")
    PRINT("Cannot syncronize. Sending an email and aborting in 5 seconds.")
    EXEC($blat_cmd_line_2)
    SLEEP(5)
    EXIT(2)
END IF
CLOSEHOST
 
 
EXIT(0)