使用 MIMEText 读取文本文件时出现奇怪的不需要的标题 [英] Strange unwanted header when reading text file with MIMEText

查看:35
本文介绍了使用 MIMEText 读取文本文件时出现奇怪的不需要的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序来读取文本文件.我已经设法让它工作了,但是我得到了一个我不想要的奇怪标题.

I am writing a program to read from a text file. I have managed to get it to work, but am getting a strange header Which I do not want.

该文本文件名为SixMonthTextFile.txt,并在 Windows 中使用记事本保存.

The Text file is called "SixMonthTextFile.txt and saved with notepad in windows.

我得到的不需要的标题是 -

The unwanted header I am getting is -

内容类型:文本/普通;charset="us-ascii"
MIME 版本:1.0

Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0

内容传输编码:7 位

从此处读取的文件正文

我试过剥离前 3 行,但这不起作用,只会导致新问题.关于它为什么会发生,更重要的是如何阻止它的任何想法?

I have tried stripping the first 3 lines and that is not working, just causes new problems. Any ideas on why it is happening and more importantly how to stop it?

我的代码是

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

#Read an external text file for body of message
fp = open('SixMonthTextFile.txt', 'r')
SixMonthMessage = MIMEText(fp.read())
fp.close()

print(SixMonthMessage)

我得到的结果是

Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

We would like to remind you that it has been six months
since your last service and its time for a precautionary service
since your equipment needs regular servicing to remain reliable.

Please reply to this email to book your FREE appointment.
[Finished in 0.4s]

我只想要文本文件中的原始文本,因为它会进入电子邮件正文.

I want only the original text from the text file as that is going into an email body.

关于为什么我会收到奇怪的不需要的额外标题以及如何摆脱它的任何想法?

Any ideas on why I am getting the strange unwanted extra header and how to get rid of it?

推荐答案

所以我尝试了 stovfl 的建议并尝试在 MIMEText(fp.read() 语句中添加 get_payload(),'3\n' 并删除了不需要的header 但也弄乱了文本文件的格式,我仍然有一个无法使用的结果.

So I tried stovfl's suggestion and played with adding get_payload(),'3\n' to the MIMEText(fp.read() statement and it removed the unwanted header but also messed up the format of the text file and I still had an unuseable result.

我以不同的角度解决了这个问题,并用

I got around the problem by coming at a different angle and replaced fp = open() etc with

open('SixMonthTextFile.txt', 'r') 作为文件:SixMonthTextFile = file.read()

open('SixMonthTextFile.txt', 'r') as file: SixMonthTextFile = file.read()

'''

这给了我可以在文本文件中使用的文本格式,以便插入到电子邮件中.

This gave me text that could then be used as formatted in the text file for insertion into the email.

def sendsixmonthemail(address, EmailTo):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText

#Read an external text file for body of message
with open('SixMonthTextFile.txt', 'r') as file:
    SixMonthTextFile = file.read()



host="smtp.gmail.com"
email="myemailaddress@gmail.com"
password = "123456786"

from_addr='myemailaddress@gmail.com'
to_addr=EmailTo
reply_address = "myemailaddress@gmail.com"

msg=MIMEMultipart()
msg['From']=from_addr
msg['To'] = to_addr
msg['reply-to'] = "myemailaddress@gmail.com"
msg['subject']='FREE 6 month Service Reminder for' + " " + address

#Data read from Text File
body= str(SixMonthTextFile)


msg.attach(MIMEText(body,'plain'))

mail=smtplib.SMTP_SSL(host,465)
mail.login(email,password)
text=msg.as_string()

mail.sendmail(email,to_addr,text)
mail.quit()

这篇关于使用 MIMEText 读取文本文件时出现奇怪的不需要的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆