logging SMTPhandler Error

Bev in TX countryone77 at yahoo.com
Sat Aug 22 23:26:16 CEST 2009


On Aug 21, 8:34 am, Bev in TX <countryon... at yahoo.com> wrote:
> Hi,
>
> I've done some Python programming, but I still consider myself a
> Python newbie.  I have a Mac Pro OS X 10.5.8 system and I installed
> Python 2.6.2 (the latest package available for the Mac) yesterday.
>
> I was working through Matt Wilson's article on using the logging
> module:http://blog.tplus1.com/index.php/2007/09/28/the-python-logging-module...
> (If that does not work, then try:http://tinyurl.com/5v2lcy)
>
> Everything worked great until his last example.  My ISP does not
> provide e-mail, so I tried using gmail in the line that sets h2.  I
> substituted "mailid" for my actual e-mail address in the following
> examples; in my test I used my actual e-mail ID.  Also, I used the
> full path to the newly installed Python 2.6.2; otherwise it picked up
> the older Python 2.5:
> #!/Library/Frameworks/Python.framework/Versions/2.6/bin/python
>
> First attempt:
> h2 = logging.handlers.SMTPHandler('smtp.gmail.com', 'mai... at gmail.com',
> ['mai... at gmail.com'],'ERROR log')
> However, that caused the following error to be issued:
>
> Traceback (most recent call last):
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/handlers.py", line 868, in emit
>     smtp.sendmail(self.fromaddr, self.toaddrs, msg)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/smtplib.py", line 698, in sendmail
>     raise SMTPSenderRefused(code, resp, from_addr)
> SMTPSenderRefused: (530, '5.7.0 Must issue a STARTTLS command first.
> 7sm3867994qwf.47', 'mai... at gmail.com')
>
> I also tried providing my gmail userid/password, I tried adding a 5th,
> credential, argument, which is a tupple, (username,password) (new in
> 2.6).
>
> Second attempt:
> h2 = logging.handlers.SMTPHandler('smtp.gmail.com', 'mai... at gmail.com',
> ['mai... at gmail.com'],'ERROR log',('mai... at gmail.com','gmail-
> password'))
> However, that caused the following error message:
>
> Traceback (most recent call last):
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/logging/handlers.py", line 867, in emit
>     smtp.login(self.username, self.password)
>   File "/Library/Frameworks/Python.framework/Versions/2.6/lib/
> python2.6/smtplib.py", line 552, in login
>     raise SMTPException("SMTP AUTH extension not supported by
> server.")
> SMTPException: SMTP AUTH extension not supported by server.
>
> I am able access gmail via Mac's Mail, in which it says that outgoing
> mail is going to:
>       smtp.gmail.com:mailid
> I tried using that as the server in the Python script, but it could
> not find that server.
>
> Is this possible?  If I am doing something incorrectly, would someone
> please indicate what it is?
>
> Thanks,
> Bev in TX

Today I tried this with both Python 2.6.2 and 3.1.1 on an XP system,
with the same error.  For the 3.1.1 test, I had to change the syntax
of the exception from "except exc, var" to "except exc as var".  Here
is the modified script, with my email ID changed to "mailid":

import logging
import logging.handlers

#Make a global logging object
x = logging.getLogger("logfun")
x.setLevel(logging.DEBUG)

#This handler writes out everything to stdout
h1 = logging.StreamHandler()
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)
d %(message)s")
h1.setFormatter(f)
h1.setLevel(logging.DEBUG)
x.addHandler(h1)

#This handler emails me anything that is an error or worse
h2 = logging.handlers.SMTPHandler('smtp.gmail.com', 'mailid at gmail.com',
['mailid at gmail.com'],'ERROR log')
f = logging.Formatter("%(levelname)s %(asctime)s %(funcName)s %(lineno)
d %(message)s")
h2.setLevel(logging.ERROR)
h2.setFormatter(f)
x.addHandler(h2)

def g():
  1 / 0

def f():
  logfun = logging.getLogger("logfun")
  logfun.debug("inside f!")
  try:
    g()
  except Exception as ex:
    logfun.exception("Something awful happened!")
  logfun.debug("Finishing f!")

if __name__ == "__main__":
  f()

Thanks,
Bev in TX



More information about the Python-list mailing list