Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Logging with SMTP Error on Mac

797 views
Skip to first unread message

Bev in TX

unread,
Aug 21, 2009, 1:34:04 PM8/21/09
to
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-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', 'mai...@gmail.com',
['mai...@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...@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...@gmail.com',
['mai...@gmail.com'],'ERROR log',('mai...@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

Bev in TX

unread,
Aug 22, 2009, 9:26:16 PM8/22/09
to
On Aug 21, 8:34 am, Bev in TX <countryon...@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...

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', 'mai...@gmail.com',
['mai...@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

Ned Deily

unread,
Aug 23, 2009, 12:07:29 AM8/23/09
to pytho...@python.org
In article
<c82d8338-c196-400a...@l35g2000vba.googlegroups.com>,

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,
n...@acm.org

Bev in TX

unread,
Aug 23, 2009, 12:45:52 AM8/23/09
to
On Aug 22, 7:07 pm, Ned Deily <n...@acm.org> wrote:
>
> 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,
>  n...@acm.org- 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

Ned Deily

unread,
Aug 23, 2009, 2:05:51 AM8/23/09
to pytho...@python.org
In article
<87236fb1-c09f-46e8...@24g2000yqm.googlegroups.com>,

Bev in TX <countr...@yahoo.com> wrote:
> On Aug 22, 7:07�pm, Ned Deily <n...@acm.org> wrote:
> > [...] 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/

--
Ned Deily,
n...@acm.org

Aahz

unread,
Aug 23, 2009, 5:48:34 AM8/23/09
to
In article <mailman.251.1250986...@python.org>,

Ned Deily <n...@acm.org> wrote:
>
>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.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

"I support family values -- Addams family values" --www.nancybuttons.com

Bev in TX

unread,
Aug 23, 2009, 10:57:49 AM8/23/09
to
On Aug 23, 12:48 am, a...@pythoncraft.com (Aahz) wrote:
> 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.
> --
> Aahz (a...@pythoncraft.com)           <*>        http://www.pythoncraft.com/

>
> "I support family values -- Addams family values" --www.nancybuttons.com

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

Aahz

unread,
Aug 23, 2009, 1:14:00 PM8/23/09
to
In article <60de8d40-61f4-4ffd...@k19g2000yqn.googlegroups.com>,

Bev in TX <countr...@yahoo.com> wrote:
>On Aug 23, 12:48=A0am, a...@pythoncraft.com (Aahz) wrote:
>>
>> 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.

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.
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/

Message has been deleted

Aahz

unread,
Aug 23, 2009, 10:44:01 PM8/23/09
to
In article <mailman.290.1251065...@python.org>,
Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:
>On 23 Aug 2009 06:14:00 -0700, aa...@pythoncraft.com (Aahz) declaimed the
>following in gmane.comp.python.general:

>>
>> 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.

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.

0 new messages