J'essaye de joindre un fichier PDF à un message électronique.Email Python PDF: Quelques fichiers PDF se corrompre
Pour un PDF (un document Word imprimé en PDF), cela fonctionne (le destinataire l'ouvre dans Outlook sans problème).
Pourtant, pour les autres fichiers PDF (qui semblent identiques à l'exception de quelques Ko de plus), ils sont corrompus.
Here est un exemple à utiliser qui échoue (devient corrompu).
import smtplib, os
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.utils import formatdate
from email import encoders
attachment_path=r'C:\Directory'+'\\'
login='login'
password='password'
part=MIMEBase('application',"octet-stream")
def message(attachment): #attachment is just the PDF file name
fromaddr = "[email protected]"
cc=fromaddr
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = "[email protected]"
msg['Date'] = formatdate(localtime = True)
msg['Subject'] = "Subject"
body='''
<!DOCTYPE html>
<html>
<body>
<p><font face="Tahoma" size=2> I hope everything is going well.</p></font>
</body>
</html>
'''
msg.attach(MIMEText(body, 'html'))
part.set_payload(open(attachment_path+attachment,'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(attachment_path+attachment)))
msg.attach(part)
mail=smtplib.SMTP('Server',587)
mail.ehlo()
mail.starttls()
mail.login(login,password)
mail.sendmail(fromaddr,[toaddr,cc],msg.as_string())
J'ai essayé d'utiliser les éléments suivants au lieu de la base 64 de codage, mais en vain:
encoders.encode_noop(part)
encoders.encode_7or8bit(part)
encoders.encode_quopri(part)
Merci à l'avance!