【问题标题】:Sending mail through Python SMTP Server通过 Python SMTP 服务器发送邮件
【发布时间】:2013-12-10 23:06:31
【问题描述】:

我正在尝试构建一个小型 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()

【问题讨论】:

  • 为什么要编写自己的 MTA?
  • 能够自定义并方便地存储所有内容
  • 在下面查看我的答案。许多优秀的 MTA 实现提供了相当多的配置选项。
  • 虽然我同意你不应该编写自己的 MTA,但 smtpd.PureProxy 类。 source code 显示了如何。

标签: python smtp smtpd


【解决方案1】:

如果您真的想要这样做 然后查看 Twisted 示例:

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

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

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

【讨论】:

  • 没问题。重新发明轮子或轮子毫无意义。
猜你喜欢
  • 2011-02-07
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 2016-06-15
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
相关资源
最近更新 更多