當我使用python從unix服務器發送郵件時,我收到了額外的內容,如下所示sendmail.
此內容顯示在郵件中。Python發送郵件,錯誤
From nobody Mon Dec 18 09:36:01 2017 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit
我的代碼如下。
#reading data from file
data = MIMEText(file('%s'%file_name).read())
#writing the content as html
content = MIMEText("<!DOCTYPE html><html><head><title></title></head><body>"+'%s'%data+"</body></html>", "html")
msg = MIMEMultipart("alternative")
msg["From"] = "[email protected]"
msg["To"] = "[email protected]"
msg["Subject"] = "python mail"
msg.attach(content)
p = Popen(["/usr/sbin/sendmail", "-t","-oi"], stdin=PIPE,universal_newlines=True)
p.communicate(msg.as_string())
你是說你不期待「text/plain」內容以及html內容? – snakecharmerb
我只是想避免這種情況,警告。我的文件內容是一個html數據,但是當我收到郵件時,我在消息上方看到這個警告。我想禁用它。 –
我懷疑你還需要明確標識數據爲「html」。 'data = MIMEText(無論,「html」)'。 –