restic-compose-backup/restic_compose_backup/alerts/smtp.py

57 lines
1.6 KiB
Python
Raw Normal View History

import os
import smtplib
import logging
from email.mime.text import MIMEText
2019-12-04 02:58:27 +00:00
from restic_compose_backup.alerts.base import BaseAlert
logger = logging.getLogger(__name__)
2019-12-04 02:58:27 +00:00
class SMTPAlert(BaseAlert):
name = 'smtp'
def __init__(self, host, port, user, password, to):
self.host = host
self.port = port
self.user = user
self.password = password
self.to = to
@classmethod
def create_from_env(cls):
instance = cls(
os.environ.get('EMAIL_HOST'),
os.environ.get('EMAIL_PORT'),
os.environ.get('EMAIL_HOST_USER'),
os.environ.get('EMAIL_HOST_PASSWORD'),
(os.environ.get('EMAIL_SEND_TO') or "").split(','),
)
if instance.properly_configured:
return instance
return None
@property
def properly_configured(self) -> bool:
return self.host and self.port and self.user and self.password and len(self.to) > 0
2019-12-04 02:58:27 +00:00
def send(self, subject: str = None, body: str = None, alert_type: str = 'INFO'):
# send_mail("Hello world!")
msg = MIMEText(body)
msg['Subject'] = f"[{alert_type}] {subject}"
msg['From'] = self.user
msg['To'] = ', '.join(self.to)
2019-12-04 02:58:27 +00:00
try:
logger.info("Connecting to %s port %s", self.host, self.port)
server = smtplib.SMTP_SSL(self.host, self.port)
server.ehlo()
server.login(self.user, self.password)
server.sendmail(self.user, self.to, msg.as_string())
logger.info('Email Sent')
except Exception as ex:
logger.error(ex)
finally:
server.close()