Sending email with VBScript

I’ve really wanted to blog for a while, but I’m bad at it.  I forget to blog then after 10 entries or so I let it die.  Well, I’m back.  And I am going to stick with it this time!]
 
The idea behind my new blog is to mainly talk tech.  I’m passionate about a few things right now, one of them being VBScript.  You can do many wonderful things with VBScript and I hope to share some of my creations through this blog.  This is my base email script and I modify it for various functions.  I’ve added argruments to handle the selection of different email addresses and reports for instance.  I hope the script below helps someone a little bit in their daily administrative work!
 
Catch ya leter!
 
‘ ************************************************************************
‘ **
‘ **  sendmail.vbs
‘ **    Written by Scott Heath
‘ **    Created on August 11, 2007
‘ **
‘ **  Description:
‘ **    This script uses the CDO COM Object to send emails via vbscript.
‘ **    You can use this to automate alerts or reporting.
‘ **
‘ ************************************************************************
 
On Error Resume Next
 
‘ Declare Variables
‘ ************************************************************************
Dim oFSO
Dim iMsg, iConf
Dim MsgBody, MsgSubject, MsgRecipients, MsgFrom
Dim Args, LogFile
 
‘ Instaniate Objects
‘ ************************************************************************
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set Logfile = oFSO.OpenTextFile("SendMail.Log",8 ,True)
 
‘ Set Initial Variable Values
‘ ************************************************************************
MsgRecipients = "
youremail@yourdomain.com"
MsgBody = "Whatever you’d like to say."
MsgSubject = "My automated email"
MsgFrom = """Friendly Name"" <
sendingemail@yourdomain.com>"
 
‘ Create CDO Object, set message properties, and send message
‘ ************************************************************************
‘ ** Instantiate Objects
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
‘ ** Set mail server values
iConf.Load -1
Set Flds = iConf.Fields
With Flds
  .Item("
http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  .Item("
http://schemas.microsoft.com/cdo/configuration/smtpserver") = "10.0.0.1" ‘<– This is the IP of your SMTP server!
  .Update
End With
‘ ** Add attachments
If oFSO.FileExists("c:\path\filename.txt") Then
  iMsg.AddAttachment "c:\path\filename.txt"
Else
  MsgBody = MsgBody & vbCrLf & "File to attach was missing."
End IF
‘ ** Set message properites
With iMsg
  Set .Configuration = iConf
  .To = MsgRecipients
  .CC = ""
  .BCC = ""
  .From = MsgFrom
  .Subject = MsgSubject
  .TextBody = MsgBody
End With
‘ **Send Message!
err.clear
iMsg.Send
If err.Number = 0 Then
  LogFile.Writeline Now & " – Action: " & Action & " – Mail sent successfully"
Else
  LogFile.Writeline Now & " – " & err.Description
End IF
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s