Skip to content

Instantly share code, notes, and snippets.

@patrickmmartin
Created January 8, 2012 11:34
Show Gist options
  • Save patrickmmartin/1578061 to your computer and use it in GitHub Desktop.
Save patrickmmartin/1578061 to your computer and use it in GitHub Desktop.
Simple VBS based mailer for integration into build scripts
If Wscript.Arguments.Count < 3 Then
WScript.Echo "usage: sendmail email title file"
WScript.Quit 1
End If
EmailTo = Wscript.Arguments(0)
EmailTitle = Wscript.Arguments(1)
EmailFile = Wscript.Arguments(2)
WScript.Echo "To: " & EmailTo
WScript.Echo "Title: " & EmailTitle
WScript.Echo "File: " & EmailFile
set shell = WScript.CreateObject( "WScript.Shell" )
server = shell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = EmailTitle
objMessage.From = server & "@example.com"
objMessage.To = EMailTo
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.example.com"
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
'==End remote SMTP server configuration section==
'These constants are defined to make the code more readable
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
'Open the file for reading
Set f = fso.OpenTextFile(EmailFile, ForReading)
'The ReadAll method reads the entire file into the variable BodyText
BodyText = f.ReadAll
'Close the file
f.Close
Set f = Nothing
Set fso = Nothing
objMessage.TextBody = BodyText
WScript.Echo "sending"
objMessage.Send
WScript.Echo "done"
@patrickmmartin
Copy link
Author

Oh yes - example usage:

cscript /nologo sendmailfile.vbs emails title logfile

Note the emails can be a semicolon separated list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment