import datetime import smtplib import os import logging from sim.config import Configuration import ssl config = Configuration() class Emailer(): def __init__(self, hostname, port, auth_user, auth_pass): self.hostname = hostname self.port = port self.auth_user = auth_user self.auth_pass = auth_pass self.logger = logging.getLogger(__name__) def read_template(self, filename): from string import Template with open(filename, 'r', encoding='utf-8') as template_file: template_file_content = template_file.read() return Template(template_file_content) def send_success_email(self, from_address, to_address, alert_message, subject, type=None, report_count=0): from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText self.logger.info("Preparando para enviar correo electrónico") head_color = "#6bab01" message_template = self.read_template(os.path.join(config.TEMPLATES_PATH,'success-message-inline.html')) message = message_template.substitute(fecha=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), alert_message=alert_message, cuenta_reportes=report_count, head_color=head_color) msg = MIMEMultipart() msg['From'] = from_address msg['To'] = to_address msg['Subject'] = subject msg.attach(MIMEText(message, 'html')) self.logger.debug("Conectando con el servidor SMTP: {0}".format(self.hostname)) context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) server = smtplib.SMTP(self.hostname, self.port) server.starttls(context=context) server.login(self.auth_user,self.auth_pass) self.logger.debug("Enviando correo electrónico") server.sendmail(msg['From'], msg['To'], msg.as_string()) self.logger.debug("Correo Electrónico enviado") self.logger.debug("Desconectando del servidor SMTP") server.quit() self.logger.info("Correo electrónico enviado con éxito a la dirección {0}".format(to_address)) def send_warning_email(self, from_address, to_address, alert_message, subject): from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText self.logger.info("Preparando para enviar correo electrónico") head_color = "#ffaa00" message_template = self.read_template(os.path.join(config.TEMPLATES_PATH, 'warning-message-inline.html')) message = message_template.substitute(fecha=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), alert_message=alert_message, head_color=head_color) msg = MIMEMultipart() msg['From'] = from_address msg['To'] = to_address msg['Subject'] = subject msg.attach(MIMEText(message, 'html')) self.logger.debug("Conectando con el servidor SMTP: {0}".format(self.hostname)) context = ssl.SSLContext(ssl.PROTOCOL_SSLv23) server = smtplib.SMTP(self.hostname, self.port) server.starttls(context=context) server.login(self.auth_user,self.auth_pass) self.logger.debug("Enviando correo electrónico") server.sendmail(msg['From'], msg['To'], msg.as_string()) self.logger.debug("Correo Electrónico enviado") self.logger.debug("Desconectando del servidor SMTP") server.quit() self.logger.info("Correo electrónico enviado con éxito a la dirección {0}".format(to_address))