1.このブログを書く目的
主な記録は、djangoを介してこの機能を実現する方法です。[メール本文の値はhtmlページ+複数の添付ファイル]を満たす単一のメールを送信します。
[メール本文の値はhtmlページ+複数の添付ファイル]を満たす単一のメールを送信するには、クラスEmailMultiAlternativeを使用できます。
EmailMultiAlternativeクラスは、attach_file()、attach_alternative()、send()の3つのメソッドを提供します。これら3つのメソッドの主な関数は次のとおりです。
- attach_file()の主な機能:指定されたファイルを電子メールの添付ファイルとして扱います。
- attach_alternative()の主な機能:電子メールの本文を指定されたhtmlページにします。
- send()の主な機能:メールを送信するアクションを実行します。
完全な操作プロセスは、次のコンテンツで確認できます。
第二に、完全な操作プロセス
1.最初のステップ:[ settings.py ]にメールボックス構成情報を追加します
# 配置qq邮箱信息
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' # 值必须为这个
EMAIL_USE_SSL = True # SSL加密方式,值必须为True
EMAIL_HOST = 'smtp.qq.com' # 发送邮件的qq邮箱的SMTP服务器
EMAIL_PORT = 465 # QQ邮箱对应的SMTP服务器端口
EMAIL_HOST_USER = '[email protected]' # 发件人
EMAIL_HOST_PASSWORD = 'xolnfbqgdybxji11' # qq授权码(不能使用qq密码只能使用qq授权码)
EMAIL_FROM = 'Rainbow<[email protected]>' # 邮件显示的发件人
2.ステップ2:[ helloworld / hello /views.py ]にビュー関数を追加します
3.ステップ3:[ helloworld / helloworld /urls.py ]にURLマッチングルールを追加します
url(r"^send_email_004/$",views.send_email_004),
4.ステップ4:サービスを再起動します
5.ステップ5:任意のブラウザでURLアドレス[ http://0.0.0.0:8000/send_email_004/ ]を入力し、アクセス後に結果を確認します
3.関連知識ポイント
1.クラスEmailMultiAlternativesのメソッド[ __init__ ]のソースコードの簡単な分析
class EmailMultiAlternatives(EmailMessage):
"""
A version of EmailMessage that makes it easy to send multipart/alternative
messages. For example, including text and HTML versions of the text is
made easier.
"""
alternative_subtype = 'alternative'
def __init__(self, subject='', body='', from_email=None, to=None, bcc=None,
connection=None, attachments=None, headers=None, alternatives=None,
cc=None, reply_to=None):
"""
Initialize a single email message (which can be sent to multiple
recipients).
"""
super().__init__(
subject, body, from_email, to, bcc, connection, attachments,
headers, cc, reply_to,
)
self.alternatives = alternatives or []
メソッド[ __init__ ]のいくつかの主要な入力パラメータの分析:
- 件名:(必須、データ型は文字列です)電子メールの件名。
- 本文:(必須、データ型は文字列です)電子メールの本文。
- from_email :(必須ではありません。データ型は文字列です)電子メールを送信します。
- to :(必須ではありません。データ型はリストです)リストの各値は、メールを受信するための電子メールアドレスです。
2.クラスEmailMultiAlternativesのメソッド[ attach_file ]のソースコードの簡単な分析
def attach_file(self, path, mimetype=None):
"""
Attach a file from the filesystem.
Set the mimetype to DEFAULT_ATTACHMENT_MIME_TYPE if it isn't specified
and cannot be guessed.
For a text/* mimetype (guessed or specified), decode the file's content
as UTF-8. If that fails, set the mimetype to
DEFAULT_ATTACHMENT_MIME_TYPE and don't decode the content.
"""
path = Path(path)
with path.open('rb') as file:
content = file.read()
self.attach(path.name, content, mimetype)
おおよそのソースコードから見ることができる[方法attach_file実際には、方法[、こと] attach_file ]はさらにカプセル化[方法のアタッチ]。
3.クラスEmailMultiAlternativesのメソッド[ attach ]のソースコードの簡単な分析
def attach(self, filename=None, content=None, mimetype=None):
"""
Attach a file with the given filename and content. The filename can
be omitted and the mimetype is guessed, if not provided.
If the first parameter is a MIMEBase subclass, insert it directly
into the resulting message attachments.
For a text/* mimetype (guessed or specified), when a bytes object is
specified as content, decode it as UTF-8. If that fails, set the
mimetype to DEFAULT_ATTACHMENT_MIME_TYPE and don't decode the content.
"""
if isinstance(filename, MIMEBase):
assert content is None
assert mimetype is None
self.attachments.append(filename)
else:
assert content is not None
mimetype = mimetype or mimetypes.guess_type(filename)[0] or DEFAULT_ATTACHMENT_MIME_TYPE
basetype, subtype = mimetype.split('/', 1)
if basetype == 'text':
if isinstance(content, bytes):
try:
content = content.decode()
except UnicodeDecodeError:
# If mimetype suggests the file is text but it's
# actually binary, read() raises a UnicodeDecodeError.
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
self.attachments.append((filename, content, mimetype))
ソースコードのこの部分をインターセプトします。
mimetype = mimetype or mimetypes.guess_type(filename)[0] or DEFAULT_ATTACHMENT_MIME_TYPE
basetype, subtype = mimetype.split('/', 1)
ソースコードのこの部分から簡単にわかります。入力パラメーターのmimetype値がNoneの場合、djangoは添付ファイル名に従ってMIMEコンテンツタイプの値を自動的に推測し、それを入力パラメーターmimetypeに割り当てます。後続の関連処理のために、パラメーターmimetypeの新しい値を入力します。
4.各アプリケーションのリソースの保存場所
リソースとは何ですか?例:画像、htmlページ、css、js、ビデオ、pdf、word、txtなど。これらはすべて異なるリソースタイプです。
各プロジェクトアプリケーションには対応するリソースがあるので、各アプリケーションのリソースの保存場所を決定するにはどうすればよいですか?アイデアを決定する方法、個人的なアプローチは主にこのアイデアのステップです:
- 最初のステップ:各アプリケーションのリソース保存場所は、各アプリケーションのルートディレクトリの下に手動で追加されたフォルダーに設定されます。たとえば、新しいフォルダーの名前は[public]です。
- 手順2:フォルダ[public]に、さまざまなリソースタイプに対応するさまざまなフォルダを追加します。たとえば、新しいフォルダの名前は[css]と[word]です。
- ステップ3:フォルダー[word]に、異なるデータテーブル/異なるビジネスモジュールに対応する異なるフォルダーを追加します。たとえば、新しいフォルダーの名前は[user]および[test]です。
この思考ステップの主な目的は次のとおりです。各アプリケーションのリソースはそれぞれのアプリケーションディレクトリに保持されます。これにより、プロジェクトコードの結合が大幅に減少し、その後の保守が容易になります。
アプリケーション【服用ハロー例としての]、のリソースの保存場所に対応する特定のファイル階層アプリケーションは、[ハロー]概ね次の通りであります: