def factory(self): """``CustomIdentController`` factory. Overrides ``super.factory()``. Creates an aiosmtpd server object. Returns: Returns the server object. """ server = Server(self.handler) server.hostname = self.ident_hostname server.__ident__ = self.ident return server
def getter(test_obj: "_TestProxyProtocolCommon", *args, **kwargs): kwargs["loop"] = loop kwargs["proxy_protocol_timeout"] = proxy_timeout protocol = SMTPServer(handler, *args, **kwargs) protocol.connection_made(transport) def runner(stop_after: float = DEFAULT_AUTOCANCEL): loop.call_later(stop_after, protocol._handler_coroutine.cancel) with suppress(asyncio.CancelledError): loop.run_until_complete(protocol._handler_coroutine) test_obj.protocol = protocol test_obj.runner = runner test_obj.transport = transport
def factory(self): """Allow subclasses to customize the handler/server creation.""" return SMTP(self.handler, loop=self.loop, data_size_limit=config.max_data_size, tls_context=get_tls_context() if config.use_tls else None, require_starttls=config.use_tls)
def factory(self): """Allow subclasses to customize the handler/server creation.""" ssl = self.ssl_context if self.startSSL: ssl = get_server_context(self.certfile, self.keyfile) return SMTP(self.handler, enable_SMTPUTF8=self.enable_SMTPUTF8, require_starttls=self.startSSL, authenticate=self.authenticate, tls_context=ssl, decode_data=True, timeout=self.timeout)
def get_server(self): return SMTP( handler=self, hostname=self.host, enable_SMTPUTF8=True, loop=self.loop, )
async def test_handle_DATA(self): self.handler.verify_dkim = False server = SMTP(self.handler) response = await self.handler.handle_DATA( self.server, Session(server.loop), self.envelope ) assert response == '250 Message accepted for delivery'
async def test_handle_DATA_fail_dkim(self): self.handler.verify_dkim = True server = SMTP(self.handler) response = await self.handler.handle_DATA( self.server, Session(server.loop), self.envelope ) assert response == '550 DKIM validation failed'
def factory(self) -> SMTP: use_starttls = self.config.use_starttls context = self._get_ssl_context() if use_starttls else None return SMTP(handler=self.handler, require_starttls=self.config.use_starttls, auth_required=self.config.enforce_auth, auth_require_tls=self.config.auth_require_tls, tls_context=context, authenticator=self._authenticator)
def setup_method(self, method): self.handler = ExampleHandler('gmail.com', verify_spf=True) self.envelope = Envelope() self.envelope.content = b"""From: Cate Person <*****@*****.**> To: Dave Person <*****@*****.**> Subject: A test Message-ID: <bee> Hi Dave, this is Cate.""" self.server = SMTP(self.handler)
def factory(self): context = None if settings.SMTP_SSL_ENABLE: context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) context.load_cert_chain(settings.SMTP_SSL_CERTIFICATE, settings.SMTP_SSL_KEY) return SMTP( self.handler, require_starttls=False, tls_context=context, )
async def test_handle_MAIL_no_spf(self): server = SMTP(self.handler) session = Session(server.loop) session.peer = ['127.0.0.1', 65000] response = await self.handler.handle_MAIL( self.server, session, self.envelope, '*****@*****.**', [] ) assert response == '550 SPF validation failed'
async def test_handle_RCPT_decline(self): # In this test, the message data comes in as bytes. server = SMTP(self.handler) response = await self.handler.handle_RCPT( self.server, Session(server.loop), self.envelope, '*****@*****.**', [] ) assert response == '550 not relaying to that domain'
async def test_handle_RCPT(self): # In this test, the message data comes in as bytes. server = SMTP(self.handler) response = await self.handler.handle_RCPT( self.server, Session(server.loop), self.envelope, '*****@*****.**', [] ) assert response == '250 OK'
def smtp_server(event_loop, unused_tcp_port, handler, server_tls_context, tls): smtp = SMTP(handler, require_starttls=tls, tls_context=server_tls_context) server = event_loop.run_until_complete( event_loop.create_server(lambda: smtp, port=unused_tcp_port)) yield smtp server.close() event_loop.run_until_complete(server.wait_closed()) # The server leaves tasks running so cancel them for task in all_tasks(event_loop): task.cancel() with suppress(Exception): event_loop.run_until_complete(task)
async def test_handle_MAIL_spf(self): server = SMTP(self.handler) session = Session(server.loop) session.peer = ['209.85.220.69', 65000] session.host_name = 'gmail.com' response = await self.handler.handle_MAIL( self.server, session, Envelope(), '*****@*****.**', [] ) assert response == '250 OK'
def factory(self): """Used to define greater data size limit""" return SMTP(self.handler, data_size_limit=99999999999, decode_data=False)
async def handle_NOOP(self, server: Server, session: Sess_, envelope, arg): self.ssl_existed = session.ssl is not None self.result = server.eof_received() return "250 OK"
def test_special_max_command_size_limit(self): server = Server(Sink()) server.command_size_limits['DATA'] = 1024 self.assertEqual(server.max_command_size_limit, 1024)
def factory(self): server = Server(self.handler) server.__ident__ = 'Identifying SMTP v2112' return server
def _get_protocol(self, *args, **kwargs): protocol = Server(*args, loop=self.loop, **kwargs) protocol.connection_made(self.transport) return protocol
def factory(self): return SMTP(self.handler, ssl_context=get_context())
def factory(self): return SMTP(self.handler, require_starttls=False, tls_context=get_context())
from smtpx import CrazySrvHandler from web import web_start from aiosmtpd.controller import Controller from aiosmtpd.smtp import SMTP import configparser if __name__ == "__main__": cf = configparser.ConfigParser() cf.read("cfg.ini") smtpd_host = cf.get("smtpd", "host") smtpd_port = cf.getint("smtpd", "port") rest_host = smtpd_host rest_port = cf.getint("rest", "port") handler = CrazySrvHandler() controller = Controller(handler, hostname=smtpd_host, port=smtpd_port) controller.factory = lambda: SMTP(handler, enable_SMTPUTF8=True) try: controller.start() web_start(rest_host, rest_port) except KeyboardInterrupt: print("Shutting down") finally: controller.stop()
def test_special_max_command_size_limit(self): server = Server(Sink()) server.command_size_limits["DATA"] = 1024 assert server.max_command_size_limit == 1024
def factory(self): """Allow subclasses to customize the handler/server creation.""" return SMTP(self.handler)
handler = MailHandler() @handler.default_route def process(message, dkim, spf): send_message(message, dkim, spf) if __name__ == "__main__": loop = asyncio.get_event_loop() ssl_context = None if os.path.exists("keys/host.crt") and os.path.exists("keys/host.key"): ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain("keys/host.crt", "keys/host.key") controller = Controller(handler, hostname=SMTP_HOST, port=SMTP_PORT) controller.factory = lambda: SMTP(handler, enable_SMTPUTF8=True, tls_context=ssl_context) controller.start() try: loop.run_forever() except KeyboardInterrupt: pass finally: controller.stop() loop.stop() loop.close()
def factory(self): """Allow subclasses to customize the handler/server creation.""" return SMTP(self.handler, enable_SMTPUTF8=self.enable_SMTPUTF8, **self.server_kwargs)
def factory(self): return SMTP(self.handler, decode_data=True, auth_required=True, auth_require_tls=False, auth_method=auth)
def factory(self): return SMTP(self.handler)
def factory(self): server = Server(self.handler) server.hostname = self.ident_hostname server.__ident__ = self.ident return server