Strange Unwanted Header When Reading Text File With Mimetext
Solution 1:
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') 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.
defsendsixmonthemail(address, EmailTo):
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
#Read an external text file for body of messagewithopen('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()
Solution 2:
The reason I wanted to be able to easily edit the message being included into the email was so we did not have to redo the program each time we want to include anything into the emails. It also made it easier to copy and modify the process for clients who only get an annual reminder who get a different message.
Post a Comment for "Strange Unwanted Header When Reading Text File With Mimetext"