I'm testing to get my python script to still run if the smtp server it uses went down. I have a local mail server im testing this with. The issue is when testing it, i get a large spew of text. It checks all its libraries and such, untill its get stuck at socket.py and the line says: raise error, msg.

The next line is: error: [Errno 111] Connection refused. Then after some location stuff, it says DeprecationWarning: BaseException has been deprecated as of Python 2.6. Then it has some more location stuff, and finishes with return apply(func, args)

Here is my function:

def email():
   global mssg
   session = smtplib.SMTP(smtpserver)
   if authrequired:
      session.login(smtpuser, smtpass)
   try:
      session.sendmail(SENDER, RECIPIENTS, mssg)
      session.quit()
   except:
      raise error
      logit('server down')
   return

Ive done some research and it seems i need to make a class in the script such as:

class MyError(Exception):
   def __init__ (self, value):
      self.value = value
   def __str__(self):
      return repr(self.value)

But im not sure if i have to swap anything here. For example, do i put 'error: [Errno 111] Connection refused' in the (Exception). Or if thats fine the way it is, how do i go about calling it in my function?

If you only want to catch the exception and let your code still run, you only need to write

import socket

def email():
   global mssg
   session = smtplib.SMTP(smtpserver)
   if authrequired:
      session.login(smtpuser, smtpass)
   try:
      session.sendmail(SENDER, RECIPIENTS, mssg)
      session.quit()
   except socket.error:
      logit('server down')

Don't write except: statements without targeting at least one specific exception class. It's much better to let unexpected errors propagate. When they occur, you can still add a new except statement if needed. An opaque except hides errors for future debugging sessions.

Nah that didnt work, same issue occuring

Nah that didnt work, same issue occuring

If the issue is the deprecation warning, it's not an issue. It's only a warning.

Here's the full error:

Traceback (most recent call last):
File "/home/user/reading.py", line 251, in <module>
  email()
File "/home/user/reading.py", line 124, in email
session = smtplib.SMTP(smtpserver)
File "/usr/lib/python2.6/smtplib.py", line 239, in __init__ 
  (code, msg) = self.connect(host, port)
File "/usr/lib/python2.6/smtplib.py", line 295, in connect
  self.sock = self._get_socket(host, port, self.timeout)
File "/usr/lib/python2.6/smtplib.py", line 273, in _get_socket
  return socket.create_connection((port, host), timeout)
File "/usr/lib/python2.6/socket.py", line 514 in
create connection
     raise error, msg
error: [Errno 111] Connection refused
/home/user/Komodo-IDE-5/lib/support/dbgp/pythonlib/dbgp/pythonlib/dbgp/client.py
:1240: DeprecationWarning: BaseException.message has been depracted
as of Python 2.6
   child = getattr(self.value, childStr)
/home/user/Komodo-IDE-5/lib/support/dbgp/pythonlib/dbgp/pythonlib/dbgp/client.py
:405: DeprecationWarning: BaseException.message has been depracted
as of Python 2.6
   return apply(func, args

If the issue is the deprecation warning, it's not an issue. It's only a warning.

Well the script is still ending in any event. and id like to prevent that if possible

It's not caught because the line session = smtplib.SMTP(smtpserver) is out of the try..except statement !

It's not caught because the line session = smtplib.SMTP(smtpserver) is out of the try..except statement !

AH!

excellent. I had figured just the session.sendmail needed to be inside. Thx for the help