?
Solved

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

Posted on 2007-10-11
9
Medium Priority
?
1,099 Views
Last Modified: 2010-04-16
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....
0
Comment
Question by:elwayisgod
  • 4
  • 4
9 Comments
 
LVL 19

Expert Comment

by:weellio
ID: 20060077
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
 

Author Comment

by:elwayisgod
ID: 20060112
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
 
LVL 26

Expert Comment

by:Farhan Kazi
ID: 20060114
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:elwayisgod
ID: 20060183
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
 
LVL 19

Accepted Solution

by:
weellio earned 1000 total points
ID: 20060203
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
 
LVL 19

Expert Comment

by:weellio
ID: 20060243
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
 

Author Comment

by:elwayisgod
ID: 20060445
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
 

Author Comment

by:elwayisgod
ID: 20060662
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
 
LVL 19

Expert Comment

by:weellio
ID: 20068168
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If like me you are one who spends a lot of time working and scripting with cmd.exe, sometimes it is handy to be able to quickly view a calendar for a given month and year. This script will quickly do just that!  Save the code posted below to a .bat …
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses
Course of the Month17 days, 3 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question