How to send email at the end of a batch script. Possibly using mailsend.exe

I have a batch process that runs various batch scripts.  I would like the very last batch script to send a email stating that the process has completed and attach a .log file from a specific directory.  Not sure what/if this is possible or not from batch or mailsend.exe.  I'm playing around with mailsend but just can't quite get it to work the way I want.  Here's my requirements:

1-The file I need to attach is named 'Essbase_Backup_Log.log' in the C:\Temp directory
2-Process needs to run in batch thus cannot prompt for anything
3-SMPT Mail Server is: RESISS045.res.newgold.com
4-I would like the recipients of the email to be in a separate text file named 'email_to.txt' in the C:\Temp directory
5-Email should be from 'Backup@newgold.com' - This is made up.  Do I need a valid one?
6-Subject should read "Backup Completed".
7-Body of message should read "Backup has completed.  Please see attached .log file for details."

Thats it.  I can only get it to work if it prompts me.  I can't get it to read anything from text files etc....
elwayisgodAsked:
Who is Participating?
 
William ElliottConnect With a Mentor Sr Tech GuruCommented:
http://www.motobit.com/tips/detpg_send-email-from-asp/


'splits the email_to.txt
' assumes each new line is a different email address
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile("c:\temp\email_to.txt").OpenAsTextStream(1)
MyArray = Split(ts.ReadAll, vbCrlf)
ts.Close
For Each person in MyArray
      strto = strto & "; " & person '
next
'
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Backup@newgold.com"
objEmail.To = strto
objEmail.Subject = "Backup Completed"
objEmail.Textbody = "Backup has completed.  Please see attached .log file for details."
objEmail.AddAttachment "c:\temp\Essbase_Backup_Log.log"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        "RESISS045.res.newgold.com"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send





0
 
William ElliottSr Tech GuruCommented:
how do you have the have the 'to' addresses delimited?


strto = "stuff from email_to.txt"

Set objEmail = CreateObject("CDO.Message")
objEmail.From = "Backup@newgold.com"
objEmail.To = strto
objEmail.Subject = "Backup Completed"
objEmail.Textbody = "Backup has completed.  Please see attached .log file for details."
objEmail.AddAttachment "c:\temp\Essbase_Backup_Log.log"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        "RESISS045.res.newgold.com"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
0
 
elwayisgodAuthor Commented:
They are each a single line in a text file:

emailuser1@hotmail.com
emailuser2@hotmail.com

Do they need to be delimited?  Do I just create a .bat file of the above and call it?

Sam
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
Farhan KaziSystems EngineerCommented:
Greetings Elwayisgod,

:: * This script requires 'Blat.exe' (Freeware) to send an email through batch file.  
:: * Download 'Blat.exe' and extract the .zip file contents into following path:
::                C:\Windows\System32
::       Download it from:
::       http://sourceforge.net/project/showfiles.php?group_id=81910&package_id=83961
:: * Copy and paste following script into notepad and save it with any name having .cmd extension
:: SCRIPT START
@ECHO OFF
ECHO. &ECHO Sending Email...
(   ECHO -server RESISS045.res.newgold.com
    ECHO -tf C:\Temp\email_to.txt
    ECHO -f  Backup@newgold.com
    ECHO -subject "Backup Completed".
    ECHO -body "Backup has completed. Please see attached .log file for details."
    ECHO -attacht C:\Temp\Essbase_Backup_Log.log
    ECHO -timestamp
    ECHO -debug
    ECHO -overwritelog
    ECHO -Log %TMP%\BlatLog.txt
)>Blat.set
Blat -of Blat.set
IF EXIST Blat.set DEL /F /Q Blat.set
EXIT /B /0
:: SCRIPT END

Hope this helps!
Farhan
0
 
elwayisgodAuthor Commented:
When i try to batch it here's what I get.  not familiar with what your code is?  visual basic or batch etc......

sam


C:\Temp>mailsendtest_2.bat > mailsend_2.log
'strto' is not recognized as an internal or external command,
operable program or batch file.
'objEmail.From' is not recognized as an internal or external command,
operable program or batch file.
'objEmail.To' is not recognized as an internal or external command,
operable program or batch file.
'objEmail.Subject' is not recognized as an internal or external command,
operable program or batch file.
'objEmail.Textbody' is not recognized as an internal or external command,
operable program or batch file.
'objEmail.AddAttachment' is not recognized as an internal or external command,
operable program or batch file.
'objEmail.Configuration.Fields.Item' is not recognized as an internal or externa
l command,
operable program or batch file.
2 was unexpected at this time.
0
 
William ElliottSr Tech GuruCommented:
oh,. my scripts is a vbs script,.. save it as sendmymail.vbs then to call it from the batch file just put

cscript sendmymail.vbs
0
 
elwayisgodAuthor Commented:
weelio,

Looks like it works, few more questions. My final script is below:

1-The 'set ts' line:  the path is a variable named 'emaillist'.  How do I reference that in vb?

Sam


*****************************************************************************************************

'splits the email_to.txt
' assumes each new line is a different email address
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile("D:\EssbaseBackup\maxl\email_list.txt").OpenAsTextStream(1)
MyArray = Split(ts.ReadAll, vbCrlf)
ts.Close
For Each person in MyArray
      strto = strto & "; " & person '
next
'
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "DEV_Essbase_Backup@Newmont.com"
objEmail.To = strto
objEmail.Subject = "Essbase Backup Completed on Resiss108"
objEmail.Textbody = "Essbase Backup has completed.  Please see attached .log file for details."
objEmail.AddAttachment "D:\EssbaseBackup\maxl\Essbase_Backup_Log.log"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
        "RESISS045.res.newmont.com"
objEmail.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
0
 
elwayisgodAuthor Commented:
To clairfy.  I'm not sure VB can recognize a variable that was set in a batch script, can it?  I ask because the line:

objEmail.AddAttachment "D:\EssbaseBackup\maxl\Essbase_Backup_Log.log"

in reality (on production server) is:

objEmail.AddAttachment "D:\EssbaseBackup\Weekly\101107_Thu\Essbase_Backup_Log.log"

However the D:\EssbaseBackup\Weekly\101107_Thu\  is really a variable named TARGETDIRECTORY which is created in a .bat file that is executed right before this script would be executed.  It basically changes everyday as the 101107 represents the date and appends the day of the week abbreviation.

So is it possible to reference the TARGETDIRECTORY variable created in the .bat file in this vb script?

sam




0
 
William ElliottSr Tech GuruCommented:
vb can recognize whatever you want,.. you just need to know how.

change  that line to this

objEmail.AddAttachment wscript.argument(0)

then call it from the batch file like this
cscript sendmymail.vbs TARGETDIRECTORY

this way the variable will be passed through to the vbs file


to explain
wscript.argument(0) = the first argument passed
wscript.argument(1) = the second argument passed
etc etc,..

so if you want to replace other things with variables, then jsut replace the text with wscript.argument(n)

where n is the number of the particular argument

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.