当我使用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”)'。 –