Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Email Alerts Script for Automated Notifications #996

Merged
merged 3 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions email_alerts/EmailAlert.py
Original file line number Diff line number Diff line change
@@ -0,0 +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


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

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

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, '[email protected]', 'your_password')
email_alert.send_email('Test Subject', 'This is a test email', ['[email protected]'])
Empty file added email_alerts/README.md
Empty file.
5 changes: 5 additions & 0 deletions email_alerts/requirements.txt
Original file line number Diff line number Diff line change
@@ -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