📧 Sending emails
This commit is contained in:
@ -5,5 +5,6 @@ BoardID=
|
|||||||
|
|
||||||
[SMTP]
|
[SMTP]
|
||||||
Server=
|
Server=
|
||||||
|
Port=
|
||||||
Username=
|
Username=
|
||||||
Password=
|
Password=
|
||||||
|
47
main.py
47
main.py
@ -1,7 +1,7 @@
|
|||||||
from configparser import ConfigParser # For reading the config.ini file
|
from configparser import ConfigParser # For reading the config.ini file
|
||||||
from shutil import copy # For creating a new config.ini file
|
from shutil import copy # For creating a new config.ini file
|
||||||
from requests import request # For using the Trello API
|
from requests import request # For using the Trello API
|
||||||
import smtplib, ssl # For sending emails
|
import smtplib, ssl, email # For sending emails
|
||||||
|
|
||||||
### Constants
|
### Constants
|
||||||
|
|
||||||
@ -163,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