Compare commits
2 Commits
37702b7380
...
5e6e40f576
Author | SHA1 | Date | |
---|---|---|---|
5e6e40f576 | |||
7edba24b51 |
@ -5,5 +5,6 @@ BoardID=
|
|||||||
|
|
||||||
[SMTP]
|
[SMTP]
|
||||||
Server=
|
Server=
|
||||||
|
Port=
|
||||||
Username=
|
Username=
|
||||||
Password=
|
Password=
|
||||||
|
56
main.py
56
main.py
@ -1,6 +1,7 @@
|
|||||||
from configparser import ConfigParser
|
from configparser import ConfigParser # For reading the config.ini file
|
||||||
from shutil import copy
|
from shutil import copy # For creating a new config.ini file
|
||||||
from requests import request
|
from requests import request # For using the Trello API
|
||||||
|
import smtplib, ssl, email # For sending emails
|
||||||
|
|
||||||
### Constants
|
### Constants
|
||||||
|
|
||||||
@ -153,8 +154,8 @@ def send_emails(config: dict, users: dict, timestamp: str) -> None:
|
|||||||
PARAMETERS
|
PARAMETERS
|
||||||
----------
|
----------
|
||||||
- config: dict
|
- config: dict
|
||||||
The config file data. Uses this for the SMTP server, username and
|
The config file data. Uses this for the SMTP server, port, username,
|
||||||
password.
|
and password.
|
||||||
|
|
||||||
- users: dict
|
- users: dict
|
||||||
Each user ID and the email connected to it.
|
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
|
- timestamp: str
|
||||||
The timestamp used to create the folder that the reports are in.
|
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():
|
def main():
|
||||||
# Reading the config file
|
# Reading the config file
|
||||||
|
Reference in New Issue
Block a user