-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalert.py
More file actions
31 lines (25 loc) · 1.12 KB
/
alert.py
File metadata and controls
31 lines (25 loc) · 1.12 KB
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
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
def send_email(index, from_email, from_password, to_email):
if from_email is None or from_password is None or to_email is None:
return
# SMTP server details (for Gmail in this case)
smtp_server = "smtp.gmail.com"
smtp_port = 587 # Port for TLS
# Create the MIME message (multipart message)
msg = MIMEMultipart()
msg['From'] = from_email
msg['To'] = to_email
msg['Subject'] = "Poor Air Quality Alert"
msg.attach(MIMEText(f"The AQI has exceeded the threshold. Current AQI: {index}.", 'plain'))
try:
# Establish a connection to the SMTP server
with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls() # Start TLS encryption
server.login(from_email, from_password) # Log in to the email account
text = msg.as_string() # Convert the message to a string
server.sendmail(from_email, to_email, text) # Send the email
print("Email sent successfully!")
except Exception as e:
print(f"Error: {e}")