1

Using CURL command output to send email notification. I have a below code which will check multiple URLs from a txt file as a input parameter and send the output as a email notification if it is failing. But my code is not sending mails for the error status.

@echo off
@for /F "usebackq delims=" %%I in ("%~dp0URL.txt") do @curl.exe -i "%%I" 
pause

URL.txt content: http://dummy.restapiexample.com/api/v1/employees; http://dummy.restapiexample.com/api/v1/employee/1; http://dummy.restapiexample.com/api/v1/create

I want to check like:

If the URL is down, it should send mail notification with the error logs (blat -subject "App Status" -body "App is Down, please find the attachment for the error logs" -attach sample.txt -to emailID

Santosh Kumar
  • 53
  • 1
  • 10
  • Santosh, have you decided not to edit your previous [off topic question](https://stackoverflow.com/q/61189635), in order to get the help you requested? Because this new question is related, _(you're using code from the previous section's comment section)_. – Compo Apr 14 '20 at 09:44
  • I thought better to open a new thread. Please suggest what i can do now. – Santosh Kumar Apr 14 '20 at 10:07
  • You could start by providing the content of `URL.txt` etc. as I have requested previously. BTW, `if not errorlevel 1` generally means that the command returned as successful, as it means the error level did not reach `1`, _(i.e. `1` or higher)_, and error level `0` is usually success. – Compo Apr 14 '20 at 10:19
  • I have a list of sample URLs for testing: http://dummy.restapiexample.com/api/v1/create; http://dummy.restapiexample.com/api/v1/employees; http://dummy.restapiexample.com/api/v1/delete/2; So these URLs are hitting one by one by the above script. But I want to acheive like if the URL is not LIVE it should send mail automatically to the recipients using the BLAT – Santosh Kumar Apr 14 '20 at 10:51
  • Your quick response will be appreciated. – Santosh Kumar Apr 15 '20 at 04:48
  • If you require us to help you, please [edit your question](https://stackoverflow.com/posts/61204477/edit) to include the contents of your `URL.txt`. What you've provided in the comment above is three incomplete URL's, two of which output 'Method not allowed' messages in a web browser. _A URL should include a protocol, (e.g. http, https, ftp, mailto, file, data, or irc), followed by a colon and two forward slashes_ As for your question, there are a lot of possible errors which can be returned and options you can use to hide/show them etc. Your question is therefore unclear and too broad. – Compo Apr 15 '20 at 09:29
  • I have edited the question now. and providing the proper URLs: `http://dummy.restapiexample.com/api/v1/employees; http://dummy.restapiexample.com/api/v1/employee/1; http://dummy.restapiexample.com/api/v1/create` Please help! – Santosh Kumar Apr 15 '20 at 10:12
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/211703/discussion-between-santosh-kumar-and-compo). – Santosh Kumar Apr 15 '20 at 11:00

1 Answers1

0

Here is an example that i tested on my side.

You should just change the variable Sender; Recipient and SMTP_Server

@echo off
Title Send email notification if the URL is not live with BLAT
Color 0A
set "URLS=%~dp0URLS.txt"

If Not Exist "%URLS%" (
    Color 0C & echo(
    echo You should provide "%URLS%" with this batch file "%~nx0"
    TimeOut /T 10 /NoBreak>nul
    Exit
)

Set "Not_OK_URLS=%~dp0Not_OK_URLS.txt"
Set "BLAT=%~dp0blat.exe"
Set "LOG_BLAT=%~dp0LogBlat.txt"

If Exist "%Not_OK_URLS%" Del "%Not_OK_URLS%"
If Exist "%LOG_BLAT%" Del "%LOG_BLAT%"

Setlocal EnableDelayedExpansion
@for /f "delims=" %%a in ('Type "%URLS%"') do (
    Call :StringFormat "%%a" URL
    (ping -n 1 "!URL!" | findstr /r /c:"[0-9] *ms">nul) && (echo %%a is OnLine ==^> Success) || (echo %%a is Dead  ==^> FAILURE)>>"%Not_OK_URLS%"
)
If Exist "%Not_OK_URLS%" Call :Mail
REM If Exist "%LOG_BLAT%" Start "" "%LOG_BLAT%"
Exit
::*************************************************************************************
:StringFormat <URL>
(  
    echo Function StringReplace(Str^)
    echo    Str = Replace(Str,"http://",""^)
    echo    Str = Replace(Str,"https://",""^)
    echo    StringReplace = str
    echo End Function
    echo wscript.echo StringReplace("%~1"^)
)>"%tmp%\%~n0.vbs"
for /f "delims=" %%a in ('Cscript /nologo "%tmp%\%~n0.vbs"') do ( set "%2=%%a" )
If Exist "%tmp%\%~n0.vbs" Del "%tmp%\%~n0.vbs"
exit /b
::*************************************************************************************
:Mail
cls
Set Sender=-f changeme@yahoo.com
set Recipient=-to changeme@yahoo.com
set Subject=-s "Multi Ping URLS Tester and sending mail with BLAT"
set SMTP_Server=-server smtp.changeme.com
Set body=-body "App is Down, please find the attachment for the error logs"
set Message=-bodyF "%Not_OK_URLS%"
set Attachment=-attach "%Not_OK_URLS%"
set Log=-log "%LOG_BLAT%"
set Debug=-debug
echo.
echo             Please Wait a While ... Sending Mail is in progress ......
%BLAT% %Sender% %Recipient% %Subject% %Message% %SMTP_Server% %Attachment% %Log% %Debug%>nul
Exit /B
::*************************************************************************************
Hackoo
  • 18,337
  • 3
  • 40
  • 70