如何使用Inbox.py通过Telnet发送电子邮件?

时间:2015-02-09 00:48:29

标签: python email smtp telnet

我想使用Inbox.py创建本地SMTP服务器,并通过Telnet从中发送电子邮件。

由于回购缺乏一个完整的例子,我在回购问题中发现有人had specified a full example,但我似乎无法让它发挥作用。

他们指定此代码:

"""
Proxy smtp to a starttls server with authentication, from a local
connection.
"""
from inbox import Inbox
from smtplib import SMTP

inbox = Inbox()

SMTP_HOST = 'mail.example.com'
SMTP_USERNAME = 'username'
SMTP_PASSWORD = 'password'

@inbox.collate
def handle(to, sender, body):
    """
    Forward a message via an authenticated SMTP connection with
    starttls.
    """
    conn = SMTP(SMTP_HOST, 25, 'localhost')

    conn.starttls()
    conn.ehlo_or_helo_if_needed()
    conn.login(SMTP_USERNAME, SMTP_PASSWORD)
    conn.sendmail(sender, to, body)
    conn.quit()

inbox.serve(address='0.0.0.0', port=4467)

但我不确定到底在做什么。我只想通过SMTP命令通过Telnet发送电子邮件。

如果我telnet 0.0.0.0 4467,则在指定数据时失败,请输入.,说明:

error: uncaptured python exception, closing channel <smtpd.SMTPChannel connected 127.0.0.1:52779 at 0x10864f950> (<type 'exceptions.TypeError'>:handle() got an unexpected keyword argument 'subject' [/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py|read|83] [/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asyncore.py|handle_read_event|449] [/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/asynchat.py|handle_read|158] [/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/smtpd.py|found_terminator|181] [/Users/Desktop/inbox.py|process_message|18])

1 个答案:

答案 0 :(得分:1)

您可以考虑专门为此目的使用telnetlib标准库。

此处的问题可能是handle()无法理解'主题:',当您尝试使用telnet所需的方式发送邮件时,尤其是关于通常会跟随它的回车。

您可以尝试的其他方法是在def句柄中加入“主题”。