Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sending an email 5

Status
Not open for further replies.

JPJeffery

Technical User
May 26, 2006
600
GB
I'm just after some pointers on how to create an (Outlook 2003) email, complete with TO: addresses, subject line and body text, from a VBScript.

This is for our 'Morning Checks' (Did the backups work? Are these servers up? etc.) email which has to be sent out by whoever is on the early shift. At the moment we're doing it all manually but I reckon much of it can be automated by script.

So, I can code together the stuff I need to test server status et. al. in to variables and/or a text file but I've no idea where to start with creating and populating an email.

Can somebody give me some clues on this, please?

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
Here is a start:

Code:
Set MyApp = CreateObject("Outlook.Application")
Set MyItem = MyApp.CreateItem(0) 'olMailItem
With MyItem
    .To = "a@b.c"
    .Subject = "Subject"
    .ReadReceiptRequested = False
    .HTMLBody = "Message<BR>Line 2"
End With
MyItem.Display

You may also wish to consider CDO.
 
How wonderfully simple!

Thank you!

What's CDO? (I'll look it up)

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
JPJeffery, if you want the email to send automatically replace MyItem.Display with MyItem.Send, or if you put it inside the With statment just as .Send

The code above makes use of the Outlook that is installed on the PC the script is being run on. If the machine does not have Outlook installed it will not work.
If Outlook exists but is not currently open, it should open it for you and work under the ID of the person logged onto the computer (providing Outlook is already configured). If the machine is setup with multiple email options it will bring up the list of accounts asking the person to sign in. I am assuming an Exchange server setup not just internet email.

CDO is a service on IIS servers for sending email, most commonly as a means of allowing ASP scripts on a web site send email from web pages. CDO also known as CDOSYS is the replacement for the older CDONTS.
I am unaware of it's availability outside of the IIS server or if it can be addressed directly from VBScript except through an ASP page on the web server.


At my age I still learn something new every day, but I forget two others.
 
Thanks, NiteOwl, particularly for the .Send option and the info on CDO (which isn't realy an option in this case but still useful to know about, which I didn't before you told me).

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
CDO does not require IIS. I use it all the time to send email from either workstations or member servers. Here is an example within a script I wrote to notify me if a server reboots unexpectantly. You just need to change to domain, To and SMTP Server info.

Code:
'==========================================================================
'
' VBScript Source File -- 
'
' NAME: NotifyReboot.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.thespidersparlor.com[/URL]	
' DATE  : 02/13/2003
'
' COMMENT: 
'
' This script can be added to a machines startup script in Group Policy to notify
' an e-mail address that the machine has restarted.
'
' You must customize the entries for oDomain, oMyIP and oTo with the proper company information.
' Items to customize are on lines 29, 31 and 33.
'=====================================
 
Dim oName, ODomain, oMyIP, oTo

' Get the computer name
Set WshNetwork = CreateObject("WScript.Network")
oName = WshNetwork.ComputerName

' Set the company specific information

' Company Internet Domain Name
ODomain = "yourcompany.com"
' Set the SMTP server IP
oMyIP = "192.168.1.1" 
' Where do you want the message to be delivered
oTo = "admin@yourcompany.com"


' Set the visual basic constants as they do not exist within VBScript.
' Do not set your smtp server information here.
Const cdoSendUsingMethod = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing",[/URL] _
cdoSendUsingPort = 2, _
cdoSMTPServer = "[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver"[/URL]

' Create the CDO connections.
Dim iMsg, iConf, Flds
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
Set Flds = iConf.Fields

' SMTP server configuration.
With Flds
.Item(cdoSendUsingMethod) = cdoSendUsingPort

' Set the SMTP server address here.
.Item(cdoSMTPServer) = oMyIP
.Update
End With

' Set the message properties.
With iMsg
Set .Configuration = iConf
.To = oTo
.From = oName & "@" & oDomain
.Subject = "Server Reboot"
.TextBody = "Server " & oName & " at company " & ODomain & " was restarted " & now
End With

' An attachment can be included.
'iMsg.AddAttachment Attachment

'Send the message.
iMsg.Send

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Aw shucks, I love you guys!

Particularly compared to the other Tek-Tips forums where I very rarely get an answer at all.

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
For another variation, this is what I usually use:

Code:
Sub SendMail
    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = strFromAddress
    objEmail.To = strToAddress
    objEmail.Subject = strSubject
    objEmail.Textbody = strBody
    objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
    objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = strEmailServer
    objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
    objEmail.Configuration.Fields.Update
    objEmail.Send
End Sub

I just call that from wherever I happen to be in the script that needs to send email. All you have to do is define the variables that start with "str" and you're good to go.
 
Cool beanies (as a South African friend of mine used to say).

Thanks!

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
this is pretty cool. actually i can use something like this in my app. but what do you put for the variable stremailserver?
thanks.
 
That would be the hostname of the (SMTP) email server...

JJ
[small][purple]Variables won't. Constants aren't[/purple][/small]
 
thanks.
i got it to work. it's my isp's smtp email server. this is pretty handy.
you deserve a star for sure.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top