automatically send an email

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
philiphill
Posts: 2
Joined: 08 Apr 2019 23:16

automatically send an email

#1 Post by philiphill » 09 Apr 2019 00:54

I write an internet connection test program via file batch , and every time lose the network connection, it will automatically send an email to my gmail,The problem is the program will send an email continuously until it is connected again,It is very troublesome,so I want the program to send email once.

Code: Select all

set /p host1= "Please Input IP: "
set logfile=%host1%.log


echo DEST = %host1% > %logfile%

:Ping
ping %host1% -w 1000 -n 10  >> %logfile%
IF '%ERRORLEVEL%'=='1'  GOTO SEND_MAIL
timeout 1 > NUL
GOTO Ping
:SEND_MAIL
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -file send_mail.ps1
GOTO Ping

penpen
Expert
Posts: 1675
Joined: 23 Jun 2013 06:15
Location: Germany

Re: automatically send an email

#2 Post by penpen » 09 Apr 2019 04:56

If you don't want to loop after sending the mail, then why do you loop using "GOTO Ping"?
You could just remove it.


penpen

philiphill
Posts: 2
Joined: 08 Apr 2019 23:16

Re: automatically send an email

#3 Post by philiphill » 09 Apr 2019 22:29

Dear penpen ! If removed "GOTO Ping" the network check process will end when it sends me a mail to my gmail.I want the network testing process to take place continuously and without interruption.

penpen
Expert
Posts: 1675
Joined: 23 Jun 2013 06:15
Location: Germany

Re: automatically send an email

#4 Post by penpen » 10 Apr 2019 06:16

Then that might help you:

Code: Select all

set /p host1= "Please Input IP: "
set logfile=%host1%.log


echo DEST = %host1% > %logfile%

:Ping ^
timeout 1 > NUL
ping %host1% -w 1000 -n 10  >> %logfile%
IF '%ERRORLEVEL%'=='1'  GOTO SEND_MAIL
GOTO Ping
:SEND_MAIL
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -file send_mail.ps1

:wait untill connected
ping %host1% -w 1000 -n 10  >> %logfile%
IF '%ERRORLEVEL%'=='0'  GOTO Ping
timeout 1 > NUL
GOTO wait
penpen

Post Reply