From 1ffd7b06ec8cf8868e0ab1d1bafd44c19fde72e7 Mon Sep 17 00:00:00 2001 From: ericji Date: Tue, 19 Mar 2024 13:47:16 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9C=AA=E5=91=BD=E5=90=8D=E9=99=84=E4=BB=B6fi?= =?UTF-8?q?x?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- yagmail/__main__.py | 13 ++++++++++++- yagmail/message.py | 7 ++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/yagmail/__main__.py b/yagmail/__main__.py index 74420e6..45d9a85 100644 --- a/yagmail/__main__.py +++ b/yagmail/__main__.py @@ -1,5 +1,4 @@ from yagmail.sender import SMTP -import sys try: import keyring @@ -28,3 +27,15 @@ def main(): args = parser.parse_args() yag = SMTP(args.user, args.password) yag.send(to=args.to, subject=args.subject, contents=args.contents, attachments=args.attachments) + + +if __name__ == '__main__': + sender_email = 'xxx@163.com' + password = 'xxx' + host = 'smtp.163.com' + + email = 'xxx@163.com' + title = '发送结果数据utf8-123' + attachments = ['/v-data/中文名附件测试2023.txt'] + yag = SMTP(user=sender_email, password=password, host=host) + yag.send(to=email, subject=title, contents=title, attachments=attachments) \ No newline at end of file diff --git a/yagmail/message.py b/yagmail/message.py index 4594889..5b08200 100644 --- a/yagmail/message.py +++ b/yagmail/message.py @@ -1,3 +1,4 @@ +import base64 import datetime import email.encoders import io @@ -188,6 +189,8 @@ def get_mime_object(is_marked_up, content_string, encoding): is_raw = type(content_string) == raw try: is_file = os.path.isfile(content_string) + if is_file: + content_name = os.path.basename(content_string) except ValueError: is_file = False content_name = str(abs(hash(content_string))) @@ -232,7 +235,9 @@ def get_mime_object(is_marked_up, content_string, encoding): mime_object = MIMEBase(content_object["main_type"], content_object["sub_type"], name=(encoding, "", content_name)) mime_object.set_payload(content) - if content_object["main_type"] == "application": + # if content_object["main_type"] == "application": + if is_file: + content_name = (str(base64.b64encode(content_name.encode('utf-8'))).replace("b'", "=?utf-8?b?").replace("'", "?=")) mime_object.add_header("Content-Disposition", "attachment", filename=content_name) content_object["mime_object"] = mime_object return content_object -- Gitee