0

I would like batch to send email but since mailto command won't send email via outlook without me clicking send button.

I found this VBS Script online which sends email without human interactions.

I just need help call or if I can embedded VBS into BATCH file.

' Create email object
Set oolApp = CreateObject("Outlook.Application")
Set email = oolApp.CreateItem(0)
email.Recipients.Add("[email protected]")

' Create the body of the email
MailBody = "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD W3 HTML//EN"">"
MailBody = MailBody & "<HTML>" & vbcrlf
MailBody = MailBody & "<HEAD><TITLE>No Invoices</TITLE></HEAD>"
MailBody = MailBody & "<BODY>" & vbcrlf
MailBody = MailBody & "<B>For Your Information</B>,<BR><BR>"
MailBody = MailBody & "No invoices were issued today.<BR><BR>"
MailBody = MailBody & "</BODY></HTML>"

' Send the Email
email.Subject = "No Invoices Issued"
email.HTMLBody = MailBody
email.Send
1

1 Answer 1

2

Edit: Now that I re-read your question, I'm not exactly sure what you're asking. Are you asking how to run a vbscript? Just save it to filename.vbs and execute it with

cscript filename.vbs

(or cscript /nologo filename.vbs if you want to avoid displaying Microsoft's cscript vanity spam).


On the other hand, if you want to incorporate this into a batch script, then there are lots of methods for echoing content from a batch script to an external file. Most people just do something like

echo Set oolApp = CreateObject^("Outlook.Application"^)>> vbsfile

or similar, escaping with ^ as necessary. However, you might find this page of heredoc methods useful. Here's an example using the script I helped you make yesterday:

@if (@X)==(@Y) @end /* (batch + jscript hybrid script init)

:: *** Batch script *****

@echo off
setlocal enabledelayedexpansion

set [email protected]

for /f "delims=" %%I in ('wget "%~1" -O- -q 2^>NUL ^| findstr /i "last.*updated.*as.*of"') do (
    for /f "delims=" %%x in ('cscript /nologo /e:jscript "%~f0" "%%I"') do (
        set /a "thirtyMinutes = 30 * 60 * 1000"
        if %%x GEQ !thirtyMinutes! (
            call :doEmail
        )
    )
    exit /b
)
exit /b

:doEmail
call :heredoc vbs >email.vbs && goto endvbs
' Create email object
Set oolApp = CreateObject("Outlook.Application")
Set email = oolApp.CreateItem(0)
email.Recipients.Add("!recipient!")

' Create the body of the email
MailBody = "<^!DOCTYPE HTML PUBLIC ""-//W3C//DTD W3 HTML//EN"">"
MailBody = MailBody & "<HTML>" & vbcrlf
MailBody = MailBody & "<HEAD><TITLE>No Invoices</TITLE></HEAD>"
MailBody = MailBody & "<BODY>" & vbcrlf
MailBody = MailBody & "<B>For Your Information</B>,<BR><BR>"
MailBody = MailBody & "No invoices were issued today.<BR><BR>"
MailBody = MailBody & "</BODY></HTML>"

' Send the Email
email.Subject = "No Invoices Issued"
email.HTMLBody = MailBody
email.Send
:endvbs

cscript /nologo email.vbs
del email.vbs
goto :EOF

:: https://stackoverflow.com/a/15032476/1683264
:heredoc <uniqueIDX>
setlocal enabledelayedexpansion
set go=
for /f "delims=" %%A in ('findstr /n "^" "%~f0"') do (
    set "line=%%A" && set "line=!line:*:=!"
    if defined go (if #!line:~1!==#!go::=! (goto :EOF) else echo(!line!)
    if "!line:~0,13!"=="call :heredoc" (
        for /f "tokens=3 delims=>^ " %%i in ("!line!") do (
            if #%%i==#%1 (
                for /f "tokens=2 delims=&" %%I in ("!line!") do (
                    for /f "tokens=2" %%x in ("%%I") do set "go=%%x"
                )
            )
        )
    )
)
goto :EOF

:: *** JScript script *****/
var args = [];
for (var i=0; i<WScript.arguments.length; i++) { args.push(WScript.arguments(i)) }
var t = args.join(' ').replace(/^\s+|<[^>]+>|\s+$/g,'').replace(/\&nbsp;/g, ' ').split(' ');
var h = t[4].split(':')[0];
if (/pm/i.test(t[5])) h = h * 1 + 12;
var ds = t[6] + ' ' + t[7] + ', ' + new Date().getFullYear() + ' ' + h + ':' + t[4].split(':')[1];
var diff = new Date() - new Date(ds);
WScript.echo(diff);

I haven't tested this, but play around with it and see what you think.

Sign up to request clarification or add additional context in comments.

10 Comments

Hi, Do you remember you help write stackoverflow.com/questions/15364653/… yesterday? so I am trying make that bat script send me email if via outlook if it has been 30 mins since last update. Thanks
Can you please tell me what is code below goto :EOF ? thanks
The JScript stuff is what scrapes the output of wget. It strips out html tags, trims whitespace and converts &nbsp; to spaces to get to the Last Updated as of 11:14 pm March 11th text. It then converts that bit of text to a Date object, subtracts it from the current Date, and echoes the number of milliseconds difference. That difference is captured as %%x near the top of the script. It's the same as it was yesterday.
I meant to say, if that vbscript doesn't work for you, you could also use blat or postie or similar to send yourself an email.
I don't see why not. Did you try it? Just replace %~1 with your static URL. Leave it quoted.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.