1

So far my code is as follows:

from socket import *
import ssl
msg = "\r\n smtp..."
endmsg = "\r\n.\r\n"

# Choose a mail server (e.g. Google mail server) and call it mailserver
mailserver = ("smtp.gmail.com", 587)

# Create socket called clientSocket and establish a TCP connection with mailserver over SSL
clientSocket = socket(AF_INET, SOCK_STREAM);
clientSocket = ssl.wrap_socket(clientSocket, ssl_version=ssl.PROTOCOL_SSLv23)
clientSocket.connect(mailserver)

#Print server response
recv = clientSocket.recv(1024)
print recv
if recv[:3] != '220':
print '220 reply not received from server.'

I get the error message ssl.SSLERrror: [Errno 1] _ssl.c:504: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol when attempting to run this script. At other times I've gotten errors regarding the server not responding in time.

Does anyone have any clues about what I'm doing wrong? (And yes I know I could use smtplib for dealing smtp servers, but this is an exercise)

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
EthanLWillis
  • 930
  • 4
  • 14
  • 27

1 Answers1

2

port 587 is not encrypted.

telnet smtp.gmail.com 587

Trying 74.125.142.109...
Connected to gmail-smtp-msa.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP aa4sm9457625igc.15
helo test.com
250 mx.google.com at your service
tMC
  • 18,105
  • 14
  • 62
  • 98
  • Ah. But when I do AUTH LOGIN and provide all my credentials and am ready to do a MAIL FROM command do I not need TLS enabled or does the smtp server not care? – EthanLWillis Sep 26 '12 at 02:30
  • @EthanWillis: You need to ask the server to switch to TLS (using the `STARTTLS` command) after making the initial connection. Gmail's server *does* care and won't let you send email without an encrypted connection. – Greg Hewgill Sep 26 '12 at 02:35
  • @GregHewgill I've tried doing this manually with telnet but after issuing STARTTLS I'm not sure how I "negotiate" the TLS connection so that I can issue the AUTH LOGIN command. Maybe I should create a new question? – EthanLWillis Sep 26 '12 at 02:40
  • 1
    @EthanWillis: After using STARTTLS, the connection immediately shifts into encrypted mode (starting with the TLS negotiation). Unless you can do encryption manually in your head, you probably won't be able to try this out interactively using telnet. – Greg Hewgill Sep 26 '12 at 02:44
  • 1
    telnet doesn't understand TLS. the moment you send the STARTTLS command, telnet is useless. I think another question about how to start TLS on an active connection is appropriate – tMC Sep 26 '12 at 02:46
  • http://stackoverflow.com/questions/10147455/trying-to-send-email-gmail-as-mail-provider-using-python – tMC Sep 26 '12 at 02:47
  • 1
    @tMC That answer would be acceptable but I am trying to accomplish this without using smtplib. http://stackoverflow.com/questions/12593944/how-to-start-tls-on-an-active-connection-in-python – EthanLWillis Sep 26 '12 at 02:55
  • Why aren't you using smtplib? Unless you have some very unusual requirements it would be better to start with a Python package that is already tested and debugged against Gmail and other SMTP servers. – RichVel Mar 15 '13 at 13:30