1.

Solve : Send email after completion of batch file execution?

Answer»

is there any way to send an email after completion of the batch file execution?Try this:

Code: [Select]::email-bat.cmd:::::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal

:: defaults
set [emailprotected]
set [emailprotected]
set Subj="email test %date% %time%"
set Body="did it work? %date% %time%"
set Serv=mail.server.com.au
set Auth=user
set Pass=pass
set fileattach=

:: if command line arguments are supplied then use them
if "%~7" NEQ "" (
set From=%1
set To=%2
set Subj="%~3"
set Body="%~4"
set Serv=%5
set "Auth=%~6"
set "Pass=%~7"
set "fileattach=%~8"
)

call :createVBS "email-bat.vbs"

call :send %From% %To% %Subj% %Body% %Serv% %Auth% %Pass%
DEL "%vbsfile%" 2>nul
goto :EOF

:send
cscript.exe /nologo "%vbsfile%" %1 %2 %3 %4 %5 %6 %7 >nul 2>nul
goto :EOF

:createVBS
set "vbsfile=%~1"
del "%vbsfile%" 2>nul
set cdoSchema=http://schemas.microsoft.com/cdo/configuration
echo >>"%vbsfile%" Set objArgs = WScript.Arguments
echo >>"%vbsfile%" Set objEmail = CreateObject("CDO.Message")
echo >>"%vbsfile%" objEmail.From = objArgs(0)
echo >>"%vbsfile%" objEmail.To = objArgs(1)
echo >>"%vbsfile%" objEmail.Subject = objArgs(2)
echo >>"%vbsfile%" objEmail.Textbody = objArgs(3)
if defined fileattach echo >>"%vbsfile%" objEmail.AddAttachment "%fileattach%"
echo >>"%vbsfile%" with objEmail.Configuration.Fields
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendusing") = 2 ' not local, smtp
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserver") = objArgs(4)
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpserverport") = 25
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpauthenticate") = 1 ' cdobasic
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendusername") = objArgs(5)
echo >>"%vbsfile%" .Item ("%cdoSchema%/sendpassword") = objArgs(6)
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpusessl") = False
echo >>"%vbsfile%" .Item ("%cdoSchema%/smtpconnectiontimeout") = 25
echo >>"%vbsfile%" .Update
echo >>"%vbsfile%" end with
echo >>"%vbsfile%" objEmail.Send
remdon't know why, but your code didn't work for me Foxidrive. If it doesn't end up working for you rkp, here's one you could try. Only got it to work for gmail so far though (must have a gmail account to send from)

Code: [Select]
'Usage: cscript sendemail.vbs <email_[emailprotected]> "<subject_line>" "<email_body>" "<optional:email_attachment_path>"
'Ex. No attach: cscript sendemail.vbs [emailprotected] "test subject line" "test email body"
'Ex. W/ attach: cscript sendemail.vbs [emailprotected] "test subject line" "test email body" "c:\scripts\log.txt"

'***********
'****CONFIGURE THE FROM EMAIL ADDRESS AND PASSWORD

Const fromEmail = "[emailprotected]"
Const password = "xxxxxxxxxx"

'****END OF CONFIGURATION
'***********

Dim emailObj, emailConfig
Set emailObj = CreateObject("CDO.Message")
emailObj.From = fromEmail
emailObj.To = WScript.Arguments.Item(0)
emailObj.Subject = WScript.Arguments.Item(1)
emailObj.TextBody = WScript.Arguments.Item(2)

If WScript.Arguments.Count > 3 Then
emailObj.AddAttachment WScript.Arguments.Item(3)
End If

Set emailConfig = emailObj.Configuration
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com"
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 465
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = true
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendusername") = fromEmail
emailConfig.Fields("http://schemas.microsoft.com/cdo/configuration/sendpassword") = password
emailConfig.Fields.Update

emailObj.Send

Set emailobj = nothing
Set emailConfig = nothing
you'll want to SAVE this as a mail.vbs

then using your batch file navigate to it and use the command:
Code: [Select]mail <[emailprotected]> "<subject>" "<body>"
or
Code: [Select]mail <[emailprotected]> "<subject>" "<body>" "<path:\\attachment>"

if those don't work, you could try the more wordy version
Code: [Select]cscript sendemail.vbs <[emailprotected]> "<subject>" "<body>" "<path:\\attachment>"

if you put mail.vbs in your system32 folder (you may not want to) you can do this without navigating to it's locationLemonilla,
Foxidrive's code WORKS just fine. You just need to tweak it for your smtp servers settings.This might be an alternative to CDO which has been deprecated on newer Window versions. If you can, try downloading Blat which can be run STANDALONE or part of a batch file.

If you have access to Powershell, a cmdlet specific for this purpose is available (Send-MailMessage) or it can be scripted with the SMTP client object.

Greetings all,

I have had success testing foxidrive's code on yahoo.com, gmail.com
and earthlink.net using XP SP3 and Win 7 Home Edition

rkp, should you wish to enhance the capabilities of foxidrive's
code, for example to have more than one line of text in the
'body' of the email, then I would suggest a review of Timo Salmi's code
which can be found at: www.netikka.net/tsneti/info/tscmd.php

Have a look at item number 188.

Best wishes!Quote from: Ocalabob on August 24, 2012, 06:31:44 PM

rkp, should you wish to enhance the capabilities of foxidrive's
code, for example to have more than one line of text in the
'body' of the email, then I would suggest a review of Timo Salmi's code
which can be found at: www.netikka.net/tsneti/info/tscmd.php

Have a look at item number 188.

Best wishes!

Thanks Ocalabob, and I confirm that there are other good solutions at Timo's link.




Discussion

No Comment Found