syphilis seems to have answered your problem, so I'd like to toss in a minor nit -- you might want to display a warning if
auth_password fails. Otherwise you have a silent error (after you remove the debugging
print "\nIN SFTP\n";). That is, you don't signon, you don't get the file, but things clean up nicely without you knowing nothing happened.