【问题标题】:Send mail to an IP Address using python使用python将邮件发送到IP地址
【发布时间】:2014-09-08 21:43:56
【问题描述】:

所以我正在尝试通过 python 脚本发送邮件。使用接收器地址“user@domain.tld”的常用格式可以正常工作。当我现在尝试将脚本与接收器 "user@[IP-Address] 一起使用时,我的所有调试输出看起来都很好,并且 sendmail 方法有效,但从未收到邮件。我通过 dig 从我的终端获取 IP 地址.

这是我期望接收器作为参数的方法(删除一些不重要的东西并混淆真实地址/凭据)

def sendmail(receiver):
    msg = MIMEText('This is the body of the mail.')
    msg['From'] = email.utils.formataddr(('me', myaddr))
    msg['To'] = email.utils.formataddr(('me', receiver))
    msg['Subject'] = "Python Mail Script"


    server = smtplib.SMTP(smtpServer, smtpPort)
    try:
        server.set_debuglevel(True)

        # identify ourselves, prompting server for supported features
        server.ehlo()

        # If we can encrypt this session, do it
        if server.has_extn('STARTTLS'):
            server.starttls()
            server.ehlo() # re-identify ourselves over TLS connection

        server.login("me@...", "...")
        server.sendmail("me@...", toAddr,  msg.as_string())
    finally:
        server.quit()

向 IP 地址发送消息时得到的输出是(我再次混淆了 smtp 服务器和邮件/IP 地址):

send: 'ehlo [127.0.1.1]\r\n'
reply: '250-mail.example.de\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 102400000\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250-AUTH=PLAIN LOGIN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 DSN\r\n'
reply: retcode (250); Msg: mail.example.de
PIPELINING
SIZE 102400000
VRFY
ETRN
STARTTLS
AUTH PLAIN LOGIN
AUTH=PLAIN LOGIN
ENHANCEDSTATUSCODES
8BITMIME
DSN
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 [127.0.1.1]\r\n'
reply: '250-mail.example.de\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 102400000\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-AUTH PLAIN LOGIN\r\n'
reply: '250-AUTH=PLAIN LOGIN\r\n'
reply: '250-ENHANCEDSTATUSCODES\r\n'
reply: '250-8BITMIME\r\n'
reply: '250 DSN\r\n'
reply: retcode (250); Msg: mail.example.de
PIPELINING
SIZE 102400000
VRFY
ETRN
AUTH PLAIN LOGIN
AUTH=PLAIN LOGIN
ENHANCEDSTATUSCODES
8BITMIME
DSN
send: 'AUTH PLAIN AHRob21hc0B0b3Jh6feldi5kZQBiYWdpbmVy\r\n'
reply: '235 2.7.0 Authentication successful\r\n'
reply: retcode (235); Msg: 2.7.0 Authentication successful
send: 'mail FROM:<me@example.de> size=234\r\n'
reply: '250 2.1.0 Ok\r\n'
reply: retcode (250); Msg: 2.1.0 Ok
send: 'rcpt TO:<me@[IP-ADDRESS]>\r\n'
reply: '250 2.1.5 Ok\r\n'
reply: retcode (250); Msg: 2.1.5 Ok
send: 'data\r\n'
reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
send: 'Content-Type: text/plain; charset="us-ascii"\r\nMIME-Version: 1.0\r\nContent-Transfer-Encoding: 7bit\r\nFrom: me <me@example.de>\r\nTo: me <me@[IP-ADDRESS]>\r\nSubject: Python Mail Script\r\n\r\nThis is the body of the mail.\r\n.\r\n'
reply: '250 2.0.0 Ok: queued as 4C5A78560196\r\n'
reply: retcode (250); Msg: 2.0.0 Ok: queued as 4C5A78560196
data: (250, '2.0.0 Ok: queued as 4C5A78560196')
send: 'quit\r\n'
reply: '221 2.0.0 Bye\r\n'
reply: retcode (221); Msg: 2.0.0 Bye

有人发现任何错误或错误吗?

编辑:在大学使用自己的 smtp 后缀服务器(不幸的是无法访问外部世界)并将邮件发送到邮件到达的这台服务器的用户@[IP-ADDRESS]。可能另一个迹象表明,在上述有问题的情况下,接收方的 smtp 服务器不允许 IP 地址作为目标。

编辑2:/var/log/mail.log

mail.log:Sep  9 00:34:06 mail postfix/qmgr[4854]: A30168560199: from=<me@example.de>, size=1197, nrcpt=1 (queue active)

mail.log:Sep  9 00:34:06 mail postfix/smtp[19355]: A30168560199: to=<me@[IP-ADDRESS]>, relay=none, delay=314, delays=313/0.04/0.01/0, dsn=4.4.1, status=deferred (connect to IP-ADDRESS[IP-ADDRESS]:25: Connection refused)

mail.log:Sep  9 00:38:31 mail postfix/smtpd[19907]: warning: Illegal address syntax from my_host_where_the_python_script_runs[IP of me] in RCPT command: <me@IP-ADDRESS>

所以从我使用的 SMTP 服务器到接收服务器的连接被拒绝,4 分钟后 smtp 守护进程说它收到了错误的语法,即使在我通过脚本发送的握手中它说 250 Ok .. .所以基本上我认为收件人SMTP拒绝它。 谢谢各位

【问题讨论】:

  • 虽然这在技术上受 RFC 支持,但电子邮件主机可能根本不接受以这种方式发送的邮件。这是一种可能的垃圾邮件策略,可将消息发送到接受入站 SMTP 连接的公共 IP,而实际上并不知道有效的目标域。
  • 邮件日志显示什么? (如果是 unix,/var/log/maillog)
  • 问题是我目前无法访问用于发送这些邮件的 smtp 服务器。联系管理员,以便他可以将日志发送给我。 @admdrew 你也得到了一个有效的观点,这可能是一个问题
  • @admdrew,检查一下:tools.ietf.org/html/rfc822.html#appendix-C.5.1 似乎 IP 地址不再有效?
  • @user3885927 在我的更新中,使用 IP 地址而不是域在理论上应该绝对有效。

标签: python email ip-address


【解决方案1】:

电子邮件地址中的域名很像网站的域名 - 单个 IP 地址上的单个服务器通常负责许多不同的域名,因此在这些情况下,您必须在请求中提供名称。例如,1.2.3.4 的邮件服务器可以同时为abc.comxyz.com 提供服务。如果您说RCPT TO: &lt;joe@[1.2.3.4]&gt;,服务器不知道您是在尝试访问joe@abc.com 还是joe@xyz.com。服务器可能会任意选择一个(并猜错),接受并丢弃该消息,或者拒绝该消息。

除非您以某种方式与服务器运营商确认服务器会将user@[ip] 寻址的消息传递到您期望的域,否则您不应假设它会。

【讨论】:

  • The server might arbitrarily pick one我真的很好奇这是否会是这样。
猜你喜欢
  • 2012-01-12
  • 2012-01-21
  • 2014-04-27
  • 1970-01-01
  • 2014-04-11
  • 2011-11-12
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多