【问题标题】:How to create a postfix-like server with python smtpd如何使用 python smtpd 创建类似后缀的服务器
【发布时间】:2019-09-04 17:28:38
【问题描述】:

关于 python smtpd 库,我尝试覆盖 process_message 方法,但是当我尝试与客户端连接并将消息发送到 gmail 帐户时,它只是在控制台上打印消息,但我想要它实际上是在本地机器上发出类似后缀的消息。我应该如何做到这一点?

我用谷歌搜索过 smtpd,但找不到太多有用的消息

import smtpd
import asyncore

class CustomSMTPServer(smtpd.SMTPServer):

    def process_message(self, peer, mailfrom, rcpttos, data, **kwargs):
        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()

【问题讨论】:

  • 您是在尝试发送电子邮件还是接收电子邮件?上面的代码是用来接收消息的。
  • 发送电子邮件,但不使用 smtplib 连接和验证其他 smtp 服务器(如 gmail),我希望此代码本身是 smtp 服务器,当它收到电子邮件时可以将其传输到其他 smtp服务器

标签: python postfix-mta smtpd


【解决方案1】:

引用Robert Putt's answer,您将难以交付能力。您最好的解决方案是在本地托管一个 SMTP 服务器(当然,最好的解决方案是使用 AmazonSES 或类似 MailGun 的 API)。 DigialOcean 这里有一个很好的教程。然后,您可以使用以下 Python 代码发送电子邮件。

import smtplib

sender = 'no_reply@mydomain.com'
receivers = ['person@otherdomain.com']

message = """From: No Reply <no_reply@mydomain.com>
To: Person <person@otherdomain.com>
Subject: Test Email

This is a test e-mail message.
"""

try:
    smtpObj = smtplib.SMTP('localhost')
    smtpObj.sendmail(sender, receivers, message)         
    print("Successfully sent email")
except SMTPException:
    print("Error: unable to send email")

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2020-10-28
    相关资源
    最近更新 更多