add emailer

This commit is contained in:
Lucas Mathews
2024-05-25 11:33:41 +02:00
parent f9e5f0d7e4
commit 8ea2f1c173
2 changed files with 41 additions and 0 deletions

View File

@@ -20,3 +20,10 @@ url=http://0.0.0.0:81/
[sessions]
secret_key=57d7dfef5a519fe73d3ba1a9ced6477f
[smtp]
host=email-smtp.us-east-1.amazonaws.com
port=465
username=AKIAYW7TTSMBEUADU2UG
password=BIR8EVWA1bsw+YL8WyAq8BZJZC8vdZrxcrnGe4GnAXEh
sender_name=Luxbank
sender_email=bank@luxdomain.xyz

34
emailer.py Normal file
View File

@@ -0,0 +1,34 @@
# Lucas Mathews - Fontys Student ID: 5023572
# Banking System Emailer
import smtplib, ssl
from config import CONFIG # Import Config
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
context = ssl.create_default_context() # Create a secure SSL context
email = "l.mathews@student.fontys.nl" # These three lines are for testing purposes
subject = "Test Email"
body = "This is a test email."
def send_email(receiver_email, subject, body):
sender_email = CONFIG["smtp"]["sender_email"]
message = MIMEMultipart() # Create a multipart message and set headers
message["From"] = f"{CONFIG["smtp"]["sender_name"]} <{CONFIG["smtp"]["sender_email"]}>"
message["To"] = receiver_email
message["Subject"] = subject
message.attach(MIMEText(body, "plain")) # Add body to email
text = message.as_string()
with smtplib.SMTP_SSL(CONFIG["smtp"]["host"], CONFIG["smtp"]["port"], context=context) as server:
server.login(CONFIG["smtp"]["username"], CONFIG["smtp"]["password"])
server.sendmail(sender_email, receiver_email, text)
print(f"Email sent to {receiver_email}.")
server.quit()
if __name__ == "__main__":
send_email(email, subject, body)