Pythonは簡単なhtmlメールコードを送信する

887 ワード

     #!/usr/bin/env python
    # -*- coding=utf-8 -*-

    import smtplib
    from email.header import Header
    from email.mime.text import MIMEText
    from email.mime.multipart import MIMEMultipart

    sender = "[email protected]"
    rcpt = "[email protected]"
    msg = MIMEMultipart(alternatvie)
    msg[Subject] = Header("    ","utf-8") #    
    msg[From] = r"%s <[email protected]>" % Header("  ","utf-8") #       
    msg[To] = rcpt

    html = open(html.tpl).read() #  HTML  
    html_part = MIMEText(html,html) #    html  
    html_part.set_charset(utf-8) #    
    msg.attach(html_part) #   message 

    try:
        s = smtplib.SMTP(smtp.163.com) #  SMTP   ,  
        s.login(pywugw,*******)
        s.sendmail(sender,rcpt,msg.as_string())
    except Exception,e:
        print e