diff --git a/controle_adhesion/main.py b/controle_adhesion/main.py index 36e3e59..59b6050 100755 --- a/controle_adhesion/main.py +++ b/controle_adhesion/main.py @@ -22,6 +22,9 @@ from typing import List from requests.auth import HTTPBasicAuth from requests.auth import HTTPDigestAuth +import smtplib +from email.mime.text import MIMEText +from email.mime.multipart import MIMEMultipart VERSION="1.0.0" GESTION_SECRET_FILE="/home/secretaire/.secret/gestion_api_password" @@ -34,10 +37,27 @@ SENDMAIL_LOCATION = "/usr/sbin/sendmail" # sendmail location BUF=[] GITEA_URL = "https://forge.a-lec.org" +# Update these with your SMTP server details +SMTP_SERVER = 'mail.a-lec.org' +SMTP_PORT = 587 +SMTP_USER = 'secretaire@a-lec.org' +SMTP_SECRET_FILE = "/home/secretaire/.secret/smtp_api_password" + +# gestion_read("SELECT * FROM services_users su \ +# INNER JOIN membres m ON m.id = su.id_user \ +# INNER JOIN services_fees sf ON sf.id = su.id_fee \ +# LEFT JOIN acc_transactions_users tu ON tu.id_service_user = su.id \ +# LEFT JOIN acc_transactions_lines l ON l.id_transaction = tu.id_transaction \ +# WHERE m.id = 3 AND l.id_account = 481;") + def gestion_get_secret(): with open(GESTION_SECRET_FILE) as sfile: return sfile.readline().replace("\n", "") +def smtp_get_secret(): + with open(SMTP_SECRET_FILE) as sfile: + return sfile.readline().replace("\n", "") + def git_get_secret(): with open(GIT_SECRET_FILE) as sfile: return sfile.readline().replace("\n", "") @@ -86,10 +106,31 @@ def setup_workdir(): if not "refuse" in os.listdir(WORKDIR): os.mkdir(WORKDIR+"/refuse") +# def sendmail(headers, data): +# print("***\nHEADERS: {}\n***\n".format(headers)) +# msg = bytes(headers + "\n", 'utf-8') + quopri.encodestring(bytes(data, 'utf-8')) +# subprocess.run([SENDMAIL_LOCATION, "-t", "-oi"], input=msg) + def sendmail(headers, data): print("***\nHEADERS: {}\n***\n".format(headers)) - msg = bytes(headers + "\n", 'utf-8') + quopri.encodestring(bytes(data, 'utf-8')) - subprocess.run([SENDMAIL_LOCATION, "-t", "-oi"], input=msg) + # Prepare the email + msg = MIMEMultipart() + for header in headers.split("\n"): + if ": " in header: + key, value = header.split(": ", 1) + msg[key] = value + msg.attach(MIMEText(data, 'plain')) + + # Connect to the SMTP server and send the email + try: + server = smtplib.SMTP(SMTP_SERVER, SMTP_PORT) + server.starttls() # Upgrade the connection to a secure TLS connection + server.login(SMTP_USER, smtp_get_secret()) + server.sendmail(SMTP_USER, msg['To'], msg.as_string()) + server.quit() + #print("Email sent successfully") + except Exception as e: + print(f"Failed to send email: {e}") def new_registrations_worker(): if (os.listdir(WORKDIR+"/nouveau") == []):