Python Forum
Wich encoding for an attachment with no-ascii characters
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wich encoding for an attachment with no-ascii characters
#1
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
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 smtplib
All 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)
Reply
#2
You can only send ascii characters (7 bit characters) using SMTP. To send unicode characters I think you need to use MIME.
https://www.differencebetween.net/techno...-and-mime/
https://www.geeksforgeeks.org/multipurpo...-protocol/

This link uses a standard python module named "email" to send contents other than 7 byte text.
https://docs.python.org/3/library/email.examples.html
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send Email with attachment metro17 4 6,823 Apr-14-2020, 04:59 PM
Last Post: CodeItBro
  email attachement with non-ascii characters will be renamed Erich1959 4 6,403 Apr-07-2020, 06:57 PM
Last Post: Erich1959
  Python3 JSON encoding error “string argument without an encoding” when porting python prayuktibid 0 3,220 Jan-16-2020, 02:43 PM
Last Post: prayuktibid

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020