-
Notifications
You must be signed in to change notification settings - Fork 0
/
telegraph_smtp.py
81 lines (66 loc) · 2.51 KB
/
telegraph_smtp.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import json
import base64
import os
import email.parser
import email.policy
import paho.mqtt.client as mqtt
from aiosmtpd.controller import Controller
def on_connect(client, userdata, flags, rc):
"""
Handles subscribing to topics when a connection to MQTT is established
:return: None
"""
print("Connected with result code "+str(rc), flush=True)
class TelegraphHandler:
def __init__(self, mqtt: mqtt.Client):
self.mqtt = mqtt
async def handle_RCPT(self, server, session, envelope, address, rcpt_options):
if address != '[email protected]':
return '553 invalid email'
envelope.rcpt_tos.append(address)
return '250 OK'
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')
print(envelope.content.decode('utf8', errors='replace'))
print('End of message', flush=True)
message = email.parser.BytesParser(policy=email.policy.default).parsebytes(envelope.content)
message_body = message.get_body(preferencelist=('html', 'plain'))
message_content = message_body.get_content()
attachments = {}
for part in message.walk():
content_id = part.get("Content-ID")
if content_id is None:
continue
content_id = content_id.strip()
if not content_id.startswith("<"):
continue
if not content_id.endswith(">"):
continue
attachments[content_id[1:-1]] = {
"type": part.get_content_type(),
"data": base64.b64encode(part.get_content()).decode()
}
data = json.dumps({
"subject": message["subject"],
"from": message["from"],
"message": message_content,
"attachments": attachments
})
self.mqtt.publish("printer/print", data)
return '250 Message accepted for delivery'
if __name__ == "__main__":
client = mqtt.Client()
client.on_connect = on_connect
client.connect(os.getenv("MQTT_SERVER", "172.30.2.3"), 1883, 60)
controller = Controller(TelegraphHandler(client), hostname='0.0.0.0', port=25)
controller.start()
print("SMTP server started", flush=True)
while True:
try:
client.loop()
except (KeyboardInterrupt, SystemExit):
print("Bye!", flush=True)
break
controller.stop()