mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Sending books by e-mail: Preserve non-English characters in attached filenames
This commit is contained in:
parent
8bc11e1d91
commit
4391311d56
@ -18,7 +18,6 @@ from qt.core import (
|
|||||||
|
|
||||||
from calibre.utils.smtp import (compose_mail, sendmail, extract_email_address,
|
from calibre.utils.smtp import (compose_mail, sendmail, extract_email_address,
|
||||||
config as email_config)
|
config as email_config)
|
||||||
from calibre.utils.filenames import ascii_filename
|
|
||||||
from calibre.customize.ui import available_input_formats, available_output_formats
|
from calibre.customize.ui import available_input_formats, available_output_formats
|
||||||
from calibre.ebooks.metadata import authors_to_string
|
from calibre.ebooks.metadata import authors_to_string
|
||||||
from calibre.constants import preferred_encoding
|
from calibre.constants import preferred_encoding
|
||||||
@ -168,7 +167,7 @@ def email_news(mi, remove, get_fmts, done, job_manager):
|
|||||||
subjects = [_('News:')+' '+mi.title]
|
subjects = [_('News:')+' '+mi.title]
|
||||||
texts = [_(
|
texts = [_(
|
||||||
'Attached is the %s periodical downloaded by calibre.') % (mi.title,)]
|
'Attached is the %s periodical downloaded by calibre.') % (mi.title,)]
|
||||||
attachment_names = [ascii_filename(mi.title)+os.path.splitext(attachment)[1]]
|
attachment_names = [mi.title+os.path.splitext(attachment)[1]]
|
||||||
attachments = [attachment]
|
attachments = [attachment]
|
||||||
jobnames = [mi.title]
|
jobnames = [mi.title]
|
||||||
do_remove = []
|
do_remove = []
|
||||||
@ -408,7 +407,7 @@ class EmailMixin: # {{{
|
|||||||
if mi.comments and gprefs['add_comments_to_email']:
|
if mi.comments and gprefs['add_comments_to_email']:
|
||||||
from calibre.utils.html2text import html2text
|
from calibre.utils.html2text import html2text
|
||||||
texts[-1] += '\n\n' + _('About this book:') + '\n\n' + textwrap.fill(html2text(mi.comments))
|
texts[-1] += '\n\n' + _('About this book:') + '\n\n' + textwrap.fill(html2text(mi.comments))
|
||||||
prefix = ascii_filename(t+' - '+a)
|
prefix = f'{t} - {a}'
|
||||||
if not isinstance(prefix, unicode_type):
|
if not isinstance(prefix, unicode_type):
|
||||||
prefix = prefix.decode(preferred_encoding, 'replace')
|
prefix = prefix.decode(preferred_encoding, 'replace')
|
||||||
attachment_names.append(prefix + os.path.splitext(f)[1])
|
attachment_names.append(prefix + os.path.splitext(f)[1])
|
||||||
|
@ -76,41 +76,32 @@ def create_mail(from_, to, subject, text=None, attachment_data=None,
|
|||||||
attachment_type=None, attachment_name=None):
|
attachment_type=None, attachment_name=None):
|
||||||
assert text or attachment_data
|
assert text or attachment_data
|
||||||
|
|
||||||
from email.mime.multipart import MIMEMultipart
|
from email.message import EmailMessage
|
||||||
from email.utils import formatdate
|
from email.utils import formatdate
|
||||||
from email import encoders
|
|
||||||
import uuid
|
import uuid
|
||||||
|
|
||||||
outer = MIMEMultipart()
|
outer = EmailMessage()
|
||||||
outer['Subject'] = subject
|
|
||||||
outer['To'] = to
|
|
||||||
outer['From'] = from_
|
outer['From'] = from_
|
||||||
|
outer['To'] = to
|
||||||
|
outer['Subject'] = subject
|
||||||
outer['Date'] = formatdate(localtime=True)
|
outer['Date'] = formatdate(localtime=True)
|
||||||
outer['Message-Id'] = "<{}@{}>".format(uuid.uuid4(), get_msgid_domain(from_))
|
outer['Message-Id'] = "<{}@{}>".format(uuid.uuid4(), get_msgid_domain(from_))
|
||||||
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
|
outer.preamble = 'You will not see this in a MIME-aware mail reader.\n'
|
||||||
|
|
||||||
if text is not None:
|
if text is not None:
|
||||||
from email.mime.text import MIMEText
|
|
||||||
if isbytestring(text):
|
if isbytestring(text):
|
||||||
msg = MIMEText(text)
|
text = text.decode('utf-8', 'replace')
|
||||||
else:
|
outer.set_content(text)
|
||||||
msg = MIMEText(text, 'plain', 'utf-8')
|
|
||||||
outer.attach(msg)
|
|
||||||
|
|
||||||
if attachment_data is not None:
|
if attachment_data is not None:
|
||||||
from email.mime.base import MIMEBase
|
|
||||||
from email.header import Header
|
|
||||||
assert attachment_data and attachment_name
|
assert attachment_data and attachment_name
|
||||||
try:
|
try:
|
||||||
maintype, subtype = attachment_type.split('/', 1)
|
maintype, subtype = attachment_type.split('/', 1)
|
||||||
except AttributeError:
|
except Exception:
|
||||||
maintype, subtype = 'application', 'octet-stream'
|
maintype, subtype = 'application', 'octet-stream'
|
||||||
msg = MIMEBase(maintype, subtype, name=Header(attachment_name, 'utf-8').encode())
|
if isinstance(attachment_data, str):
|
||||||
msg.set_payload(attachment_data)
|
attachment_data = attachment_data.encode('utf-8')
|
||||||
encoders.encode_base64(msg)
|
outer.add_attachment(attachment_data, maintype=maintype, subtype=subtype, filename=attachment_name)
|
||||||
msg.add_header('Content-Disposition', 'attachment',
|
|
||||||
filename=Header(attachment_name, 'utf-8').encode())
|
|
||||||
outer.attach(msg)
|
|
||||||
|
|
||||||
return outer
|
return outer
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user