2

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でこれを試しましたが、正確な順序で取得できません。

4

2 に答える 2

1

プレーンテキストと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()

このメールの結果(コンソールバックエンドを使用):

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==--

Content-Typeはありませんが、multipart/relatedプレーンテキスト(リンクを表示)とHTMLクライアント(HTMLバージョンを表示)の両方で機能します。

于 2012-07-18T10:40:53.983 に答える
0

emailおよびsmtplibモジュールを直接使用します。

電子メールを処理するための Django 組み込みメソッドは非常に限られています。

于 2012-07-18T09:07:34.087 に答える