sendmail subprocess

This commit is contained in:
2025-02-25 17:07:35 +08:00
parent bfb49f0a12
commit ba572d7d84
+12 -5
View File
@@ -5,6 +5,7 @@ from email.mime.text import MIMEText
import os
import uuid
import config
import subprocess
from werkzeug.utils import secure_filename
from app import logger
@@ -35,11 +36,17 @@ def send_email(recipient_email, subject, body):
logger.error(f"Failed to send email via SMTP to {recipient_email}: {e}")
else:
try:
process = os.popen("/usr/sbin/sendmail -t")
process.write(f"To: {recipient_email}\n")
process.write(f"Subject: {subject}\n")
process.write(f"\n{body}")
process.close()
process = subprocess.Popen(
["/usr/sbin/sendmail", "-t"],
stdin=subprocess.PIPE,
stderr=subprocess.PIPE
)
email_message = f"To: {recipient_email}\nSubject: {subject}\n\n{body}"
stdout, stderr = process.communicate(input=email_message.encode())
if process.returncode != 0:
logger.error(f"Sendmail error: {stderr.decode().strip()}")
else:
logger.info(f"Email sent successfully via sendmail to {recipient_email}")
except Exception as e:
logger.error(f"Failed to send email via local sendmail to {recipient_email}: {e}")