2017-06-04 120 views
0

我試圖編寫一些非常簡單的代碼。這個想法是讀取一個HTML代碼寫入的.txt文件。具體來說,它是一個表格,其中包含一些css和樣式修飾符:用python,html + css發送電子郵件,閱讀.txt文件

<td style=""background-color:red"" >-49,2%</td> 

我可以成功讀取文件的內容併發送電子郵件。但是,發送電子郵件時,不會應用背景樣式。桌子的其餘部分是完美創建的。 我的代碼是這樣的:

fp = open(r"filepath", 'r',encoding='utf-8-sig') 
# Create a text/plain message 
msg = MIMEText(fp.read()) #Here I also tried with the argument 'html' 
fp.close() 
s.sendmail("email1", "email2",msg.as_string()) 

有什麼辦法,我也可以得到應用的風格?電子郵件服務提供者可以在Gmail和我知道它可以工作,因爲如果我嘗試使用「味精」變量爲文字字符串,如:

msg= r"<table...> <td style=""background-color:red"" >-49,2%</td> </table>" 

它正確地發送背景格式。這個問題必須是其他類型的。

非常感謝!

回答

0

已解決。

我希望它有幫助, modifier.as_string()是問題。刪除它:

s.sendmail("email1", "email2",msg.as_string()) 

s.sendmail("email1", "email2",msg) 

使它工作。