简体   繁体   English

使用Gmail发送电子邮件的Python脚本

[英]Python script to send email using Gmail

I have absolutely zero experience with Python socket programming. 我对Python套接字编程的经验绝对为零。 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.) 无论如何,我的讲师给我们分配了一个任务,以使用此处提供的框架代码创建邮件客户端: http : //www.cs.wm.edu/~dnguyen/cs335/project/emailclient.pdf (注意:我不参加WM )

We are not allowed to use smtplib. 我们不允许使用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. 我正在使用Gmail发送此练习的电子邮件,进一步阅读后发现,我必须使用STARTTLS,然后使用SSL的wrap_socket(),然后再次发送EHLO。 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. 我在某处读到,最后250行意味着某些内容,因为它在250和文本之间没有连字符/破折号(-)。

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. 更多代码信息:在文件的开头,我导入ssl和base64。 I define two variables 'login' and 'password' and used base64.b64encode() to encode those. 我定义了两个变量“ login”和“ password”,并使用base64.b64encode()对其进行编码。 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 我在此线程的STARTTLS之前和之后发送HELO: 使用Python连接到SMTP(SSL或TLS)

How do I read the output to determine what is going on? 如何读取输出以确定发生了什么? Thank you in advance for your help. 预先感谢您的帮助。

...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. ...并且进一步阅读后,我发现我必须使用STARTTLS ...我在SO上的某个地方读到,最后250行意味着某些内容,因为它在250和文本之间没有连字符/破折号(-)。

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. 我建议您阅读相关的标准RFC 2821RFC3207 ,以了解协议,而不是尝试从一些stackoverflow帖子中猜测协议的详细信息。 You might also have a look at the implementation of smtplib, even if you are not allowed to use the code directly. 即使不允许直接使用代码,您也可以查看smtplib的实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM