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("myemail@esomeemail.com")

' 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
Mowgli
  • 3,422
  • 21
  • 64
  • 88
  • http://stackoverflow.com/questions/9074476/is-it-possible-to-embed-and-execute-vbscript-within-a-batch-file-without-using-a/26922679#26922679 –  Nov 14 '14 at 04:04

1 Answers1

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 recipient=myemail@esomeemail.com

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.

Community
  • 1
  • 1
rojo
  • 24,000
  • 5
  • 55
  • 101
  • Hi, Do you remember you help write http://stackoverflow.com/questions/15364653/can-we-fetch-scrape-particular-data-from-html-site-into-batch-to-do-following/15368190#15368190 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 – Mowgli Mar 13 '13 at 14:54
  • Can you please tell me what is code below `goto :EOF` ? thanks – Mowgli Mar 13 '13 at 15:08
  • 1
    The JScript stuff is what scrapes the output of `wget`. It strips out html tags, trims whitespace and converts ` ` 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](http://stackoverflow.com/a/15368190/1683264). – rojo Mar 13 '13 at 15:12
  • I meant to say, if that vbscript doesn't work for you, you could also use [blat](http://www.blat.net/) or [postie](http://www.infradig.com/command-line-email/index.html) or similar to send yourself an email. – rojo Mar 13 '13 at 15:20
  • Thanks a lot for putting it together, I will play with it to get it working. if not I'll just have batch script from yesterday call the first version of code you posted in this question(which sends email). – Mowgli Mar 13 '13 at 15:27
  • rojo, I was able to successfully able to get it working and send email as well. I just have one more thing I need your help with. Instead taking url from user with argument, can we hard code specific url ? instead of `%~1` may be like `http://stackoverflow.com/questions/15364653/` thanks – Mowgli Mar 13 '13 at 16:08
  • 1
    I don't see why not. Did you try it? Just replace `%~1` with your static URL. Leave it quoted. – rojo Mar 13 '13 at 16:10
  • Thanks a lot, I don't know why but it didn't work when I tired it, but it works now :) Thanks. I would give you my 100 points if I can for all the work you helped me with. – Mowgli Mar 13 '13 at 18:10
  • rojo, I am stuck again, I am trying to set up a task scheduler for this batch script. it works fine if I run it manually but I get following error if this is ran from windows task scheduler `C:\bin\nf\email.vbs(2, 1) Microsoft VBScript runtime error: ActiveX component can't create object: 'Outlook.Application'` I am almost there plz help. – Mowgli Mar 14 '13 at 16:24
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26173/discussion-between-rojo-and-mowgli) – rojo Mar 14 '13 at 16:32