Hotmail/Outlook 不显示 Python SMTPLib 电子邮件的正文

Hotmail/Outlook don't show body of Python SMTPLib Email

提问人:Matheus Mansour 提问时间:9/27/2023 最后编辑:tripleeeMatheus Mansour 更新时间:9/27/2023 访问量:80

问:

我正在尝试使用 Python 发送电子邮件,但它只是在 Hotmail/Outlook 中显示为空白。在其他提供商中一切都很好,电子邮件原始内容以 base64 显示 html 正文。但不是在 Hotmail/Outlook 中!!它甚至没有显示我的消息的内容部分。smtplib

我已经在这个上呆了几个小时了,我不知道如何解决它。以前有人经历过这种情况吗?这是我的代码:

text = f"Este és su código. Este cógido caduca en 1 hora."
html = (
    f'''
        <html>
            <body>
                <p>Este és su código.</p>
                <p style=\"color:tomato; font-size:25px;letter-spacing:2px;\"><b>1234</b></p><p>Este código <b>caduca en 1 hora</b>.</p>
            </body>
        </html>
    '''
)

port = 465
smtp_server = "smtpout.secureserver.net"
email_domain = "mydomain.com"

content = MIMEMultipart("mixed")
content["Subject"] = subject
content["From"] = f"My User<{EMAIL_ADDRESS}>"
content["To"] = company.email
content['Date'] = email.utils.formatdate()
content['Message-ID'] = email.utils.make_msgid(domain=email_domain)

body = MIMEMultipart("alternative")

part1 = MIMEText(text, "plain", 'UTF-8')
part2 = MIMEText(html, "html")

body.attach(part1)
body.attach(part2)

content.attach(body)

context = ssl.create_default_context()

try:
    # Python 3 libraries expect bytes.
    msg_data = content.as_bytes()
except:
    # Python 2 libraries expect strings.
    msg_data = content.as_string()


with SMTP_SSL(smtp_server, port, context=context) as server:
    server.login(EMAIL_ADDRESS, EMAIL_PASSWORD)
    server.sendmail(EMAIL_ADDRESS, company.email, msg_data)

尝试在 HTML 中添加前导空行,但没有成功。我希望电子邮件正文显示在 Hotmail/Outlook 中。

这是我的变量:msg_data

b'Content-Type: multipart/alternative;\n boundary="===============6009941277021292094=="\nMIME-Version: 1.0\nSubject: =?utf-8?q?Restablezca_su_contrase=C3=B1a_de_Pix_Global?=\nFrom: Pix Global <[email protected]>\nTo: [email protected]\nDate: Sun, 24 Sep 2023 14:20:26 -0000\nMessage-ID: <[email protected]>\nDKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=globalpix.shop;\n [email protected]; q=dns/txt; s=20230818; t=1695565226; h=to : from :\n subject; bh=H5ViRl7MX8W8vE0Xl9ATs0qJDS5xj24KAAdRTa8kNJ0=;\n b=XVWSKZshE+RNUa//VNsMSj7fOty5UMlnCq2n01N34KLrJW02QSeu9MZywUahjxdiiLZav\n cT5ZeikXEyBSfZux6pvn0Ze4AqxbVz6H89XvLCMHIMgeIONr/LAm3q/7LmZKS42vTsGb+iL\n lij5O31TU5a8vTMpke5ukBom/sUFQqA=\n\n--===============6009941277021292094==\nContent-Type: text/plain; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: base64\n\nRnJvbTogaG9sYUBnbG9iYWxwaXguc2hvcA0KVG86IG1hdGhldXNtYW5zb3VydGVzdGVAaG90bWFp\nbC5jb20NCg0Kw4lzdGUgZXMgc3UgUElOIGRlIFBpeCBHbG9iYWwgcGFyYSByZXN0YWJsZWNlciBz\ndSBjb250cmFzZcOxYS4uIEVzdGUgY8OzZ2lkbyBjYWR1Y2EgZW4gMSBob3JhLg==\n\n--===============6009941277021292094==\nContent-Type: text/html; charset="utf-8"\nMIME-Version: 1.0\nContent-Transfer-Encoding: base64\n\nRnJvbTogaG9sYUBnbG9iYWxwaXguc2hvcA0KVG86IG1hdGhldXNtYW5zb3VydGVzdGVAaG90bWFp\nbC5jb20NCg0KPGh0bWw+PGJvZHk+PHA+w4lzdGUgZXMgc3UgUElOIGRlIFBpeCBHbG9iYWwgcGFy\nYSByZXN0YWJsZWNlciBzdSBjb250cmFzZcOxYS48L3A+PHAgc3R5bGU9ImNvbG9yOnRvbWF0bzsK\nZm9udC1zaXplOjI1cHg7bGV0dGVyLXNwYWNpbmc6MnB4OyI+PGI+OTg3MjwvYj48L3A+CjxwPkVz\ndGUgY8OzZGlnbyA8Yj5jYWR1Y2EgZW4gMSBob3JhPC9iPi48L3A+PC9ib2R5PjwvaHRtbD4=\n\n--===============6009941277021292094==--\n'

Outlook 邮件源:

Received: from SJ0PR03MB7080.namprd03.prod.outlook.com (2603:10b6:a03:4e9::11)
 by CY4PR03MB3399.namprd03.prod.outlook.com with HTTPS; Wed, 27 Sep 2023
 03:26:58 +0000
Received: from MW4PR03CA0006.namprd03.prod.outlook.com (2603:10b6:303:8f::11)
 by SJ0PR03MB7080.namprd03.prod.outlook.com (2603:10b6:a03:4e9::11) with
 Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6813.21; Wed, 27 Sep
 2023 03:26:58 +0000
Received: from CO1NAM11FT064.eop-nam11.prod.protection.outlook.com
 (2603:10b6:303:8f:cafe::ed) by MW4PR03CA0006.outlook.office365.com
 (2603:10b6:303:8f::11) with Microsoft SMTP Server (version=TLS1_2,
 cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6792.35 via Frontend
 Transport; Wed, 27 Sep 2023 03:26:57 +0000
Authentication-Results: spf=pass (sender IP is 97.74.135.230)
 smtp.mailfrom=globalpix.shop; dkim=none (message not signed)
 header.d=none;dmarc=pass action=none header.from=globalpix.shop;compauth=pass
 reason=100
Received-SPF: Pass (protection.outlook.com: domain of globalpix.shop
 designates 97.74.135.230 as permitted sender)
 receiver=protection.outlook.com; client-ip=97.74.135.230;
 helo=p3plsmtpa14-10.prod.phx3.secureserver.net; pr=C
Received: from p3plsmtpa14-10.prod.phx3.secureserver.net (97.74.135.230) by
 CO1NAM11FT064.mail.protection.outlook.com (10.13.175.77) with Microsoft SMTP
 Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id
 15.20.6838.21 via Frontend Transport; Wed, 27 Sep 2023 03:26:57 +0000
X-IncomingTopHeaderMarker:
 OriginalChecksum:010B9E785D7A8D5F802268200768DD3279404A2F7EC607EAC9DB5F0D75EA68EF;UpperCasedChecksum:50EF3B1D2877EE5C1BCA5DC065E0FC5C1AAD2A0E3A3FBF36F0C7B5861C041DC1;SizeAsReceived:2000;Count:11
Received: from [172.18.0.2] ([191.17.88.132])
    by :SMTPAUTH: with ESMTPSA
    id lLCRqVyd1gLd2lLCSqc8gQ; Tue, 26 Sep 2023 20:26:57 -0700
X-CMAE-Analysis: v=2.4 cv=b9d0oPKx c=1 sm=1 tr=0 ts=6513a101
 a=GTfYd3sRU+i6G4xf9tOLfQ==:117 a=GTfYd3sRU+i6G4xf9tOLfQ==:17
 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=dCs_blNjXhEA:10 a=5KLPUuaC_9wA:10
 a=CYqRCMiiIZHEVys7HUIA:9 a=QEXdDO2ut3YA:10 a=58YBMok73DsF6R3qTEYA:9
 a=_W_S_7VecoQA:10
X-SECURESERVER-ACCT: [email protected]
Content-Type: text/plain
Subject: =?utf-8?q?Restablezca_su_contrase=C3=B1a_de_Pix_Global=3A_1653?=
From: Pix Global <[email protected]>
To: [email protected]
Date: Sun, 24 Sep 2023 16:53:53 -0000
Message-ID: <[email protected]>
X-CMAE-Envelope: MS4xfCviOwvmudnYh3rvEvF/2Q8q0I68VChNvfuBcwwl8mQL/k1BY3fEUNshWhHB3DqeXNm3poFIoQMAVeAsfLRo7noEkYrptmqV+sdJ/WSm0GhO80gBB9MJ
 4Kqlg1wCFqyUOF+zdZmBKCqYmUhKC6yuRhFtift8uJFj4xmqvRRYfYLSrJCrtEsswwm0zlawTKVwQfLU+9TkS8dR5fcFf3K7prE=
X-IncomingHeaderCount: 11
Return-Path: [email protected]
X-MS-Exchange-Organization-ExpirationStartTime: 27 Sep 2023 03:26:57.9283
 (UTC)
X-MS-Exchange-Organization-ExpirationStartTimeReason: OriginalSubmit
X-MS-Exchange-Organization-ExpirationInterval: 1:00:00:00.0000000
X-MS-Exchange-Organization-ExpirationIntervalReason: OriginalSubmit
X-MS-Exchange-Organization-Network-Message-Id:
 d76bd302-9b3c-431b-3e22-08dbbf099b02
X-EOPAttributedMessage: 0
X-EOPTenantAttributedMessage: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa:0
X-MS-Exchange-Organization-MessageDirectionality: Incoming
X-MS-PublicTrafficType: Email
X-MS-TrafficTypeDiagnostic:
 CO1NAM11FT064:EE_|SJ0PR03MB7080:EE_|CY4PR03MB3399:EE_
X-MS-Exchange-Organization-AuthSource:
 CO1NAM11FT064.eop-nam11.prod.protection.outlook.com
X-MS-Exchange-Organization-AuthAs: Anonymous
X-MS-UserLastLogonTime: 9/27/2023 3:11:11 AM
X-MS-Office365-Filtering-Correlation-Id: d76bd302-9b3c-431b-3e22-08dbbf099b02
X-MS-Exchange-EOPDirect: true
X-Sender-IP: 97.74.135.230
X-SID-PRA: HOL[email protected]
X-SID-Result: PASS
X-MS-Exchange-Organization-PCL: 2
X-MS-Exchange-Organization-SCL: 1
X-Microsoft-Antispam: BCL:0;
X-MS-Exchange-CrossTenant-OriginalArrivalTime: 27 Sep 2023 03:26:57.7721
 (UTC)
X-MS-Exchange-CrossTenant-Network-Message-Id: d76bd302-9b3c-431b-3e22-08dbbf099b02
X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa
X-MS-Exchange-CrossTenant-AuthSource:
 CO1NAM11FT064.eop-nam11.prod.protection.outlook.com
X-MS-Exchange-CrossTenant-AuthAs: Anonymous
X-MS-Exchange-CrossTenant-FromEntityHeader: Internet
X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg:
 00000000-0000-0000-0000-000000000000
X-MS-Exchange-Transport-CrossTenantHeadersStamped: SJ0PR03MB7080
X-MS-Exchange-Transport-EndToEndLatency: 00:00:01.2273647
X-MS-Exchange-Processed-By-BccFoldering: 15.20.6813.027
X-Microsoft-Antispam-Mailbox-Delivery:
    wl:1;pcwl:1;ucf:0;jmr:0;ex:0;auth:1;dest:I;OFR:TrustedSenderList;ENG:(5062000305)(920221119095)(90000117)(920221120095)(91040095)(9050020)(9060121)(9100341)(944500132)(2008001134)(4810010)(4910033)(9930004)(9545005)(10172021)(9439006)(9310011)(9220031)(120001);
X-Message-Info:
    qZelhIiYnPlDALU4MEr+nZRXojxj8gEC6CFLScfQxVf6dHI0/ZZuX393R27b5Tlw1d+U36zzw7WMxDWPLADTJMgniNHoqap1CaLhi2snzZcu/i0d2eM9tr+Tz0sPOR20nABo1trfoBPIyRUOfNdDnS/X8D9xQxbCRsaEtx64Lp5RrlJG0cfXKWy6w+Svie9p+RG9Ii1bseIS6kZn7rO7Yg==
X-Message-Delivery: Vj0xLjE7dXM9MDtsPTA7YT0wO0Q9MTtHRD0yO1NDTD0tMQ==
X-Microsoft-Antispam-Message-Info:
    =?iso-8859-1?Q?zzf2sspx8xNxHZnSiMC2dvppZRK9+0cve53FR1a/aZoGM7HiWeL8pGCWWK?=
 =?iso-8859-1?Q?j4yJUiql21jtKlgXdMqf3EbduSdshr1Z6tsKkCVq7io2WqUbEexf3RyeDo?=
 =?iso-8859-1?Q?kKZpLFDKih7ZnYxaoMkycBq7PECCZ+ZaJwu9SN83oEV3X0qftLaqZLGvpz?=
 =?iso-8859-1?Q?iAMUhL+nXm3zvpWYuMZSycEeQSFyKcs78YyuzuhaL/SKNwAJs/nC/BJFMB?=
 =?iso-8859-1?Q?wZpg/3Be7J1tz9bSw5N+BXWcCatPefB6X+gyK7JraBvFcf4ertKtPtZ8oY?=
 =?iso-8859-1?Q?i8/y48zZJi2xGD4qzYIVUoDDOOLfEIUALcyQ1h5DNTmOYjUSUK1QTbxLaX?=
 =?iso-8859-1?Q?Qj38DGxPan8tx43qw4sU1TvsPrupmdFiAeRegFZ3ePUTtze0sicDltkOEB?=
 =?iso-8859-1?Q?6lUv+3g52y+8gBvhTrxGmFcQkt3P4F+/nk4zStvZyiYXLfA365p16mi1+c?=
 =?iso-8859-1?Q?4O34Qoi6KVW68vHkYaKhWWRpGEvEWRci96kKllvJNrAg9oSIiya3YBWODE?=
 =?iso-8859-1?Q?VRNHbzb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DL/h5l4+XNOShR/9osRmKIUgbZCIUOeM?=
 =?iso-8859-1?Q?b7/o82G/zP/uqFSei3UXQxzKYjD6aLYNG27hIXYDmdgtpko+IQHq7VgJ2F?=
 =?iso-8859-1?Q?aktnwwg4EN1nawA3j3fbC5WV6sGF8JQON9E+JW/MgE/3iJqpMmhhCFfuXS?=
 =?iso-8859-1?Q?k85l+A+juN78Tn/ZBZcAXNhSt+cPOQ2YPnZacO+Wd2wVmg3iGiitXLAC8D?=
 =?iso-8859-1?Q?3ZPuC6XKzHI8UENmZTLvQozEvsQ1TomfkP51Qxl2JdKiJl7dNcCytXBgdY?=
 =?iso-8859-1?Q?avILLi8yTWFkcPfLHLBtQFvHPNkrWbC90fryniPCIobid1aliu3dH25h5y?=
 =?iso-8859-1?Q?hmoRTkj1WTCQb8vTaF/xMnvsI=3D?=
MIME-Version: 1.0
电子邮件 Outlook SMTP HTML-EMAIL

评论

0赞 Dmitry Streblechenko 9/27/2023
请出示您发送的邮件的完整 MIME 源。
0赞 Matheus Mansour 9/27/2023
嗨,@DmitryStreblechenko,谢谢你的回答。我添加了msg_data变量。
0赞 Dmitry Streblechenko 9/27/2023
如果我将其保存到 eml 文件并使用 Outlook 打开它,对我来说看起来非常好。如果您将其发送到 Gmail 邮箱,然后查看邮件源,这是您看到的吗?
0赞 Matheus Mansour 9/27/2023
是的,如果我将其发送到 Gmail、Yahoo、Yandex,...它完美地显示在 msg_data 变量中。但是,当该变量由有问题的 smtp 服务器发送到 Hotmail/Outlook 时,原始邮件完全不同。它没有显示任何正文内容,如问题末尾更新的那样。你认为这可能与 smtp 服务器类型有关吗?我对这一切很陌生。
1赞 tripleee 9/27/2023
顺便说一句,您的代码似乎是为 Python 3.5 或更早版本编写的。电子邮件库在 3.6 中进行了大修,现在更加通用和合乎逻辑。可能会扔掉你所拥有的,然后从电子邮件文档中的示例重新开始。

答: 暂无答案