Created
January 8, 2012 11:34
-
-
Save patrickmmartin/1578061 to your computer and use it in GitHub Desktop.
Simple VBS based mailer for integration into build scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
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
Intended for unattended Windows build machines.
Uses CDO for sending the emails, which seems to end up pretty universally installed, hence this script should be a drop-in.
In keeping with my general "no fuss, no muss" philosphy, the only most probable configuration required is to set the
Search for "example.com" to update these.
Of course, your main server admins may validate senders and other content, so I suggest you confirm these emails will get through with them.