-
Notifications
You must be signed in to change notification settings - Fork 583
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add email alert system for automated notifications
- Loading branch information
1 parent
d91a925
commit 59244e3
Showing
1 changed file
with
42 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]']) |