-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_notifications.py
37 lines (32 loc) · 1.37 KB
/
send_notifications.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr
import smtplib
def send_notification(sender_email, sender_auth, receiver_email, msg, subject="Training Update", server="smtp.qq.com", port=465, retries=3):
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = formataddr(["ML Training", sender_email])
message["To"] = formataddr(["User", receiver_email])
part = MIMEText(msg, 'plain')
message.attach(part)
for i in range(retries):
try:
with smtplib.SMTP_SSL(server, port) as server:
server.login(sender_email, sender_auth)
server.sendmail(sender_email, receiver_email, message.as_string())
print("Email sent successfully")
break
except Exception as e:
print(f"Failed to send notification. Error: {e}")
if i < retries - 1: # If not the last retry
print("Retrying...")
else: # On last retry, raise error
raise
if __name__ == "__main__":
# Test cases
sender_email = "[email protected]"
sender_auth = "sender_auth"
receiver_email = "[email protected]"
msg = "This is a test message."
subject = "Test Email"
send_notification(sender_email, sender_auth, receiver_email, msg, subject)