blat: batch command line emailer

April 20, 2010 at 00:44:03
Specs: Windows XP, 2 gb
I’m using blat http://www.blat.net/ as a command line emailer and it’s working fine

Now I would like to add to my batch script some improvement: i.e. errorlevel checking in order to control the user input data to be sure that a valid email address is set to the %from% variable;

This is my very rough attempt not working on the error level part (errorlevel 12 is when -server or -f options is not specified or not found in registry)

@echo off

:: setting vars
set to=xxx@xxx.xxx.xx
set cc= xxx@xxx.xxx.xx
set bcc= xxx@xxx.xxx.xx
set /p from="your email..."
set subject="subject text here"
set server=-server xxx.xxx.xxx.xx
set debug=-debug -log blat.log -timestamp

:: running blat
blat -attach file.txt -f %from% -to %to% -cc %cc% -bcc %bcc% -s %subject% %server% %debug% -body "body text here"

:: control error
IF ERRORLEVEL=12 THEN GOTO :ERROR12
::if no error code...
GOTO :NOERROR


:NOERROR
GOTO :EXITNOPAUSE

:ERROR12
ECHO ERROR: specify email address
GOTO :EXIT

:EXIT
PAUSE

:EXITNOPAUSE
::end of batch here

Any help for that?

To improve further the batch my ideal solution would be to loop through until a valid email address is specified: the email address is composed by a variable part (the name of the sender) and by a fixed part (the name of the domain, which is in my case always the same)
someone(sender name)@xxxx.xxxx.xx(domain)

thanks


See More: blat: batch command line emailer

Report •


#1
April 20, 2010 at 04:20:48
If ErrorLevel is TRUE if ErrorLevel is Greater or equal to the coded value, i.e.

if ErrorLevel 12 goto Error12

will jump if ErrorLevel is 12 or greater. So to detect no errors code

if not ErrorLevel 1 goto NoErrors

Due to ErrorLevel behavior you have to check ErrorLevel in reverse order, i.e. from highest values to lowest

if ErrorLevel 12
if ErrorLevel 11

Another way is to code

if %ErrorLevel% equ 12

In the above case the condition is satisfied for the coded value only.

Report •

#2
April 20, 2010 at 06:20:54
thanks ivo for your reply, I understand what you mean;
yet I'm still confused on how to properly set your help in the above batch;

how would you do that?
for some reasons I could not catch the errorlevel 12;
and what about the loop until a valid email is entered? can you suggest me something...


Report •

#3
April 20, 2010 at 06:58:30
Fhis is just a hint. I assume BLAT sets correctly the return codes:

@echo off

:: setting vars
set to=xxx@xxx.xxx.xx
set cc= xxx@xxx.xxx.xx
set bcc= xxx@xxx.xxx.xx
set subject="subject text here"
set server=-server xxx.xxx.xxx.xx
set debug=-debug -log blat.log -timestamp 
set domain=@xxxx.xxxx.xx

:: prompting for sender name
:LOOP
cls
set /P from=Enter your (sender) name^>
set from=%from%%domain%

:: running blat
blat -attach file.txt -f %from% -to %to% -cc %cc% -bcc %bcc% -s %subject% %server% %debug% -body "body text here"
  if not ErrorLevel 1 goto :EOF
  if ErrorLevel 12 (
    echo ERROR: invalid email address, please retry...
    pause
    goto :LOOP
  )
:: End_Of_Batch


Report •

Related Solutions

#4
April 20, 2010 at 07:37:54
ivo

the batch code is working fine: as usual your hints are neat and smart...but now I definitely realise that there must be some problems with the blat return codes (and this is due to your cross-checking of the batch code, thanks again for that)

my very last question: how to catch error level codes?

I had a very good errorlevel lesson

ciao


Report •

#5
April 20, 2010 at 08:00:29
Sorry, there is no general rule to handle applications' return codes. First of all the application is supposed to set a return code as that is not true in general. Then you must know which codes are (maybe) returned.

The only general rules are:

- check the codes in reverse order;
- check the code immediately after the command is issued;
- ErrorLevel 0 usually means no errors.

Excluding what stated above checking error codes is always an adventure into unknown.


Report •

#6
April 20, 2010 at 10:49:45
If blat returns error output, you can capture that into a variable. Same situation as Ivo pointed out with errorcodes, the output is not dictated by a ruleset, but is up to the whims of the application (blat). So if there is error output, it is supposed to go out channel "2": blat... 2>log,
but not necessarily. so you can just capture both and see:
for /f "tokens=*" %%a in ('blat %xx% %yy... ...') do set xx=%%a
then you can examine contents of !xx! to see what error occurred. (the for-loop is only needed to capture the variable).

Report •

Ask Question