blob: e3aabf2365c251709fe45008f7dd9840aa181007 [file] [log] [blame]
# Email notification package
# This should be upgraded by integrating with mail server to send batch
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import staffeln.conf
from staffeln.common import time as xtime
CONF = staffeln.conf.CONF
def sendEmail(src_email, src_pwd, dest_email, subject, content, smtp_server_domain, smtp_server_port):
try:
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = src_email
message["To"] = dest_email
part = MIMEText(content, "html")
message.attach(part)
s = smtplib.SMTP(host=smtp_server_domain, port=smtp_server_port)
s.ehlo()
s.starttls()
# we can comment this auth func when use the trusted ip without authentication against the smtp server
s.login(src_email, src_pwd)
s.sendmail(src_email, dest_email, message.as_string())
s.close()
return True
except Exception as e:
print(str(e))
return False
def SendBackupResultNotification(success_volume_list, failed_volume_list):
subject = "Backup result"
html = "<h3>${TIME}</h3>" \
"<h3>Success List</h3>" \
"<h4>${SUCCESS_VOLUME_LIST}</h4>" \
"<h3>Failed List</h3>" \
"<h4>${FAILED_VOLUME_LIST}</h4>"
success_volumes = '<br>'.join([str(elem) for elem in success_volume_list])
failed_volumes = '<br>'.join([str(elem) for elem in failed_volume_list])
html = html.replace("${TIME}", xtime.get_current_strtime())
html = html.replace("${SUCCESS_VOLUME_LIST}", success_volumes)
html = html.replace("${FAILED_VOLUME_LIST}", failed_volumes)
return sendEmail(src_email=CONF.notification.sender_email,
src_pwd=CONF.notification.sender_pwd,
dest_email=CONF.notification.receiver,
subject=subject,
content=html,
smtp_server_domain=CONF.notification.smtp_server_domain,
smtp_server_port=CONF.notification.smtp_server_port)