django送信メール

14316 ワード

概要:
pythonはsmtplibライブラリを提供し、emailの送信機能を完成させるが、djangoはそれをパッケージ化し、メールを送信するインタフェースをより簡単にし、より便利にし、djangoのパッケージは
django.core.mail
例:
   
from
 django.core.mail
 import
 send_mail


send_mail
(
'Subject here'
,
 'Here is the message.'
,
 '[email protected]'
,

    [
'[email protected]'
],
 fail_silently
=
False
)


       django setting       EMAIL_HOSTEMAIL_PORTEMAIL_HOST_USEREMAIL_HOST_PASSWORD

   SMTP   、 EMAIL_USE_TLSDEFAULT_CHARSET



(1) 
send_mail
(subject
, message
, from_email
, recipient_list
, 
          fail_silently=False
, auth_user=None
,auth_password=None
)
  :      

subject:     
message:     
from_email:     
recipient_list

:       
fail_silently:    ,   False,   False,       ,   

smtplib.SMTPException  

,       ,
                  smtplib docs
,      SMTPException   。

auth_user

:     ,   ,    SMTP Server ,     ,    EMAIL_HOST_USER
  
auth_password:    ,   ,    SMTP 

Server ,     ,    EMAIL_HOST_PASSWORD

  

(2)

send_mass_mail(datatuple
, fail_silently=False
, auth_user=None
, auth_password=None
)

datatuple

:     ,     :
  (
subject
,
 message
,
 from_email
,
 recipient_list
)fail_silently

, auth_user

 and auth_password        send_mail。


send_mass_mailとsend_mailの違いsend_mailはメールを送信するたびに新しい接続を開いて送信します.
send_mass_mailはメールを送信するたびに1つの接続だけを開き、今回のメタグループのすべてのメールを送信します.
だからsend_mass_mailの方が効率的です.
管理者にメールを送信する2つのショートカット
mail_admins (
subject ,
message ,
fail_silently=False )
および
mail_managers (
subject ,
message ,
fail_silently=False )
この2つの方法は、メールを送信する際に以下の2つの点に注意します.
(1)メールのタイトルの前にdjangoが自動的に付けられる
EMAIL_SUBJECT_PREFIXで設定された接頭辞、デフォルトは
"[Django] " .
(2)メールの送信者は
SERVER_EMAILで設定
(3)メールの受信者は
MANAGERSおよび
ADMINSで設定
EmailMessageとSMTPConnectionで提供されているemail関数は、最も基本的なメール送信機能のみを提供することができます.その他、ファイル添付ファイルやマルチメディアemailなどは、EmailMessageクラスで提供する必要があります.
EmailMessageコンストラクション関数のすべてのパラメータは次のようになります.すべてのパラメータはオプションです.
subject:メールのタイトル
body:メールの本文、このフィールドは純粋なテキストファイルであるべきです.
from_Email:送信者のメールアドレス、
[email protected]および
Fred 、どちらのフォーマットも合法で、フィールドが設定されていない場合はsettingの
DEFAULT_FROM_EMAIL
to:すべての受信者からなるメタグループ
bcc:すべての秘密抄送者からなるユニット
接続:メールの送信に使用
SMTPConnection接続は、このフィールドが適用されない場合、メールを送信するたびに新規作成されます.
1つ
SMTPConnectionオブジェクト
attachments:添付ファイルリスト、
メールです.MIMEBase.MIMEBaseインスタンス、または
(filename,content,mimetype)メタグループ
headers:ヘッダーフィールド、追加のヘッダーを追加する場合は、キー、値valueのフィールドを使用します.
  :
email
 =
 EmailMessage
(
'Hello'
,
 'Body goes here'
,
 '[email protected]'
,

            [
'[email protected]'
,
 '[email protected]'
],
 [
'[email protected]'
],

            headers
 =
 {
'Reply-To'
:
 '[email protected]'
})


         :
(1)send(fail_silently=False)     ,  fail_silently=False ,        (2)

message()     

django.core.mail.SafeMIMEText       

django.core.mail.SafeMIMEMultipart     。


(3)

recipients()

        ,  to bcc      
(4)attach()         ,      

email.MIMEBase.MIMEBase   ,   

filename

, content

 and mimetype,   filename          ,content      ,

mimetype

    MIME  
                 :message
.
attach
(
'design.png'
,
 img_data
,
 'image/png'
)

(5)attach_file()

                ,  mime      ,  django          。
                 :message
.
attach_file
(
'/images/weather_map.png'
)

 EmailMultiAlternatives  


  EmailMessage        ,        "text/html"  
"text/plain"。       ,
            text/html

   ,  text/plain   ,        EmailMultiAlternatives
EmailMessage
attach_alternative()

 ,               
  :
from
 django.core.mail
 import
 EmailMultiAlternatives


subject
,
 from_email
,
 to
 =
 'hello'
,
 '[email protected]'
,
 '[email protected]'

text_content
 =
 'This is an important message.'

html_content
 =
 '<p>This is an <strong>important</strong> message.</p>'

msg
 =
 EmailMultiAlternatives
(
subject
,
 text_content
,
 from_email
,
 [
to
])

msg
.
attach_alternative
(
html_content
,
 "text/html"
)

msg
.
send
()


EmailMessage       


EmailMessage 

         "text/plain",      subtype  ,      "text/html"
  :
msg
 =
 EmailMessage
(
subject
,
 html_content
,
 from_email
,
 [
to
])

msg
.
content_subtype
 =
 "html"
  # Main content is now text/html

msg
.
send
()

SMTPConnectionオブジェクト
SMTPConnectionの初期化には4つのパラメータが必要です.SMTPサーバに接続するために必要なhost,port,username and passwordです.デフォルトがある場合はsettingファイルからデフォルト値を読み出し、send_Messagesはメールリストを送信するために使用されます
例:
connection
 =
 SMTPConnection
()
   # Use default settings for connection

messages
 =
 get_notification_email
() #get_notification_email            ,             

connection
.
send_messages
(
messages
)