0

我已经“成功”建立了一个 SMTP 服务器。该代码可以正常连接到 SMTP 客户端。但是它既不能接收电子邮件也不能发送它。我尝试了各种测试服务器以及标准 gmail/yahoo 等。这是代码:

    # Copyright 2014-2021 The aiosmtpd Developers
# SPDX-License-Identifier: Apache-2.0

import asyncio
from asyncio.base_events import Server
import logging
import aiosmtpd
from aiosmtpd.controller import DEFAULT_READY_TIMEOUT, Controller
import ssl
from aiosmtpd.smtp import Envelope, Session
from smtplib import SMTP as SMTPCLient


context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain('cert.pem', 'privkey.pem')


class ExampleHandler():
    async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
        if address.endswith('@example.com'):
            print('not relaying to that domain bro :(')
            return '550 not relaying to that domain'
        envelope.rcpt_tos.append(address)
        print(address+" "+"is added to rcpt_tos")
      

        # Make an envelope for the recipient with the same content.
        
        
        
        return '250 OK'



    # async def handle_EHLO(self, server, session, envelope):
    #     print('EHLO from %s' % envelope.mail_from)
    #     return '250-Hello, how are you?\n250-I am fine\n250 HELP'    


    async def handle_DATA(self, server, session, envelope):
        print('Message from %s' % envelope.mail_from)
        print('Message for %s' % envelope.rcpt_tos)
        print('Message data:\n')
        for ln in envelope.content.decode('utf8', errors='replace').splitlines():
            print(f'> {ln}'.strip())
        print()
        print('End of message')
        # Dump the contents of envelope.content to a file.
        fi=open('./mailbox/firstletter.txt','w')
        fi.write(envelope.content.decode('utf8', errors='replace'))
        fi.close()




        # print everything in DATA.
        # Send the envelope to the recipient.


        return '250 Message will be delivered'
    #Define Relay server.
  
   




async def amain(loop):
    cont = Controller(ExampleHandler(),hostname='x.x.x.x', port=25, server_hostname='Galam Limited',ready_timeout=5000) 
    # Combining ExampleHandler and Controller into a single Controller.
   
    cont.start()


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    loop = asyncio.get_event_loop()
    loop.create_task(amain(loop=loop))
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
  

您可以测试服务器的可达性。我被卡住了,花了整整 2 天无济于事。问题绝对不是连通性,我把 25 端口打开了。确保godaddy也没有外部问题。任何帮助将不胜感激。

编辑:1 Wire Shark 数据的快速峰值显示,当我运行客户端脚本时,绝对没有数据包被传输到外部。这是我用于测试的 clinet 脚本。

from smtplib import SMTP as Client
from aiosmtpd.controller import Controller
class controller:
    hostname='192.168.1.33'
    port=25
client = Client(controller.hostname, controller.port)
r = client.sendmail('a@galam.in', ['tester@192.168.1.200'], """\
From: Anne Person <anne@galam.in>
To: Bart Person <tester@192.168.1.200>
Subject: A test
Message-ID: <ant>
Hi Bart, this is Anne.
""")
4

2 回答 2

1

在您的代码中,cont.start()启动一个 SMTP 服务器,该服务器在端口 25 上接收消息并使用 handle_RCPT 和 handle_DATA 处理它们,但是您还没有编写任何代码来实际将电子邮件转发到远程主机。这就是为什么发送到您的服务器的电子邮件不会出现在任何人的收件箱中的原因——服务器实现不是一个完整的电子邮件中继。

如果您想通过 Internet 向 发送电子邮件anne@galam.in,那么您应该联系负责该galam.in域的 SMTP 服务器,而不是您正在使用 aiosmtpd 运行的 SMTP 服务器。对于这个任务,您不需要运行SMTP 服务器,因为据说 Internet 上已经有一个 SMTP 服务器正在运行galam.in;相反,您只需要一个SMTP 客户端组件,例如用于测试服务器的 smtplib 脚本。使用 SMTP 发送电子邮件不是 aiosmtpd 以任何方式参与的事情。

进一步阅读:电子邮件§维基百科上的操作。另请参阅 SO 问题“python smtp 服务器”

于 2021-12-27T12:20:36.190 回答
0

SMTP 250 代码表示已建立成功连接,但是您发送邮件的远程主机可能已将发送邮件的域归类为不合法的域。

如果您的域未经过身份验证/验证,则可能会发生这种情况。您可以通过受信任的 SMTP 服务(如sendgrid)中继您的消息

您还可以通过将邮件从您的服务发送到 来检查您的域是否经过验证check-auth@verifier.port25.com。Port25 是一个自动化工具,可以验证您的 DNS 记录、SPF 记录等。

希望这对你有用!

于 2021-12-25T04:45:52.827 回答