Jump to content

Mail not sending with Task Scheduler


Recommended Posts

Greets all,

Got an oddity with my back up script.  I have it backing up a few folders to another drive on the network each day around 5am.  The script does its job partly, I have it sending a mail to me that is was successful of failed.  It should send a mail every day regardless.

The problem is, when the task scheduler (Win7) fires it off at 5am the mail doesn't send, the folders get backed up, but no mail.  Further, if I fire it off manually, the mail sends as designed.

I leave the machine logged in, so conditions are the same weather fired by task scheduler or manually.

Ideas?

Link to comment
Share on other sites

Without the script, it's all specuation.  Add logging around your email functions, and see where the failure point is...then fix it.

Or, maybe you are running the scheduled task as system user?  Try changing that to your own user...that will help with outlook...and by the way, what email provider are you using?

Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

Sorry about that, I didn't think the code would be helpful since it works fine when manually clicked.  But here it is, please pardon the sloppiness

#include <Date.au3>
#include <INet.au3>


;Make Todays Date
$tTime = _Date_Time_GetSystemTime()
$aTime = _Date_Time_SystemTimeToArray($tTime)
Global $Month = $aTime[0]
Global $Day = $aTime[1]
Global $Year = $aTime[2]
Global $FolderDate = $Month&"-"&$Day&"-"&$Year

Global  $s_SmtpServer = "mail.FooBar.com"
Global  $s_FromName = "Backup Script"
Global  $s_FromAddress = "info@FooBar.com"
Global  $s_ToAddress = "brian@FooBar.com"

Global $Fail = ""
Global $Folder = ""
Global $FreeSpace =  Round ((DriveSpaceFree( $Folder )/1000),2)

Global $Databases = "C:\Data 8-2012\"
Global $Rolodex = "\\ALYSON\Database BUs\"

ToolTip("Preparing for Database Back up",300,1,'Daily Back up',1)
Sleep(1000)

ALYSON()



Func ALYSON()

    $Folder = "\\ALYSON\Database BUs\"
    $FreeSpace =  Round ((DriveSpaceFree( $Folder )/1000),2)&" GB"
    ToolTip("Preparing for Database Back up"&@CR&"Backing up to ALYSON"&@CR&"Free Space: "&$FreeSpace,300,1,'Daily Back up',1)
    Sleep(1000)
    ;MsgBox(4096, "Free space on :"&$Folder, $FreeSpace & " GB",10)
     $Copy = FileCopy($Databases&"*.*",$Folder&$FolderDate&"\*.*",8)

        If $Copy = 1 Then
            ;MsgBox(0,"Success","Copied "&$Copy,10)
            ToolTip("Preparing for Database Back up"&@CR&"Backing up to ALYSON"&@CR&"Free Space: "&$FreeSpace&@CR&"Back up Successful",300,1,'Daily Back up',1)
            MailSuc()
            ToolTip("Preparing for Database Back up"&@CR&"Backing up to ALYSON"&@CR&"Free Space: "&$FreeSpace&@CR&"Back up Successful"&@CR&"Mail Sent to "&$s_ToAddress,300,1,'Daily Back up',1)
            Sleep(1000)
        Else
            ;MsgBox(0,"Fail","Fail "&$Copy,10)
            ToolTip("Preparing for Database Back up"&@CR&"Backing up to ALYSON"&@CR&"Free Space: "&$FreeSpace&@CR&"Back up Failed",300,1,'Daily Back up',2)
            MailFail()
            ToolTip("Preparing for Database Back up"&@CR&"Backing up to ALYSON"&@CR&"Free Space: "&$FreeSpace&@CR&"Back up Failed"&@CR&"Mail Sent to "&$s_ToAddress,300,1,'Daily Back up',2)
            Sleep(1000)
        EndIf
EndFunc



Func MailSuc()

    $s_Subject = "Back Up Summary"
Dim  $as_Body[3]
    $err = @error
    $as_Body[0] = "Copy to "&$Folder&$FolderDate
    $as_Body[1] = "Driver Free Space "&$FreeSpace
    $as_Body[2] = "Back Up was Good!"
    $s_helo = "info@FooBar.com"
    $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)
    $err = @error

EndFunc



Func MailFail()

    $s_Subject = "Failed Back up"
    Dim $as_Body[2]
    $err = @error
    $as_Body[0] = "Failed Copy to "&$Folder&$FolderDate
    $as_Body[1] = "Driver Free Space "&$FreeSpace&" "&$err
    $s_helo = "info@FooBar.com"
    $Response = _INetSmtpMail ($s_SmtpServer, $s_FromName, $s_FromAddress, $s_ToAddress, $s_Subject, $as_Body)


EndFunc

I am running it with Admin rights from an Admin login and it is saved in Task Scheduler with the Admin password.  Its just the mail portion that wont work from the Task Scheduler side.  Same Script fired manually will function normally.

Thanks

Link to comment
Share on other sites

I know some interactive funcs don't work unless the sched task is running interactively, but since you are running as an admin user I'd assume that's not an issue.  When run from the task sched do the tooltips appear?

Have you inspected the value of $Response after calling _INetSmtpMail?

Link to comment
Share on other sites

Burr,

I see you are doing the check for the error, '$err = @error', but not writing it to a log or txt file.  Try writing the $err to a log file so you can see what message _INetSmtpMail is giving you.

_FileWriteLog("C:\somelocationhere\somelogfilename.log", "_INetSmtpMail error..." & $err)
Link to comment
Share on other sites

-$Response returns 1 (Success)  but the mail worked at this point, read on...

-Tool Tips DO NOT appear when run from task scheduler

-error.log reports 0 errors

The Plot thickens...

Here's the clincher,,,  When fired from task scheduler for testing, the mail worked while I  sat there and watched, go figure...  

So maybe something when the machine is inactive for a bit.  I  never have the machine go to sleep or hibernate.  Only screen saver and monitor shut down.  So I tested further.

When run from Task Scheduler and screen saver on, mail worked.

When run from Task Scheduler and screen saver on and monitor asleep, mail worked.

Now I'm Confused...  Everything is the same except I bump the time so I can test it, so I'm going to change the time from 5am to 4am, just to see what happens.  I will let it run the weekend and report back on Monday.  But as of right now I can't duplicate the problem...

Thanks all for the help :)

Link to comment
Share on other sites

Ok, so the script fired at the new time.  Preformed the backups, but did not send the mail.   So it seems if it sits hours its doesn't send the mail.

I have the screen saver turned on after 15 min, then monitor go off 5 minutes later.  For testing I screen saved in 2 minutes, and monitor off in 4.  So the conditions I would think would be the same as they are at 4am.

I do not have the machine go to sleep, nor hibernate, nor do the HDs spin down.  Aside from all that, we are a 24 hour company, 3~5am the slowest time, but the server/HDs where the script fires is accessed all the time from client shares.

Sorta clueless..

Ideas?

Edit:

I just pulled the error report from last nights back up.

The _INetSmtpMail reported an error of 51.  Help says:

"5x - Cannot open SMTP session. x indicates the index number of last command issued to the SMTP server."

Forgive my ignorance,  so I read this as an opening error on index 1...  Whats that mean? =

Thanks

Edited by Burr
Link to comment
Share on other sites

  • 3 weeks later...

Woot!  that did it.  My guess maybe adding the password in was the key.  But got the mail no problem this morning.  I'll try to strip it down to plug into my script, maybe see exactly what it is. 

 

Cheers

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...