N2

Meh

Archive for the ‘vbscript’ tag

Send email from windows command line using vbscript

with one comment

Working as a DBA I do a lot of command line scripting. Unfortunately the windows CLI is not that advanced as Linux. One thing I missed out was sending email from the shell prompt. After a little bit of googling around I found this nice little vb script which now I use for regular server monitoring.

Its using CDO objects to send email and can include the content of a log file or an HTML page in the body.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
 
option explicit
 
'Parameters: 
'  /to:  to address
'  /from:  from address
'  /text:  mail format=text
'  /html:  mail format=html
'  /body:  file
'  /sub:  subject
'
 
dim NamedArgs,oEmail, sTo,sFrom,sText,sHtml,sBody,sSub, objfso, objfile, sMessage
 
if wscript.arguments.count < 4 then 
	'show usage options
	usage
else
 Set NamedArgs = WScript.Arguments.Named
 sText=false
 sHtml=false
 
 'get parameters
 sTo = NamedArgs.Item("to")
 sFrom = NamedArgs.Item("from")
 sText = NamedArgs.Item("text")
 if(NamedArgs.Exists("text")) then sText=true end if
 if(NamedArgs.Exists("html")) then sHtml=true end if
 sBody = NamedArgs.Item("body")
 sSub = NamedArgs.Item("sub")
 
 'create file system object
 set objfso = CreateObject("Scripting.FileSystemObject")
 
 if ((sText="true" and sHtml="true") or ( sText="false" and sHtml="false" ))  then 
  wscript.echo "/text and /html are mutually exclusive options"
  usage
 elseif (objfso.FileExists(sBody)= false) then 
  wscript.echo "file : [" & sBody & "] does not exist or there is some access issue."
  usage()
 else
  Set objfile = objfso.OpenTextFile(sBody, 1)
 
 if (objFile.AtEndOfStream = false) then
  'read file content
  sMessage=objFile.ReadAll()
 end if
 objFile.close
 
 'create mail object
 set oEmail = CreateObject("CDO.Message")
 oEmail.From = sFrom
 oEmail.To = sTo
 oEmail.Subject = sSub
 if(sHtml=true) then 	
  oEmail.HTMLbody = sMessage
 else 
  oEmail.Textbody = sMessage
 end if
 
 oEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 oEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your-smtp-server-name"
 oEmail.Configuration.Fields.Update
 oEmail.Send
 end if
end if
 
 
function usage
 wscript.echo ""
 wscript.echo " ------ vbs sendmail ------ "
 wscript.echo "/to:      to address"
 wscript.echo "/from:    from address"
 wscript.echo "/text:    mail format=text"
 wscript.echo "/html:    mail format=html"
 wscript.echo "/body:    file"
 wscript.echo "/sub:     subject"
 wscript.echo ""
 wscript.echo "- Example :"
 wscript.echo ""
 wscript.echo " >sendmail /to:navket@my-domain.com /from:my-server@my-domain.com.com /text /body:c:\some\log-file.txt /sub:""JOB Results"" ! "
 wscript.echo ""
end function

Written by Singh

April 1st, 2009 at 10:00 pm

Posted in Technology

Tagged with , , ,