core/source/logic/channels/email.py
2023-04-01 11:33:29 +02:00

126 lines
2.5 KiB
Python

class implementation_notification_channel_email(interface_notification_channel):
'''
[implementation]
'''
def parameters_schema(self):
return {
"type": "object",
"additionalProperties": False,
"properties": {
"access": {
"type": "object",
"additionalProperties": False,
"properties": {
"host": {
"type": "string"
},
"port": {
"type": "integer"
},
"username": {
"type": "string"
},
"password": {
"type": "string"
}
},
"required": [
"host",
"port",
"username",
"password"
]
},
"sender": {
"type": "string"
},
"receivers": {
"type": "array",
"items": {
"type": "string"
}
},
"tags": {
"description": "list of strings, which will be placed in the e-mail subject",
"type": "array",
"items": {
"type": "string"
},
"default": []
}
},
"required": [
"access",
"sender",
"receivers"
]
}
'''
[implementation]
'''
def normalize_conf_node(self, node):
return dict_merge(
{
},
node
)
'''
[implementation]
'''
def notify(self, parameters, name, data, state, info):
datetime = _datetime.datetime.utcnow()
smtp_connection = _smtplib.SMTP(
parameters["access"]["host"]
)
smtp_connection.login(
parameters["access"]["username"],
parameters["access"]["password"]
)
message = MIMEText(
_json.dumps(info, indent = "\t", ensure_ascii = False)
)
message["Subject"] = string_coin(
"{{tags}} {{title}}",
{
"tags": " ".join(
map(
lambda tag: ("[%s]" % tag.upper()),
(
parameters["tags"]
+
[condition_show(state["condition"])]
)
)
),
"title": data["name"],
}
)
message["From"] = parameters["sender"]
message["To"] = ",".join(parameters["receivers"])
message["Date"] = string_coin(
"{{day_of_week}}, {{day_of_month}} {{month}} {{year}} {{hour}}:{{minute}}:{{second}} {{time_offset}}",
{
"day_of_week": datetime.strftime("%a"),
"day_of_month": datetime.strftime("%d"),
"month": datetime.strftime("%b"),
"year": datetime.strftime("%Y"),
"hour": datetime.strftime("%H"),
"minute": datetime.strftime("%M"),
"second": datetime.strftime("%S"),
# "time_offset": datetime.strftime("%z"),
"time_offset": "+0000",
}
)
smtp_connection.sendmail(
parameters["sender"],
parameters["receivers"],
message.as_string()
)
smtp_connection.quit()