2

我正在尝试构建一个小型 SMTP 服务器,通过它我可以发送一些消息。查看smtpd库发现有东西。但我只能创建一个服务器来读取收到的电子邮件,但从未将其发送到请求的地址。

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):

def process_message(self, peer, mailfrom, rcpttos, data):
    print 'Receiving message from:', peer
    print 'Message addressed from:', mailfrom
    print 'Message addressed to  :', rcpttos
    print 'Message length        :', len(data)
    return

server = CustomSMTPServer(('127.0.0.1', 1025), None)

asyncore.loop()

客户:

import smtplib
import email.utils
from email.mime.text import MIMEText

# Create the message
msg = MIMEText('This is the body of the message.')
msg['To'] = email.utils.formataddr(('Recipient', 'recipient@example.com'))
msg['From'] = email.utils.formataddr(('Author', 'author@example.com'))
msg['Subject'] = 'Simple test message'

server = smtplib.SMTP('127.0.0.1', 1025)
server.set_debuglevel(True) # show communication with the server
try:
    server.sendmail('author@example.com', ['myadress@gmail.com'], msg.as_string())
finally:
    server.quit()
4

1 回答 1

3

如果您真的想这样做,请查看 Twisted 示例:

http://twistedmatrix.com/documents/current/mail/examples/index.html#auto0

我真的不建议您编写自己的 MTA(邮件传输代理),因为这是一项复杂的任务,需要担心许多边缘情况和标准。

使用现有的 MTA,例如 Postfix、Exim 或 Sendmail。

于 2013-12-10T15:06:31.517 回答