def __init__(self): self.notify_email = os.environ.get('NOTIFY_EMAIL') self.notify_email_password = os.environ.get('NOTIFY_EMAIL_PASSWORD') self.notify_smtp_server = os.environ.get('NOTIFY_SMTP_SERVER') self.notify_smtp_server_port = os.environ.get('NOTIFY_SMTP_SERVER_PORT') self.notify_server = os.environ.get('NOTIFY_SERVER') self.notify_server_port = os.environ.get('NOTIFY_SERVER_PORT') self.mail_template = MailTemplate() self.mail_template.notify_server = self.notify_server self.mail_template.notify_server_port = self.notify_server_port
class EmailService(object): def __init__(self): self.notify_email = os.environ.get('NOTIFY_EMAIL') self.notify_email_password = os.environ.get('NOTIFY_EMAIL_PASSWORD') self.notify_smtp_server = os.environ.get('NOTIFY_SMTP_SERVER') self.notify_smtp_server_port = os.environ.get('NOTIFY_SMTP_SERVER_PORT') self.notify_server = os.environ.get('NOTIFY_SERVER') self.notify_server_port = os.environ.get('NOTIFY_SERVER_PORT') self.mail_template = MailTemplate() self.mail_template.notify_server = self.notify_server self.mail_template.notify_server_port = self.notify_server_port def notify(self, data): for info in data: self.mail_template.server_id = info['server_id'] self.mail_template.server_name = info['server_name'] self.mail_template.server_status = info['server_status'] self.mail_template.server_created = info['server_created'] self.mail_template.user_name = info['user_name'] self.mail_template.user_email = info['user_email'] self.mail_template.flavor_vcpus = info['flavor_vcpus'] self.mail_template.flavor_ram = info['flavor_ram'] self.mail_template.flavor_disk = info['flavor_disk'] try: mail = smtplib.SMTP(self.notify_smtp_server, int(self.notify_smtp_server_port)) mail.ehlo() mail.starttls() mail.login(self.notify_email, self.notify_email_password) mail.sendmail(self.notify_email, self.mail_template.user_email, self.mail_template.generate()) mail.close() except smtplib.SMTPException as e: raise SendEmailException('[ERROR - SENDING EMAIL] ' + e.smtp_error)