Skip to content

Commit

Permalink
feat: add email alert system for automated notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
Acuspeedster committed Oct 12, 2024
1 parent d91a925 commit 59244e3
Showing 1 changed file with 42 additions and 10 deletions.
52 changes: 42 additions & 10 deletions email_alerts/EmailAlert.py
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]"
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", "[email protected]")
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]'])

0 comments on commit 59244e3

Please sign in to comment.