使用python将邮件发送到IP地址

时间:2014-09-08 21:43:56

标签: python email ip-address

所以我试图通过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 postfix服务器(不幸地无法访问外部世界)并将邮件发送到此服务器的用户@ [IP-ADDRESS]邮件到达。可能另一个迹象表明,在上面提到的问题情况下,接收器的smtp服务器只是不允许IP地址作为目的地。

Edit2:/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 deamon表示它收到了错误的语法,即使在我通过我的脚本发送的握手中它说250 Ok ..所以基本上我认为收件人SMTP拒绝它。 Thx家伙

1 个答案:

答案 0 :(得分:1)

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

除非您以某种方式与服务器运营商验证服务器将user@[ip] - 已发送消息发送到您期望的域,否则您不应该认为它会发生。

相关问题