データストア(データベース版なし)の4:Emailアラート

5419 ワード

#coding:utf-8
'''
    Email        ,                       ,
    Email       
            STMP,python   SMTP   ,         ,HTML  
        。python SMTP   smtplib email    ,
email      ,smtplib      。
         Email  ,      163  ,  SMTP  ,         
     smtp.163.com.
'''

# SMTP    ,            :
from email.mime.text import MIMEText
msg = MIMEText('python      ,       HTTP 403','plain','utf-8')

#  MIMEText     3   :
#      。
#  MIME subtype,  "plain"     ,   MIME  "text/plain"。
#        ,UTF-8          。

#          ,           ,   STMP    。  :
from email.header import Header
from email.mime.text import MIMEText
from email.utils import parseaddr, formataddr

import smtplib

def _format_addr(s):
    name, addr = parseaddr(s)
    return formataddr((Header(name, 'utf-8').encode(), addr))
#     
from_addr = '[email protected]'
#    
password = 'pass'
#     
to_addr = '[email protected]'
#163         
smtp_server = 'smtp.163.com'
#      
msg = MIMEText('python      ,       HTTP 403','plain','utf-8')
msg['From'] = _format_addr('    ' % from_addr)
msg['To'] = _format_addr('   ' % to_addr)
msg['Subject'] = Header('        ','utf-8').encode()
#    
server = smtplib.SMTP(smtp_server,25)
server.login(from_addr, password)
server.sendmail(from_addr, [to_addr], msg.as_string())
server.quit()

'''
                   ,    HTML  ,           。
   MIMEText   , HTML      ,        "plain"  "html"
    。  :
msg = MIMEText('

hello

' +
'

cnblogs...

' +
'','html','utf-8') '''