如何在smtpd.SMTPServer.process_message中拒绝收件人?

Car*_*ver 1 python email smtp

假设您在重写的类中处理消息,例如:

class MailProcessorServer(smtpd.SMTPServer):
  def process_message(self, peer, sender, rcpttos, data):
    badrecipients = []
    for rcpt in rcpttos:
      badrecipients.append(rcpt)

    #Here I want to warn the sender via a bounced email
    # that the recipient does not exist
    raise smtplib.SMTPRecipientsRefused(badrecipients)
    #but this just crashes the process and eventually the sender times out,
    # not good enough
Run Code Online (Sandbox Code Playgroud)

我只想立即回复发件人.相反,发送服务(比如GMail)最终会放弃,并在几小时后警告用户.该文件似乎很稀疏.

Ale*_*lli 5

正如仅在消息来源中记录的那样(对不起!),其process_message规格包括:

对于正常的"250 Ok"响应,此函数应返回None; 否则它以RFC 821格式返回所需的响应字符串.

所以你可以"返回'554个坏收件人%s'%badrecipients"而不是使用该raise声明 - 不完全令人满意(没有正确解释好坏的混合,RFC 821 应该返回'250 Ok'但也会在稍后发送警告邮件)但它似乎确实是你正在寻找的"立即反弹"效果raise.