fix mime config

This commit is contained in:
Adrien Bourmault 2024-09-16 19:13:09 +02:00
parent 02c155cbe5
commit 3589ab01cd
Signed by: neox
GPG Key ID: 57BC26A3687116F6
2 changed files with 24 additions and 20 deletions

View File

@ -115,8 +115,10 @@ def setup_workdir():
def sendmail_with_attachment(headers, data, attachment_path=None, filename=None): def sendmail_with_attachment(headers, data, attachment_path=None, filename=None):
# Parse headers # Create a multipart message
msg = MIMEMultipart() msg = MIMEMultipart()
# Add the headers without manually setting Content-Type or MIME-Version
for header in headers.split("\n"): for header in headers.split("\n"):
if ": " in header: if ": " in header:
key, value = header.split(": ", 1) key, value = header.split(": ", 1)
@ -128,8 +130,8 @@ def sendmail_with_attachment(headers, data, attachment_path=None, filename=None)
# Add the attachment only if attachment_path is provided # Add the attachment only if attachment_path is provided
if attachment_path and filename: if attachment_path and filename:
attachment = MIMEBase('application', 'octet-stream')
with open(attachment_path, "rb") as attach_file: with open(attachment_path, "rb") as attach_file:
attachment = MIMEBase('application', 'octet-stream')
attachment.set_payload(attach_file.read()) attachment.set_payload(attach_file.read())
encoders.encode_base64(attachment) encoders.encode_base64(attachment)
attachment.add_header('Content-Disposition', f'attachment; filename={filename}') attachment.add_header('Content-Disposition', f'attachment; filename={filename}')

View File

@ -78,36 +78,38 @@ def setup_workdir():
os.makedirs(path) os.makedirs(path)
# Send email with attachment via SMTP, password fetched from file # Send email with attachment via SMTP, password fetched from file
def sendmail_with_attachment(headers, body, attachment_path, attachment_filename): def sendmail_with_attachment(headers, data, attachment_path=None, filename=None):
# Create a multipart message
msg = MIMEMultipart() msg = MIMEMultipart()
# Add headers # Add the headers without manually setting Content-Type or MIME-Version
for header in headers.split("\n"): for header in headers.split("\n"):
if ": " in header: if ": " in header and "Content-Type" not in header and "Mime-Version" not in header:
key, value = header.split(": ", 1) key, value = header.split(": ", 1)
msg[key] = value.strip() msg[key] = value.strip()
# Email body # Add the email body
msg.attach(MIMEText(body, 'plain', 'utf-8')) body = MIMEText(data, 'plain', 'utf-8')
msg.attach(body)
# Attach PDF file # Add the attachment only if attachment_path is provided
if attachment_path: if attachment_path and filename:
with open(attachment_path, "rb") as attachment: with open(attachment_path, "rb") as attach_file:
part = MIMEBase('application', 'octet-stream') attachment = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read()) attachment.set_payload(attach_file.read())
encoders.encode_base64(part) encoders.encode_base64(attachment)
part.add_header('Content-Disposition', f'attachment; filename={attachment_filename}') attachment.add_header('Content-Disposition', f'attachment; filename={filename}')
msg.attach(part) msg.attach(attachment)
# Send email # Send the email
try: try:
with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server: with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server:
server.starttls() # Secure the connection server.starttls() # Start TLS encryption
server.login(SMTP_USER, smtp_get_secret()) # Fetch SMTP password from file server.login(SMTP_USER, smtp_get_secret())
server.sendmail(SMTP_USER, msg['To'], msg.as_string()) server.sendmail(SMTP_USER, msg['To'], msg.as_string())
print(f"Email sent to {msg['To']}") print("Email sent successfully")
except Exception as e: except Exception as e:
print(f"Error sending email: {e}") print(f"Failed to send email: {e}")
# Notify unpaid donors # Notify unpaid donors
def notify_unpaid(record): def notify_unpaid(record):