Apr-06-2020, 09:13 PM
I have created code to send an email with an attachement and it works fine as long as there are no non-ascii characters in the file name and it works fine if there are only ascii-characters in the file-name - e.g. file "KOEST.txt" will be sent and is correctly named "KOEST.txt" in the InBox at the receivers end; but if the file-name contains a non-ascii-character - e.g. file "KÖST.TXT" it is sent but in the InBox at the receivers and that file is suddenly called "attych3r.txt" (and if I send the same message again the file gets another name - but never "KÖST.txt".
I'm using Python 3.8 in Windows 7 and a domino-Server with an IBM-Notes-Client and I do not think that the server or the client could be the problem because we have sent messages with attachments like that from a scanner and file-names like "KÖST.txt" were never a problem and were sent to my InBox with the attachment correctly named "KÖST.txt".
I have already searched with Google but could not find a solution - the non-ascii-characters are not a problem in the "subject" and in the "body" (the module "email" changes everything automatically to UTF-8)but file-names with a non-ascii-character seem to be a problem.
I have already searched in 4 really complete books to Python 3.x and also in the documentation - but all the examples I could find told me what I already knew but it seems nobody ever tried to send an attachement with non-ascii-characters.
I'd be really grateful if somebody could solve the problem ...
I'm using Python 3.8 in Windows 7 and a domino-Server with an IBM-Notes-Client and I do not think that the server or the client could be the problem because we have sent messages with attachments like that from a scanner and file-names like "KÖST.txt" were never a problem and were sent to my InBox with the attachment correctly named "KÖST.txt".
I have already searched with Google but could not find a solution - the non-ascii-characters are not a problem in the "subject" and in the "body" (the module "email" changes everything automatically to UTF-8)but file-names with a non-ascii-character seem to be a problem.
I have already searched in 4 really complete books to Python 3.x and also in the documentation - but all the examples I could find told me what I already knew but it seems nobody ever tried to send an attachement with non-ascii-characters.
I'd be really grateful if somebody could solve the problem ...
import smtplib import os # damit os.path.join funktioniert from email.message import EmailMessage # vgl. https://stackoverflow.com/questions/954 ... on-smtplib # Variablen zum Testen definieren (das File muss im angegebenen Ordner stehen) ordner = r'G:\Posteingang\Ordner_Test' file = 'KÖST.txt' # - Attachement wird umbenannt??? # file = 'KOEST.txt' # - Attachement wird NICHT umbenannt path = os.path.join(ordner, file) # stellt Dateinamen + Pfad in die Variable # me == the sender's email address me = " EINGEBEN " # Mail-Adresse des Absenders! # you == the recipient's email address you = " EINGEBEN " # Mail-Adresse des Empfängers! msg = EmailMessage() # muss VOR Verwendung von msg stehen msg["From"] = me msg["Subject"] = file # Umlaute im Subject sind kein Problem?! msg["To"] = you msg.set_content("This is the message body") # msg.add_attachment(open(filename, "r").read(), filename=path) msg.add_attachment(open(path, "r").read(), filename=file) # Attachement mit Umlaut wird umbenannt! # s = smtplib.SMTP('smtp.sendgrid.net', 587) s = smtplib.SMTP( EINGEBEN ) # (" Mail-Server", Port) # s.login(USERNAME, PASSWORD) s.send_message(msg)