Apr-18-2025, 12:36 PM
Hi to all of you
I create html documents, with bs4, which I convert into pdf files with xhtml2pdf, then into electronic messages with email and send with smtplib.
0) Import commads
The sequence of my script is as follows:
1) create the message
A few lines from the html file:
Sending such documents via Thunderbird works fine.
Arbiel
I create html documents, with bs4, which I convert into pdf files with xhtml2pdf, then into electronic messages with email and send with smtplib.
0) Import commads
from bs4 import BeautifulSoup as btfs from xhtml2pdf import pisa from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.mime.base import MIMEBase from email import encoders import smtplibAll works fine until I attempt to send the e-mail.
The sequence of my script is as follows:
1) create the message
texte_message = 'blablaba' msg = MIMEMultipart() # 2. Compléter l'entête msg['Subject'] = "Sujet du courriel" msg['Keywords'] = "mots clés" msg['From'] = nom_expéditeur msg['To'] = destinataire msg['Bcc'] = expéditeur['copie'] msg.attach(MIMEText(texte_message, 'plain'))2) add attachment
# Insertion de la pièce jointe part = MIMEBase('application', 'octet-stream') part.set_payload(pj) encoders.encode_base64(part) part.add_header( 'Content-Disposition', f'attachment; filename={pj.split("/")[-1]}', ) msg.attach(part)3) send the mail
with smtplib.SMTP_SSL(expéditeur['serveur_sortant'], expéditeur['port_sortant']) as mailServer : s = mailServer.login(expéditeur, expéditeur['mdp']) mailServer.send_message(msg) mailServer.close()4) python's result
Output: s = mailServer.login(expéditeur, expéditeur['mdp'])
File "/usr/lib/python3.10/smtplib.py", line 739, in login
(code, resp) = self.auth(
File "/usr/lib/python3.10/smtplib.py", line 641, in auth
response = encode_base64(initial_response.encode('ascii'), eol='')
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 62: ordinal not in range(128)
\xe9 corresponds to the “é” character which is actually present in the document I've created.A few lines from the html file:
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> … <p class="Espacées"><span class="T2" title="expéditeur"><br/>Rémi Mathieu<br/>'nnn rue yyyy<br/>39220 Les Rousses</span></p> <p>Je soussigné, Rémi Mathieu,</p>I don't know how to start fixing this problem.
Sending such documents via Thunderbird works fine.
Arbiel
using Ubuntu 18.04.4 LTS, Python 3.8
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)