1 from email.mime.multipart import MIMEMultipart 2 from email.mime.text import MIMEText 3 from email.mime.application import MIMEApplication 4 import base64 5 import mimetypes 6 import smtplib 7 from email.mime.multipart import MIMEMultipart 8 from email.mime.text import MIMEText 9 from configmanager import configManager 10 import requests 11 import json 12 from typing import List 13 14 class send_alert: 15 def __inti__(): 16 self.configmng = configManager() 17 18 def CreateMessageHtml(sender, to, subject, msgHtml, msgPlain, attachmentFile,date): 19 print('***********************************\n') 20 msg = MIMEMultipart('alternative') 21 msg['Subject'] = subject 22 msg['From'] = sender 23 msg['To'] = to 24 msg.attach(MIMEText(msgPlain, 'plain')) 25 if True: 26 msgPlain = msgPlain.replace("\n", "<br />\n") 27 msgHtml = msgHtml.replace("\n", "<br />\n") 28 msgHtml = """\ 29 <html> 30 <head></head> 31 <body> 32 <p>{} 33 </p> 34 <p>{} 35 </p> 36 </body> 37 </html> 38 """.format(msgPlain,msgHtml) 39 msg.attach(MIMEText(msgHtml, 'html')) 40 if attachmentFile is not None: 41 filename = 'BT_CH SKU Validation Summary for {}.csv'.format(date) 42 attachment = MIMEApplication(attachmentFile) 43 attachment['Content-Disposition'] = 'attachment; filename="{}"'.format(filename) 44 msg.attach(attachment) 45 46 return msg 47 48 def send_email_oauth(sender, to, subject, msgHtml, msgPlain, attachmentFile, date): 49 mail_creds = self.configmng.get_details('email_secret') 50 username = mail_creds['username'] 51 password = mail_creds['password'] 52 53 connection = smtplib.SMTP(host='smtp.office365.com', port=587) 54 connection.starttls() 55 connection.login(username,password) 56 try: 57 msg= self.CreateMessageHtml(sender, to,subject, msgHtml, msgPlain, attachmentFile, date) 58 connection.sendmail(msg['From'], msg['To'].split(','), msg.as_string()) 59 connection.quit() 60 except Exception as e: 61 print(e) 62 63 def send_message_to_slack(channel, username, message): 64 65 url = self.configmng.get_credentials('slackwebhook')['slack_webhook'] 66 payload={"channel": channel, "username": username, "text": message} 67 headers = {'Content-Type': 'application/x-www-form-urlencoded'} 68 69 try: 70 response = requests.post(url, headers=headers, data=json.dumps(payload)) 71 except: 72 pass 73 return None 74 75 def send_alert_email_ses(subject:str, message: str, 76 source: str, destination : List , 77 secret_string :str ='secrets'): 78 79 keys= self.configmng.get_credentials(secret_string) 80 client = boto3.client("ses", aws_access_key_id="", 81 aws_secret_access_key="", region_name="us-east-1") 82 message = {"Subject": {"Data": subject}, "Body": {"Text": {"Data": message}}} 83 response = client.send_email_oauth(Source=source, 84 Destination={"ToAddresses": destination }, 85 Message=message) 86