watchDirectory Help > Plugins > Automatically run .bat files
Sending email from .bat files
There is another plugin installed with watchDirectory that allows most common email tasks. If you want more flexibility, you can also send email from inside your .bat file. The method described here depends on the wdPostMan program that you configure using the "View -> Email settings" menu option. See Email Settings for more information.
If you want to send email from inside your .bat file, this is fairly easy because the wdPostMan program will automatically process email scripts created inside the Out folder of your mail directory. Email scripts are plain text files with a wdmail extension.
An example of sending email from inside your .bat file
The letters in front of each line below are used to explain the script, do not enter them in your .bat file.
A set EMAILFILE=%WD_TASKDIR%\..\System.Mail\Out\%WD_CONFIG%_%WD_CUREVT%.TMP B echo this is the subject > "%EMAILFILE%" C echo John Doe^<j.doe@johndoe.com^>;Mary Doe^<m.doe@johndoe.com^> >> "%EMAILFILE%" D echo. >> "%EMAILFILE%" E echo ^<secretbcc@johndoe.com^> >> "%EMAILFILE%" F echo %WD_FILE_SHORTPATH% >> "%EMAILFILE%" G echo pecunia non olet >> "%EMAILFILE%" H echo Dear John and Mary, >> "%EMAILFILE%" I echo Please find attached the file %WD_FILE_N% >> "%EMAILFILE%" J echo best regards, >> "%EMAILFILE%" K echo %COMPUTERNAME% >> "%EMAILFILE%" L rem okay, the script is created, now rename it so the wdPostMan program processes it. M ren "%EMAILFILE%" %WD_CONFIG%_%WD_CUREVT%.wdmail
The simple example explained
Line A: set EMAILFILE=%WD_TASKDIR%\..\System.Mail\Out\%WD_CONFIG%_%WD_CUREVT%.TMP
Here a .bat file variable is created with a value that will be unique to this invocation of your .bat file
as we don't want 2 invocations of your .bat file to overwrite each others email scripts.
%WD_TASKDIR%\..\System.Mail\Out expands to the directory that is monitored by the wdPostMan program for email to send.
%WD_CONFIG% expands to the name you have assigned to this watchDirectory task.
%WD_CUREVT% expands to a number assigned to the current event.
So, the EMAILFILE variable will contain something like:
C:\Documents and Settings\All Users\Application Data\watchDirectory\System.Mail\Out\AutoCopy_76234.TMP
We use a TMP extension (instead of the WDMAIL extension that is expected by the wdPostMan program)
so the email script will not be processed by the wdPostMan program until we have finished writing to it. On line "M", when
we are done writing to the script, we will rename the file so the wdPostMan can see it.
Line B: echo this is the subject > "%EMAILFILE%"
This writes the subject to the email script. The first line of the email script is interpreted by the wdPostMan
program as the subject. Make sure you only use the single redirect character (">") for the first line
you write to the email script.
Line C: echo John Doe^<j.doe@johndoe.com^>;Mary Doe^<m.doe@johndoe.com^> >> "%EMAILFILE%"
The second line of the email script is interpreted by the wdPostMan program as the "TO:" line. All email addresses
on this line will receive the message. You can send to multiple addresses by separating them with a semicolon (;).
Important: As the < and > characters have special meaning inside .bat scripts they need to be "escaped" by placing a ^ in front of them.
Line D: echo. >> "%EMAILFILE%"
The third line of the email script is interpreted by the wdPostMan program as the "CC:" line. The email addresses
you write here will receive a carbon copy (CC) message.
The sample script above doesn't send CC messages, but has to account for the simple algorithm used by the wdPostMan program.
You will need to make sure there is an empty line there. An ECHO statement that is immediately followed by a dot (ECHO.)
will produce the empty line.
Important: Do not enter a space between ECHO and the dot, as that will write a line containing
a dot to the email script.
Line E: echo ^<secretbcc@johndoe.com^> >> "%EMAILFILE%"
The fourth line of the email script is interpreted by the wdPostMan program as the "BCC:" line. The email addresses
you write here will receive a blind carbon copy (BCC) message.
Line F: echo %WD_FILE_SHORTPATH% >> "%EMAILFILE%"
The fifth line of the email script is interpreted as the list of files to attach to the email message.
In this example we use the so-called DOS 8.3 compatible filename. You can attach multiple
files by separating them with a semicolon (;). If you want the file to be deleted after it has been sent, start the
name with an exclamation mark (!):
echo !%WD_FILE_SHORTPATH% >> "%EMAILFILE%"
Remember to write an empty line if you do not want to attach files:
ECHO. >> "%EMAILFILE%"
Line G: echo pecunia non olet >> "%EMAILFILE%"
The sixth line of the email script is ignored by the wdPostMan program. You can write anything
you like here, it will not be sent or processed.
Lines H until K: echo ... >> "%EMAILFILE%"
All further lines written are used as the actual message body.
You can use special Environment Variables here to
customize the message to the event you are handling. The %COMPUTERNAME% variable used in the example
is actually a normal windows environment variable that can be used as well.
Line M: ren "%EMAILFILE%" %WD_CONFIG%_%WD_CUREVT%.wdmail
As the wdPostMan program only processes files with a .wdmail extension, this last step
renames the email script.