Hi
I wrote a python script to create and send pdf files with "smtplib". I'm creating these files from a template html file with "beautiful soap". I convert them to pdf with "xhtml2pdf".
In the resulting e-mail the name of the attachment is not always correctly set. It is sometime set to "default", instead of the file name.
I ran several tests with various names (hereunder pj0 and pj1). Runing pj0 gets a correct attachment name. Running pj1 gets "default". I have not been able to find what is wrong.
The code to attach the files and set the attachment name is as follows :
I wrote a python script to create and send pdf files with "smtplib". I'm creating these files from a template html file with "beautiful soap". I convert them to pdf with "xhtml2pdf".
In the resulting e-mail the name of the attachment is not always correctly set. It is sometime set to "default", instead of the file name.
I ran several tests with various names (hereunder pj0 and pj1). Runing pj0 gets a correct attachment name. Running pj1 gets "default". I have not been able to find what is wrong.
The code to attach the files and set the attachment name is as follows :
def smtp () :
msg = MIMEMultipart()
# Compléter l'entête
msg['Subject'] = ''
msg['From'] = ''
msg['To'] = ''
texte_message =''
msg.attach(MIMEText(gbl.objets['texte_message'], 'plain'))
# Insérer la pièce jointe
part = MIMEBase('application', 'pdf')
html_template = ''
p_jointe = btfs(html_template, 'html.parser')
# I ran several test, commenting the appropriate lines
pj0 = '/home/arbiel/Téléchargements/ANNEXES 1 A 5.pdf'
pj1 = '/home/arbiel/Téléchargements/Quittance N°132 (123 €).pdf'
# with open(pj0, "rb") as f:
# part.set_payload(f.read())
# encoders.encode_base64(part)
# part.add_header(
# 'Content-Disposition',
# f'attachment; filename="{pj0.split("/")[-1]}"',
# )
# msg.attach(part)
# convert p_jointe to pdf into pj1
with open(pj1, "rb") as f:
part.set_payload(f.read())
encoders.encode_base64(part)
part.add_header(
'Content-Disposition',
f'attachment; filename="{pj1.split("/")[-1]}',
)
msg.attach(part)
# contacter le serveur
mailServer = smtplib.SMTP('outgoing_server','outgoing_port')
s = mailServer.login('sender_email', send_password')
# envoyer le courriel
mailServer.send_message(msg)
mailServer.close()
using Ubuntu 22.04.5 LTS, Python 3.10.12
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)
having substituted «https://www.lilo.org/fr/» to google, «https://protonmail.com/» to any other unsafe mail service and bépo to azerty (french keyboard layouts)
