52 lines
1.8 KiB
Python
52 lines
1.8 KiB
Python
|
import smtplib
|
||
|
from email.mime.multipart import MIMEMultipart
|
||
|
from email.mime.text import MIMEText
|
||
|
import ssl
|
||
|
import toml
|
||
|
|
||
|
def send_email(event_summary, next_alert, next_event, config):
|
||
|
with open('config.toml', 'r') as f:
|
||
|
config = toml.load(f)
|
||
|
|
||
|
# Set up the SMTP server details
|
||
|
smtp_server = config["email"]["smtp_server"]
|
||
|
port = config["email"]["port"]
|
||
|
sender_email = config["email"]["username"]
|
||
|
receiver_email = config["email"]["recipient"]
|
||
|
password = config["email"]["password"]
|
||
|
|
||
|
# Event details
|
||
|
event_name = event_summary
|
||
|
event_date = next_event
|
||
|
event_delta = next_event - next_alert
|
||
|
|
||
|
# Create a multipart message and set headers
|
||
|
message = MIMEMultipart()
|
||
|
message["From"] = sender_email
|
||
|
message["To"] = receiver_email
|
||
|
message["Subject"] = "Event Alert: '{}' @ {}".format(event_name, event_date)
|
||
|
|
||
|
# Add body to email
|
||
|
body = """\
|
||
|
Hi,
|
||
|
This is an alert for the event named '{}' which will occur on {}.
|
||
|
This event will start in '{}'
|
||
|
""".format(event_name, event_date, event_delta)
|
||
|
message.attach(MIMEText(body, "plain"))
|
||
|
text = message.as_string()
|
||
|
|
||
|
try:
|
||
|
# Create a secure SSL context and connect to the server
|
||
|
context = ssl.create_default_context()
|
||
|
with smtplib.SMTP(smtp_server, port) as server: # Use 'with' statement for automatic cleanup
|
||
|
server.ehlo() # Can be omitted
|
||
|
server.starttls(context=context) # Secure the connection
|
||
|
server.login(sender_email, password)
|
||
|
|
||
|
# Send email here
|
||
|
server.sendmail(sender_email, receiver_email, text)
|
||
|
except Exception as e:
|
||
|
# Print any error messages to stdout
|
||
|
print(e)
|
||
|
return print("Message sent via email")
|