Repository navigation
Expand file tree
/
Copy pathmqtt2mail.py
More file actions
executable file
·54 lines (44 loc) · 1.87 KB
/
Copy pathmqtt2mail.py
File metadata and controls
executable file
·54 lines (44 loc) · 1.87 KB
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
#!/usr/bin/python3
import paho.mqtt.client as mqtt
import ssl
import smtplib
import email
sender = ['oliver.hahm@fb2.fra-uas.de']
#sender = 'oleg+mqttforwarder@dahahm.de'
#receivers = ['oliver.hahm@fb2.fra-uas.de']
receivers = ['oleg@dahahm.de']
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("thesis/request/+")
# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload))
try:
message = """From: MQTT-Forwarder <oliver.hahm@fb2.fra-uas.de>
To: Oliver Hahm <oleg@dahahm.de>
Subject: New thesis application received
Date: %s
""" % (email.utils.formatdate(localtime=True))
smtpObj = smtplib.SMTP('mail.stillroot.org')
#smtpObj = smtplib.SMTP('sea-01.cit.frankfurt-university.de')
message += "New message for %s published\n\n" % msg.topic
message += "Message content is:\n%s\n" % (msg.payload.decode())
smtpObj.sendmail(sender, receivers, message)
print("Successfully sent email")
except smtplib.SMTPException as e:
print("Error: %s " % (str(e)))
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.tls_set("/home/oleg/allcerts.pem", tls_version=ssl.PROTOCOL_TLSv1_2)
client.tls_insecure_set(False)
client.username_pw_set("oleg", "$1$G3d1pZrD$3tWVOP4u0cJECLk9v3LRy1")
client.connect("mqtt.dahahm.de", 8883, 60)
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()