Compare commits

..

2 Commits

Author SHA1 Message Date
5e6e40f576 📧 Sending emails 2024-08-29 10:50:19 +02:00
7edba24b51 📝 Commenting imports 2024-08-29 10:36:00 +02:00
2 changed files with 51 additions and 6 deletions

View File

@ -5,5 +5,6 @@ BoardID=
[SMTP]
Server=
Port=
Username=
Password=

56
main.py
View File

@ -1,6 +1,7 @@
from configparser import ConfigParser
from shutil import copy
from requests import request
from configparser import ConfigParser # For reading the config.ini file
from shutil import copy # For creating a new config.ini file
from requests import request # For using the Trello API
import smtplib, ssl, email # For sending emails
### Constants
@ -153,8 +154,8 @@ def send_emails(config: dict, users: dict, timestamp: str) -> None:
PARAMETERS
----------
- config: dict
The config file data. Uses this for the SMTP server, username and
password.
The config file data. Uses this for the SMTP server, port, username,
and password.
- users: dict
Each user ID and the email connected to it.
@ -162,7 +163,50 @@ def send_emails(config: dict, users: dict, timestamp: str) -> None:
- timestamp: str
The timestamp used to create the folder that the reports are in.
"""
pass
# Create a secure SSL context
context = ssl.create_default_context()
# Logging in
server_url = config["SMTP"]["server"]
port = config["SMTP"]["port"]
username = config["SMTP"]["username"]
password = config["SMTP"]["password"]
with smtplib.SMTP_SSL(server_url, port, context=context) as server:
server.login(username, password)
# Looping through each user and sending them an email with the pdf
for user_id, receiver in users.items():
pdf_path = f"reports/{timestamp}/{user_id}.pdf"
subject = "Trello Report"
body = "Here is your Trello Report"
# Preparing the message
message = email.mime.multipart.MIMEMultipart()
message["From"] = username
message["To"] = receiver
message["Subject"] = subject
message.attach(email.mime.text.MIMEText(body, "plain"))
# Open PDF file in binary mode, and attach it to the email
with open(pdf_path, "rb") as file_pointer:
part = email.mime.base.MIMEBase("application", "octet-stream")
part.set_payload(file_pointer.read())
# Encode file in ASCII characters to send by email
email.encoders.encode_base64(part)
# Add header as key/value pair to attachment part
part.add_header(
"Content-Disposition",
f"attachment; filename= TrelloReport-{user_id}-{timestamp}.pdf",
)
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
server.sendmail(username, receiver, text)
def main():
# Reading the config file