2013-08-24 86 views
0

我有以下代碼在gerrit.txt文本中的哪些電子郵件... gerrit.txt的內容是HTML代碼,並在http://pastie.org/8264638,問題是在電子郵件(在Outlook中)它的電子郵件HTML內容......如何以HTML格式發送電子郵件?如何發送電子郵件在html格式

from email.mime.text import MIMEText 
from subprocess import check_call,Popen,PIPE 

def email (body,subject,to=None): 
     msg = MIMEText("%s" % body) 
     msg['Content-Type'] = "text/html;" 
     msg["From"] = "[email protected]" 
     if to!=None: 
      to=to.strip() 
      msg["To"] = to 
     else: 
      msg["To"] = "[email protected]" 
     msg["Subject"] = '%s' % subject 
     p = Popen(["/usr/sbin/sendmail", "-t", "-f" + msg["From"]], stdin=PIPE) 
     (stddata, errdata) = p.communicate(input="To: " + msg["To"] + "\r\nFrom: " + msg["From"] + "\r\nSubject: " + subject + "\r\nImportance: Normal\r\n\r\n" + body) 
     print stddata, errdata 
     print "Done" 


def main(): 
    # open gerrit.txt and read the content into body 
    with open('gerrit.txt', 'r') as f: 
     body = f.read() 

    Subject ="test email" 
    email(body, Subject, "[email protected]") 

if __name__ == '__main__': 
    main() 

回答

0

嘗試發送文本作爲HTML格式

/*"<html><body>Email Body text<\body><\html>"*/ 

我沒有測試它,但它應該工作。