Answer the question
In order to leave comments, you need to log in
Why do files in Russian attached to a letter arrive broken?
In my application, some notifications are duplicated by sending a letter to the mail. Files uploaded by other users are attached to letters and they are often named in Russian. Empirically, it was calculated that if the file name is written in Latin, then everything comes fine, and those that have Cyrillic in the name come in .dat format.
It is also interesting that in mailboxes in the browser, files with Russian characters in the name can be displayed normally, but in Outlook the same letter will contain broken files.
Here is the code for sending the email:
if self.send_mail and not self.mail_was_send:
self.mail_was_send = True
msg = MIMEMultipart()
msg["Subject"] = self.short_text
msg["From"] = EMAIL_HOST_USER
msg["To"] = self.user.email
msg.attach(MIMEText(self.text))
# получаем список файлов действвия и добавляем их в сообщение
for document in documents if documents else []:
path = os.path.join(MEDIA_ROOT, document.file.path)
attachment = MIMEApplication(open(path, "rb").read(), _subtype=document.file.name.split(".")[-1],
name=document.filename)
attachment.add_header('Content-Disposition', 'attachment', filename=os.path.basename(path))
msg.attach(attachment)
s = smtplib.SMTP_SSL('smtp.yandex.ru', 465)
s.ehlo(EMAIL_HOST_USER)
s.login(EMAIL_HOST_USER, EMAIL_HOST_PASSWORD)
s.sendmail(EMAIL_HOST_USER, [self.user.email], msg.as_string())
s.quit()
Answer the question
In order to leave comments, you need to log in
How can I specify the encoding for a file name? If I can have both English and Russian files.
// set appropriate headers for attachment or streamed file
// The theoretically correct syntax for use of UTF-8 in Content-Disposition is very weird: filename*=UTF-8''foo%c3%a4 (yes, that's an asterisk, and no quotes except an empty single quote in the middle)
// This example is the same as the one above, but adding the "filename" parameter for compatibility with user agents not implementing RFC 5987:
// Content-Disposition: attachment;
// filename="EURO rates";
// filename*=utf-8''%e2%82%ac%20rates
// Note: Those user agents that do not support the RFC 5987 encoding ignore “filename*” when it occurs after “filename”.
// http://tools.ietf.org/html/rfc6266#section-5
//
header('Content-Disposition: attachment; filename="'.rawurlencode($file_name).'"; filename*=utf-8\'\''.rawurlencode($file_name));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question