Feb-07-2018, 09:54 PM
(This post was last modified: Feb-07-2018, 09:55 PM by downloaderfan.)
(Feb-07-2018, 07:00 PM)lghauth Wrote: You will need to do something like this, to read line by line:if main_type == 'text': print("text") output_file = "" with open(attached_file, 'r') as fp: # 'rb' will send this error: 'bytes' object has no attribute 'encode' while True: cur_line = fp.readline() if cur_line == '': break output_file += cur_line.replace('\n', '\r\n') attachement = MIMEText(output_file, _subtype=sub_type)Or try this, see if it works:if main_type == 'text': print("text") temp = open(attached_file, 'r') # 'rb' will send this error: 'bytes' object has no attribute 'encode' attachement = MIMEText(temp.read().replace('\n', '\r\n'), _subtype=sub_type) temp.close()
I tried both those codes, still same problem. Notepad is ignoring line breaks. I also tried few solutions I found from stack overflow & they didn't work either. No idea what the problem is. Anyway, I don't want to trouble you any more. So I came up with an alternative. I modified the script to convert the txt file to zip file & then send it to Gmail. That way I have no issues. Thanks for all your help.