Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 email attachement with non-ascii characters will be renamed
#1
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 ...


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)

Quote
#2
Try something like this:
filename=file.encode('utf-8')
Quote
#3
That is not possible - if I try to change line 27 to

msg.add_attachment(open(path, "r").read(), filename=file.encode('utf-8'))
then I only get a lot of error-messages like this:

C:\Users\ESchuett>"C:/Program Files (x86)/Python38-32/python.exe" g:/Posteingang/Python-Forum/sendmail_2020-04-07.py
Traceback (most recent call last):
File "g:/Posteingang/Python-Forum/sendmail_2020-04-07.py", line 27, in <module>
msg.add_attachment(open(path, "r").read(), filename=file.encode('utf-8')) # Attachement mit Umlaut wird umbenannt!
File "C:\Program Files (x86)\Python38-32\lib\email\message.py", line 1156, in add_attachment
self._add_multipart('mixed', *args, _disp='attachment', **kw)
File "C:\Program Files (x86)\Python38-32\lib\email\message.py", line 1144, in _add_multipart
part.set_content(*args, **kw)
File "C:\Program Files (x86)\Python38-32\lib\email\message.py", line 1171, in set_content
super().set_content(*args, **kw)
File "C:\Program Files (x86)\Python38-32\lib\email\message.py", line 1101, in set_content
content_manager.set_content(self, *args, **kw)
File "C:\Program Files (x86)\Python38-32\lib\email\contentmanager.py", line 37, in set_content
handler(msg, obj, *args, **kw)
File "C:\Program Files (x86)\Python38-32\lib\email\contentmanager.py", line 191, in set_text_content
_finalize_set(msg, disposition, filename, cid, params)
File "C:\Program Files (x86)\Python38-32\lib\email\contentmanager.py", line 118, in _finalize_set
msg.set_param('filename',
File "C:\Program Files (x86)\Python38-32\lib\email\message.py", line 731, in set_param
[ctype, _formatparam(param, value, requote)])
File "C:\Program Files (x86)\Python38-32\lib\email\message.py", line 59, in _formatparam
value.encode('ascii')
AttributeError: 'bytes' object has no attribute 'encode'
Quote
#4
Right, my bad. Go back with your previous version. What is your output of
print(msg)
before send? Because I tried your code (without sending so far). Do you have information about 'utf-8' and filename is correct? See below my output
From:  EINGEBEN 
Subject: KÖST.txt
To:  EINGEBEN 
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============9220985300008811412=="

--===============9220985300008811412==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

This is the message body

--===============9220985300008811412==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="KÖST.txt"
MIME-Version: 1.0



--===============9220985300008811412==--
Take a loook on other sending emails implementations here:
https://stackoverflow.com/questions/3362...ttachments
http://code.activestate.com/recipes/5781...-python-3/
Quote
#5
The output from "print(msg) reads like that:

Microsoft Windows [Version 6.1.7601]
Copyright © 2009 Microsoft Corporation. Alle Rechte vorbehalten.

C:\Users\XXX>"C:/Program Files (x86)/Python38-32/python.exe" g:/Posteingang/Python-Forum/sendmail.py
From: ( = sent from)
Subject: KÖST.txt
To: ( = sent to)
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============0917264486=="

--===============0917264486==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit

This is the message body

--===============0917264486==
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="KÖST.txt"
MIME-Version: 1.0
--===============0917264486==--

but the problem as far as I can see it is that the filename is printed correct even if there are non-ascii-characters included (if I include somewhere "print (filename)" the console shows "KÖST.txt".

Only if the message is sent the filename is changed to "attych3r.txt" and I think that could be a bug in the email-module which is new in Python 3.x because in the subject and in the body of the same message I could use "KÖST.txt" and it is shown correctly when the message is sent in the Inbox.

I have just now tried no. 5 ( = Another way with python 3 (If someone is searching):" but as in all other examples it is the same problem - "KÖST.txt" will be printed in Python and if the message is sent it is shown correctly in the "subject" and in the "body" but an attachment with the name "KÖST.txt" is renamed in the message.

I really appreciate your help but I think it's a bug in the email-module.

Heureka - I have just tried (a bit modified for our mail-server)

http://code.activestate.com/recipes/5781...-python-3/

and it works fine - the attachment "KÖST.txt" is sent and is shown in the InBox as "KÖST.txt".

Thank you very much for your help!

Smile Smile Smile
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)