gpt4 book ai didi

使用边界的 Python/Django mime 多部分电子邮件

转载 作者:太空宇宙 更新时间:2023-11-04 06:22:02 26 4
gpt4 key购买 nike

在 Django(使用 python)中,如何构建如下所示的电子邮件:

我需要的是一封以以下内容开头的电子邮件:

Content-Type: multipart/alternative;
boundary="=_5f8686714d769f9476ce778798b84ff4"

边界是放置纯文本的地方:

--=_5f8686714d769f9476ce778798b84ff4
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7bit

This e-mail is in HTML-format. Click the following link:

http://www.company.nl/enews/7
--=_5f8686714d769f9476ce778798b84ff4

之后我需要一个相关的 Content-Tye:

Content-Type: multipart/related;
boundary="=_e49477d06c604958b9b2bc038628a26f"

边界是放置html的地方

--=_e49477d06c604958b9b2bc038628a26f 
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.= w3.org/TR/html4/loose.dtd"> <html style=3D"height: 100%"> <head>/head> <body></body> </html>
--=_e49477d06c604958b9b2bc038628a26f

最终它看起来像这样:

Received: (qmail 6116 invoked by uid 48); 16 Jul 2012 18:00:49 +0200
Date: 16 Jul 2012 18:00:49 +0200
To: my@email.nl
Subject: Something
MIME-Version: 1.0
From: Company <company@company.nl>

Content-Type: multipart/alternative;
boundary="=_5f8686714d769f9476ce778798b84ff4"
Message-ID: <m79ghd.ezdd6g@company.nl>

--=_5f8686714d769f9476ce778798b84ff4
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 7bit

This e-mail is in HTML-format. Click the following link:

http://www.company.nl/enews/7
--=_5f8686714d769f9476ce778798b84ff4

Content-Type: multipart/related;
boundary="=_e49477d06c604958b9b2bc038628a26f"

--=_e49477d06c604958b9b2bc038628a26f
Content-Type: text/html; charset="ISO-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.=
w3.org/TR/html4/loose.dtd">
<html style=3D"height: 100%">
<head>/head>
<body></body>
</html>
--=_e49477d06c604958b9b2bc038628a26f

我用 Django 的 EmailMessage(Alternatives), python MimeMultipart 尝试过这个,但我无法按照确切的顺序得到它。

最佳答案

如果您尝试发送纯文本和 HTML 版本的电子邮件(其中纯文本版本仅指 URL),EmailMultiAlternatives类似乎适合我。

文档中示例的略微修改版本:

from django.core.mail import EmailMultiAlternatives

subject, from_email, to = 'hello', 'from@example.com', 'to@example.com'
text_content = 'This e-mail is in HTML-format. Click the following link:\n\nhttp://www.company.nl/enews/7'
html_content = '<p>This is an <strong>HTML-format</strong> version.</p>'
msg = EmailMultiAlternatives(subject, text_content, from_email, [to])
msg.attach_alternative(html_content, "text/html")
msg.send()

此电子邮件中的结果(使用 console backend ):

Content-Type: multipart/alternative;
boundary="===============2514609180855632526=="
MIME-Version: 1.0
Subject: hello
From: from@example.com
To: to@example.com
Date: Wed, 18 Jul 2012 10:31:22 -0000
Message-ID: <20120718103122.11355.59803@localhost6.localdomain6>

--===============2514609180855632526==
Content-Type: text/plain; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This e-mail is in HTML-format. Click the following link:

http://www.company.nl/enews/7
--===============2514609180855632526==
Content-Type: text/html; charset="utf-8"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

<p>This is an <strong>HTML-format</strong> version.</p>
--===============2514609180855632526==--

虽然它没有multipart/related Content-Type,但它确实适用于纯文本(显示链接)和 HTML 客户端(显示 HTML 版本)。

关于使用边界的 Python/Django mime 多部分电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11537670/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com