change for the send function
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
from email.mime.multipart import MIMEMultipart
|
from email.mime.multipart import MIMEMultipart
|
||||||
from email.mime.text import MIMEText
|
from email.mime.text import MIMEText
|
||||||
|
from email.mime.application import MIMEApplication
|
||||||
import smtplib
|
import smtplib
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
@@ -21,12 +22,21 @@ def _build_smtp_client():
|
|||||||
return smtp
|
return smtp
|
||||||
|
|
||||||
|
|
||||||
def send(email: list | str, subject: str, text_body: str, html_body: str = None) -> bool:
|
def send(
|
||||||
"""Sends the email with both Plain Text and HTML support."""
|
email: list | str,
|
||||||
|
subject: str,
|
||||||
|
text_body: str,
|
||||||
|
html_body: str = None,
|
||||||
|
attachments: list = None
|
||||||
|
) -> bool:
|
||||||
|
"""Sends the email with Plain Text, HTML, and optional File Attachments support."""
|
||||||
|
|
||||||
if isinstance(email, str):
|
if isinstance(email, str):
|
||||||
email = [email]
|
email = [email]
|
||||||
|
|
||||||
|
if attachments is None:
|
||||||
|
attachments = []
|
||||||
|
|
||||||
TEXT_SIGNATURE = (
|
TEXT_SIGNATURE = (
|
||||||
"\n\n--\n"
|
"\n\n--\n"
|
||||||
"Mit freundlichen Grüßen\n"
|
"Mit freundlichen Grüßen\n"
|
||||||
@@ -72,13 +82,26 @@ def send(email: list | str, subject: str, text_body: str, html_body: str = None)
|
|||||||
for i, recipient in enumerate(email):
|
for i, recipient in enumerate(email):
|
||||||
start_time = time.time()
|
start_time = time.time()
|
||||||
|
|
||||||
msg = MIMEMultipart("alternative")
|
# Root message structure for email with attachments
|
||||||
|
msg = MIMEMultipart("mixed")
|
||||||
msg["Subject"] = subject
|
msg["Subject"] = subject
|
||||||
msg["From"] = "Invario Team <no-reply@invario-software.de>"
|
msg["From"] = "Invario Team <no-reply@invario-software.de>"
|
||||||
msg["To"] = str(recipient)
|
msg["To"] = str(recipient)
|
||||||
|
|
||||||
msg.attach(MIMEText(text_content, "plain"))
|
# Sub-container for Plain Text and HTML bodies
|
||||||
msg.attach(MIMEText(html_content, "html"))
|
msg_body = MIMEMultipart("alternative")
|
||||||
|
msg_body.attach(MIMEText(text_content, "plain"))
|
||||||
|
msg_body.attach(MIMEText(html_content, "html"))
|
||||||
|
msg.attach(msg_body)
|
||||||
|
|
||||||
|
# Attach files if provided
|
||||||
|
for file_path in attachments:
|
||||||
|
if file_path and os.path.isfile(file_path):
|
||||||
|
filename = os.path.basename(file_path)
|
||||||
|
with open(file_path, "rb") as file:
|
||||||
|
part = MIMEApplication(file.read(), Name=filename)
|
||||||
|
part['Content-Disposition'] = f'attachment; filename="{filename}"'
|
||||||
|
msg.attach(part)
|
||||||
|
|
||||||
smtp.sendmail(
|
smtp.sendmail(
|
||||||
from_addr="no-reply@invario-software.de",
|
from_addr="no-reply@invario-software.de",
|
||||||
|
|||||||
Reference in New Issue
Block a user