commandline mail sender (vbscript)


By: nbrane
June 11, 2013

I tested this somewhat, but there may still be glitches. Feel free to Pmail me if something doesn't work as it should.

w=wscript.arguments.count
 

const forreading=1, forwriting=2, forappend=8
dim z(100)
dim atts
set control=createobject("scripting.dictionary")
set fso=createobject("scripting.filesystemobject")
set config=fso.opentextfile("config",forreading)
if w>0 then a=wscript.arguments(0)
if instr(a,"?")>0 then
m=fso.opentextfile("smail.vbs",forreading).readall
hlp=split(m,"'----------------")(2)
hlp=replace(hlp,"'","")
wscript.stdout.write hlp
wscript.quit
end if
'====== defaults
control("timeout")=10
control("text")=""
control("port")=25
control("ssl")=False
'======= file keys: server from port ssl user pass timeout
do until config.atendofstream
i=i+1 : cf=config.readline
call load( cf)
loop
'======== argument keys: from (override) subject text file to att
for i=0 to wscript.arguments.count-1
call load (wscript.arguments(i))
next
if isarray(atts) then
for i=0 to ubound(atts)
wscript.echo atts(i)
next
end if
'wscript.quit
 


sub load (cf)
p=instr(cf,":")
if p=0 then p=instr(cf,"=")
if p=0 then
wscript.echo "invalid entry: " & cf
wscript.quit
end if
z(0)=left(cf,p-1)
z(1)=mid(cf,p+1)
z(0)=rtrim(ltrim((lcase(z(0)))))
if z(0)="att" then
atts=split(z(1),",")
else
'wscript.echo ":"&z(0)&":" & z(1)
if z(0)="text" or z(0)="file" then
if z(0)="file" then
z(1)=fso.opentextfile(z(1),forreading).readall
z(0)="text"
end if
control.item(z(0))=control.item(z(0)) & z(1)
else
control.item(z(0))=z(1)
end if
'removed display so sensitive content won't be on screen
'wscript.echo z(0) & " __ " & control(z(0))
end if
end sub
 


Set oMessage = CreateObject("CDO.Message")
 

'(5)
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'(9)
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = control("server")
'(10)
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = control("port")
 

'not sure about this one,.. so far only value 2 works.
oMessage.Configuration.Fields.Item(5) = 2
'(9) smtp server
oMessage.Configuration.Fields.Item(9) = control("server")
'(10) server smtp port (always 25)
oMessage.Configuration.Fields.Item(10) = control("port")
'
'timeout for fail
oMessage.Configuration.Fields.Item(8) = control("timeout")
'only 0, 1 or 2. must be authentication? doesn't seem to affect much.
oMessage.Configuration.Fields.Item(1) = 0
 


'(4) username@domain
oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = control("user")
 

'(3) Your password on the SMTP server
oMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = control("pass")
 

 
 

oMessage.Configuration.Fields.Update
 

'removed (ditto)
'on error resume next
'for i=0 to 100
'wscript.echo cstr(i) & ": " & omessage.configuration.fields.item(i)
'next
err.clear
on error goto 0
 

oMessage.Subject = control("subject")
oMessage.From = control("from")
oMessage.To = control("to")
oMessage.TextBody = control("text")
if isarray(atts) then
'removed display (ditto)
'wscript.echo cstr(ubound(atts))& "attachments: list follows"
for i=0 to ubound(atts)
'wscript.echo "+" & atts(i)
oMessage.AddAttachment atts(i)
next
end if
 

h=control.keys
'info blurb: collected data being used for this transmission
'removed display (ditto)
'for i=0 to control.count-1
'wscript.echo h(i) & ": " & control(h(i))
'next
 

oMessage.Send
 

'fields values (output from above) obtained from the http stuff above.
'0: en-us                     language
'1: 0                         authentication? 0=none, 1=basic, 2=ntlm
'2: "me" <my_id@mydomain.net>    "send using"
'3: mypasswd0                   **** password
'4: my_id@my_domain.net       **** username
'5: 2                         **** ???
'6: pop.mydomain.net          **** pop server
'7: 3
'8: 60                        time-out for failure
'9: smtp.suddenlink.net       **** smtp server
'10: 25                       **** server smtp port (almost always 25)
'11: True
'12: 10


'reference used:
'http://www.paulsadowski.com/wsh/cdo.htm
'----------------------------------------
'Usage: cscript smail.vbs {FROM:from-data} {TO:to-data} [SUBJECT:"subject text"]
' [TEXT:"msg-body content"] [FILE:"textfile source for msg-body"]
' [ATT:"attach#1"[,"attach#2"...]

'File CONFIG is used to supply any further data required (generally more
'technical and less volatile). Same format but on lines, like this:

'PASS=password
'USER=me@mydomain.net
'[PORT=25]
'SERVER=smtp.mydomain.net
'FROM="my friendly name" <my_id@mydomain.net>
'[TIMEOUT=nn]


'NOTE: Keyword (or "tag") case is ignored. I used caps to set them off as constants.
'Items in [] are optional, items in {} are required but may come from either
'source (command-line or the config file). Command-line will override the
'config file. The message-body can be simple quote (TEXT), or supplied by a
'file (FILE) or both. If both, they will be "stacked" in the order that they
'are encountered. Attachment filenames MUST BE FULLY QUALIFIED, not relative
'or assumed. (But the msg-body filename does not have this requirement).
'PORT defaults to 25 if not specified.


Need more help?
Describe your Problem
Example: Hard Drive Not Detected on My PC

Ask Question