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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Send Different Emails based on Time

Status
Not open for further replies.

tqeonline

MIS
Joined
Oct 5, 2009
Messages
304
Location
US
Hello All!

I need to be able to send three different emails all based off of time.

I am going to setup a scheduled task to send emails at 5am, 11am, and 5pm all saying almost the same thing, but I have to change the times in them.

Ex. Right now it says "We are closing the window for 6PM" and i will be sending that at 5pm. So i'd have to make it say 12pm and 6am... etc

I have the VBS written and it sends an email but now i need something that will allow me to change the subject and content based on time.
Code:
if time is closer to 11am then 
   new subject and content
elseif time is closer to 5pm then
   new subject and content
elseif time is closer to 5am then
   new subject and content
end if

reason I say closer to is I will set it off at 11, 5, and 5 but who's to know if it actually gets off at 11 on the dot it could be 11:00.50 before it sends the email.

Any ideas?

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
You can check the current time and branch on that:

Code:
'is the current time within 1 hour of 11 o'clock?
if Abs(11 - Hour(Now())) <= 1 then
  wscript.echo("it is within 1 hour of 11 o'clock")
  'send the 11 o'clock email
else
  wscript.echo("it is not within 1 hour of 11 o'clock")
end if

The above code uses the Hour() function. There is also a Minute() function if you need better resolution. The above is just an example, you'll have to add more checks for each time of day you want to send an email.
 
If you have 3 different scheduled tasks (one executing at 5am, one at 11am, and one at 5pm), and each task is running the same script that you created, then you can pass a command-line parameter to the script. For example, "cscript sendemail.vbs 5am" and in your script, you can parse the commandline parameter.

Or, you can use DateDiff to get the difference between the current time and 5am, 11am, and 5pm, and pick the closest.
Code:
iDiff5am = Abs(DateDiff("h", Time(), "5:00 AM"))
iDiff11am = Abs(DateDiff("h", Time(), "11:00 AM"))
iDiff5pm = Abs(DateDiff("h", Time(), "5:00 PM"))

If (iDiff5am < iDiff11am) And (iDiff5am < iDiff5pm) Then
   SEND 5AM EMAIL
Else
   If iDiff11am < iDiff5pm Then
      SEND 11AM EMAIL
   Else
      SEND 5PM EMAIL
   End If
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top