- GETTING STARTED
- Server connection
- File Transfer
- Directory operations
- File operations
- Script output
Sending emails from within a script
ScriptFTP has been designed for unattended operation and usually you will know what has happened during a script run just by taking a look at the ScriptFTP window. Additionally, you may log a script's output messages to a text file and even have it send emails to you which contain information about the run. This section is dedicated to this topic.
In ScriptFTP there is no built-in command for sending emails because it is not needed. We will rather use the EXEC command to call an external command-line program that will send the email. The following examples use Blat, a free and tremendously useful command-line mail program for all kinds of batch jobs. However, you can use any other mail program just by adjusting the EXEC call.
The following example script will send an email if it cannot connect to the FTP server or if the file synchronization fails. Note that we first setup the way how Blat will be invoked. We construct two different commands for calling Blat, one for each type of error, since the user should tell from the email what happened exactly and not be confused by a generic error message.
# FTP server settings
# Set the directories that this script
# will synchronize.
# Blat parameters. This is the command line program
# used in this example to send emails from within ScriptFTP
$email_subject="ScriptFTP error message"
$email_body1="Could not connect to FTP server"
$email_body2="Could not syncronize files"
# Build the log file path by retrieving Windows' temp path from
# the system environment variable TEMP and appending the current
# date to the file name "logfile-". For example:
# Start logging the script's output
# The blat command for sending an email is supposed
# to look like this (without the line breaks):
# -server smtp.myserver.com
# -u myuser_at_myserver.com
# -pw mypassword
# -f firstname.lastname@example.org
# -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 kinds of emails depending
# on the errors that may have happened we need to build
# two different blat commands
# Both have a common part which consists of 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 ".'"'
$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 strings to build the complete commands
# Connect to the FTP server
PRINT("Cannot connect to FTP server. Sending an email and aborting in 5 seconds.")
# Synchronize the files
PRINT("Cannot syncronize. Sending an email and aborting in 5 seconds.")