Browse Source

do not use aiostmpd controller

Son NK 4 years ago
parent
commit
d8a415c00a
1 changed files with 22 additions and 7 deletions
  1. 22 7
      email_handler.py

+ 22 - 7
email_handler.py

@@ -44,6 +44,7 @@ from io import BytesIO
 from smtplib import SMTP
 from smtplib import SMTP
 from typing import List, Tuple
 from typing import List, Tuple
 
 
+import aiosmtpd
 import aiospamc
 import aiospamc
 import arrow
 import arrow
 import spf
 import spf
@@ -1271,16 +1272,30 @@ class MailHandler:
 
 
 
 
 if __name__ == "__main__":
 if __name__ == "__main__":
-    controller = Controller(MailHandler(), hostname="0.0.0.0", port=20381)
-
-    controller.start()
-    LOG.d("Start mail controller %s %s", controller.hostname, controller.port)
-
     if LOAD_PGP_EMAIL_HANDLER:
     if LOAD_PGP_EMAIL_HANDLER:
         LOG.warning("LOAD PGP keys")
         LOG.warning("LOAD PGP keys")
         app = create_app()
         app = create_app()
         with app.app_context():
         with app.app_context():
             load_pgp_public_keys()
             load_pgp_public_keys()
 
 
-    while True:
-        time.sleep(2)
+    handler = MailHandler()
+    loop = asyncio.new_event_loop()
+    asyncio.set_event_loop(loop)
+
+    def factory():
+        return aiosmtpd.smtp.SMTP(handler, enable_SMTPUTF8=True)
+
+    server = loop.run_until_complete(
+        loop.create_server(factory, host="0.0.0.0", port=20381)
+    )
+
+    try:
+        loop.run_forever()
+    except KeyboardInterrupt:
+        pass
+
+    # Close the server
+    LOG.info("Close SMTP server")
+    server.close()
+    loop.run_until_complete(server.wait_closed())
+    loop.close()