From 36e639b8b30bd417be97e25a113c5dc17cf42833 Mon Sep 17 00:00:00 2001 From: Acuspeedster Date: Sun, 13 Oct 2024 00:28:07 +0530 Subject: [PATCH 1/3] feat: add email alert system for automated notifications --- email_alerts/EmailAlert.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 email_alerts/EmailAlert.py diff --git a/email_alerts/EmailAlert.py b/email_alerts/EmailAlert.py new file mode 100644 index 00000000..faf8dc1c --- /dev/null +++ b/email_alerts/EmailAlert.py @@ -0,0 +1,15 @@ +import smtplib + +def send_email(subject, body, to_email): + sender_email = "your_email@example.com" + sender_password = "your_password" + + message = f"Subject: {subject}\n\n{body}" + + server = smtplib.SMTP('smtp.gmail.com', 587) + server.starttls() + server.login(sender_email, sender_password) + server.sendmail(sender_email, to_email, message) + server.quit() + +send_email("Test Alert", "This is an automated email", "recipient@example.com") From d91a925d35e1754eabce0fc677dc4d4e4cab0a79 Mon Sep 17 00:00:00 2001 From: Acuspeedster Date: Sun, 13 Oct 2024 00:37:07 +0530 Subject: [PATCH 2/3] feat: add email alert system for automated notifications --- email_alerts/README.md | 0 email_alerts/requirements.txt | 5 +++++ 2 files changed, 5 insertions(+) create mode 100644 email_alerts/README.md create mode 100644 email_alerts/requirements.txt diff --git a/email_alerts/README.md b/email_alerts/README.md new file mode 100644 index 00000000..e69de29b diff --git a/email_alerts/requirements.txt b/email_alerts/requirements.txt new file mode 100644 index 00000000..d0b47d4e --- /dev/null +++ b/email_alerts/requirements.txt @@ -0,0 +1,5 @@ +smtplib==1.0.0 +email==6.0.0 +requests==2.25.1 +logging==0.5.1.2 +pandas==1.2.0 From 59244e3517446d36b83cf22e9b64e8c561186e38 Mon Sep 17 00:00:00 2001 From: Acuspeedster Date: Sun, 13 Oct 2024 00:42:40 +0530 Subject: [PATCH 3/3] feat: add email alert system for automated notifications --- email_alerts/EmailAlert.py | 52 ++++++++++++++++++++++++++++++-------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/email_alerts/EmailAlert.py b/email_alerts/EmailAlert.py index faf8dc1c..d3547204 100644 --- a/email_alerts/EmailAlert.py +++ b/email_alerts/EmailAlert.py @@ -1,15 +1,47 @@ +# Before class definition, ensure there are two blank lines import smtplib +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText -def send_email(subject, body, to_email): - sender_email = "your_email@example.com" - sender_password = "your_password" - message = f"Subject: {subject}\n\n{body}" +class EmailAlert: + def __init__(self, smtp_server, smtp_port, email_user, email_password): + self.smtp_server = smtp_server + self.smtp_port = smtp_port + self.email_user = email_user + self.email_password = email_password - server = smtplib.SMTP('smtp.gmail.com', 587) - server.starttls() - server.login(sender_email, sender_password) - server.sendmail(sender_email, to_email, message) - server.quit() + def send_email(self, subject, body, recipients): + try: + # Create the email message + msg = MIMEMultipart() + msg['From'] = self.email_user + msg['To'] = ", ".join(recipients) + msg['Subject'] = subject -send_email("Test Alert", "This is an automated email", "recipient@example.com") + msg.attach(MIMEText(body, 'plain')) + + # Set up the server + server = smtplib.SMTP(self.smtp_server, self.smtp_port) + server.starttls() + + # Login to the email server + server.login(self.email_user, self.email_password) + + # Send the email + server.sendmail(self.email_user, recipients, msg.as_string()) + + # Log success + print(f"Email sent successfully to {recipients}") + + except Exception as e: + # Log failure + print(f"Failed to send email. Error: {str(e)}") + finally: + server.quit() + + +# After the class definition, ensure two blank lines +if __name__ == "__main__": + email_alert = EmailAlert('smtp.gmail.com', 587, 'your_email@gmail.com', 'your_password') + email_alert.send_email('Test Subject', 'This is a test email', ['recipient@example.com'])