1

使用 CURL 命令输出发送电子邮件通知。我有一个下面的代码,它将检查 txt 文件中的多个 URL 作为输入参数,如果失败,则将输出作为电子邮件通知发送。但是我的代码没有发送错误状态的邮件。

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

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

我想检查如下:

如果 URL 关闭,它应该发送带有错误日志的邮件通知 (blat -subject "App Status" -body "App is Down, please find the attachment for the error logs" -attach sample.txt -to emailID

4

1 回答 1

0

这是我在我身边测试的一个例子。

您应该只更改变量SenderRecipientSMTP_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
::*************************************************************************************
于 2020-04-16T19:01:50.607 回答