smtplib 和 gmail - python 脚本问题
问题描述
这是我的脚本:
#!/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()
我只是想从我的 gmail 帐户发送一封电子邮件.由于 gmail 的要求,该脚本使用 starttls.我已经在两个网络主机上尝试过这个,1and1 和 webfaction.1and1 给了我一个连接被拒绝"的错误,webfaction 没有报告错误,但只是不发送电子邮件.我看不出脚本有什么问题,所以我认为它可能与网络主机有关.任何想法和意见将不胜感激.
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.
我打开了调试模式.从输出来看,它似乎成功发送了消息……我只是没有收到它.
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
'
reply: '250-mx.google.com at your service, [174.133.21.84]
'
reply: '250-SIZE 35651584
'
reply: '250-8BITMIME
'
reply: '250-STARTTLS
'
reply: '250-ENHANCEDSTATUSCODES
'
reply: '250 PIPELINING
'
reply: retcode (250); Msg: mx.google.com at your service, [174.133.21.84]
SIZE 35651584
8BITMIME
STARTTLS
ENHANCEDSTATUSCODES
PIPELINING
send: 'STARTTLS
'
reply: '220 2.0.0 Ready to start TLS
'
reply: retcode (220); Msg: 2.0.0 Ready to start TLS
send: 'ehlo web65.webfaction.com
'
reply: '250-mx.google.com at your service, [174.133.21.84]
'
reply: '250-SIZE 35651584
'
reply: '250-8BITMIME
'
reply: '250-AUTH LOGIN PLAIN
'
reply: '250-ENHANCEDSTATUSCODES
'
reply: '250 PIPELINING
'
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 *****
'
reply: '235 2.7.0 Accepted
'
reply: retcode (235); Msg: 2.7.0 Accepted
send: 'mail FROM:<myname@gmail.com> size=12
'
reply: '250 2.1.0 OK 4sm652580yxq.48
'
reply: retcode (250); Msg: 2.1.0 OK 4sm652580yxq.48
send: 'rcpt TO:<myname@gmail.com>
'
reply: '250 2.1.5 OK 4sm652580yxq.48
'
reply: retcode (250); Msg: 2.1.5 OK 4sm652580yxq.48
send: 'data
'
reply: '354 Go ahead 4sm652580yxq.48
'
reply: retcode (354); Msg: Go ahead 4sm652580yxq.48
data: (354, 'Go ahead 4sm652580yxq.48')
send: 'Hello world.
.
'
reply: '250 2.0.0 OK 1240421143 4sm652580yxq.48
'
reply: retcode (250); Msg: 2.0.0 OK 1240421143 4sm652580yxq.48
data: (250, '2.0.0 OK 1240421143 4sm652580yxq.48')
解决方案
我认为 GMail SMTP 服务器会对您连接的 IP 地址进行反向 DNS 查找,如果找不到域,则拒绝连接.这是为了避免垃圾邮件发送者将他们的 SMTP 服务器用作开放中继.
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.
相关文章