smtplib和gmail - python脚本问题 [英] smtplib and gmail - python script problems

查看:149
本文介绍了smtplib和gmail - python脚本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的脚本:
$ b

 #!/ usr / bin / python 

导入smtplib
msg ='Hello world。'

server = smtplib.SMTP('smtp.gmail.com',587)#port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login('myname@gmail.com','mypass')
server.sendmail('myname@gmail.com ','somename @ somewhere.com',msg)
server.close()

我只是想从我的Gmail帐户发送电子邮件。由于gmail的要求,该脚本使用starttls。我已经在两个Web主机上尝试了这种方法,1and1和webfaction。 1and1给了我一个'连接被拒绝'的错误,webfaction报告没有错误,但只是不发送电子邮件。我看不到脚本有什么问题,所以我认为它可能与Web主机有关。任何想法和意见将不胜感激。编辑:我打开调试模式。从输出中,它看起来像它发送成功的消息...我只是从来没有收到它。

 发送:'ehlo web65 .webfaction.com\r\\\
'
回覆:'250-mx.google.com为您服务,[174.133.21.84] \r\\\
'
回覆:'250- SIZE 35651584\r\\\
'
回覆:'250-8BITMIME\r\\\
'
回覆:'250-STARTTLS\r\\\
'
回覆:' 250-ENHANCEDSTATUSCODES \r\\\
'
回覆:'250 PIPELINING \r\\\
'
回覆:retcode(250);消息:mx.google.com为您提供服务,[174.133.21.84]
尺寸35651584
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
流水线
发送:'STARTTLS \r\\\
'
回覆:'220 2.0.0准备开始TLS\r\\\
'
回覆:retcode(220); Msg:2.0.0准备开始TLS
发送:'ehlo web65.webfaction.com\r\'
回复:'250-mx.google.com为您服务,[174.133。 21.84] \r\\\
'
回覆:'250-SIZE 35651584\r\'
回覆:'250-8BITMIME\r\\\
'
回覆: '250-AUTH LOGIN PLAIN \r\\\
'
回复:'250-ENHANCEDSTATUSCODES \r\\\
'
回复:'250 PIPELINING \r\'
回覆:retcode(250); Msg:mx.google.com为您服务,[174.133.21.84]
SIZE 35651584
8BITMIME
AUTH LOGIN PLAIN
ENHANCEDSTATUSCODES
流水线
发送: 'AUTH PLAIN ***** \r\\\
'
回复:'235 2.7.0 Accepted \r\\\
'
回复:retcode(235);消息:2.7.0已接受
发送:'mail FROM:< myname@gmail.com> size = 12 \r\\\
'
回复:'250 2.1.0 OK 4sm652580yxq.48 \r\''
回复:retcode(250); Msg:2.1.0 OK 4sm652580yxq.48
send:'rcpt TO:< myname@gmail.com> \r\\\
'
reply:'250 2.1.5 OK 4sm652580yxq.48\\ \\ r \'n
回覆:retcode(250); Msg:2.1.5 OK 4sm652580yxq.48
send:'data\r\\\
'
reply:'354继续4sm652580yxq.48\r\\\
'
回复: retcode(354);消息:继续4sm652580yxq.48
data:(354,'Go ahead 4sm652580yxq.48')
send:'Hello world.\r\\\
.\r\'
回覆:'250 2.0.0 OK 1240421143 4sm652580yxq.48\r\''
回覆:retcode(250); Msg:2.0.0 OK 1240421143 4sm652580yxq.48
data:(250,'2.0.0 OK 1240421143 4sm652580yxq.48')


解决方案

我认为GMail SMTP服务器会对您连接的IP地址进行反向DNS查找,如果没有找到域,则拒绝连接。这是为了避免垃圾邮件发件人使用他们的SMTP服务器作为开放中继。


Here's my script:

#!/usr/bin/python

import smtplib
msg = 'Hello world.'

server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
server.ehlo()
server.starttls()
server.ehlo()
server.login('myname@gmail.com','mypass')
server.sendmail('myname@gmail.com','somename@somewhere.com',msg)
server.close()

I'm just trying to send an email from my gmail account. The script uses starttls because of gmail's requirement. I've tried this on two web hosts, 1and1 and webfaction. 1and1 gives me a 'connection refused' error and webfaction reports no error but just doesn't send the email. I can't see anything wrong with the script, so I'm thinking it might be related to the web hosts. Any thoughts and comments would be much appreciated.

EDIT: I turned on debug mode. From the output, it looks like it sent the message successfully...I just never receive it.

send: 'ehlo web65.webfaction.com\r\n'
reply: '250-mx.google.com at your service, [174.133.21.84]\r\n'
reply: '250-SIZE 35651584\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250 PIPELINING\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [174.133.21.84]
SIZE 35651584
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
PIPELINING
send: 'STARTTLS\r\n'
reply: '220 2.0.0 Ready to start TLS\r\n'
reply: retcode (220); Msg: 2.0.0 Ready to start TLS
send: 'ehlo web65.webfaction.com\r\n'
reply: '250-mx.google.com at your service, [174.133.21.84]\r\n'
reply: '250-SIZE 35651584\r\n'
reply: '250-8BITMIME\r\n'
reply: '250-AUTH LOGIN PLAIN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250 PIPELINING\r\n'
reply: retcode (250); Msg: mx.google.com at your service, [174.133.21.84]
SIZE 35651584
8BITMIME
AUTH LOGIN PLAIN
ENHANCEDSTATUSCODES
PIPELINING
send: 'AUTH PLAIN *****\r\n'
reply: '235 2.7.0 Accepted\r\n'
reply: retcode (235); Msg: 2.7.0 Accepted
send: 'mail FROM:<myname@gmail.com> size=12\r\n'
reply: '250 2.1.0 OK 4sm652580yxq.48\r\n'
reply: retcode (250); Msg: 2.1.0 OK 4sm652580yxq.48
send: 'rcpt TO:<myname@gmail.com>\r\n'
reply: '250 2.1.5 OK 4sm652580yxq.48\r\n'
reply: retcode (250); Msg: 2.1.5 OK 4sm652580yxq.48
send: 'data\r\n'
reply: '354  Go ahead 4sm652580yxq.48\r\n'
reply: retcode (354); Msg: Go ahead 4sm652580yxq.48
data: (354, 'Go ahead 4sm652580yxq.48')
send: 'Hello world.\r\n.\r\n'
reply: '250 2.0.0 OK 1240421143 4sm652580yxq.48\r\n'
reply: retcode (250); Msg: 2.0.0 OK 1240421143 4sm652580yxq.48
data: (250, '2.0.0 OK 1240421143 4sm652580yxq.48')

解决方案

I think that the GMail SMTP server does a reverse DNS lookup on the IP address that you connect from, and refuses the connection if no domain can be found. This is to avoid spammer from using their SMTP server as an open relay.

这篇关于smtplib和gmail - python脚本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆