Hannah
Hannah

Reputation: 87

Python script to send email using Gmail

I have absolutely zero experience with Python socket programming. I am basically learning as I go. Anyway, my instructor gave us an assignment to create a mail client using the skeleton code provided here: http://www.cs.wm.edu/~dnguyen/cs335/project/emailclient.pdf (NOTE: I do not attend WM.)

We are not allowed to use smtplib. I'm using Gmail to send email for this exercise, and upon further reading, I found that I have to use STARTTLS and then use SSL's wrap_socket(), then send EHLO again. This is the output I am getting and I have no idea what to do with it. I read somewhere on SO that the last 250 line means something as it has no hyphen/dash (-) between 250 and the text.

220 mx.google.com ESMTP z15sm36730pdi.6 - gsmtp

250-mx.google.com at your service, [xxx.xxx.xxx.xxx]
250-SIZE 35882577
250-8BITMIME
250-STARTTLS
250-ENHANCEDSTATUSCODES
250-PIPELINING
250-CHUNKING
250 SMTPUTF8

220 2.0.0 Ready to start TLS

Below is just part of my code. Since this is an individual assignment, I don't want to post the entire code for others in my class. If you would like to see additional code, please ask.

starttls = 'STARTTLS\r\n'
clientSocket.send(starttls)
recv2 = clientSocket.recv(1024)
print recv2

sslClientSocket = ssl.wrap_socket(clientSocket)
sslClientSocket.send(login)
getLogin = sslClientSocket.recv(1024)
print getLogin
sslClientSocket.send(password)
getPass = sslClientSocket.recv(1024)
print getPass

if recv2[:3] != '220':
    print '220 tls reply not received from server.'

More code info: At the beginning of the file, I import ssl and base64. I define two variables 'login' and 'password' and used base64.b64encode() to encode those. The rest of the code is based on the skeleton code provided above. I send HELO before and after STARTTLS per this thread: Connect to SMTP (SSL or TLS) using Python

How do I read the output to determine what is going on? Thank you in advance for your help.

Upvotes: 0

Views: 448

Answers (1)

Steffen Ullrich
Steffen Ullrich

Reputation: 123461

...and upon further reading, I found that I have to use STARTTLS ... I read somewhere on SO that the last 250 line means something as it has no hyphen/dash (-) between 250 and the text.

You ask a question about a behavior clearly defined in the standards. I suggest you read the relevant standards RFC 2821 and RFC3207 in order to understand the protocols, instead of trying to guess protocol details from some stackoverflow posts. You might also have a look at the implementation of smtplib, even if you are not allowed to use the code directly.

Upvotes: 3

Related Questions