sendmail subprocess

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