diff --git a/yagmail/__main__.py b/yagmail/__main__.py index 74420e6919f4fc7b492cf2ebf05be1924ce91af4..45d9a85b5fffb92284114d053b815ce989da0ff3 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 459488923ca28262eb397f69e9389bf7ebcf0cbf..5b08200ac3bb8bc704f5a16d55fd7e2b8c97a5fd 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