0

When I try to connect to the Gmail server, python throws an error:

>>> from smtplib import SMTP
>>> m = SMTP('smtp.gmail.com', 587)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python27\lib\smtplib.py", line 249, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Python27\lib\smtplib.py", line 309, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Python27\lib\smtplib.py", line 284, in _get_socket
    return socket.create_connection((port, host), timeout)
  File "C:\Python27\lib\socket.py", line 571, in create_connection
    raise err
socket.error: [Errno 10060] 

The rest of the output is in a diferent language but it basically says that the host (gmail) didn't respond.

I can see my email on a browser here at my work, probably there's a network configuration that doesn't allow me to automate the email delivering.

Is there a work around to let python act as a regular browser?

loki
  • 2,271
  • 5
  • 32
  • 46

1 Answers1

-1

First, you can only access gmail's SMTP servers as a client with some form of authentication; the recommended way is with oauth. See this page and this one for details. So, your code won't work, even when you get past this problem.

However, that doesn't explain why it's rejecting your connection before you even get a chance to log in.

The most likely possibility is that gmail's routers are maintaining a dynamic whitelist of IPs. When you use a properly-logged-in connection of some other kind, you get added to the whitelist for N seconds, meaning you're allowed to connect to port 587; otherwise, you're rejected. This would be similar to the traditional SMTP-after-IMAP auth scheme, but not restricted to IMAP, and handled at the router instead of the SMTP service (presumably to lower the cost or make DoS attacks more difficult).

There's a good way to test this: Configure Outlook, Mail.app, or some other mail client that knows how to connect to gmail, and uses SMTP to send mail via gmail. Run your script a few seconds after fetching mail in the mail client. If it works, that's the problem. And in that case, the fix is to do the same kind of connection and login (IMAP? web service?) that the mail client does.

Or, of course, you can use the sample code Google provides at the above links instead of working it out from first principles.

(Of course gmail also has to accept server-to-server SMTP connections, but they could easily have a different auth scheme for that. I'm assuming you're trying to do a client-to-server connection, rather than server-to-server.)

The other possibility is that you're on some kind of blacklist—e.g., gmail thinks your IP belongs to a spammer. This could also be dynamic—maybe anyone who makes a connection to port 587 but doesn't oauth properly gets blocked for the next N seconds. At any rate, this is also easy to test: Configure Outlook, Mail.app, etc. If this is the problem, they won't be able to send mail either.

There's a third possibility, that no one is allowed to connect to port 587, and Google wants you to use port 565 or 25 instead.

For easier debugging, you might want to consider writing an even simpler script that just creates a socket and connects, instead of using smtplib:

import socket
s = socket.socket()
s.connect(('smtp.gmail.com', 587))

Or, even more simply, just netcat from the command line:

nc smtp.gmail.com 587

To answer your side question:

Is there a work around to let python act as a regular browser?

That's not the issue. A regular browser doesn't make SMTP connections; it makes web service connections using Javascript code downloaded from gmail.com.

Of course Python can also make web services connections.

And it can act as much or as little like a "regular browser" (e.g., User-Agent, Referer, etc. headers) as you desire, but that probably isn't relevant—either the gmail web service API is public and has clear, published rules for how to authenticate yourself (in which case you just do what the rules say), or it's private and you shouldn't be trying to fool whatever protection they're using unless you want to get into an arms race.

At any rate, in this case, we know it's public, so we don't have to guess.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • 2
    `gmail's SMTP servers with oauth` - that's not true at all... That'd break far too many email clients... `s = SMTP('smtp.gmail.com', 587); s.starttls(); s.login('user', 'pass')` is perfectly valid – Jon Clements Oct 30 '12 at 22:07
  • Totaly wrong about the OAuth, you should consider fixing your answer since you might mislead other people. – Rafael Herscovici Jan 13 '15 at 08:59