English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Django メールの送信

   Django提供了一个现成的,易于使用发送电子邮件的轻型引擎。类似Python,你需要导入 smtplib。在Django中只需要导入django.core.mail。 要发送电子邮件,编辑项目settings.py文件,并设置下列选项 −

EMAIL_HOST − smtp 服务器 EMAIL_HOST_USER − 登录凭证SMTP服务器     EMAIL_HOST_PASSWORD − SMTP服务器密码凭证     EMAIL_PORT − smtp服务器端口     EMAIL_USE_TLS 或   _SSL − 如果设置为True则为安全连接。    

发送一个简单的电子邮件

让我们创建一个“sendSimpleEmail”视图发送一个简单的电子邮件。

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.core.mail import send_mail
 from django.http import HttpResponse
 def sendSimpleEmail(request, emailto):
    res = send_mail("hello paul", "comment tu vas?", "xxx@w"3codebox.com", [emailto])
    return HttpResponse('%s'%res)

codebox.com", [emailto])

send_mailパラメータの詳細は以下の通り − − E-subject     message − E-メールの件名     from_email − E-メールの件名内容     メール送信元 recipient_list     fail_silently − 受信者のメールアドレスリスト     auth_user − ブール値、falseの場合、send_mailはエラーが発生したときに例外を発生させます     auth_password − ユーザーログイン、settings.pyに設定されていない場合     connection − E-mail 后端     html_message − (Django1.7中新增功能),如果存在的话,该邮件将为 multipart/alternative。    

− ユーザーID、settings.pyに設定されていない場合 -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.conf.urls import patterns, url
 私たちのビューのURLにアクセスしましょう/(?P<emailto>
    [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4)/, 
    urlpatterns = patterns('myapp.views', url(r'^simpleemail

'sendSimpleEmail', name='sendSimpleEmail'),) /myapp/したがって、アクセスする際には/  [email protected]/ simpleemail  

send_mass_mailを使って複数のメールを送信する場合、以下のページに移動します −

メソッドが返すのは、成功して送信されたメッセージの数です。send_mailと同様ですが、追加のパラメータが必要です;datatuple、sendMassEmailビューは以下の通りです −

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.core.mail import send_mass_mail
 from django.http import HttpResponse
 def sendMassEmail(request, emailto):
    msg1 = ('subject 1', 'message 1', '[email protected]', [emailto1])
    msg2 = ('subject 2', 'message 2', '[email protected]', [emailto2])
    res = send_mass_mail((msg1, msg2), fail_silently=False)
    return HttpResponse('%s'%res)

私たちのビューのURLにアクセスを作成しましょう −

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.conf.urls import patterns, url
 urlpatterns = patterns('myapp.views', url(r'^massEmail/(?P<emailto1>
    [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4)/(?P<emailto2>
    [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4})', 'sendMassEmail', name='sendMassEmail'),)

アクセスする際に /myapp/massemail/[email protected]/[email protected]/, そして我们得到 −  

send_mass_mail パラメータの詳細は以下の通り −

datatuples    − タプル、各要素は (subject, message, from_email, recipient_list) などの形をしています     fail_silently − 布尔,如果为false,send_mail将在出现错误时引发异常     auth_user − ユーザーログイン、settings.py に設定されていない場合     auth_password − ユーザーパスワード、settings.py に設定されていない場合     connection − E-mail 后端    

上の画像で確認できるように、2つのメッセージが成功して送信されました

注 - この例では、Pythonのsmtpd debuggingserverを使用しており、以下のコマンドで起動できます

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
$python -m smtpd -n -c DebuggingServer localhost:1025

これは、すべての送信されたメールが stdout に印刷され、localhost 上で動作するバーチャルサーバーに表示されることを意味します1025。

mail_admins と mail_managers メソッドを使用して管理者と管理人员にメールを送信します

これらのメソッドは、settings.py ファイルの ADMINS オプションで定義されたウェブサイト管理者にメールを送信します。ウェブサイト管理者は settings.py ファイルの MANAGERS アイテムで定義されています。以下のように見えると仮定します-

ADMINS   = (('polo', '[email protected]'),)

MANAGERS = (('popoli', '[email protected]'),)

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.core.mail import mail_admins
 from django.http import HttpResponse
 def sendAdminsEmail(request):
    res = mail_admins('my subject', 'site is going down.')
    return HttpResponse('%s'%res)

上面的代码将发送一封电子邮件,在ADMINS 部分定义的每个管理员。

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.core.mail import mail_managers
 from django.http import HttpResponse
 def sendManagersEmail(request):
    res = mail_managers('my subject 2', '変更 日付 on the site.')
    return HttpResponse('%s'%res)

上面的代码会发邮件到MANAGERS 部分定义的每个管理员。

参数详细信息 −

件名 − E-mail 件名。     message − E-mail 主题.     fail_silently − 布尔,如果为false,send_mail将在出现错误时引发异常     connection − E-mail后端.     html_message − (Django1.7中新增功能),如果存在的话,该邮件将为 multipart/alternative。    

HTML Eを送信します-mail

Django>=1.7HTMLメッセージの送信も簡単です -

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.core.mail import send_mail
 from django.http import HttpResponse
    res = send_mail("hello paul", "comment tu vas?", "[email protected]", 
          ["[email protected]"], html_message=")

これはmultipart/代替のメール。

しかし、Django<1.7 HTMLメールの送信は、django.core.mail.mailMessageクラスを使用して完了し、「send」オブジェクトを呼び出します。

HTMLメールを送信するための「sendHTMLEmail」ビューを作成します。

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.core.mail import EmailMessage
 from django.http import HttpResponse
 def sendHTMLEmail(request, emailto):
    html_content = "<strong>Comment tu vas?</strong>"
    email = EmailMessage("my subject", html_content, "[email protected]", [emailto])
    email.content_subtype = "html"
    res = email.send()
    return HttpResponse('%s'%res)

詳細なパラメータ情報を持つメールメッセージの作成クラス −

件名 − E-mail 件名。     message − E-mail HTML本文。     from_email − E-mail 送信元。     to − 受信者のメールアドレスリスト。     bcc − 「BCC」の受信者のメールアドレスリスト。     connection − E-mail バックエンド。    

アクセスするビューURLを生成します。

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.conf.urls import patterns, url
 urlpatterns = patterns('myapp.views', url(r'^htmlemail/(?P<emailto>
    [\w.%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4)/, 
    sendHTMLEmail, name = 'sendHTMLEmail'),

アクセスする際に /myapp/htmlemail/[email protected]、以下の内容が得られます。  

添付ファイル付きのメールの送信

これは、指定されたEmailMessageオブジェクトの「attach」メソッドを使用して行われます。

以下の添付メールを送信するビューが一つあります −

# Filename : example.py
# Copyright : 2020 By w3codebox
# Author by : ja.oldtoolbag.com
# Date : 2020-08-08
from django.core.mail import EmailMessage
 from django.http import HttpResponse
 def sendEmailWithAttach(request, emailto):
    html_content = "Comment tu vas?"
    email = EmailMessage("my subject", html_content, "[email protected]", emailto)
    email.content_subtype = "html"
    fd = open('manage.py', 'r')
    email.attach('manage.py', fd.read(), 'text/plain')
    res = email.send()
    return HttpResponse('%s'%res)

添付内の詳細パラメータ −

filename − 添付ファイルの名前     content − このファイルの内容、添付。     mimetype − 附件の内容MIMEタイプ。