implementation of the Email add on
Release Inventarsystem / release-docker (push) Successful in 2m24s

This commit is contained in:
2026-09-15 20:53:18 +02:00
parent 00d45a9d1b
commit e9aea7e039
7 changed files with 174 additions and 31 deletions
+39 -19
View File
@@ -1,5 +1,6 @@
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
import smtplib
import time
@@ -17,18 +18,25 @@ def _build_smtp_client():
smtp.starttls()
smtp.ehlo()
if cfg.EMAIL_USERNAME:
smtp.login("no-reply@invario-software.de", "#,EATwIn,68" or "")
smtp.login(cfg.EMAIL_USERNAME, cfg.EMAIL_PASSWORD or "")
return smtp
def send(email: list | str, subject: str, note: str, sender: str) -> bool:
"""Sends the email with the link to the Clients."""
def _normalize_recipients(email: list | str) -> list[str]:
if isinstance(email, str):
email = email.replace(';', ',').split(',')
return [str(recipient).strip() for recipient in (email or []) if str(recipient).strip()]
def _send_message(email: list | str, subject: str, note: str, sender: str, attachment=None) -> bool:
"""Send a plain/HTML message, optionally with one PDF attachment."""
if not cfg.MODULES.is_enabled("mail"):
print("Debug: Module not enabled")
return False
if isinstance(email, str):
email = [email]
recipients = _normalize_recipients(email)
if not recipients:
return False
body_message = note
@@ -63,25 +71,27 @@ def send(email: list | str, subject: str, note: str, sender: str) -> bool:
try:
smtp = _build_smtp_client()
for i, recipient in enumerate(email):
for i, recipient in enumerate(recipients):
start_time = time.time()
msg = MIMEMultipart("alternative")
msg = MIMEMultipart("mixed" if attachment else "alternative")
msg["Subject"] = str(subject)
msg["From"] = f"{sender} <no-reply@invario-software.de>"
from_address = cfg.EMAIL_FROM_ADDRESS or cfg.EMAIL_USERNAME or "no-reply@invario-software.de"
msg["From"] = f"{sender} <{from_address}>"
msg["To"] = str(recipient)
msg.attach(MIMEText(text_content, "plain"))
msg.attach(MIMEText(html_content, "html"))
#smtp.sendmail(
# from_addr=cfg.EMAIL_USERNAME,
# to_addrs=[recipient],
# msg=msg.as_string()
#)
body = MIMEMultipart("alternative") if attachment else msg
body.attach(MIMEText(text_content, "plain"))
body.attach(MIMEText(html_content, "html"))
if attachment:
msg.attach(body)
attachment_payload, filename = attachment
pdf_part = MIMEApplication(attachment_payload, _subtype="pdf")
pdf_part.add_header("Content-Disposition", "attachment", filename=filename)
msg.attach(pdf_part)
smtp.sendmail(
from_addr="no-reply@invario-software.de",
from_addr=from_address,
to_addrs=[recipient],
msg=msg.as_string()
)
@@ -89,7 +99,7 @@ def send(email: list | str, subject: str, note: str, sender: str) -> bool:
elapsed_time = time.time() - start_time
sleep_time = interval - elapsed_time
if sleep_time > 0 and i < len(email) - 1:
if sleep_time > 0 and i < len(recipients) - 1:
time.sleep(sleep_time)
return True
@@ -101,4 +111,14 @@ def send(email: list | str, subject: str, note: str, sender: str) -> bool:
try:
smtp.quit()
except Exception:
pass
pass
def send(email: list | str, subject: str, note: str, sender: str) -> bool:
"""Send an HTML/plain email without an attachment."""
return _send_message(email, subject, note, sender)
def send_pdf(email: list | str, subject: str, note: str, sender: str, pdf_bytes: bytes, filename: str) -> bool:
"""Send an email with a PDF attachment."""
return _send_message(email, subject, note, sender, attachment=(pdf_bytes, filename))