-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.py
63 lines (46 loc) · 1.44 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3.6
# -*- coding: utf-8 -*-
import asyncio
import logging
import os
import ssl
import sys
from aiosmtpd.controller import Controller
from aiosmtpd.smtp import SMTP
from server import ExampleHandler
__version__ = '0.0.1'
__author__ = "Denys Ivanets"
__email__ = "[email protected]"
logging.basicConfig(
format='%(asctime)s %(name)-12s %(levelname)-7s %(message)s',
level=logging.DEBUG,
stream=sys.stdout
)
log = logging.getLogger()
if __name__ == '__main__':
host = os.getenv('SMTP_HOST', '*')
port = os.getenv('SMTP_PORT', '25')
accept_host = os.getenv('ACCEPT_HOST')
ssl_keys = os.getenv('SSL_KEYS')
loop = asyncio.get_event_loop()
handler = ExampleHandler(accept_host)
ssl_context = None
if ssl_keys:
ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
ssl_context.load_cert_chain(ssl_keys + '.crt', ssl_keys + '.key')
controller = Controller(handler,
hostname=host,
port=port)
controller.factory = lambda: SMTP(handler,
enable_SMTPUTF8=True,
tls_context=ssl_context)
controller.start()
log.info("SMTP server started on %s:%s", host, port)
try:
loop.run_forever()
except KeyboardInterrupt:
print("Shutting down")
finally:
controller.stop()
loop.stop()
loop.close()