Bonjour,

Je cherche à envoyer un email avec une pièce jointe.

Seulement, la pièce jointe comporte des espaces et donc mon scripte envoie bien le mail mais le nom de la pièce jointe s'arrête au premier espace (ou s'appelle "Partie1.2")
La pièce jointe est bien le bon fichier (fichier .pdf) que j'arrive à lire en faisant "ouvrir avec..." (sous Windows).

Mais savez comment résoudre ce problème, s'il vous plait?

Voici mon code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import smtplib, email, ssl
 
from email import encoders
from email.mime.base import MIMEBase
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
 
subject = "Sujet du mail"
html = """\
<html>
  <body>
     <p>
         Contenu du mail.
     </p>
  </body>
</html>
"""
sender_email = "sender@mail.fr"
receiver_email = "receiver@mail.fr"
password = "XXXXX"
 
# Create a multipart message and set headers
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = subject
#message["Bcc"] = receiver_email  # Recommended for mass emails
 
# Record the MIME types of both parts - text/plain and text/html.
part2 = MIMEText(html, "html")
 
# Attach parts into message container.
# According to RFC 2046, the last part of a multipart message, in this case
# the HTML message, is best and preferred.
message.attach(part2)
 
filename = "Mon fichier.pdf"  # In same directory as script
 
# Open PDF file in binary mode
with open(filename, "rb") as attachment:
	# Add file as application/octet-stream
	# Email client can usually download this automatically as attachment
	part = MIMEBase("application", "octet-stream")
	part.set_payload(attachment.read())
 
# Encode file in ASCII characters to send by email
encoders.encode_base64(part)
 
# Add header as key/value pair to attachment part
part.add_header(
	"Content-Disposition",
	f"attachment; filename= {filename}",
	)
 
# Add attachment to message and convert message to string
message.attach(part)
text = message.as_string()
 
# Log in to server using secure context and send email
context = ssl.create_default_context()
with smtplib.SMTP_SSL("ssl0.ovh.net", 465, context=context) as server:
	server.login(sender_email, password)
	server.sendmail(sender_email, receiver_email, text)
Merci pour votre aide.

Bonne journée.

Avinetor