How to create an SMTP alert when a script is finished.

Hello,
I wrote a VBScript that copies about 125GB of data. I was wondering how I can get the script to send an SMTP message to my epager when the script is finished. Also if there are any errors, i would like to be alerted.

Thanks!
Drew_MoraAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
jbarbataConnect With a Mentor Commented:
Great Drew, y

Your almost there... just need to point that message at  a mail server that you can relay through.

In:
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "amora@umcaz.edu"

"amora@umcaz.edu" - should be so something like "mail.umcaz.edu" or "smtp.umcaz.edu" or even simply the ip address of your mail server. try: "smtp1.umcaz.edu" or "198.60.159.41" - I got these results back from your domain:

# nslookup
> set type=mx
> umcaz.edu

Non-authoritative answer:
umcaz.edu       preference = 10, mail exchanger = smtp2.umcaz.edu
umcaz.edu       preference = 10, mail exchanger = smtp1.umcaz.edu

Authoritative answers can be found from:
smtp2.umcaz.edu internet address = 198.60.159.41
smtp1.umcaz.edu internet address = 198.60.159.40
0
 
jbarbataCommented:
Hi Drew,

Here is a function I use frequently... You'll need to replace "mail.example.com" with your own mail server address - It's in like line 27.

'Setup Message Variables
 msgSndr = "sender@example.com"
 msgRcvr = "receiver@example.com"
 msgSubj = "Message Subject"
 msgBoby = "Message body"

'Call the sendmail routine!
Call sendmail(msgSndr, msgRcvr, msgSubj, msgBoby)


Sub sendmail(msgSndr, msgRcvr, msgSubj, msgBoby)
'###################################
'### EMAIL NOTIFICATION ROUTINE ###
'###################################
Dim iMsg, iConf, Flds, strHTML

Const cdoSendUsingPort = 2

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

Set Flds = iConf.Fields

With Flds
 .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.example.com"
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
 .Update
End With

With iMsg
 Set .Configuration = iConf
 .To = msgRcvr
 .From = msgSndr
 .Subject = msgSubj
 .TextBody = msgBoby
 .Send
End With

 Set iMsg = Nothing
 Set iConf = Nothing
 Set Flds = Nothing
End Sub
0
 
Drew_MoraAuthor Commented:
Here is my code. I ran the code and got an error @ line 28: Failed to connect to the server.

On line 27 where I put the email domain, do I have to do anything else?

'Setup Message Variables
 msgSndr = "amora@umcaz.edu"
 msgRcvr = "amora@umcaz.edu"
 msgRcvr = "drew.mora@Gmail.com"
 msgSubj = "File-Copy-Complete"
 msgBoby = "The Files have been copied"

'Call the sendmail routine!
Call sendmail(msgSndr, msgRcvr, msgSubj, msgBoby)


Sub sendmail(msgSndr, msgRcvr, msgSubj, msgBoby)
'###################################
'### EMAIL NOTIFICATION ROUTINE ###
'###################################
Dim iMsg, iConf, Flds, strHTML

Const cdoSendUsingPort = 2

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

Set Flds = iConf.Fields

With Flds
 .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "umcaz.edu"
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
 .Update
End With

With iMsg
 Set .Configuration = iConf
 .To = msgRcvr
 .From = msgSndr
 .Subject = msgSubj
 .TextBody = msgBoby
 .Send
End With

 Set iMsg = Nothing
 Set iConf = Nothing
 Set Flds = Nothing
End Sub
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Drew_MoraAuthor Commented:
Now I am getting:

Error: The transport failed to connect to the server.
Line: 38

Here is my code:
'#####################################################################################
'Setup Message Variables
 msgSndr = "amora@umcaz.edu"
 msgRcvr = "amora@umcaz.edu"
 msgRcvr = "drew.mora@Gmail.com"
 msgSubj = "File-Copy-Complete"
 msgBoby = "The Files have been copied"

'Call the sendmail routine!
Call sendmail(msgSndr, msgRcvr, msgSubj, msgBoby)


Sub sendmail(msgSndr, msgRcvr, msgSubj, msgBoby)
'###################################
'### EMAIL NOTIFICATION ROUTINE ###
'###################################
Dim iMsg, iConf, Flds, strHTML

Const cdoSendUsingPort = 2

Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")

Set Flds = iConf.Fields

With Flds
 .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "amora@umcaz.edu"
 .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 10
 .Update
End With

With iMsg
 Set .Configuration = iConf
 .To = msgRcvr
 .From = msgSndr
 .Subject = msgSubj
 .TextBody = msgBoby
 .Send
End With

 Set iMsg = Nothing
 Set iConf = Nothing
 Set Flds = Nothing
End Sub
0
 
Drew_MoraAuthor Commented:
Ohhhhh cool, I misunderstood, sorry for that. I understand now! Thanks mucho!

It works like a charm!
0
 
Drew_MoraAuthor Commented:
One other question......How do I send to more than 1 Rcvr? I need 3 people to be notified.
0
 
jbarbataCommented:
That’s awesome!

Here are three diferent ways to send multipal messages:

1.) Just change the receiver variable and call the mail function again: (not recomended)
  msgRcvr = "new.address@Gmail.com" 're-write receiver variable.
  Call sendmail(msgSndr, msgRcvr, msgSubj, msgBoby)

2.) Simply add multipal receiver addresses: (recomended)
   msgRcvr = "new.address@Gmail.com; new.address2@Gmail.com; new.address3@Gmail.com"

3.) You could change the function to include a CC. field:

With iMsg
 Set .Configuration = iConf
 .To = msgRcvr
 .Cc = "cc.address1@somedomain.com; cc.address2@somedomain.com"  
 .From = msgSndr
 .Subject = msgSubj
 .TextBody = msgBoby
 .Send
End With

Good luck!
 
0
 
Drew_MoraAuthor Commented:
Great Thanks!
0
All Courses

From novice to tech pro — start learning today.