Logging with SMTP Error on Mac


B

Bev in TX

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/20...-module-is-much-better-than-print-statements/
(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', '(e-mail address removed)',
['(e-mail address removed)'],'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', '(e-mail address removed)')

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', '(e-mail address removed)',
['(e-mail address removed)'],'ERROR log',('(e-mail address removed)','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
 
Ad

Advertisements

B

Bev in TX

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', '(e-mail address removed)',
['(e-mail address removed)'],'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', '(e-mail address removed)')

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', '(e-mail address removed)',
['(e-mail address removed)'],'ERROR log',('(e-mail address removed)','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', '(e-mail address removed)',
['(e-mail address removed)'],'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
 
N

Ned Deily

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', '(e-mail address removed)',
['(e-mail address removed)'],'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', '(e-mail address removed)')

The problem here is that gmail, like most modern mail services, requires
the use of an encrypted (SSL or TLS) connection for mail relaying so
that the required user name and password are not sent in the clear. The
logging SMTP handler uses the smtplib module from the standard module to
send mail and, when built properly, smtplib does support TLS. However,
the caller of smtplib does need to do some extra work in that case, i.e.
it needs to call the server object's starttls method at the right point
in the protocol handshaking. It's currently not done automatically in
smtplib and, unfortunately, there is no code in the logging smtp handler
to detect the need for and to call starttls (in response to a
250-STARTTLS response to an EHLO).

To make this work, either the logging module or, perhaps better, the
smptlib module needs to be smarter about this case. I didn't see an
open issue on the Python bug tracker about this; you might want to open
one. In the meantime, some options would be to find an SMTP mail host
that doesn't require TLS. Or write a custom logger. Or on OS X it's
not *too* difficult to set up a local host mailer using the
Apple-supplied prefix that would accept mail locally and forward it to a
more sophisticated remote mailer.
 
B

Bev in TX

The problem here is that gmail, like most modern mail services, requires
the use of an encrypted (SSL or TLS) connection for mail relaying so
that the required user name and password are not sent in the clear.  The
logging SMTP handler uses the smtplib module from the standard module to
send mail and, when built properly, smtplib does support TLS.  However,
the caller of smtplib does need to do some extra work in that case, i.e.
it needs to call the server object's starttls method at the right point
in the protocol handshaking. It's currently not done automatically in
smtplib and, unfortunately, there is no code in the logging smtp handler
to detect the need for and to call starttls (in response to a
250-STARTTLS response to an EHLO).

To make this work, either the logging module or, perhaps better, the
smptlib module needs to be smarter about this case.  I didn't see an
open issue on the Python bug tracker about this; you might want to open
one.  In the meantime, some options would be to find an SMTP mail host
that doesn't require TLS.  Or write a custom logger.  Or on OS X it's
not *too* difficult to set up a local host mailer using the
Apple-supplied prefix that would accept mail locally and forward it to a
more sophisticated remote mailer.

--
 Ned Deily,
 (e-mail address removed)- Hide quoted text -

- Show quoted text -

Thanks for the excellent and informative response :). I'll
investigate further, as you suggested, now that I understand what is
happening.

Bev in TX
 
N

Ned Deily

[...] Or on OS X it's
not *too* difficult to set up a local host mailer using the
Apple-supplied prefix that would accept mail locally and forward it to a
more sophisticated remote mailer.

Um, notation fail: s/prefix/Postfix/
 
A

Aahz

Or on OS X it's not *too* difficult to set up a local host mailer using
the Apple-supplied prefix that would accept mail locally and forward it
to a more sophisticated remote mailer.

It's also not too difficult to do the moral equivalent with dnspython and
find the MX host for the domain you're trying to send e-mail to; that
usually does not require an encrypted connection.
 
Ad

Advertisements

B

Bev in TX

It's also not too difficult to do the moral equivalent with dnspython and
find the MX host for the domain you're trying to send e-mail to; that
usually does not require an encrypted connection.

Thanks :). I'm not sure that I understand exactly what needs to be
done, but I'll read the documentation and see what I can come up with.

Bev in TX
 
A

Aahz

Thanks :). I'm not sure that I understand exactly what needs to be
done, but I'll read the documentation and see what I can come up with.

Slightly expanded: most mail servers accept connections from any other
mail server for addresses that are hosted by them. The MX address for a
domain tells you what mail server hosts that address. By connecting
directly to the MX, your script is essentially acting as a mail server
itself.
 
D

Dennis Lee Bieber

Slightly expanded: most mail servers accept connections from any other
mail server for addresses that are hosted by them. The MX address for a
domain tells you what mail server hosts that address. By connecting
directly to the MX, your script is essentially acting as a mail server
itself.

But is not possible if one's ISP blocks pass-through SMTP
connections.
 
Ad

Advertisements

A

Aahz

But is not possible if one's ISP blocks pass-through SMTP
connections.

Correct, but the OP said that zir ISP did not have a mailserver; AFAIK
all ISPs that block SMTP have their own mailserver that you can point at
instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top