controle_adhesion/main.py: change sendmail to use actual mail system

This commit is contained in:
Adrien Bourmault 2024-09-16 15:59:32 +02:00
parent 43774b4af7
commit 652e7b1f20
Signed by: neox
GPG Key ID: 57BC26A3687116F6
1 changed files with 43 additions and 2 deletions

View File

@ -22,6 +22,9 @@ from typing import List
from requests.auth import HTTPBasicAuth from requests.auth import HTTPBasicAuth
from requests.auth import HTTPDigestAuth from requests.auth import HTTPDigestAuth
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
VERSION="1.0.0" VERSION="1.0.0"
GESTION_SECRET_FILE="/home/secretaire/.secret/gestion_api_password" GESTION_SECRET_FILE="/home/secretaire/.secret/gestion_api_password"
@ -34,10 +37,27 @@ SENDMAIL_LOCATION = "/usr/sbin/sendmail" # sendmail location
BUF=[] BUF=[]
GITEA_URL = "https://forge.a-lec.org" 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(): def gestion_get_secret():
with open(GESTION_SECRET_FILE) as sfile: with open(GESTION_SECRET_FILE) as sfile:
return sfile.readline().replace("\n", "") 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(): def git_get_secret():
with open(GIT_SECRET_FILE) as sfile: with open(GIT_SECRET_FILE) as sfile:
return sfile.readline().replace("\n", "") return sfile.readline().replace("\n", "")
@ -86,10 +106,31 @@ def setup_workdir():
if not "refuse" in os.listdir(WORKDIR): if not "refuse" in os.listdir(WORKDIR):
os.mkdir(WORKDIR+"/refuse") 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): def sendmail(headers, data):
print("***\nHEADERS: {}\n***\n".format(headers)) print("***\nHEADERS: {}\n***\n".format(headers))
msg = bytes(headers + "\n", 'utf-8') + quopri.encodestring(bytes(data, 'utf-8')) # Prepare the email
subprocess.run([SENDMAIL_LOCATION, "-t", "-oi"], input=msg) 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(): def new_registrations_worker():
if (os.listdir(WORKDIR+"/nouveau") == []): if (os.listdir(WORKDIR+"/nouveau") == []):